View Full Version : Here's a tip on collision detection
Andrew
2005.08.16, 02:54 AM
I worked out this algorithm last night which works really well, and I thought I'd share it with you guys. ;)
Here's the pseudo-code:
BOOL animateAndReportWhetherCollisionOccurred() {
advanceOneFrame();
if (shapesOverlapEachOther()) {
rewindOneFrame();
changeVelocityAsAppropriate(); // could be a simple reflection
advanceOneFrame();
return YES;
}
return NO;
}
This is for a rigid body bouncing off another rigid body (E.G. a super bouncy ball hitting a wall).
The reason why I find it better than my old method (which was using a simple "if overlap, change direction" scheme) is that it prevents false positives from occurring. False positives are what often lead to objects getting stuck.
Skorche
2005.08.16, 04:09 AM
That's what they do in this (http://graphics.stanford.edu/papers/rigid_bodies-sig03/rigid_bodies.pdf) article. (1.9MB pdf)
Good read, but can anyone figure out their impulse equations? For the life of me I can't figure out how to get frictional impulses to work.
BTW: Can't say I've ever seen pseudo code written with C syntax. :p
Andrew
2005.08.16, 06:13 AM
Can't say I've ever seen pseudo code written with C syntax. :p
What can I say? I have a fetish for semicolons and curly brackets.
Thanks for the link! I'm no physics major, so it's going to take me quite a while to decipher all of the symbols they use in their equations. Nevertheless, judging from the portions of the paper which I do understand, it looks like exactly the type of info I'm after ATM.
Also, knowing that these guys suggest using the same algorithm I proposed in the parent gives me confidence in my code. ;)
imikedaman
2005.08.16, 04:55 PM
I'm no 3D expert, but when I wanted to add some collision detection functions to my game engine I just looked up the equation for a ray intersecting a triangle in 3D space.
The ray is defined as two points, so I give it the start point and end point of the player (before and after he moved during that iteration), then the equation returns the exact point of collision. Since rays are infinite in one direction, I just add a simple check at the end to make sure the two points are indeed long enough to intersect the triangle.
That algorithm would work even if the object was going so fast that it passed completely through the wall during that iteration.
Skorche
2005.08.16, 07:00 PM
I'm no 3D expert, but when I wanted to add some collision detection functions to my game engine I just looked up the equation for a ray intersecting a triangle in 3D space.
That works fine for particles, but you need something more robust for rigid bodies.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.