Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
fail to get AABB collision repsonses
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Nov 17, 2023
1 parent 084964f commit dc78ecd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 26 deletions.
85 changes: 70 additions & 15 deletions Common/Collision/AABB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,83 @@ public AABB(dvec3 a, dvec3 b) {

[Pure]
public bool CollidesWith(BlockView world) {
var min = Min.WorldToBlockPosition();
var min = (ivec3)dvec3.Floor(Min);
var max = (ivec3)dvec3.Ceiling(Max);
return Iteration.Cubic(min, max).Any(pos => world.GetBlock(pos).IsSolidBlock);
foreach (var pos in Iteration.Cubic(min, max))
if (world.GetBlock(pos).IsSolidBlock)
return true;
return false;
}

[Pure]
public dvec3 MoveAndSlide(BlockView world, dvec3 delta) {
var newDelta = new dvec3();
var min = (ivec3)dvec3.Floor(Min) - new ivec3(1,1,1);
var max = (ivec3)dvec3.Ceiling(Max) + new ivec3(1,1,1);

// Console.WriteLine(delta);

if (!CollideOnAxis(world, delta * dvec3.UnitX))
newDelta += delta * dvec3.UnitX;
if (!CollideOnAxis(world, delta * dvec3.UnitY))
newDelta += delta * dvec3.UnitY;
if (!CollideOnAxis(world, delta * dvec3.UnitZ))
newDelta += delta * dvec3.UnitZ;
foreach (var pos in Iteration.Cubic(min, max))
if (world.GetBlock(pos).IsSolidBlock)
delta = GetMinimumDistance(new(pos, pos + new ivec3(1, 1, 1)), delta);

return newDelta;
return delta;
}

[Pure]
private bool CollideOnAxis(BlockView world, dvec3 axisDelta) {
var box = new AABB(Min + axisDelta, Max + axisDelta);
return box.CollidesWith(world);

private bool CollidesWith(AABB other)
=> Min.x < other.Max.x &&
Max.x > other.Min.x &&
Min.y < other.Max.y &&
Max.y > other.Min.y &&
Min.z < other.Max.z &&
Max.z > other.Min.z;

private dvec3 GetMinimumDistance(AABB other, dvec3 delta) {
if (!new AABB(Min + delta, Max + delta).CollidesWith(other))
return delta;

delta.x = GetMinimumDistanceForAxis(other, delta * dvec3.UnitX);
delta.y = GetMinimumDistanceForAxis(other, delta * dvec3.UnitY);
delta.z = GetMinimumDistanceForAxis(other, delta * dvec3.UnitZ);

// Console.WriteLine(delta);

return delta;
}

private double GetMinimumDistanceForAxis(AABB other, dvec3 axisDelta) {
double delta = axisDelta.Sum;
var axis = dvec3.Sign(axisDelta);

if (delta == 0)
return 0;

double s, o;

if (delta > 0) {
s = (Max * axis).Sum;
o = (other.Min * axis).Sum;

if (s < o && s + delta > o) {
double dist = s - o;
if (dist <= 0.02)
return 0;
return dist * Math.Sign(delta);
}
return delta;
}

s = (Min * axis).Sum;
o = (other.Max * axis).Sum;

if (s > o && s + delta < o) {
double dist = o - s;

if (dist <= 0.02)
return 0;

return (s - o) * Math.Sign(delta);
}

return delta;
}
}
5 changes: 5 additions & 0 deletions Common/Collision/Tile/TileShape.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Voxel.Common.Collision.Tile;

public interface TileShape {

}
9 changes: 0 additions & 9 deletions Common/Tile/Behavior/RandomTick.cs

This file was deleted.

2 changes: 0 additions & 2 deletions Common/Util/Iteration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using GlmSharp;

namespace Voxel.Common.Util;
Expand Down

0 comments on commit dc78ecd

Please sign in to comment.