0
\$\begingroup\$

I'm working on a platformer as part of my course, the problem I have come across is Left/Right sided collision is not working very well. I already have bottom & top collision working. Here is an example of the problem in action:

Issue

As you can see pretty much all the time I'm slowing down, I know this issue but haven't found a work around code solution yet. The main problem I'm having as you can see is my player being able to slide almost into the middle of the block if he's coming down and moves into the block.

The slow down problem is due to my left/right collision code, I just haven't found a viable solution yet for sided collision.

Collision Code:

        foreach (SkyTile block in PlatformBlocks)
        {

            IntersectRectangle = block.GetRect();
            GetRect().Intersects(ref IntersectRectangle, out IntersectResult);
            if (GetRect().Intersects(block.GetRect()))
            {
                if (IntersectResult && (IntersectRectangle.Bottom > GetRect().Bottom))
                {
                    isPlatformColliding = true;
                }

                if (IntersectResult && (IntersectRectangle.Top < GetRect().Top))
                {
                    isPlatformBottomColliding = true;
                }

                if (IntersectResult && (IntersectRectangle.Left > GetRect().Left))
                {
                    isPlatformLeftColliding = true;
                }

                if (IntersectResult && (IntersectRectangle.Right < GetRect().Right))
                {
                    isPlatformRightColliding = true;
                }
                break;
            }
        }

        if (isPlatformColliding)
        {
            velocity.Y = 0;           
        }

        if (isPlatformBottomColliding && !isPlatformColliding)
        {
            velocity.Y = 20;
        }

        if (isPlatformLeftColliding)
        {
            velocity.X = 0;
        }

        if (isPlatformRightColliding)
        {
            velocity.X = -0;
        }
\$\endgroup\$
1
  • \$\begingroup\$ I think it should be IntersectRectangle.Left < GetRect().Left and IntersectRectangle.Right > GetRect().Right. You have it vice versa \$\endgroup\$
    – dimitris93
    Aug 15, 2015 at 16:00

1 Answer 1

2
\$\begingroup\$

Could you post your update loop? I don't immediately see something wrong with the code you posted which causes this behavior so it is possible the problem is somewhere else in your code.

Usually you would do something like this when programming a simple physics engine in your game: 1. Update positions of the objects based on their velocity 2. Check for collision and resolve these

If I understand your code correctly you do not resolve collisions but rather update the velocity to prevent the player from moving through a rectangle. There are a couple of problems with this approach:

  1. The player will still be colliding with the rectangle which you can argue is an undesired state.
  2. If you update the position of the player before checking for collisions the player will move through the object (I suspect this could be the problem you are experiencing).
\$\endgroup\$

You must log in to answer this question.

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