Page 1 of 1

What to do after I encounter a collision?

Posted: Sat Mar 30, 2013 1:54 am
by tommyroyall
How would I go about handling a collision between two solid objects after I detect it. I have the detection down, but what would I do past that?

Re: What to do after I encounter a collision?

Posted: Sat Mar 30, 2013 4:35 am
by MarekkPie
That really depends on what your game is.

Re: What to do after I encounter a collision?

Posted: Sat Mar 30, 2013 10:49 am
by substitute541
Be specific when asking questions. It really depends on what your game is. For example, if you're checking for collisions between mouse and button, the next (obvious) step is to check whether the mouse clicked the button, else just change the button to it's hover state (if there is).

Re: What to do after I encounter a collision?

Posted: Sat Mar 30, 2013 2:46 pm
by veethree
Depends on what you're doing.

One of the more common collision situations is that one of the objects is immobile (like a wall) and the other is not, Such as your player. Once collision between the two is detected, You would stop the player from moving, Then correcting his position so collision no longer occurs. In that situation it'd be handy if your collision detection function returned by how much the 2 objects are overlapping so you can move the player back by that amount.

But that's just one example, If you provide more information regarding what you're trying to achieve, You'll probably get a better answer.

Re: What to do after I encounter a collision?

Posted: Tue Apr 02, 2013 1:22 am
by Lafolie
This thread certainly merits the description of "general"!

Re: What to do after I encounter a collision?

Posted: Wed Apr 03, 2013 9:49 am
by abigsmurf
Sorry for hijacking a topic a bit but the OP hasn't replied for a few days and it's better posting in an existing thread than creating a new one. Would just like advice on my approach to collision detection for a platform game.

Tried doing a teleport where if you walked into a block it would transport you to the edge you walked into (which would always be the opposite to the way you were facing and set speed to 0. This worked slightly better but you could turn around at a specific time after walking into the block and find yourself teleported to the other side. To solve this I had the collision detection 'ignore' the back 20 pixels of the player and this seems to fix it. The only problem is it'll make it impossible for a moving block to push the player along from behind.

Would a better approach be to do a bounding box detection as I'm currently doing, but instead teleporting you based on the direction you're moving and setting the speed on that axis, do it based on which edge you collided with (ie left, right, top, bottom) and stop you teleporting across and entire block when facing the other way?

How would I reliably test for which edge was collided with? Colliding with the left side would probably result in the player's box covering the whole of the left edge but also slightly covering a pixel or 2 of both the top and bottom edges which makes it much more complicated.

Re: What to do after I encounter a collision?

Posted: Wed Apr 03, 2013 4:16 pm
by Lafolie
Try updating the player position before you do any collision checks.

Re: What to do after I encounter a collision?

Posted: Thu Apr 04, 2013 7:52 am
by abigsmurf
I've got it sorted now,

Doing the collision detections after all the position changes are done did help a lot thanks, made it far simpler to do collision detection without the player 'vibrating' against edges.

Had issues with the player getting teleported around the block (the code preventing players getting stuck in a block misjudging which edge the player was touching), fixed that by shrinking the detection area on the y axis (preventing it detecting you hitting the top/bottom when walking into a block from the sides) and made sure to only activate the x axis collision detection if the top corner or the bottom corner of the block intersected the right or left sides of the player (which prevents it thinking you're hitting the edges when walking on top.

A neat (unexpected) side effect is that the 3-4pixel Y-axis leeway the code gives when walking left or right means that the code can handle the player walking up shallow stairs smoothly.