how do perfect Circle Collision Detection?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
baby_nux
Prole
Posts: 25
Joined: Fri Mar 04, 2011 3:21 pm

how do perfect Circle Collision Detection?

Post by baby_nux »

any one know how do perfect circle collision detection in love2d?

here some article :
http://compsci.ca/v3/viewtopic.php?t=14897

but i dont know how to write this code in love2D,
any help would be appreciated

(my english is bad,sry for thats ^^)
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: how do perfect Circle Collision Detection?

Post by Jasoco »

It's simple. Just check that the distance from the center of circle A to the distance from the center of circle B minus the radius of both circles is less than zero.

Code: Select all

function distance(x1, y1, x2, y2)
  return d = math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
end

distance_between_circles = distance(circle_1_x, circle_1_y, circle_2_x, circle_2_y)
if distance_between_circles - (circle_1_radius + circle_2_radius) < 0 then
  --The circles collided/overlap!
end 
User avatar
ivan
Party member
Posts: 1912
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: how do perfect Circle Collision Detection?

Post by ivan »

Jasoco wrote:It's simple. Just check that the distance from the center of circle A to the distance from the center of circle B minus the radius of both circles is less than zero. [/code]
Not that simple. You're talking about a static intersection test, there are also continous collision tests that calculate the exact time of impact. Especially with moving circle vs aabb and triangles the code becomes quite hairy.
I think the link is ok although the code looks a bit sloppy, I couldn't understand all of it frankly. Also, I would like to point out too that there is no such thing as 'perfect' collision detection in game programming. This is due to several limitations such as:
-Floating point precision in a computer is not infinately accurate
-You can't make a program (at least a REALTIME program) that moves all shapes simultaneously. That is, there is always an order in which the shapes are going to be moved/updated. Imagine 3 equal-sized circles (A, B and C) moving towards the same point (which is an equal distance between all 3 of them) at the same speed. Which collision pair are you going to resolve first? A vs B, B vs C or A vs C? The results will be different depening on the order.
User avatar
sharpobject
Prole
Posts: 44
Joined: Fri Mar 18, 2011 2:32 pm
Location: California

Re: how do perfect Circle Collision Detection?

Post by sharpobject »

What kind of shapes are you interested in colliding? If this is strictly about circle-circle collisions it should be pretty straightforward.
ivan wrote:there are also continous collision tests that calculate the exact time of impact.
Right. The complexity of this test depends on what kind of interpolation you want to use. If you're willing to use linear interpolation, you can fix one of the bodies at the origin and calculate the velocity of the other one in the first one's inertial frame of reference (so circle A is at (0,0) with velocity (0,0) and all the information about position and movement is in circle B's attributes). Then you can do some simple tests that involve more linear algebra than I want to type right now to determine whether B passes through the fixed circle A. Once you know any time at which they intersect it is trivial to find the time at which they start intersecting with a binary search.
ivan wrote:-Floating point precision in a computer is not infinately accurate
I doubt your game needs to have perfect precision, but if you want you can use a representation for numbers that can hold arbitrary expressions and evaluate them only when it needs to compare them.
ivan wrote:-You can't make a program (at least a REALTIME program) that moves all shapes simultaneously. That is, there is always an order in which the shapes are going to be moved/updated. Imagine 3 equal-sized circles (A, B and C) moving towards the same point (which is an equal distance between all 3 of them) at the same speed. Which collision pair are you going to resolve first? A vs B, B vs C or A vs C? The results will be different depening on the order.
To solve a situation like this you could do something like sorting the collisions by time, simulating the earliest collision, and recalculating the results for nearby bodies (where "nearby" is "any of the quadtree nodes that either of the two colliding bodies could have moved into or will now move into").

I sincerely think you're overthinking the problem though. Most games can get by with static tests.
User avatar
ivan
Party member
Posts: 1912
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: how do perfect Circle Collision Detection?

Post by ivan »

sharpobject wrote:most games can get by with static tests.
I agree.
sharpobject wrote:I sincerely think you're overthinking the problem though.
Nah, I just wanted to point out that collision detection in games is built on approximations and compromises.
There's not really an ideal approach that will produce a 'perfect' simulation.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests