0
votes

The collection detection method I'm using currently can interpret a collision, but causes strange effects depending on the direction. It will:

  • Always work if the player is hitting the right side of the object.
  • Push the player to the side if the player is hitting the top or bottom of the object.
  • Work the first time if the player is hitting the left side of the object, but will teleport the player to the opposite side of the object the next time a collision is detected on the left side.

This is the current collision detection code:

if(player.playerBounds.intersects(portal.bounds)&&player.isMovingLeft){
       player.playerX=(portal.x+portal.width);
       player.playerX++;
    }
    else if(player.playerBounds.intersects(portal.bounds)&&player.isMovingRight){
        player.playerX=(portal.x-player.width);
        player.playerX--;
    }
    else if(player.playerBounds.intersects(portal.bounds)&&player.isMovingUp){
        player.playerY=(portal.y+portal.height);
        player.playerY--;
    }
    else if(player.playerBounds.intersects(portal.bounds)&&player.isMovingDown){
        player.playerY=(portal.y+player.height);
        player.playerY++;
    }
2

2 Answers

0
votes

Can you try the following code and see if it works?

bool collide = player.playerBounds.intersects(portal.bounds);

if(collide && player.isMovingLeft){
   player.playerX = (portal.x + portal.width) + 1;
}
else if(collide && player.isMovingRight){
    player.playerX = (portal.x - player.width) - 1;
}
else if(collide && player.isMovingUp){
    player.playerY = (portal.y + portal.height) + 1;
}
else if(collide && player.isMovingDown){
    player.playerY = (portal.y + player.height) - 1;
}

Possible reason could be:

  • checking for collision multiple times.
  • moving the player is changing the direction of movement.
  • some other code is conflicting with this code.
  • isMovingX is not working correctly.
0
votes

Going to answer my own question here. The flag for direction moving in was never being changed from true, which was the root of the problems. One direction was normal because it lined up with the first direction I would usually move in.