Right now I have a player object and an obstacle, both with bounding box rectangles around them. The issue is that when my player runs into this obstacle, as long as I'm holding down the movement key, my player goes slightly inside the obstacle, then when I let go of the key my player is placed outside of it.
The movement code in the update function of my player class is as follows:
if (Input.isKeyDown(Keys.Left))
{
velocityX += 5;
}
_position.X += velocityX;
The collision code in my SpriteManager game component is:
if(player.collisionRect.Intersects(platform.CollisionRect))
{
if(player.position.X >= (platform.position.x - player.width))
{
player.velocityX = 0;
player.position.X = platform.position.X - player.width;
}
}
I have searched online for the past two days and have been unable to find any solution to this problem and I have been unable to figure out what I have been doing wrong. Even though this seems like a fairly simple problem. Any help would be greatly appreciated!
Edit: I can tell that my issue is that when I collide, my player is still moving into my object by the amount of velocityX
. So what I need to do is check if there is about the be a collision with the box, however I am still unsure of how to do this.