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. Wink

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.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
That's what they do in this 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. Rasp
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #3
Skorche Wrote:Can't say I've ever seen pseudo code written with C syntax. Rasp

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. Wink
Quote this message in a reply
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.
Quote this message in a reply
Sage
Posts: 1,482
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Optimize the collision detection alaslipknot 1 2,236 May 12, 2013 08:02 PM
Last Post: SethWillits
  Collision detection tutorial ThemsAllTook 7 21,571 Nov 5, 2011 05:20 PM
Last Post: SethWillits
  Help with Collision Detection..(i'm almost there) carmine 1 4,307 Jun 29, 2011 12:33 PM
Last Post: ThemsAllTook
  Time Delta, collision detection mk12 19 14,698 Sep 8, 2010 06:40 PM
Last Post: AnotherJake
  Collision detection for pinball game johncmurphy 0 4,501 Sep 6, 2009 02:46 PM
Last Post: johncmurphy