Skip to content

Commit 5ad34db

Browse files
committed
merged:
2 parents 9b122d7 + 592048f commit 5ad34db

11 files changed

+147
-126
lines changed

Assets/Scripts/Geom/ArcCurve.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,12 @@ public Vector2 Point(float t)
1919
return Center + Radius * new Vector2(Mathf.Cos(theta), Mathf.Sin(theta));
2020
}
2121

22-
public float TangentAngle(float t)
22+
public Vector2 Tangent(float t)
2323
{
2424
var theta = Mathf.Lerp(Theta0, Theta1, t);
25-
return (Theta1 > Theta0) ? theta : -theta;
26-
}
27-
28-
public (ICurve, Vector2, ICurve) Split(float t)
29-
{
30-
var point = Point(t);
31-
var theta = Mathf.Lerp(Theta0, Theta1, t);
32-
return (
33-
new ArcCurve{Theta0 = Theta0, Theta1 = theta, Radius = Radius, Center = Center},
34-
point,
35-
new ArcCurve{Theta0 = theta, Theta1 = Theta1, Radius = Radius, Center = Center});
25+
var tangent = new Vector2(-Mathf.Sin(theta), Mathf.Cos(theta));
26+
if (Theta1 > Theta0) tangent *= -1;
27+
return tangent;
3628
}
3729

3830
public ICurve Reverse()

Assets/Scripts/Geom/BezierCurve.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Linq;
2+
using UnityEngine;
3+
4+
namespace Geom
5+
{
6+
public class BezierCurve : ICurve
7+
{
8+
public readonly Vector2[] Points;
9+
private const int Segments = 10;
10+
11+
public BezierCurve(Vector2[] points)
12+
{
13+
Points = points;
14+
}
15+
16+
public float Length()
17+
{
18+
float length = 0f;
19+
Vector2 prevPoint = Point(0);
20+
21+
for (int i = 1; i <= Segments; i++)
22+
{
23+
float t = i / (float)Segments;
24+
Vector2 currentPoint = Point(t);
25+
length += Vector2.Distance(prevPoint, currentPoint);
26+
prevPoint = currentPoint;
27+
}
28+
29+
return length;
30+
}
31+
32+
public Vector2 Point(float t)
33+
{
34+
float u = 1 - t;
35+
float tt = t * t;
36+
float uu = u * u;
37+
float uuu = uu * u;
38+
float ttt = tt * t;
39+
40+
Vector2 p = uuu * Points[0];
41+
p += 3 * uu * t * Points[1];
42+
p += 3 * u * tt * Points[2];
43+
p += ttt * Points[3];
44+
45+
return p;
46+
}
47+
48+
public Vector2 Tangent(float t)
49+
{
50+
float u = 1 - t;
51+
float tt = t * t;
52+
float uu = u * u;
53+
float uuu = uu * u;
54+
float ttt = tt * t;
55+
56+
Vector2 tangentVector = -3 * uu * Points[0] +
57+
3 * (2 * uu - 3 * u) * (Points[1] - Points[2]) +
58+
3 * (3 * t - 2) * (Points[2] - Points[3]) +
59+
3 * ttt * (Points[3] - Points[2]);
60+
tangentVector.Normalize();
61+
return tangentVector;
62+
}
63+
64+
public ICurve Reverse()
65+
{
66+
return new BezierCurve(Points.Reverse().ToArray());
67+
}
68+
}
69+
}

