Movement in response to collisions

Monkey Forums/Monkey Programming/Movement in response to collisions

elite_dtn(Posted April) [#1]
I have successfully coded the code which detects when there is a collision between the code, but i am struggling on the code for the player when they react to a collision. For example when they hit a wall / platform they should stop and not be able to go through it but when they land on top player jumping should be disabled and the player should be held at that position.


Gerry Quinn(Posted April) [#2]
The simplest way to do it is to move fractionally in the direction you are going, but if there is a collision with something you can't (at least currently) go through, you move back normal to the object (at right angles to its surface) until you are just touching it.

I'm sure you know how to make a ball bouncing in a rectangle - everybody's first collision code. In that code you reverse the velocity perpendicular to the wall when you detect a collision. But in a platform game, you just ignore or zero that velocity component, and move the character back to the surface - no 'bounce'. You'll have gravity always trying to create a downward velocity, so this will keep happening when you're walking on a surface.


elite_dtn(Posted April) [#3]
I have done this but because my game is a platform game the response to a tile should be different depending on which side of the tile the player collided with however how do i program it so that i can calculate which side of the tile the player has collided with so that i can program different responses to this?


Gerry Quinn(Posted April) [#4]
I think the usual way is to base it on which way the player is moving (of course you'll need to use relative movement if the tile is moving too).

This ties in nicely with basic platform concepts. For example it's easy to see how it works in the common situation in which you can jump up through tiles, but they stop you when you fall down from above. [The player position is usually relevant here too - i.e. the player doesn't get stopped unless he is completely above the tile when he falls onto it. If he jumped so that only his head went through the platform, he would fall down again.]

So player position and player movement direction (both relative to the tile) have to be considered.