Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Pathfinding (Incomplete) #330

Draft
wants to merge 22 commits into
base: dev
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
64a6d04
Create helper node class.
TheNexusAvenger Mar 1, 2022
72031a1
Remove neighbors to self when split.
TheNexusAvenger Mar 1, 2022
75fa18b
Add GetOuterNeighbors helper method.
TheNexusAvenger Mar 1, 2022
ca3d6de
Create edge helper class.
TheNexusAvenger Mar 1, 2022
5a927c2
Add polygon helper classes.
TheNexusAvenger Mar 1, 2022
b150cdd
Fix edge case with extra edges not part of a shape.
TheNexusAvenger Mar 1, 2022
c765e30
Add polygon optimization.
TheNexusAvenger Mar 1, 2022
6818ffa
Move classes. Change Polygons to Shapes.
TheNexusAvenger Mar 2, 2022
1fe8a8b
Remove unused methods.
TheNexusAvenger Mar 2, 2022
775f273
Implement height map to shapes solver.
TheNexusAvenger Mar 2, 2022
3e5c562
Add point in shape helper method.
TheNexusAvenger Mar 16, 2022
c26ba08
Add line intersection tester.
TheNexusAvenger Mar 16, 2022
832a0ca
Add generating nodes from shape.
TheNexusAvenger Mar 16, 2022
c46000a
Add shape nesting.
TheNexusAvenger Mar 16, 2022
2bc1a7e
Add additional test cases.
TheNexusAvenger Mar 23, 2022
3ca1a7f
Fix edge cases with point in shape.
TheNexusAvenger Mar 23, 2022
57526ba
Store bounding shape with inner shapes.
TheNexusAvenger Mar 23, 2022
c8cc170
Split line intersection test.
TheNexusAvenger Mar 23, 2022
8fda7b5
Test for inner shapes with checking for valid lines.
TheNexusAvenger Mar 23, 2022
23317e2
Generate nodes to contained shapes.
TheNexusAvenger Mar 23, 2022
5bced66
Generate nodes for shapes.
TheNexusAvenger Mar 23, 2022
fddd513
Add caching for solver results.
TheNexusAvenger Mar 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add point in shape helper method.
  • Loading branch information
TheNexusAvenger committed Mar 16, 2022
commit 3e5c5620304065f94a35aaebdf85dbafa286d286
32 changes: 32 additions & 0 deletions Uchu.NavMesh.Test/Shape/OrderedShapeTest.cs
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ public class OrderedShapeTest
[Test]
public void TestOptimize()
{
// Create the test shape.
var shape = new OrderedShape()
{
Points = new List<Vector2>()
@@ -31,6 +32,7 @@ public void TestOptimize()
},
};

// Test optimizing the shape.
shape.Optimize();
Assert.AreEqual(new List<Vector2>()
{
@@ -40,4 +42,34 @@ public void TestOptimize()
new Vector2(0, -1),
}, shape.Points);
}

/// <summary>
/// Tests the PointInShape method.
/// </summary>
[Test]
public void TestPointInShape()
{
// Create the test shape.
var shape = new OrderedShape()
{
Points = new List<Vector2>()
{
new Vector2(0, 0),
new Vector2(-2, -2),
new Vector2(-2, 2),
new Vector2(2, 2),
new Vector2(2, -2),
},
};

// Test that various parts are in the shape.
Assert.IsTrue(shape.PointInShape(new Vector2(0, 0)));
Assert.IsTrue(shape.PointInShape(new Vector2(1, 1)));
Assert.IsTrue(shape.PointInShape(new Vector2(1, -1)));
Assert.IsTrue(shape.PointInShape(new Vector2(0, 1)));
Assert.IsFalse(shape.PointInShape(new Vector2(0, -1)));
Assert.IsFalse(shape.PointInShape(new Vector2(-3, -1)));
Assert.IsFalse(shape.PointInShape(new Vector2(3, -1)));
Assert.IsFalse(shape.PointInShape(new Vector2(0, 3)));
}
}
25 changes: 25 additions & 0 deletions Uchu.NavMesh/Shape/OrderedShape.cs
Original file line number Diff line number Diff line change
@@ -36,4 +36,29 @@ public void Optimize()
this.Points.RemoveAt(0);
}
}

/// <summary>
/// Returns if a point is in the shape.
/// </summary>
/// <param name="point">Point to check.</param>
/// <returns>Whether the point is in the shape.</returns>
public bool PointInShape(Vector2 point)
{
// Get the lines that are left of the point.
var linesLeftOfPoint = 0;
for (var i = 0; i < this.Points.Count; i++)
{
var currentPoint = this.Points[i];
if (point == currentPoint) return true;
var lastPoint = this.Points[i == 0 ? this.Points.Count - 1 : (i - 1)];
if (!((currentPoint.Y > point.Y && lastPoint.Y < point.Y) || (currentPoint.Y < point.Y && lastPoint.Y > point.Y))) continue;
var lineRatio = (point.Y - currentPoint.Y) / (lastPoint.Y - currentPoint.Y);
var lineX = currentPoint.X + ((lastPoint.X - currentPoint.X) * lineRatio);
if (lineX > point.X) continue;
linesLeftOfPoint += 1;
}

// Return if the points to the left is odd.
return linesLeftOfPoint % 2 == 1;
}
}