1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Brains.Framework.PathFinding;
using Brains.Framework.Map;
namespace Brains.Framework.Behaviors.PathFinding
{
public class FollowPathBehavior:CompositeBehavior,IActionBehavior
{
protected PathFinder pathFinder;
protected int currentNode = 0;
protected bool copiedPath = false;
protected bool createdBiNormals = false;
public FollowPathNode[] pathNodes;
protected float tolerance;
public float Tolerance
{
get { return tolerance; }
set { tolerance = value; }
}
// taken from http://www.ziggyware.com/readarticle.php?article_id=78
public bool IntersectionOfTwoLines(Vector2 a, Vector2 b, Vector2 c,
Vector2 d, ref Vector2 result)
{
float r, s;
float denominator = (b.X - a.X) * (d.Y - c.Y) - (b.Y - a.Y) * (d.X - c.X);
// If the denominator in above is zero, AB & CD are colinear
if (denominator == 0)
return false;
float numeratorR = (a.Y - c.Y) * (d.X - c.X) - (a.X - c.X) * (d.Y - c.Y);
// If the numerator above is also zero, AB & CD are collinear.
// If they are collinear, then the segments may be projected to the x-
// or y-axis, and overlap of the projected intervals checked.
r = numeratorR / denominator;
float numeratorS = (a.Y - c.Y) * (b.X - a.X) - (a.X - c.X) * (b.Y - a.Y);
s = numeratorS / denominator;
// If 0<=r<=1 & 0<=s<=1, intersection exists
// r<0 or r>1 or s<0 or s>1 line segments do not intersect
if (r < 0 || r > 1 || s < 0 || s > 1)
return false;
///*
// Note:
// If the intersection point of the 2 lines are needed (lines in this
// context mean infinite lines) regardless whether the two line
// segments intersect, then
//
// If r>1, P is located on extension of AB
// If r<0, P is located on extension of BA
// If s>1, P is located on extension of CD
// If s<0, P is located on extension of DC
//*/
// Find intersection point
result.X = (float)(a.X + (r * (b.X - a.X)));
result.Y = (float)(a.Y + (r * (b.Y - a.Y)));
return true;
}
public PathFinder PathFinder
{
get { return pathFinder; }
set { pathFinder = value; }
}
public FollowPathBehavior()
{
pathFinder = new PathFinder();
tolerance = 2.0f; // by default the tolerance when following path
// is 1 meter each side
}
~FollowPathBehavior()
{
pathNodes = null;
}
public override void Reset()
{
currentNode = 0;
copiedPath = false;
createdBiNormals = false;
pathNodes = null;
this.State = BehaviorState.Idle;
}
public override void Update(GameTime gameTime)
{
// make sure everything is ok.
if (pathFinder != null)
{
if (pathFinder.State == PathFinderState.Finished)
{
this.State = BehaviorState.Running;
if (copiedPath)
{
if (createdBiNormals)
{
// do the follow path thing
// Get node we are going towards
//currentNode
if (currentNode >= pathNodes.Length)
{
// end!
this.State = BehaviorState.Success;
return;
}
Vector2 vDistance = Owner.Position - pathNodes[currentNode].nodePosition;
float fDistance = vDistance.Length();
if (fDistance <= Owner.Radius)
{
pathNodes[currentNode].nodeVisited = true;
//Console.WriteLine("Visited node: [" +pathNodes[currentNode].X + "-" + pathNodes[currentNode].Y + "]");
currentNode++;
if (currentNode >= pathNodes.Length)
{
// end!
this.State = BehaviorState.Success;
return;
}
return;
}
//Vector3 registeredMotion =
// character.Position - character.PreviousPosition;
// did we cross next bi-normal?
Vector2 dummyVector = new Vector2();
if (IntersectionOfTwoLines(
Owner.Position,
Owner.PreviousPosition,
pathNodes[currentNode].biNormalStart,
pathNodes[currentNode].biNormalEnd,
ref dummyVector))
{
pathNodes[currentNode].nodeVisited = true;
//Console.WriteLine("Visited node: [" + pathNodes[currentNode].X + "-" + pathNodes[currentNode].Y + "]");
currentNode++;
}
if (currentNode < pathNodes.Length)
{
Vector2 desiredDirection =
pathNodes[currentNode].nodePosition -
Owner.Position;
desiredDirection.Normalize();
Owner.DesiredOrientation = desiredDirection;
Owner.DesiredPosition = pathNodes[currentNode].nodePosition;
if (Owner.Locomotion is Locomotion.LocomtionSteering)
((Locomotion.LocomtionSteering)Owner.Locomotion).TurnOnSeek(Owner.DesiredPosition);
}
else
{
Owner.DesiredOrientation= new Vector2(0);
this.State = BehaviorState.Success;
}
}
else
{
// create bi-normals to help with path following.
for (int index = 0; index < pathNodes.Length - 1; index++)
{
pathNodes[index].nodeDirection =
pathNodes[index + 1].nodePosition - pathNodes[index].nodePosition;
// create direction vector to the next node
pathNodes[index].nodeDirection.Normalize();
}
Vector3 vR = new Vector3(0, 0,1);
Vector3 vB = new Vector3();
for (int index = 0; index < pathNodes.Length - 1; index++)
{
//pathNodes[index].nodeDirection;
//vB=Util.Perp(pathNodes[index].nodeDirection vR);
vB = Vector3.Cross(pathNodes[index].nodeDirection.ToVector3(), vR);
vB.Normalize();
pathNodes[index].biNormalGenerated = true;
pathNodes[index].biNormalStart =
pathNodes[index].nodePosition - (vB * tolerance).ToVector2();
pathNodes[index].biNormalEnd =
pathNodes[index].nodePosition +( vB * tolerance).ToVector2();
pathNodes[index].nodeVisited = false;
}
// no direction for the last node, use bi-normal data
// from the previous one
if (pathNodes.Length > 1)
{
pathNodes[pathNodes.Length - 1].biNormalGenerated = true;
pathNodes[pathNodes.Length - 1].biNormalStart = pathNodes[pathNodes.Length - 2].biNormalStart;
pathNodes[pathNodes.Length - 1].biNormalEnd = pathNodes[pathNodes.Length - 2].biNormalEnd;
pathNodes[pathNodes.Length - 1].nodeVisited = false;
// now shift binormal data accordingly.
Vector2 shiftVector = pathNodes[pathNodes.Length - 1].nodePosition -
pathNodes[pathNodes.Length - 2].nodePosition;
pathNodes[pathNodes.Length - 1].biNormalStart += shiftVector;
pathNodes[pathNodes.Length - 1].biNormalEnd += shiftVector;
}
createdBiNormals = true;
}
}
else
{
// copy path to the local container, reverse the nodes
// order
if (pathNodes != null)
{
//TODO:pathNodes.Clear();
}
else
{
// copying nodes
//if(!StartAtClosetCell)
pathNodes = new FollowPathNode[pathFinder.closeList.Count];
int counter = 0;
//for (int index = pathFinder.closeList.Count - 1; index >= 0; index--)
for (int index = 0; index <= pathFinder.closeList.Count - 1; index++)
{
FollowPathNode newNode = new FollowPathNode();
Grid grid=PathFinder.Grid;
GridCell _node =grid.GetCell(pathFinder.closeList[index].X,
pathFinder.closeList[index].Y);
newNode.nodePosition.X = grid.GetCell(pathFinder.closeList[index].X,
pathFinder.closeList[index].Y).Position.X;
newNode.nodePosition.Y = grid.GetCell(pathFinder.closeList[index].X,
pathFinder.closeList[index].Y).Position.Y;
newNode.X = pathFinder.closeList[index].X;
newNode.Y = pathFinder.closeList[index].Y;
pathNodes[counter] = newNode;
counter++;
}
copiedPath = true;
}
}
}
else
{
this.State = BehaviorState.Failed;
}
}
else
{
this.State = BehaviorState.Failed;
}
base.Update(gameTime);
}
private bool NodeMatch(FollowPathNode item, GridCell _aa)
{
if (item.X == _aa.X && item.Y == _aa.Y)
{
return true;
}
return false;
}
}
public struct FollowPathNode
{
public Vector2 nodePosition;
public Vector2 nodeDirection;
public Vector2 biNormalStart;
public Vector2 biNormalEnd;
public bool biNormalGenerated;
public bool nodeVisited;
public int X;
public int Y;
}
public class PreStoredPath
{
public int FromGrid { get; set; }
public int ToGrid{ get; set; }
public List<StoredPathNode> Nodes{ get; set; }
}
public struct StoredPathNode
{
public Vector2 nodePosition;
public Vector2 nodeDirection;
public Vector2 biNormalStart;
public Vector2 biNormalEnd;
public bool biNormalGenerated;
public bool nodeVisited;
public int X;
public int Y;
}
} |