# 2D collision detection

This topic is 4138 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a circle and a point. the point is moved by the mouse. On frame x the point is outside the circle and on frame x+1 the point is inside the circle. At this point I can get the direction of the point movement. What I want to do is on frame x+1 find the collision between the point and the circle's perimeter (taking in acount ofcourse the direction of the point movement). So I have : circle's radius, circle's center position, the position of the point inside the circle. So how you do it ? thx alot. p.s. providing D3DX functions to be used in the problem will be appriciated :)

##### Share on other sites
Not sure if this is the best way to do it, but it is A way to do it:

http://local.wasp.uwa.edu.au/~pbourke/geometry/sphereline/

Treat the points position at x as P1, and the position at x+1 as P2.

The only problem with that equation is that it doesn't seem to cover what happens if the point lies inside the circle. If it were me, I'd just multiply the length of the line by a lot (creating P2') to make sure it intersected at both points. Then, the intersection that you're interested in is the one between P1 and the circles centre.
Also, you're probably going to want to do the segment circle check every frame, rather than just when the point lies inside the circle. That way if the point goes from one side of the circle to the other side, but never actually ends up IN the circle, then you will still get the two points and still be able to detect the collision. Otherwise the point will be able to "teleport" past the circle.

I'm no expert in this, but that seems like a simple way to do it.

##### Share on other sites
Just check the distance from the point to the circle center and compare it to the radius. If current point position is vector r and it's current velocity is dr then it's next position will be r' = r + dr. Check if the distance from r' to the circle center will be less than the radius and apply appropriate collision response, for example the simplest response would be not to allow the move from current position r to r' if the new distance will be less than radius.

EDIT: Using D3DX you can the distance between two points by first subtracting their position vectors and then applying the function D3DXVec3Length on the resulting vector. Something like this (Note code is just a quick write-up from memory, not tested)

D3DXVECTOR3* my_vector = new D3DXVECTOR3();float distance = D3DXVec3Length(D3DXVec3Subtract(my_vector, point, sphere_center));

Good night [smile]

[Edited by - Omid Ghavami on May 25, 2007 1:07:46 AM]

##### Share on other sites
CIJolly, it seems to be what I was looking for, thx alot.
Omid, your solution is fast but not accurate since if my point has large velocity
and it collides with the circle there will be a visible gap between the point and the circle on the collision response.

##### Share on other sites
Quote:
 Original post by DesignerXOmid, your solution is fast but not accurate since if my point has large velocity and it collides with the circle there will be a visible gap between the point and the circle on the collision response.

Not necessarily, it depends on what you do in your collision response. Ideally you would rescale your velocity vector and also project it along the tangent of the surface to obtain the so called 'sliding collision'.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
12

• 9
• 21
• 13
• 9
• 17
• ### Forum Statistics

• Total Topics
632608
• Total Posts
3007391

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!