#### Archived

This topic is now archived and is closed to further replies.

# Line-Sphere Collision Detection Problem.How do you guys do this?

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

## Recommended Posts

Hi Everyone! I am currently working on my sphere-line/point-line collision detection. Basically, as how everyone is probably doing it, i do a line segment intersection check using parametrization wherein one line segment is the wall, and and the other line segment is the movement vector of sphere/point as it travels from one game frame to the other. Then after i verified that there is collision, i get the intersection point(which is also the collision point) and set the new coordinates of the movement vector of the sphre/point with the value of collision point. Say: |W1-W2| <- line segment of wall from W1 to W2 {P1-P2| <- movement vector of point/sphere from P1(its location at time 0) to P2(its location at time 1) where time 0 is time the point/sphere moved in the last game frame and time 1 is time the point/sphere moved in the current game frame Checking for line intersection between the 2 vectors and assuming intersection at point H, the new point/sphere movement vector becomes |P1-H| and then i calculate the new velocity. Now here''s my problem. After this frame my sphere/point is now located at H, and H is a point where it collided with wall. Therefore, in the next game frame, if i check the line intersection between wall line segment and sphere/point''s movement vector(which is |P1-H|), then i will still detect collision(at the same point H) wherein i am not supposed to. How do i fix this? I appreciate any help from you guys and i thank you in advance.

##### Share on other sites
I may very well be misunderstanding you, but...

Since you''re describing the wall as a line, I assume you''re in 2D? In which case you''re dealing with a circle rather than with a sphere...

Anyway, if you''re just finding the intersection between the segment describing the wall and the segment describing the motion of the circle center, you''re not taking into account the radius of the circle. But if it''s just a point, I suppose it''s ok...

I''m not sure if I follow what you''re doing after that. Why would the movement vector still be P1 - H next frame?

Generally after finding the time of intersection using a swept test like this, you want to move *just short* of the intersection point. That way you won''t be initially intersecting the same object next frame.

##### Share on other sites
I had the same first instinct when I first tried collision. Nope, it''s all wrong. You can''t just use a direction vector for the new position of the sphere and cast a ray there. I''m not sure if this is related to your problem, but your collision will be VERY glitchy, allowing half of the sphere to sink into things, just because it''s not moving towards them. You have to go through ALL of the triangles in the area and find the closest point on them to the sphere, then make sure the distance to that point is more than the sphere''s radius.

It''s a real bitch. I would recommend taking your time. Here are some functions you''ll need to write:

BOOL IsPointInTriangle
VOID GetClosestPointOnPlane
VOID GetClosestPointOnLine
FLOAT GetClosestPointOnTriangle // main function

Sorry if I misunderstood you.

##### Share on other sites
Hi Guys,
Thanks everyone for your response on my message. My sincere apologies for not being able to reply so soon.
With regards to my problem, and to explain to you further, i am actually working on a pool/billiards 2d game and this is my first try on writing a pc game. all my collisions ( circle-wall and circle-circle) is working. Also they are integrated and in such a way that all object movements are relative to each other. Here''s the sequence of my game loop:

1. update/move ball positions
2. using movement vectors (ball) and line vectors ( wall ), verify collision and get collision point of each collision detected
3. then calculate earliest collision ( shortest vector from previous position to hit point among all the balls that moved, if collision occured
4. move back the rest of the balls with longer collision vector to points relative to time the first ball collided with something. Also, move back the time elapsed
5. As Jyk mentioned, yes i move back the objects with additional "just short" so the balls will be placed "just barely" hitting another ball or wall.
6. then update new velocities of balls
7. do this until there are no more collisions or time elapsed reach 0

The problem i actually have is in no 5, but i seem to have solved it already as previous i am calculating the new position incorrectly. Now they seem ok.
but now i have noticed a new problem. Ideally after item 6 i would expect that no more balls will still be in collision with something as they have all moved back a certain position or at least "just barely" hitting another ball/wall.
But to be sure, i have placed collision check at the end of item 5, and surprisingly, it still detects ball-ball collision, while no more ball-wall collision.
Therefore it seems that i still have problem with ball-ball collision. may i know if any of you guys experience this as well? what may be causing it? and how to fix/eliminate it?

##### Share on other sites
"But to be sure, i have placed collision check at the end of item 5, and surprisingly, it still detects ball-ball collision, while no more ball-wall collision."

If you''ve rolled back the simulation to the time of the first registered collision minus ''a little bit'', then there shouldn''t be any intersections at that time. Can you do some more debugging? Maybe display again after step 5 to see where the intersections are? Anyway, I think your concept is sound, but you must have a glitch in your code somewhere.

##### Share on other sites
Hi Jyk,
Yes, you are right. After spending 2 days, i have found that "glitch" in my code that's causing it. I made a mistake in subtracting an EPSILON value to the hit vectors as i subtracted an absolute value of EPSILON in the hit vectors of all the balls so that they will end up "just barely hitting" the wall or another ball. I realized that the value to be subtracted must be relative to the amount i moved back the hit vector of the first ball that collided.
In summary, i am happy to say the problem is fixed.

[edited by - harmless on June 13, 2004 2:51:05 AM]

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 9
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633331
• Total Posts
3011393
• ### Who's Online (See full list)

There are no registered users currently online

×