Skip to content

Commit

Permalink
Add line intersection tests in WorldViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
Helco committed Feb 3, 2024
1 parent 522d9fe commit 7bc16cc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion zzre.core/math/IntersectionQueries.Mixin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ partial class IntersectionQueries
{ a: Sphere A, b: Sphere B } => Intersects(A, B),
{ a: Sphere A, b: Plane B } => Intersects(A, B),
{ a: Plane A, b: Plane B } => Intersects(A, B),
{ a: IRaycastable raycastable, b: Line line } => raycastable.Cast(line).HasValue,
{ a: IRaycastable raycastable, b: Line line } => a.Intersects(line.Start) || raycastable.Cast(line).HasValue,
_ when shouldTryToSwitch => Intersects(b, a, false),
_ => throw new ArgumentException($"Intersection between {a.GetType().Name} and {b.GetType().Name} is missing")
};
Expand Down
11 changes: 0 additions & 11 deletions zzre.core/math/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,4 @@ namespace zzre;
public Raycast? Cast(OrientedBox box) => CheckRaycast(new Ray(Start, Direction).Cast(box));
public Raycast? Cast(Plane plane) => CheckRaycast(new Ray(Start, Direction).Cast(plane));
public Raycast? Cast(Triangle triangle) => CheckRaycast(new Ray(Start, Direction).Cast(triangle));

public bool Intersects(Box box) => Cast(box) != null;
public bool Intersects(OrientedBox box) => Cast(box) != null;
public bool Intersects(Sphere sphere) => Cast(sphere) != null;
public bool Intersects(Plane plane) => Cast(plane) != null;
public bool Intersects(Triangle triangle) => Cast(triangle) != null;

// Line-Line intersections in 3D are super rare and are not necessary for zzio.
// This will be a fun surprise for anyone actually rellying on this and taking
// zzio math without looking into what it actually can and cannot do.
public bool Intersects(Line line) => false;
}
10 changes: 9 additions & 1 deletion zzre/tools/WorldViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ private enum IntersectionPrimitive
Box,
OrientedBox,
Sphere,
Triangle
Triangle,
Line
}

private const byte DebugPlaneAlpha = 0xA0;
Expand Down Expand Up @@ -580,6 +581,13 @@ private void UpdateIntersectionPrimitive()
edges = triangle.Edges();
break;

case IntersectionPrimitive.Line:
var pos = camera.Location.GlobalPosition;
var line = new Line(pos, pos - camera.Location.GlobalForward * intersectionSize);
intersections = worldCollider.Intersections(line);
edges = new[] { line };
break;

default: return;
}

Expand Down

0 comments on commit 7bc16cc

Please sign in to comment.