Skip to content

Commit

Permalink
moved AABB extensions and axis class from MineSharp.Physics to MineSh…
Browse files Browse the repository at this point in the history
…arp.Core because MineSharp.Pathfinder will need it too
  • Loading branch information
psu-de committed Mar 12, 2024
1 parent 718db37 commit 15a38e5
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 90 deletions.
4 changes: 2 additions & 2 deletions Components/MineSharp.Physics/PlayerPhysics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ private Vector3 Collide(Vector3 vec)
aabb);
var collidedUp0 = CheckBoundingBoxCollisions(
new Vector3(0.0, PhysicsConst.MAX_UP_STEP, 0.0),
aabb.ExpandedBoundingBox(vec.X, 0, vec.Z));
aabb.Clone().Expand(vec.X, 0, vec.Z));

if (collidedUp0.Y < PhysicsConst.MAX_UP_STEP)
{
Expand Down Expand Up @@ -435,7 +435,7 @@ private Vector3 CheckBoundingBoxCollisions(Vector3 direction, AABB aabb)
aabb = aabb.Clone();

var shapes = WorldUtils.GetWorldBoundingBoxes(
aabb.ExpandedBoundingBox(direction.X, direction.Y, direction.Z),
aabb.Clone().Expand(direction.X, direction.Y, direction.Z),
this.World,
this.Data);

Expand Down
68 changes: 0 additions & 68 deletions Components/MineSharp.Physics/Utils/AABBExtensions.cs

This file was deleted.

59 changes: 58 additions & 1 deletion MineSharp.Core/Geometry/AABB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ public AABB Deflate(double x, double y, double z)
return this;
}

public AABB Extend(double x, double y, double z)
/// <summary>
/// Expand this bounding box by x, y, z
/// </summary>
public AABB Expand(double x, double y, double z)
{
if (x > 0)
this.Max.X += x;
Expand Down Expand Up @@ -193,6 +196,60 @@ public bool IntersectsLine(Vector3 origin, Vector3 direction)
return !(tMax < 0 || tMin > tMax);
}

/// <summary>
/// Returns the min value of the given axis
/// </summary>
public double GetMinValue(Axis axis)
=> axis.Choose(this.Min);

/// <summary>
/// Returns the max value of the given axis
/// </summary>
public double GetMaxValue(Axis axis)
=> axis.Choose(this.Max);

/// <summary>
/// Whether <paramref name="other"/> intersects this bounding box in the given axis
/// </summary>
public bool IntersectsAxis(AABB other, Axis axis)
{
var minA = this.GetMinValue(axis);
var maxA = this.GetMaxValue(axis);

var minB = other.GetMinValue(axis);
var maxB = other.GetMaxValue(axis);

return minA > minB && minA < maxB
|| maxA > minB && maxA < maxB
|| minB > minA && minB < maxA
|| maxB > minA && maxB < maxA
|| minA == minB && maxA == maxB;
}

/// <summary>
/// Calculates the offset to the <paramref name="other"/> aabb on the given axis
/// </summary>
public double CalculateMaxOffset(AABB other, Axis axis, double displacement)
{
if (!IntersectsAxis(other, axis.Next) || !IntersectsAxis(other, axis.Previous))
{
return displacement;
}

var minA = this.GetMinValue(axis);
var maxA = this.GetMaxValue(axis);
var minB = other.GetMinValue(axis);
var maxB = other.GetMaxValue(axis);

if (displacement > 0 && maxB <= minA && maxB + displacement > minA)
return Math.Clamp(minA - maxB, 0.0, displacement);

if (displacement < 0 && maxA <= minB && minB + displacement < maxA)
return Math.Clamp(maxA - minB, displacement, 0.0);

return displacement;
}

/// <inheritdoc />
public override string ToString() =>
$"AABB ({Min} -> {Max})";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,79 @@
using MineSharp.Core.Common;
using MineSharp.Core.Geometry;
namespace MineSharp.Core.Geometry;

namespace MineSharp.Physics.Utils;

internal abstract class Axis
/// <summary>
/// Represents a basis axis
/// </summary>
public abstract class Axis
{
/// <summary>
/// The default X Axis
/// </summary>
public static readonly Axis X = XAxis.Instance;

/// <summary>
/// The default Y Axis
/// </summary>
public static readonly Axis Y = YAxis.Instance;

/// <summary>
/// The default Z Axis
/// </summary>
public static readonly Axis Z = ZAxis.Instance;

/// <summary>
/// The next axis
/// </summary>
public abstract Axis Next { get; }

/// <summary>
/// The previous axis
/// </summary>
public abstract Axis Previous { get; }

/// <summary>
/// Returns the coordinate of this axis
/// </summary>
public abstract double Choose(double x, double y, double z);
public abstract double GetBBMin(AABB aabb);
public abstract double GetBBMax(AABB aabb);

/// <summary>
/// Returns the coordinate of this axis
/// </summary>
public double Choose(Vector3 vec)
=> this.Choose(vec.X, vec.Y, vec.Z);

private class XAxis : Axis
{
public static XAxis Instance = new();
public static readonly XAxis Instance = new();

public override Axis Next => YAxis.Instance;
public override Axis Previous => ZAxis.Instance;

public override double Choose(double x, double y, double z) => x;
public override double GetBBMin(AABB aabb) => aabb.MinX;
public override double GetBBMax(AABB aabb) => aabb.MaxX;

private XAxis() { }
}

private class YAxis : Axis
{
public static YAxis Instance = new();

public static readonly YAxis Instance = new();

public override Axis Next => ZAxis.Instance;
public override Axis Previous => XAxis.Instance;

public override double Choose(double x, double y, double z) => y;
public override double GetBBMin(AABB aabb) => aabb.MinY;
public override double GetBBMax(AABB aabb) => aabb.MaxY;

private YAxis() { }
}

private class ZAxis : Axis
{
public static ZAxis Instance = new();


public static readonly ZAxis Instance = new();

public override Axis Next => XAxis.Instance;
public override Axis Previous => YAxis.Instance;

public override double Choose(double x, double y, double z) => z;
public override double GetBBMin(AABB aabb) => aabb.MinZ;
public override double GetBBMax(AABB aabb) => aabb.MaxZ;

private ZAxis() { }
}
}

0 comments on commit 15a38e5

Please sign in to comment.