What to do after I encounter a collision?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
tommyroyall
Prole
Posts: 14
Joined: Fri Nov 02, 2012 12:47 am

What to do after I encounter a collision?

Post 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?
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: What to do after I encounter a collision?

Post by MarekkPie »

That really depends on what your game is.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: What to do after I encounter a collision?

Post 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).
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: What to do after I encounter a collision?

Post 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.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: What to do after I encounter a collision?

Post by Lafolie »

This thread certainly merits the description of "general"!
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
abigsmurf
Prole
Posts: 2
Joined: Wed Apr 03, 2013 9:22 am

Re: What to do after I encounter a collision?

Post 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.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: What to do after I encounter a collision?

Post by Lafolie »

Try updating the player position before you do any collision checks.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
abigsmurf
Prole
Posts: 2
Joined: Wed Apr 03, 2013 9:22 am

Re: What to do after I encounter a collision?

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest