# Make a 2D character stop moving and going back when encounter an obstacle

I am using a 2d character who is sliding infinite ( huge incrementation of the X or Y value ) when moving. He can only move up, down, left, right inside a virtual grid, made of multiple squares.

Now I want to add obstacle to stop his sliding and make him go back to the center of the previous square

The obstacles can be of different size and are located in the center of a square, so the collision occurs with the obstacle and NOT when the character enter the square containing this object !

How can I manage to stop the incrementation of X or Y when colliding ? And get the previous value of X or Y for which the character was on the center of the square just before the one containing the obstacle.

Thank you everybody !

I am not certain to have the good reasoning. Actually I was thinking about something like that :

Lets say each square is 5 * 5 and each square center is at X + 5 from the previous square center and X - 5 from the next. I was thinking about a loop which is moving the character from + 5 on the X axis and checking if there is a collision. If yes, the character goes back to the previous square center, if not, the loop keep going.

So assuming that my character is in ( 0 ; 0 ) coordinates and the obstacle is in ( 20 ; 0 ) coordinates ( but not filling the square ), my character will be in ( 5 ; 0), then ( 10 ; 0 ), then ( 15 ; 0 ), then it will collide with the object in ( 18 ; 0 ) ( because the object dont match exactly with the square size, considering the fact that on object can be 3*3 and just drop on a 5*5 square ), and the character will go back to ( 15 ; 0 ).

With this reasoning :

• is the movement of my character going to be smooth ( like a slide ) or is it going to be square by square ?
• if checking the collision BEFORE moving, is my character going to move from ( 15 ; 0 ) to ( 18 ; 0 ) and bounce back to ( 15 ; 0 ) or is it just going to stay in ( 15 ; 0 ) ?

because I would like to have my character to move smoothly AND bouncing back when colliding !

Thank you

• I'm not sure what answer you want, beyond "if a collision happens, send the character back the opposite direction". What is preventing you from just coding exactly what you've said in the question? Jan 15, 2013 at 23:55

You'll stop the incrementing of X or Y when a collision is detected. That'll depend on where you're looking for collisions. I assume it would be before you move your character to its new position.

Getting the center of the previous square isn't too hard either. Since your squares are equal sized, it's easy to round to the nearest center position in the opposite direction of travel. For example, if the direction of travel is in the X positive direction and the X position is at 43.3 when you collide. You know that there's a center square at n.5 along the X axis. Since you were going in the X positive direction, you want to find the nearest center in the X negative direction. So round the X position down to the nearest integer and subtract .5.

You generally want to have the player's X and Y coordinates and the player's X and Y velocity...You determine the velocity then add the velocity to the x coordinate to move, then you subtract the velocity from the x coordinate to stop.

if (right)     player.xVel+=player.movementSpeed;
else if (left) player.xVel-=player.movementSpeed;

player.x+=player.xVel;
if (collides)  player.x-=player.xVel;


You could probably do the following, but I've always moved the player before checking collisions to simplify my code, so I don't know how well this method would work...

if (!collides) player.x+=player.xVel;