Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
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. –  Mikolaj Marcisz Apr 1 '13 at 3:18
    
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;? –  Mutoh Apr 1 '13 at 3:40
    
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 –  Lou Apr 1 '13 at 4:46
    
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. –  Dialock Apr 1 '13 at 5:09
add comment

1 Answer

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;
} 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.