What to do after I encounter a collision?
-
- Prole
- Posts: 14
- Joined: Fri Nov 02, 2012 12:47 am
What to do after I encounter a collision?
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?
That really depends on what your game is.
- 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?
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.
Sometimes lurks around the forum.
Re: What to do after I encounter a collision?
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.
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?
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.
Re: What to do after I encounter a collision?
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.
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?
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.
Re: What to do after I encounter a collision?
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.
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.
Who is online
Users browsing this forum: No registered users and 2 guests