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