-4

I'm trying to make obstacles that stops the player after colliding. Simply, the walls.

I've created collision detection, like this:

if (x > (wall.x1 - wall.boundX) && x < (wall.x + wall.boundX) &&
    y >(wall.y1 - wall.boundY) && y < (wall.y + wall.boundY))
{
    x = 0;
    y = 0;

}

but, as you can see, it just returns my player object to (0,0) on map (by the way, I'm using mouse to control my character). I want it to stop right where he collides and to be unable to move through this wall.

How can I do that?

  • 1
    do you know what the last position of the character was at before the collision? – NathanOliver May 11 '15 at 19:10
  • That's the problem too, because the position of character is related to the mouse pointer position. It's like " if (x < move.x) { x += speed; if (x >= move.x) x = move.x; } " and so on for the rest of the directions (x is the character's X, move.x is the mouse's X). – Only Face May 11 '15 at 20:00

1 Answers1

0

You will need to separate your if statement to handle each wall separately, and then set the x or y position to the bounds of the wall:

if (x > wall.x1 - wall.boundX)
    x = wall.x1 - wall.boundX;

if (x < (wall.x + wall.boundX))
    x = wall.x + wall.boundX;

// etc

No matter how far the mouse attempts to move past the bounds of the wall, it will just remain at that position.

Tas
  • 7,023
  • 3
  • 36
  • 51