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.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.

  • \$\begingroup\$ I am not sure if this would work, but you can try to to add a factor of 1% to the player width, that way you should have a little gap between the object it once helped me with camera collision-detection in unity it would follow the player and bump inside every physical body, it got solved with adding a tiny hole between them. (platform.position.x - player.width*1.01) Might work to keep the bumping out. \$\endgroup\$ Apr 1, 2013 at 3:18
  • \$\begingroup\$ I find this excerpt of code pretty weird: player.position.X = platform.position.X - player.width; Shouldn't it be player.position.X = platform.position.X - platform.width;? \$\endgroup\$
    – Mutoh
    Apr 1, 2013 at 3:40
  • \$\begingroup\$ well the actual origin of the platform is in it's top left corner so if the player is walking to their right and collides with the platform's left side it should theoretically put then at the smallest possible distance from the platform after the collision \$\endgroup\$
    – Lou
    Apr 1, 2013 at 4:46
  • \$\begingroup\$ hint Currently each 'frame' you move '5' magic units. You then check your position for collision. Maybe you should be checking for 'future' collision before allowing movement. \$\endgroup\$
    – Dialock
    Apr 1, 2013 at 5:09

1 Answer 1


I have drawed this to explain the behaviour when a collision with AABB boxes is present.

enter image description here

When you have choosen what dimension has to be fixed, you can change the position of the object that was moved accordingly.

Here is some code to accomplish the task, (it's not tested and will require tuning for sure)

var P = player.CollisionRect;
var Q = platform.CollisionRect;

int W, H;

W = H = int.Max;

if (P.Right < Q.Left) return;  // No Collision
if (P.Left > Q.Right) return;
if (P.Top > Q.Bottom) return;
if (P.Bottom < Q.Top) return;

W = (P.Left<Q.Left)  
       ? P.Right - Q.Left
       : P.Left  - Q.Right;
H = (P.Top<Q.Top)  
       ? P.Bottom - Q.Top
       : P.Top  - Q.Bottom;

if (Math.Abs(W)<=(Math.Abs(H))
   P.Location.X -= W;
} else {
   P.Location.Y -= H;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .