Archived

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

JonW

Time based movement and collision detection

Recommended Posts

Hi, I''m using time based movement to bounce a ball around the screen in my 2D game. The movement processing code looks like this: // Get # of milliseconds since last frame DWORD dwMilliseconds = GetTickCount() - dwLastFrame; x += fPixelsPerMillisecond * dwMilliseconds; y += fPixelsPerMillisecond * dwMilliseconds; // Check for collision... But a problem occurs if the computer falls way behind, and the number of milliseconds since the last frame is so high that the ball goes right through a bunch of objects when it suddenly makes a huge leap forward. How can I prevent this? I thought about something like this... // Get # of milliseconds since last frame DWORD dwMilliseconds = GetTickCount() - dwLastFrame; // Check collision for each update for (int i = 0; i < dwMilliseconds; i++) { x += fPixelsPerMillisecond; y += fPixelsPerMillisecond; // Check for collision... } ...but of course, you would have to check for collision 1000 times per second, which is not good.

Share this post


Link to post
Share on other sites
the trick is to check for collision before you move the object. If you check to see whether the vector that makes up you velocity will intersect something then you can deal with the collision. The article Advanced Character Physics on Gamasutra.com, don''t know the exact link, explains these sorts of techniques quite well.

Share this post


Link to post
Share on other sites
Thanks. The article was a pretty good read.

I was thinking about the problem more. The bounds of all the obstacles in the game can be represented with 2D line segments. All of the moving objects use bounding spheres to check for collision. What I''m trying to do is to go through all of the lines in each obstacle, find out when the moving object''s vector intersects the line segment (if ever), and subtract the time of the collision from the total time the object has to move (since the object bounces off that line, it has to continue its movement in a different direction).

I know how to find the time when the two parametric lines (one for object vector, one for obstacle bounding box edge) intersect, but I came across a problem; what if the bounding sphere of the moving object intersects the obstacle line segment, but the moving object''s vector doesn''t? Since the vector is just a skinny line segment coming from the center of the sphere, the edge of the sphere could easily clip the obstacle line segment without the lines intersection.

So the question:

How I can test to see if a sphere intersects a line segment sometime in between its current position and its position at some time t? Also, I need to know at what time the intersection was made.

Thank you.

Share this post


Link to post
Share on other sites
For your new problem, sit down with some paper and a pencil, make some drawing and try to figure it out. You will feel a lot better if you can figure it out yourself then if someone told you. Also, if you have any bugs you will understand your code better.

Otherwise, look on google for: line intersect circle.

Share this post


Link to post
Share on other sites
just needs a few changes, depending on how you do it now. assuming youre solving an equation to find out when the two lines intersect. instead make it a little more complicated. calculate the distance between a point and a plane, but insert the line equation for the point. then solve for a distance of radius to find out at which point they intersect and see if the point of intersection is in your line segment. problem: at the corners it might result in an intersection outside the line segment and misses that a little later there would have been a collision with the corner. i didnt try that (and depending on the size of your sphere it wont be noticed), but in such a case you could add a collision test with the corner. working quite the same except you now solve the distance between two points for radius.

btw. this can be a lot easier of all your obstacles are axis aligned.

another approach would be to either assume enough speed to keep the movement small or subdivide your movement if a step is too big. of course if your physics still get complex its not really helping to react by doing even more work when the machine cant keep up anymore ,-)

Share this post


Link to post
Share on other sites
Do more physics iterations per frame - check your last frameinterval and divide by the deltatime of your physics loop then do that number of iterations - when there are only a few objects there will be no problem.



[ My Site ]
''I wish life was not so short,'' he thought. ''Languages take such a time, and so do all the things one wants to know about.'' - J.R.R Tolkien
Founding member of "Un-Ban nes8bit" association (UNA) (to join put this in your sig) (Welcome back to JesperT)
/*ilici*/

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
That''s still not a reliable solution. Depending on the size of the timestep and the speed of the object there is still the chance you can pass through an object without a collision being detected. The best way is to do an intersection of the vector your object is traveling along, or create a bounding capsule out of the object''s bounding sphere and test that for collision.

Share this post


Link to post
Share on other sites