Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Why getGlobalBounds + rest of my code doesn't forbid object moving this way  (Read 1590 times)

0 Members and 1 Guest are viewing this topic.

MDK

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
In this fragment of my code, that i posted below i expect to make the object stop moving in one direction, but let him to move in other directions. Like this:


But the outcome is that they colide, because of getGlobalBounds, but i can move my object freely in every direction and through second object.
I have this in code: playerPosition.x + 60 to move x point to the end of x axis of the object, but i think it does nothing.
And when i change equality sign "==" to not equal it forbid me from entering the object from both sides, and when i enter from back side it slowly move me towards -x till it push me out from object.
What is wrong with this code?

     Vector2f playerPosition;
     Vector2f enemyPosition;
     playerPosition = playerS.getPosition();
     enemyPosition = enemy1S.getPosition();

             if(playerS.getGlobalBounds().intersects(enemy1S.getGlobalBounds())) {
                playerS.setColor(Color(255, 80, 0, 200));

                if((playerPosition.x + 60) == (enemyPosition.x)) playerSpeedVector.x = -10;
             }
             else playerS.setColor(Color(255, 255, 255));

            playerS.move(playerSpeedVector * time.asSeconds());
            heroMoveClock.restart();
 

« Last Edit: March 04, 2017, 12:58:36 am by MDK »

MDK

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
I solved it... partially but solved it.
Instead of what i started:  if((playerPosition.x + 60) == (enemyPosition.x)) playerSpeedVector.x = -10;
I have this:

                if((playerPosition.x) <= (enemyPosition.x)) playerSpeedVector.x = -10;
                if((playerPosition.x) >= (enemyPosition.x)) playerSpeedVector.x = 10;
                if((playerPosition.y) <= (enemyPosition.y)) playerSpeedVector.y = -10;
                if((playerPosition.y) >= (enemyPosition.y)) playerSpeedVector.y = 10;
 

But now when my object bouncing from second object it bounce diagonally. And i want to bounce it, well, not diagonally. Can someone help me with that?