0
\$\begingroup\$

I think that I found what the problem is, see below

So I'm working on a collision system and I use multiple tiles in my project, each with a collision box. The player also has a collision box.

I'm testing this collision method so it's not done yet (very basic). I had another collision method in the past but it only worked for one tile and used a lot of booleans so I decided to make a new method.

This is my collision method in my SpriteManager class:

public void Collision(List<PlayerMovement> playerCollision, List<TileSprite> tileCollision)
{
    // For each sprite in the tileCollision list
    foreach (TileSprite t in tileCollision)
    {
        // For each player (head, body, legs) in the playerCollision list
        foreach (PlayerMovement p in playerCollision)
        {
            if (p.collisionRect.Intersects(t.bCollisionRect))
            {
                // Collision methods
                player.CollisionPlayer(p, t);

                // TileBase collision method not used in this test version
                tileBase.CollisionBlock(t);
            }

            if (!(tileBase.bCollision.Any(c => player.collisionRect.Intersects(c))))
            {
                p.ground = false;
            }
        }
    }
}

I basically need to place the player outside the collision block of the tile (now with testing version only above the tile), but when there are multiple tiles next to each other, then the overlapping rectangle changes because it's a simple variable (see below).

This is my collision method in my PlayerMovement class:

public override void CollisionPlayer(PlayerMovement p, TileSprite t)
{
    rect2 = Rectangle.Intersect(p.collisionRect, t.bCollisionRect);
    p.position.Y = p.position.Y - rect2.Height;
    p.jumping = false;
    p.ground = true;
}

You can see that it's really simple (for now). I make a new rectangle that consists of the intersection of the tile rectangle and the player rectangle and then I subtract the rect2 (new rectangle) height from the player position.

This works fine when the player collides with one tile, but he rapidly jumps up and down one pixel (position - lag, not performance) when he collides with more rectangles at the same time. I think this is because the collision method makes a new rect2 and overwrites the old one because the rect2 is only an overlapping of 2 rectangles. But I do not know how to fix this.

Images:

player jumping up and down on 2 tiles player on a normal tile

(NOTE: These images are the lagging behavior of the player when he collides with two tiles at the same time. The right image is how it should be. (it is like this with one tile))

PROBLEM: I think the problem is that only ONE overlapping rectangle is being made because the collision saves the overlapping rectangles all in the same variable (not in a list or an array, so it is overwritten). The problem I have now is that I don't know how to expand the overlapping rectangle to two or more tiles OR how to make multiple overlapping rectangles for this collision method.

\$\endgroup\$
2

1 Answer 1

0
\$\begingroup\$

I found the answer to my own problem:

I now use 4 different colliding rectangles on the player (and one base rectangle). Each rectangle represents a different side of the player so I can see on which side he collides with a block.

Then I calculate the difference in height (y) or width (x) and I return the position of the player with that x, y.

Images:

player on two tiles -> This is the player on two tiles.

player on two tiles -> This is the player on two tiles with the colliding boxes visual.

\$\endgroup\$

You must log in to answer this question.

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