
-----------------------------------
Dragon20942
Wed Feb 23, 2011 9:53 pm

Something Interesting Regarding Circle collision
-----------------------------------
What is it you are trying to achieve?
So I was wondering if we use the Pythagorean theorem to find the exact pixels of a circle's circumference, or if there is an easier way. From my current Gr. 10 math, it is suggested that we must substitute every value of x to get y (or vice versa) using the radius as the hypotenuse. However, this would create some issues concerning the amount of computing required to run a for-loop running as many times as there are pixels in the radius.

-----------------------------------
Tony
Wed Feb 23, 2011 10:04 pm

RE:Something Interesting Regarding Circle collision
-----------------------------------
Checking all the pixels is not guaranteed to work. The trivial counter-example is where one circle is completely inside a bigger circle. More interesting edgecases are those where pixels round in such a way that they don't end up in the same spot.

Try drawing some diagrams to get other ideas; http://compsci.ca/blog/super-paper-programming/

http://compsci.ca/blog/wp-content/uploads/2007/10/circle_collision_detection.png

-----------------------------------
Dragon20942
Wed Feb 23, 2011 10:09 pm

RE:Something Interesting Regarding Circle collision
-----------------------------------
The fact that you use the centers of both circles instead of just the center of one circle and the end of its radius provides a new concept for me. Id better think that out before coding. Thanks!

-----------------------------------
Insectoid
Thu Feb 24, 2011 12:01 am

RE:Something Interesting Regarding Circle collision
-----------------------------------
There are a number of tutorials on this from very basic concepts to advanced collision correction scattered around the forums. Have a look.

-----------------------------------
mirhagk
Thu Feb 24, 2011 8:26 am

RE:Something Interesting Regarding Circle collision
-----------------------------------
Cicle collision is the simplest, and arguably the most important collision detection.

Almost any shape can be rougly estimated with a circle, allowing you to quickly check to see if something could potentially intersect something else without a full collision detection.

Also it's a matter of how far something is from another thing, every mission objective that involves getting to a specific point uses circle collision detection to see if your close enough.


Oh and a quick tip, the mathematical algorithm is
[code]
d=sqrt((x2-x1)^2+(y2-y1)^2)
if (d (maxx - rad1) then
        circleXVel := circleXVel * (-1)
    end if
    if circleY < rad1 then
        circleYVel := circleYVel * (-1)
    elsif circleY > (maxy - rad1) then
        circleYVel := circleYVel * (-1)
    end if

    if circle2X < rad2 then
        circle2XVel := circle2XVel * (-1)
    elsif circle2X > (maxx - rad2) then
        circle2XVel := circle2XVel * (-1)
    end if
    if circle2Y < rad2 then
        circle2YVel := circle2YVel * (-1)
    elsif circle2Y > (maxy - rad2) then
        circle2YVel := circle2YVel * (-1)
    end if

    xDiff := (circleX - circle2X)
    yDiff := (circleY - circle2Y)
    cDiff := (xDiff ** 2 + yDiff ** 2) ** (1 / 2)

    put "Pixels between midpoints: ",cDiff

    if cDiff < rad1 + rad2 then %if the hypotenuse is less than the combined radii, then
        circleColour := blue
        circleColour2 := blue
        delay (100)
    else
        circleColour := red
        circleColour2 := green
    end if

    drawfilloval (circleX, circleY, rad1, rad1, circleColour) %green
    drawfilloval (circle2X, circle2Y, rad2, rad2, circleColour2) %red

    View.Update
    delay (5)
    cls
end loop


Ok so I go and make up some code before realizing that you guys already posted it...
