I have a collision method where the parameters for the method are the rectangle of the player and a list with the rectangles of the tiles.
I work with booleans like bLeft
, bRight
,... and this creates a problem if my player collides with two tiles at the same time, then one tile may say True
while the other says false
.
This is my collision method (NOTE: Very buggy and incomplete collision detection, this is because the reason above)
public override void Collision(Rectangle collisionRect, List<Rectangle> tileCollision)
{
foreach (Rectangle rect in tileCollision)
{
if (collisionRect.Intersects(rect))
{
rect2 = Rectangle.Intersect(rect, collisionRect);
collision = true;
stopping = true;
walking = false;
if (collisionRect.X < rect.X && !(collisionRect.Y <= rect.Y - 50))
{
bLeft = true;
}
if (collisionRect.X > rect.X && !(collisionRect.Y <= rect.Y - 50))
{
bRight = true;
}
if (collisionRect.Y <= rect.Y - 50)
{
bUp = true;
}
if (collisionRect.Y >= rect.Y + rect.Height)
{
bDown = true;
}
if (bLeft && !bRight && !bUp && !bDown)
{
fallingTest = false;
stopping = true;
if (left)
{
stopping = false;
fallingTest = false;
}
}
if (bRight && !bLeft && !bUp && !bDown)
{
fallingTest = false;
stopping = true;
if (right)
{
stopping = false;
fallingTest = false;
}
}
if (bUp)
{
ground = true;
fallingTest = true;
stopping = false;
if (moving)
{
walking = true;
}
if (bLeft && bUp)
{
stopping = true;
if (left)
{
stopping = false;
}
}
if (bRight)
{
stopping = true;
if (right)
{
stopping = false;
}
}
}
if (bDown && !bRight && !bLeft && !bUp)
{
stopping = true;
fallingTest = false;
if (moving)
{
walking = true;
}
}
}
if (!(tileCollision.Any(c => collisionRect.Intersects(c))))
{
collision = false;
fallingTest = false;
stopping = false;
ground = false;
if (moving)
{
walking = true;
}
}
}
}
So I basically need a line of code that counts the colliderboxes that the player collides with at the moment of collision. I tried doing this by adding 1 to the variable collisionBoxes
but this results in doing +1 each frame (because this collision method is called in Update
)
I also use rect2
for visual overlapping of the player collision box and the tile collision box, but when the player touches more than one tile, then the rect2 only shows on one tile. This is because I use the foreach
but I tried using a list to store the overlapping rectangles in but it didn't work.
One more question, should I use all these booleans? Because I don't know how to handle them if more than one block touches the player (all the tiles are checking for the booleans, resulting in bugs).