Assets/Scripts/Geom/BezierCurve.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Geom/ICurve.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ public interface ICurve
77
{
88
float Length();
99
Vector2 Point(float t);
10-
float TangentAngle(float t);
11-
ICurve Reverse();
10+
11+
Vector2 Tangent(float t);
12+
13+
public ICurve Reverse();
14+
15+
public float TangentAngle(float t)
16+
{
17+
Vector2 tangent = Tangent(t);
18+
return Mathf.Atan2(tangent.y, tangent.x);
19+
}
1220

1321
public IEnumerable<Vector2> ToPointStream(float deltaLength = 10)
1422
{

Assets/Scripts/Geom/LineCurve.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,21 @@ public LineCurve(Vector2 p0, Vector2 p1)
1414
P1 = p1;
1515
}
1616

17-
public Vector2 AsVector()
17+
public Vector2 Tangent(float t = 0)
1818
{
1919
return P1 - P0;
2020
}
2121

2222
public float Length()
2323
{
24-
return AsVector().magnitude;
24+
return Tangent().magnitude;
2525
}
2626

2727
public Vector2 Point(float t)
2828
{
2929
return Vector2.Lerp(P0, P1, t);
3030
}
3131

32-
public float TangentAngle(float t)
33-
{
34-
Vector2 vec = AsVector();
35-
return Mathf.Atan2(vec.y, vec.x);
36-
}
37-
3832
public (ICurve, Vector2, ICurve) Split(float t)
3933
{
4034
var point = Point(t);

Assets/Scripts/MyDebug/DebugRenderer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public class DebugRenderer : MonoBehaviour
3030
/*** Move stuff ***/
3131
private float lerpDuration = 1.0f; // You can adjust the duration to control the speed of movement
3232
private float lerpStartTime;
33-
private BezierCurveMover curveMover;
3433
/*
3534
private bool justEntered = false;
3635
*/

Assets/Scripts/Navigation/BezierCurveMover.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

Assets/Scripts/Navigation/BezierCurveMover.cs.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

Assets/Scripts/Navigation/Graph.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ public IEnumerable<Rectangle> Rectangles()
3838
}
3939
}
4040
}
41-
public List<VertexInfo> GetVertices()
41+
42+
public VertexInfo GetVertex(int vertexId)
4243
{
43-
return _vertices;
44-
}
44+
return _vertices[vertexId];
45+
}
4546

