C
C#6mo ago
hostage

player collision not working for my monogame game!

so i have tried absolutely everything. im so stuck right now, not even chatgpt or copilot can help me out and i dont know whats going wrong... ive written collision code and iterated over it multiple times but there seems to always be 1 thing wrong... currently, the left wall, top wall and bottom wall collision work perfectly fine. i can walk against them side to side and up/down, but walking into the right wall while going up or down makes my sprite spasm out and im not sure why. this is my function:
private void ResolveCollisions()
{
isColliding = false;
const float buffer = 0.1f; // Buffer to handle small overlaps uniformly

foreach (Tile tile in currentMapBG.Objects)
{
if (WallTiles.Contains(tile.TileID) && CollidesWith(tile))
{
isColliding = true;

// Calculate overlaps
float overlapX = Math.Min(BoundingBox.Right, tile.BoundingBox.Right) - Math.Max(BoundingBox.Left, tile.BoundingBox.Left);
float overlapY = Math.Min(BoundingBox.Bottom, tile.BoundingBox.Bottom) - Math.Max(BoundingBox.Top, tile.BoundingBox.Top);

Console.WriteLine($"TileID: {tile.TileID}, PlayerPos: {Position}, TilePos: {tile.Position}");
Console.WriteLine($"OverlapX: {overlapX}, OverlapY: {overlapY}");

// Adjust position based on the smallest overlap with a uniform buffer
if (Math.Abs(overlapX) < Math.Abs(overlapY))
{
// Move out of collision along X axis
if (Position.X < tile.Position.X)
{
Position = new Vector2(tile.Position.X - BoundingBox.Width - buffer, Position.Y);
Console.WriteLine($"Adjusting X Position (Left): NewPos: {Position}");
}
else
{
Position = new Vector2(tile.Position.X + tile.BoundingBox.Width + buffer, Position.Y);
Console.WriteLine($"Adjusting X Position (Right): NewPos: {Position}");
}
}
else
{
// Move out of collision along Y axis
if (Position.Y < tile.Position.Y)
{
Position = new Vector2(Position.X, tile.Position.Y - BoundingBox.Height - buffer);
Console.WriteLine($"Adjusting Y Position (Up): NewPos: {Position}");
}
else
{
Position = new Vector2(Position.X, tile.Position.Y + tile.BoundingBox.Height + buffer);
Console.WriteLine($"Adjusting Y Position (Down): NewPos: {Position}");
}
}
}
}
}
private void ResolveCollisions()
{
isColliding = false;
const float buffer = 0.1f; // Buffer to handle small overlaps uniformly

foreach (Tile tile in currentMapBG.Objects)
{
if (WallTiles.Contains(tile.TileID) && CollidesWith(tile))
{
isColliding = true;

// Calculate overlaps
float overlapX = Math.Min(BoundingBox.Right, tile.BoundingBox.Right) - Math.Max(BoundingBox.Left, tile.BoundingBox.Left);
float overlapY = Math.Min(BoundingBox.Bottom, tile.BoundingBox.Bottom) - Math.Max(BoundingBox.Top, tile.BoundingBox.Top);

Console.WriteLine($"TileID: {tile.TileID}, PlayerPos: {Position}, TilePos: {tile.Position}");
Console.WriteLine($"OverlapX: {overlapX}, OverlapY: {overlapY}");

// Adjust position based on the smallest overlap with a uniform buffer
if (Math.Abs(overlapX) < Math.Abs(overlapY))
{
// Move out of collision along X axis
if (Position.X < tile.Position.X)
{
Position = new Vector2(tile.Position.X - BoundingBox.Width - buffer, Position.Y);
Console.WriteLine($"Adjusting X Position (Left): NewPos: {Position}");
}
else
{
Position = new Vector2(tile.Position.X + tile.BoundingBox.Width + buffer, Position.Y);
Console.WriteLine($"Adjusting X Position (Right): NewPos: {Position}");
}
}
else
{
// Move out of collision along Y axis
if (Position.Y < tile.Position.Y)
{
Position = new Vector2(Position.X, tile.Position.Y - BoundingBox.Height - buffer);
Console.WriteLine($"Adjusting Y Position (Up): NewPos: {Position}");
}
else
{
Position = new Vector2(Position.X, tile.Position.Y + tile.BoundingBox.Height + buffer);
Console.WriteLine($"Adjusting Y Position (Down): NewPos: {Position}");
}
}
}
}
}
and this is how i call it:
private void WallCollision()
{
if(!IsInState(PlayerState.RoomTransition) && !IsInState(PlayerState.Cutscene))
{
ResolveCollisions();
}
}
private void WallCollision()
{
if(!IsInState(PlayerState.RoomTransition) && !IsInState(PlayerState.Cutscene))
{
ResolveCollisions();
}
}
for some reason it just doesnt work. please help anyone hahaha
1 Reply
Buddy
Buddy6mo ago
Collision Detection
Collision Detection
An online book about collision detection using Processing.
Want results from more Discord servers?
Add your server