2D Collision Detection and Response?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: 2D Collision Detection and Response?

Post by N64vSNES »

koolgraph wrote:The Collision Detection works great, except when it collide at a high speed. Exemple

Speed = 10 Char by tick
X = the Player
OO = Some kind of brick/wall

First Tick

Code: Select all

--------OOOO
-X------OOOO
--------OOOO
Enverything's fine

Next Tick

Code: Select all

--------OOOO
--------OOXO
--------OOOO
Now it says the YDistance is less than X distance, so pull back top, but the collision did happened on the side. How to fix this ?

I should say, how to take into account the Velocity in the Collision Response ?
Well of course if you have let's say a wall of 12 pixels and you are 2 pixels away from it and you add 24 pixels onto the first rectangle's position, then a collision never occurred in the first place.
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: 2D Collision Detection and Response?

Post by LeonBlade »

You can also make sure to "pop" your character outside the collision as well by taking the position of the object and subtracting half the size to get the center, then seeing which side the object is closest to and subtract the amount of pixels you are inside away so it "pops" out.

Just make sure if you do this, to make the boundary a pixel in and not just touching, otherwise you'll run into the object and constantly jitter.
There's no place like ~/
User avatar
koolgraph
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Thu Feb 17, 2011 1:49 am
Current Project: A-RPG (MMO ?) Engine
Favorite Gaming Platforms: SNES, GameBoy, PS3, PC
Programming Language of Choice: C++/#

Re: 2D Collision Detection and Response?

Post by koolgraph »

this is alredy done, the problem is the response think it's a collision from top since it's the lowest value between XMagnitude and YMagnitude. So the player don't stick into wall, he's been pulled-back top, but the collision happened on the side.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: 2D Collision Detection and Response?

Post by Falco Girgis »

Welcome to the dark side of collision response... there are various different things you can do.

Make your overall algorithm shittier (in regards to time complexity) and simply check for collision every time you move the character (so that it doesn't actually penetrate). Or you can check in discrete timesteps... Check for collision every time an object moves a distance as large as the smallest half-width in the scene...

You also know the direction of the collision normal, and the direction that the character moved that frame. Obviously, if the collision normal is in the same direction as the object's velocity vector, he has penetrated too far and is being pushed out the wrong side...

Or you can do my personal suggestion: don't let that happen. In a 2D game, 24 pixels per frame is fast as FUCK. Nothing should be traveling that fast, and your collidables should not be so tiny that things can pass through them that quickly.

Regardless, the overall tradeoff here is going to compromising the speed of your collision detection to either add special logic or use another algorithm to resolve this scenario...
User avatar
koolgraph
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Thu Feb 17, 2011 1:49 am
Current Project: A-RPG (MMO ?) Engine
Favorite Gaming Platforms: SNES, GameBoy, PS3, PC
Programming Language of Choice: C++/#

Re: 2D Collision Detection and Response?

Post by koolgraph »

Thanks, but i found a way using the Velocity Vector along with a formula to know the collision point between 2 straight. Then check the 2 edges of the Collision rectangle where it could logically collide with (if going right-top and it's left-bottom) and return the edge which as been check successfully.
Post Reply