C # 2D collision detection problem

I am stuck trying to figure out how to change my collision detection to work correctly, I got all my wall objects stacked inside the list and then when the player moves me through each wall object and calls the DetectCollision method this returns true or false depending on on whether the object is inside the wall or not.

Collision with wall detection (X and Y coordinates are the position of the wall)

public bool DetectCollision(float x, float y)
    {
        if ((x >= this.XCoordinate && x <= (this.XCoordinate + this.BlockWidth)) && (y >= this.YCoordinate && y <= (this.YCoordinate + this.BlockHeight)))
            return true;            
        else
            return false;
    }

      

So in my player function, when the player tries to move, I add movement to the X, Y time coordinate and check if they are touching the wall if they do nothing, otherwise I move the player.

But I noticed that it doesn't work as it should, if I add a part of the wall inside the playfield, does it only check the bottom right corner for collision detection?

Player movement method:

        float x, y;
        if (direction == Direction.E)
        {
            x = LiveObjects.player.XCoordinate - MovementSpeed;
            y = LiveObjects.player.YCoordinate;
        }
        else if (direction == Direction.W)
        {
            x = LiveObjects.player.XCoordinate + MovementSpeed;
            y = LiveObjects.player.YCoordinate;
        }
        else if (direction == Direction.N)
        {
            x = LiveObjects.player.XCoordinate;
            y = LiveObjects.player.YCoordinate - MovementSpeed;
        }
        else
        {
            x = LiveObjects.player.XCoordinate;
            y = LiveObjects.player.YCoordinate + MovementSpeed;
        }

        if (GameMechanics.DetectWallCollision(x, y) || GameMechanics.DetectWallCollision((x + LiveObjects.player.BlockWidth), (y + LiveObjects.player.BlockHeight))
        {
            OnPlayerInvalidMove(null, new PlayerEventArgs());
            return;
        }

      

and the loop for DetectWallCollision is fair:

foreach (Wall wall in LiveObjects.walls)
        {
            if (wall.DetectCollision(x, y))
                return true;
        }
        return false;

      

Any ideas?

+2


source to share


3 answers


There is something that bothers me, you said that the DetectCollision method gets the position of the wall, but if I interpret your code correctly, you pass the x and y parameter to DetectWallCollision, which is the position (after the movement) of the player and place this position before the DetectCollision method ...

Have you debugged your code to see what coordinates are passed to the collision methods and traced the paths your if statements take?



if it is not possible to debug your code for any reason - write a trace file - I think the solution will fall into your lap;)

0


source


I am assuming there is nothing infinitely small (i.e. pixel size) in your world. To have a collision with the true bounding rectangle, you must consider the size of both objects, not just one.

boolean intersectsEntity(Entity e)
{
    return (e.position.x <= position.x + size.x) &&
           (e.position.y <= position.y + size.y) &&
           (e.position.x + e.size.x >= position.x) &&
           (e.position.y + e.size.y >= position.y);
}

      



Assuming the Entity has a vector for its position and its size of course. So size.x == width and size.y == height.

+1


source


Your east and west is the wrong way. With a coordinate system of 0.0 at the top left, increasing positively as you move down or to the right, then moving West usually means moving to the left, which means decreasing X values ​​and vice versa for East. You are doing the opposite.

0


source







All Articles