collision refactor

This commit is contained in:
maxwes08
2026-03-24 00:14:17 +01:00
parent 9ffda6f5af
commit 24ca4288af
4 changed files with 242 additions and 140 deletions

251
Entity.cs
View File

@@ -20,12 +20,17 @@ namespace Voxel
private float _gravity = 0.08f;
private float _terminalVelocity = -3.92f;
private float _airMultiplier = 0.91f;
private float _yMultiplier = 0.98f;
private float _groundMultiplier = 0.6f;
private const float COLLISION_EPSILON = 0.01f;
private const float COLLISION_EPSILON = 0.001f;
protected World _world;
// Helper methods for consistent block coordinate conversion
private static int BlockCoordMin(float coord) => (int)MathF.Floor(coord - 1e-6f);
private static int BlockCoordMax(float coord) => (int)MathF.Floor(coord + 1e-6f);
public Entity(Vector3 position, float width, float height, World world)
{
Position = position;
@@ -47,8 +52,9 @@ namespace Voxel
{
Vector3 acceleration = new Vector3(0, -_gravity, 0);
Velocity += acceleration;
Velocity *= _airMultiplier;
Velocity.X *= _airMultiplier;
Velocity.Z *= _airMultiplier;
Velocity.Y *= _yMultiplier;
if (Velocity.Y < _terminalVelocity)
{
@@ -77,36 +83,42 @@ namespace Voxel
return;
}
// Collision detected, resolve each axis separately but independently
// Collision detected, resolve each axis independently
position = originalPosition;
// Resolve Y collision (always first for ground detection)
// Resolve Y collision first
ResolveYCollisionIndependent(ref position, movement.Y);
// Resolve X and Z collisions independently of each other
Vector3 tempPosX = new Vector3(position.X + movement.X, position.Y, position.Z);
if (!HasCollision(GetBoundingBoxAt(tempPosX)))
{
position.X = tempPosX.X;
}
else
{
ResolveXCollisionIndependent(ref position, movement.X);
}
// Resolve X collision
ResolveXCollisionIndependent(ref position, movement.X);
Vector3 tempPosZ = new Vector3(position.X, position.Y, position.Z + movement.Z);
if (!HasCollision(GetBoundingBoxAt(tempPosZ)))
{
position.Z = tempPosZ.Z;
}
else
{
ResolveZCollisionIndependent(ref position, movement.Z);
}
// Resolve Z collision
ResolveZCollisionIndependent(ref position, movement.Z);
// After moving horizontally, reresolve Y to handle any new vertical collisions
// (e.g., sliding into a ledge)
ResolveYCollisionIndependent(ref position, 0);
}
private void ResolveYCollisionIndependent(ref Vector3 position, float velocityY)
{
// First, handle if we're already inside a block (shouldn't happen normally)
AABB currentBox = GetBoundingBoxAt(position);
if (HasCollision(currentBox))
{
// Try to push upward to escape
float step = 0.05f;
for (int i = 0; i < 10; i++)
{
position.Y += step;
if (!HasCollision(GetBoundingBoxAt(position)))
break;
}
Velocity.Y = 0;
OnGround = false;
return;
}
if (velocityY == 0) return;
Vector3 testPos = new Vector3(position.X, position.Y + velocityY, position.Z);
@@ -117,13 +129,13 @@ namespace Voxel
if (velocityY > 0) // Hitting ceiling
{
float ceilingY = GetCeilingHeight(testBox);
position.Y = ceilingY - (Height / 2) - COLLISION_EPSILON;
position.Y = ceilingY - (Height / 2);
Velocity.Y = 0;
}
else // Hitting floor
{
float floorY = GetFloorHeight(testBox);
position.Y = floorY + (Height / 2) + COLLISION_EPSILON;
position.Y = floorY + (Height / 2);
Velocity.Y = 0;
OnGround = true;
}
@@ -138,89 +150,75 @@ namespace Voxel
{
if (velocityX == 0) return;
// Check if we're already inside a block at current position
AABB currentBox = GetBoundingBoxAt(new Vector3(position.X, position.Y, position.Z));
if (HasCollision(currentBox))
{
// Already inside a block, allow movement to escape
position.X += velocityX;
return;
}
float halfWidth = Width / 2;
float direction = Math.Sign(velocityX);
float checkDistance = Math.Abs(velocityX) + COLLISION_EPSILON;
float targetX = position.X + velocityX;
for (float offset = 0; offset <= checkDistance; offset += 0.01f)
// Sweep the bounding box to the target X position
Vector3 testPos = new Vector3(targetX, position.Y, position.Z);
AABB testBox = GetBoundingBoxAt(testPos);
if (HasCollision(testBox))
{
float testX = position.X + (offset * direction);
AABB testBox = GetBoundingBoxAt(new Vector3(testX, position.Y, position.Z));
if (HasCollision(testBox))
// Find the wall we hit
float wallX;
if (direction > 0) // Moving right
{
if (direction > 0) // Moving right
{
position.X = GetRightWallPosition(testBox) - (Width / 2) - COLLISION_EPSILON;
}
else // Moving left
{
position.X = GetLeftWallPosition(testBox) + (Width / 2) + COLLISION_EPSILON;
}
Velocity.X = 0;
return;
wallX = GetRightWallPosition(testBox) - halfWidth - COLLISION_EPSILON;
}
else // Moving left
{
wallX = GetLeftWallPosition(testBox) + halfWidth + COLLISION_EPSILON;
}
position.X = wallX;
Velocity.X = 0;
}
else
{
position.X = targetX;
}
// No collision found, apply full movement
position.X += velocityX;
}
private void ResolveZCollisionIndependent(ref Vector3 position, float velocityZ)
{
if (velocityZ == 0) return;
// Check if we're already inside a block at current position
AABB currentBox = GetBoundingBoxAt(new Vector3(position.X, position.Y, position.Z));
if (HasCollision(currentBox))
{
// Already inside a block, allow movement to escape
position.Z += velocityZ;
return;
}
float halfWidth = Width / 2;
float direction = Math.Sign(velocityZ);
float checkDistance = Math.Abs(velocityZ) + COLLISION_EPSILON;
float targetZ = position.Z + velocityZ;
for (float offset = 0; offset <= checkDistance; offset += 0.01f)
// Sweep the bounding box to the target Z position
Vector3 testPos = new Vector3(position.X, position.Y, targetZ);
AABB testBox = GetBoundingBoxAt(testPos);
if (HasCollision(testBox))
{
float testZ = position.Z + (offset * direction);
AABB testBox = GetBoundingBoxAt(new Vector3(position.X, position.Y, testZ));
if (HasCollision(testBox))
// Find the wall we hit
float wallZ;
if (direction > 0) // Moving forward (+Z)
{
if (direction > 0) // Moving forward
{
position.Z = GetFrontWallPosition(testBox) - (Width / 2) - COLLISION_EPSILON;
}
else // Moving backward
{
position.Z = GetBackWallPosition(testBox) + (Width / 2) + COLLISION_EPSILON;
}
Velocity.Z = 0;
return;
wallZ = GetFrontWallPosition(testBox) - halfWidth - COLLISION_EPSILON;
}
else // Moving backward (-Z)
{
wallZ = GetBackWallPosition(testBox) + halfWidth + COLLISION_EPSILON;
}
position.Z = wallZ;
Velocity.Z = 0;
}
else
{
position.Z = targetZ;
}
// No collision found, apply full movement
position.Z += velocityZ;
}
private float GetFloorHeight(AABB box)
{
int minX = (int)MathF.Floor(box.Min.X);
int maxX = (int)MathF.Floor(box.Max.X);
int minZ = (int)MathF.Floor(box.Min.Z);
int maxZ = (int)MathF.Floor(box.Max.Z);
int checkY = (int)MathF.Floor(box.Min.Y);
int minX = BlockCoordMin(box.Min.X);
int maxX = BlockCoordMax(box.Max.X);
int minZ = BlockCoordMin(box.Min.Z);
int maxZ = BlockCoordMax(box.Max.Z);
int checkY = BlockCoordMin(box.Min.Y);
float highestFloor = float.MinValue;
@@ -241,11 +239,11 @@ namespace Voxel
private float GetCeilingHeight(AABB box)
{
int minX = (int)MathF.Floor(box.Min.X);
int maxX = (int)MathF.Floor(box.Max.X);
int minZ = (int)MathF.Floor(box.Min.Z);
int maxZ = (int)MathF.Floor(box.Max.Z);
int checkY = (int)MathF.Floor(box.Max.Y);
int minX = BlockCoordMin(box.Min.X);
int maxX = BlockCoordMax(box.Max.X);
int minZ = BlockCoordMin(box.Min.Z);
int maxZ = BlockCoordMax(box.Max.Z);
int checkY = BlockCoordMax(box.Max.Y); // Fix: use the block above the entity
float lowestCeiling = float.MaxValue;
@@ -266,11 +264,11 @@ namespace Voxel
private float GetLeftWallPosition(AABB box)
{
int minY = (int)MathF.Floor(box.Min.Y);
int maxY = (int)MathF.Floor(box.Max.Y);
int minZ = (int)MathF.Floor(box.Min.Z);
int maxZ = (int)MathF.Floor(box.Max.Z);
int checkX = (int)MathF.Floor(box.Min.X);
int minY = BlockCoordMin(box.Min.Y);
int maxY = BlockCoordMax(box.Max.Y);
int minZ = BlockCoordMin(box.Min.Z);
int maxZ = BlockCoordMax(box.Max.Z);
int checkX = BlockCoordMin(box.Min.X); // leftmost block the entity overlaps
float rightmostWall = float.MinValue;
@@ -291,11 +289,11 @@ namespace Voxel
private float GetRightWallPosition(AABB box)
{
int minY = (int)MathF.Floor(box.Min.Y);
int maxY = (int)MathF.Floor(box.Max.Y);
int minZ = (int)MathF.Floor(box.Min.Z);
int maxZ = (int)MathF.Floor(box.Max.Z);
int checkX = (int)MathF.Floor(box.Max.X);
int minY = BlockCoordMin(box.Min.Y);
int maxY = BlockCoordMax(box.Max.Y);
int minZ = BlockCoordMin(box.Min.Z);
int maxZ = BlockCoordMax(box.Max.Z);
int checkX = BlockCoordMax(box.Max.X); // rightmost block the entity overlaps
float leftmostWall = float.MaxValue;
@@ -316,11 +314,11 @@ namespace Voxel
private float GetBackWallPosition(AABB box)
{
int minX = (int)MathF.Floor(box.Min.X);
int maxX = (int)MathF.Floor(box.Max.X);
int minY = (int)MathF.Floor(box.Min.Y);
int maxY = (int)MathF.Floor(box.Max.Y);
int checkZ = (int)MathF.Floor(box.Min.Z);
int minX = BlockCoordMin(box.Min.X);
int maxX = BlockCoordMax(box.Max.X);
int minY = BlockCoordMin(box.Min.Y);
int maxY = BlockCoordMax(box.Max.Y);
int checkZ = BlockCoordMin(box.Min.Z); // backmost block the entity overlaps
float frontmostWall = float.MinValue;
@@ -341,11 +339,11 @@ namespace Voxel
private float GetFrontWallPosition(AABB box)
{
int minX = (int)MathF.Floor(box.Min.X);
int maxX = (int)MathF.Floor(box.Max.X);
int minY = (int)MathF.Floor(box.Min.Y);
int maxY = (int)MathF.Floor(box.Max.Y);
int checkZ = (int)MathF.Floor(box.Max.Z);
int minX = BlockCoordMin(box.Min.X);
int maxX = BlockCoordMax(box.Max.X);
int minY = BlockCoordMin(box.Min.Y);
int maxY = BlockCoordMax(box.Max.Y);
int checkZ = BlockCoordMax(box.Max.Z); // frontmost block the entity overlaps
float backmostWall = float.MaxValue;
@@ -366,12 +364,12 @@ namespace Voxel
public bool HasCollision(AABB box)
{
int minX = (int)MathF.Floor(box.Min.X);
int maxX = (int)MathF.Floor(box.Max.X);
int minY = (int)MathF.Floor(box.Min.Y);
int maxY = (int)MathF.Floor(box.Max.Y);
int minZ = (int)MathF.Floor(box.Min.Z);
int maxZ = (int)MathF.Floor(box.Max.Z);
int minX = BlockCoordMin(box.Min.X);
int maxX = BlockCoordMax(box.Max.X);
int minY = BlockCoordMin(box.Min.Y);
int maxY = BlockCoordMax(box.Max.Y);
int minZ = BlockCoordMin(box.Min.Z);
int maxZ = BlockCoordMax(box.Max.Z);
for (int x = minX; x <= maxX; x++)
{
@@ -408,13 +406,13 @@ namespace Voxel
Vector3 min = new Vector3(
position.X - halfWidth,
position.Y - halfHeight, // Center Y minus half height
position.Y - halfHeight,
position.Z - halfWidth
);
Vector3 max = new Vector3(
position.X + halfWidth,
position.Y + halfHeight, // Center Y plus half height
position.Y + halfHeight,
position.Z + halfWidth
);
@@ -424,19 +422,20 @@ namespace Voxel
public void UpdateOnGround()
{
AABB box = GetBoundingBox();
float yCheck = box.Min.Y - 0.05f;
float yCheck = box.Min.Y - COLLISION_EPSILON;
int minX = (int)MathF.Floor(box.Min.X);
int maxX = (int)MathF.Floor(box.Max.X);
int minZ = (int)MathF.Floor(box.Min.Z);
int maxZ = (int)MathF.Floor(box.Max.Z);
int minX = BlockCoordMin(box.Min.X);
int maxX = BlockCoordMax(box.Max.X);
int minZ = BlockCoordMin(box.Min.Z);
int maxZ = BlockCoordMax(box.Max.Z);
int y = BlockCoordMin(yCheck);
OnGround = false;
for (int x = minX; x <= maxX; x++)
{
for (int z = minZ; z <= maxZ; z++)
{
Blocks block = _world.GetBlock(x, (int)MathF.Floor(yCheck), z);
Blocks block = _world.GetBlock(x, y, z);
if (block != Blocks.Air)
{
OnGround = true;