public virtual void HandleCollisions(ChunkManager chunks, float dt) { if (CollideMode == CollisionMode.None) { return; } Voxel currentVoxel = new Voxel(); bool success = chunks.ChunkData.GetVoxel(null, LocalTransform.Translation, ref currentVoxel); List <Voxel> vs = new List <Voxel> { currentVoxel }; VoxelChunk chunk = chunks.ChunkData.GetVoxelChunkAtWorldLocation(LocalTransform.Translation); if (!success || currentVoxel == null || chunk == null) { return; } Vector3 grid = chunk.WorldToGrid(LocalTransform.Translation); List <Voxel> adjacencies = chunk.GetNeighborsEuclidean((int)grid.X, (int)grid.Y, (int)grid.Z); vs.AddRange(adjacencies); // TODO: Find a faster way to do this // Vector3 half = Vector3.One*0.5f; //vs.Sort((a, b) => (MathFunctions.L1(LocalTransform.Translation, a.Position + half).CompareTo(MathFunctions.L1(LocalTransform.Translation, b.Position + half)))); int y = (int)Position.Y; foreach (Voxel v in vs) { if (v == null || v.IsEmpty) { continue; } if (CollideMode == CollisionMode.UpDown && (int)v.GridPosition.Y == y) { continue; } if (CollideMode == CollisionMode.Sides && (int)v.GridPosition.Y != y) { continue; } BoundingBox voxAABB = v.GetBoundingBox(); if (Collide(voxAABB)) { OnTerrainCollision(v); } } }
public bool CollidesWithChunks(ChunkManager chunks, Vector3 pos, bool applyForce) { BoundingBox box = new BoundingBox(pos - new Vector3(0.5f, 0.5f, 0.5f), pos + new Vector3(0.5f, 0.5f, 0.5f)); Voxel currentVoxel = new Voxel(); bool success = chunks.ChunkData.GetVoxel(null, pos, ref currentVoxel); List <Voxel> vs = new List <Voxel> { currentVoxel }; VoxelChunk chunk = chunks.ChunkData.GetVoxelChunkAtWorldLocation(pos); if (!success || currentVoxel == null || chunk == null) { return(false); } Vector3 grid = chunk.WorldToGrid(pos); List <Voxel> adjacencies = chunk.GetNeighborsEuclidean((int)grid.X, (int)grid.Y, (int)grid.Z); vs.AddRange(adjacencies); bool gotCollision = false; foreach (Voxel v in vs) { if (v.IsEmpty || !v.IsVisible) { continue; } BoundingBox voxAABB = v.GetBoundingBox(); if (box.Intersects(voxAABB)) { gotCollision = true; if (applyForce) { Collide(box, voxAABB); } else { return(true); } } } return(gotCollision); }