## Here's a tip on collision detection

Member
Posts: 208
Joined: 2005.04
Post: #1
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:

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.
Sage
Posts: 1,487
Joined: 2002.09
Post: #2

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.
Member
Posts: 208
Joined: 2005.04
Post: #3
Skorche Wrote:Can't say I've ever seen pseudo code written with C syntax.

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.
Member
Posts: 161
Joined: 2005.07
Post: #4
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.
Sage
Posts: 1,487
Joined: 2002.09
Post: #5
imikedaman Wrote: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.