TileMap Collision movement problem

BlitzMax Forums/BlitzMax Beginners Area/TileMap Collision movement problem

Rixarn(Posted 2008) [#1]
Hi! Iīm giving up in trying to code this by my own and i humbly ask for help :).

My problem is not the collision actually, but what has to be done when a collision is detected... flag the direction the player is moving to prevent that input (key_up for example) to keep moving the player in that way?

Im really lost here, i can have one collision detection at a time, but when i do lets say top and down collisions at the same time, the player freezes as soon as it collides...

thanks in advance!


tonyg(Posted 2008) [#2]
Before moving the player check whether the move will cause a collision. If it will then don't move the player or change the number of pixels moved by the player to 0.


QuietBloke(Posted 2008) [#3]
As tonyg says you dont stop the user from pressing the key but rather set the movement to zero if that movement will cause a collision. From what I am make out from your post your problem is when the player is moving diagonally for example up and to the right and hits a wall at the top you want the player to continue sliding along the wall to the right.

One possible solution could be:

Say you have the two variables PlayerX, PlayerY which have the players position. You would then have two more variables PlayerMoveX, PlayerMoveY which are reset to zero before you check for player input.

Then based on what keys the player is pressing you would set the movement variables accordingly.
So if the player is pressing the up key and the right key then PlayerMoveX would equal 1 and PlayerMoveY would equal -1.

Now you can check to see if the movement would cause a collision.
The trick here is to check each axis seperately.
First check if the movement in the x direction would cause a collision and if it does then reset the x movement.

If checkCollision(playerX + playerMoveX, playerY ) = true Then
playerMoveX = 0
End If

Now check in the yDirection

If checkCollision(playerX, playerY + playerMoveY ) = true Then
playerMoveX = 0
End If

Finally you can now set the new position of the player

playerX :+ playerMoveX
playerY :+ playerMoveY


Hope this is what you were after and it helps.

Cheers


Rixarn(Posted 2008) [#4]
Hey! thanks both for your fast replyes! :)

I did it and it worked! was doing something similar like you said but not in advance (ie. stop movement when player had allready collided.. thatīs why he got stuck) and btw,,, the conflict i had was in mi "Player viewport" code, since i want to avoid to allways move screen when player moves, i did a no move screen zone for player (ie. x:300,y:300,w:200,h:200 so as long as player image is inside that zone the whole map doesnt need to scroll)...also your idea is good QuietBloke, i might do it some time later :)