Archived

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

Codejoy

2d circle-line collision problem, velocities so fast miss collision....

Recommended Posts

Codejoy    122
I am using a algorithm gotten from : http://astronomy.swin.edu.au/~pbourke/geometry/sphereline/ to test if a ball in my 2d golf game hits a line (right now just have two lines on the field one vertical and the other horizontal). The collision works well, when the ball runs over the line causing a collision i flash that up , and write out to a log file the values. So I know that the collision is happening more or less correctly, the problem comes in when the ball travels faster in a frame than the collision can test. Since its just a line, there is a problem: We have a line, and a ball flying on the screen, if the ball is going slow enough and at the right angle, the collision happens, if the ball is going to fast at the wrong angle to create the scenario: Frame 1: Ballx, Bally Frame 2: Ballx+Ballxv, Bally+Ballyv Ball is before the line, then due to the xv and yv being high enough the ball is over the line in the next frame, so nowhere does a collision happen, even though the ball graphically moves through the line... im sure this is a common problem, im just not sure how to go about fixing it, anyideas? I have tried this in my sphere line inetersect function calling it with not just the x+offset, y+offset (where offset is to get the center of the ball or circle) but with: x+offset+xv, y+offset+yv and even x+offset-xv, and y+offset-yv just to see if i can get the collision to happen more on time no matter what the velocities are, any ideas what i have to do to get this to work properly every time? Thanks, Shane

Share this post


Link to post
Share on other sites
fup    463

Keep track of the ball''s position at the previous update-step (P1) and the position at the current update-step (P2). Then test the line segment P1-P2 against your line. If it intersects then you know the ball has travelled across the line.



My Website: ai-junkie.com | My Book: AI Techniques for Game Programming

Share this post


Link to post
Share on other sites