4647
public List<List<EdgeInfo>> GetAdjList()
4748
{

Assets/Scripts/Navigation/Wanderer.cs

Lines changed: 55 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ public class Wanderer : MonoBehaviour {
3030
private float lerpStartTime;
3131

3232
// yum BEZIER STUFF
33-
private BezierCurveMover curveMover;
34-
3533
private List<Vector2> controlPoints;
3634
private float movementDuration;
3735

@@ -45,28 +43,27 @@ public void Initialize(Graph navGraph, EdgeInfo start, EdgeInfo end, PathFinder
4543
}
4644

4745

48-
void Update(){
49-
if (!isInRoom) {
50-
if (!isMoving && path.Count > 0) {
46+
void Update()
47+
{
48+
if (isInRoom) return;
49+
if (!isMoving && path.Count > 0) {
5150
currEdge = path.Pop();
5251
StartCoroutine(MoveToTarget(currEdge.Curve));
5352
if (currEdge.Tag == EdgeTag.Doorway && justEntered == false)
54-
{
55-
isInRoom = true;
56-
StartCoroutine(EnterRoom(currEdge));
57-
}
58-
else
59-
{
60-
justEntered = false;
61-
StartCoroutine(MoveToTarget(currEdge.Curve));
62-
}
63-
53+
{
54+
isInRoom = true;
55+
StartCoroutine(EnterRoom(currEdge));
6456
}
65-
else if (path.Count == 0) {
57+
else
58+
{
59+
justEntered = false;
60+
StartCoroutine(MoveToTarget(currEdge.Curve));
61+
}
62+
63+
}
64+
else if (path.Count == 0) {
6665
endEdge = navGraph.GetRandomEdge();
6766
path = pathFinder.FindPath(currEdge, endEdge);
68-
}
69-
7067
}
7168
}
7269

@@ -94,64 +91,54 @@ private IEnumerator MoveToTarget(ICurve curve)
9491

9592
}
9693
private IEnumerator EnterRoom(EdgeInfo edge)
97-
{
98-
curveMover = new BezierCurveMover(this, 5.0f);
99-
List<Vector2> randomPoints = new List<Vector2>();
100-
randomPoints.Add(currEdge.Curve.Point(1));
101-
int points = UnityEngine.Random.Range(4, 10);
102-
103-
for (int i = 0; i < 5; i++)
104-
{
105-
Vector2 randomPoint = navGraph.GetVertices()[edge.ToVertex].region.RandPoint();
106-
randomPoints.Add(randomPoint);
107-
}
108-
109-
//changed post exit code
110-
var exit = path.Pop();
111-
randomPoints.Add(exit.Curve.Point(0));
112-
path = pathFinder.FindPath(exit, navGraph.GetRandomEdge());
113-
lerpStartTime = Time.time;
114-
Vector2 startPosition = this.Position;
115-
while (Time.time - lerpStartTime < lerpDurationRoom)
116-
{
117-
float t = (Time.time - lerpStartTime) / lerpDuration;
118-
Vector2 bezierPosition = DeCasteljauRecursive(randomPoints, t);
119-
MoveTo(bezierPosition);
120-
yield return null;
121-
}
122-
123-
justEntered = true;
124-
isInRoom = false;
125-
yield return null;
126-
127-
}
94+
{
95+
List<Vector2> randomPoints = new List<Vector2>();
96+
randomPoints.Add(currEdge.Curve.Point(1));
97+
int points = UnityEngine.Random.Range(4, 10);
98+
99+
for (int i = 0; i < 5; i++)
100+
{
101+
Vector2 randomPoint = navGraph.GetVertex(edge.ToVertex).region.RandPoint();
102+
randomPoints.Add(randomPoint);
103+
}
128104

129-
private Vector2 DeCasteljauRecursive(List<Vector2> points, float t)
105+
//changed post exit code
106+
var exit = path.Pop();
107+
randomPoints.Add(exit.Curve.Point(0));
108+
path = pathFinder.FindPath(exit, navGraph.GetRandomEdge());
109+
lerpStartTime = Time.time;
110+
Vector2 startPosition = this.Position;
111+
while (Time.time - lerpStartTime < lerpDurationRoom)
130112
{
131-
if (points.Count == 1)
132-
{
133-
return points[0];
134-
}
113+
float t = (Time.time - lerpStartTime) / lerpDuration;
114+
Vector2 bezierPosition = DeCasteljauRecursive(randomPoints, t);
115+
MoveTo(bezierPosition);
116+
yield return null;
117+
}
118+
119+
justEntered = true;
120+
isInRoom = false;
121+
yield return null;
135122

136-
List<Vector2> newPoints = new List<Vector2>();
137-
for (int i = 0; i < points.Count - 1; i++)
138-
{
139-
Vector2 newPoint = Vector2.Lerp(points[i], points[i + 1], t);
140-
newPoints.Add(newPoint);
141-
}
123+
}
142124

143-
return DeCasteljauRecursive(newPoints, t);
125+
private Vector2 DeCasteljauRecursive(List<Vector2> points, float t)
126+
{
127+
if (points.Count == 1)
128+
{
129+
return points[0];
144130
}
145131

132+
List<Vector2> newPoints = new List<Vector2>();
133+
for (int i = 0; i < points.Count - 1; i++)
134+
{
135+
Vector2 newPoint = Vector2.Lerp(points[i], points[i + 1], t);
136+
newPoints.Add(newPoint);
137+
}
146138

147-
148-
139+
return DeCasteljauRecursive(newPoints, t);
140+
}
149141
}
150-
151-
152-
153-
154-
155142
}
156143

157144

Assets/Scripts/Navigation/WandererManage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ void Start() {
4545
wanderers.Add(wander_component);
4646
_drawables.Add(new DebugSquare() {position = wander_component.Position});
4747
}
48-
4948
}
5049

5150
void Update() {

0 commit comments

Comments
 (0)