#### Archived

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

# accuracy in collision resolving

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

## Recommended Posts

I have circles moving at a defined velocity each frame. I know that at frame1 no collision occured, and that at frame2 there was a collision. To find the collision point in time, I find the rate of change of distance between the circles, and calculate the percentage of time between frame1 and frame2 when the distance was the sum of the two radii. I then move the circles back to the position they were at at that point in time along the frame. The problem is that due, I assume, to innacuracies in the calculations, the final positions of the two circles ends up being a very small amount ''out'' (ie, the distance between the two is not exactly r1+r2). Does anyone know of a nice simple example of resolving this situation gracefully, or have any comments on the mechanism?

##### Share on other sites
I''m not familiar with the method of collision detection you are describing, so I can''t tell you whether your problem is related to that. But in general, you usually can''t count on a continuous swept test to give you exactly accurate results. The spheres (or circles) may end up a little apart, just touching, or just intersecting.

A common solution is to move the objects by the collision time minus an epsilon, so that they end up just short of each other. Generally the effect is adequate, and you won''t have any problems with initial intersections.

##### Share on other sites
With fixed or floating point arithmetic, it is inevitable that there is some error in the location of the circles in the end. This is roundoff error. You're going to have to get used to that. The roundoff error will be better or worse depending on your time and length scales (seconds or days, millimeters or kilometers), the size of the objects relative to each other and to their velocity, etc. Single precision floating point will have larger roundoff than double precision.

If all is reasonable (you're using meters and seconds, your objects are neither very massive or very microscopic, velocities are neither practically zero nor the speed of light), the roundoff error should be small enough to be manageable. You may want to use r1 + r2 + an epsilon to be sure the roundoff doesn't put the objects closer than r1 + r2. If your objects are "reasonable" and your velocities are "reasonable" and your units are all consistent, and your error is still basically non-negligible compared to r1 + r2, then you may have an error in your implementation. You could post some brief code or pseudocode...

It is possible to use numerical techniques to get a bit closer than algebraic solutions in some cases, but my feeling is that you don't have one of those cases.

Graham Rhodes
Principal Scientist
Applied Research Associates, Inc.

[edited by - grhodes_at_work on June 9, 2004 10:38:53 PM]

##### Share on other sites
Thanks guys, it sounds like I''m just having the sort of rounding errors I''d expect (insteady of distance apart being 100, I get 99.94, for instance).

Pseudo code for what I''m doing is as follows (since people might either learn or kick my butt for having done stupid things, and both are good):

db = distance between two circular objects (obj1, obj2)if( db < radius1+radius2 then	set db_last = distance between objects at last frame (before collision)		change_db = db_last - db // The change in the distance between the two objects		// Calculate the time (percentage of) during moving from frame1 to frame2 at which the distance between the two objects was radius1+radius2	percent_time = ( radius1+radius2 ) / change_db;		Move both objects to location they would hold at percent_time/1 of the full frameend if

##### Share on other sites
you can use the squared length to remove the sqrt.

lookup pool hall lessons on gamasutra by jan van den heuvel i think. it describes predicting positions and time of collision for pool balls which are the best balls.