-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavmesh.cpp
801 lines (698 loc) · 18.5 KB
/
navmesh.cpp
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
#include "navmesh.h"
#include <math.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
// HACK!!
#define inline static
#endif
inline float lerp(float a, float b, float t) { return a + (b-a)*t; }
inline float mini(int a, int b) { return a < b ? a : b; }
inline float maxi(int a, int b) { return a > b ? a : b; }
inline float minf(float a, float b) { return a < b ? a : b; }
inline float maxf(float a, float b) { return a > b ? a : b; }
inline float clamp(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); }
inline float sqr(float x) { return x*x; }
inline void vscale(float* v, const float* a, const float s) { v[0] = a[0]*s; v[1] = a[1]*s; }
inline void vset(float* a, const float x, const float y) { a[0]=x; a[1]=y; }
inline void vcpy(float* a, const float* b) { a[0]=b[0]; a[1]=b[1]; }
inline float vdot(const float* a, const float* b) { return a[0]*b[0] + a[1]*b[1]; }
inline float vperp(const float* a, const float* b) { return a[0]*b[1] - a[1]*b[0]; }
inline void vsub(float* v, const float* a, const float* b) { v[0] = a[0]-b[0]; v[1] = a[1]-b[1]; }
inline void vadd(float* v, const float* a, const float* b) { v[0] = a[0]+b[0]; v[1] = a[1]+b[1]; }
bool _less(heapele*l,heapele*r) { return ((mapnode*)l)->F < ((mapnode*)r)->F; }
void _clear(heapele*e){ ((mapnode*)e)->F = ((mapnode*)e)->G = ((mapnode*)e)->H = ((mapnode*)e)->z = 0; ((mapnode*)e)->parent = NULL; }
inline float vdistsqr(const float* a, const float* b)
{
const float dx = b[0]-a[0];
const float dy = b[1]-a[1];
return dx*dx + dy*dy;
}
inline void vlerp(float* v, const float* a, const float* b, const float t)
{
v[0] = a[0] + (b[0]-a[0])*t;
v[1] = a[1] + (b[1]-a[1])*t;
}
inline int next(int i, int n) { return i+1 < n ? i+1 : 0; }
inline float triarea(const float* a, const float* b, const float* c)
{
return (b[0]*a[1] - a[0]*b[1]) + (c[0]*b[1] - b[0]*c[1]) + (a[0]*c[1] - c[0]*a[1]);
}
inline int left(const float* a, const float* b, const float* c)
{
const float EPS = 0.00001f;
return triarea(a,b,c) < EPS;
}
inline int pntri(const float* a, const float* b, const float* c, const float* pt)
{
return left(a, b, pt) && left(b, c, pt) && left(c, a, pt);
}
inline float distpt(const float* a, const float* b)
{
const float dx = b[0] - a[0];
const float dy = b[1] - a[1];
return dx*dx + dy*dy;
}
void* poolAlloc( void* userData, unsigned int size )
{
struct MemPool* pool = (struct MemPool*)userData;
if (pool->size + size < pool->cap)
{
unsigned char* ptr = pool->buf + pool->size;
pool->size += size;
return ptr;
}
return 0;
}
bool requestLink(mapnode *tmp, float* va, float* vb, float* vc, float x1, float y1, float x2, float y2, int i)
{
if (va[0] == x1 && va[1] == y1) {
if (vb[0] == x2 && vb[1] == y2) {
return true;
} else if (vc[0] == x2 && vc[1] == y2) {
return true;
}
} else if (vb[0] == x1 && vb[1] == y1) {
if (va[0] == x2 && va[1] == y2) {
return true;
} else if (vc[0] == x2 && vc[1] == y2) {
return true;
}
} else if (vc[0] == x1 && vc[1] == y1) {
if (va[0] == x2 && va[1] == y2) {
return true;
} else if (vb[0] == x2 && vb[1] == y2) {
return true;
}
}
return false;
}
struct Edge
{
unsigned short vert[2];
unsigned short polyEdge[2];
unsigned short poly[2];
};
static int buildadj(unsigned short* tris, const int ntris, const int nverts)
{
int maxEdgeCount = ntris*3;
unsigned short* firstEdge = 0;
unsigned short* nextEdge = 0;
struct Edge* edges = 0;
int edgeCount = 0;
int i,j;
firstEdge = (unsigned short*)malloc(sizeof(unsigned short)*(nverts + maxEdgeCount));
if (!firstEdge)
goto cleanup;
nextEdge = firstEdge + nverts;
edges = (struct Edge*)malloc(sizeof(struct Edge)*maxEdgeCount);
if (!edges)
goto cleanup;
for (i = 0; i < nverts; i++)
firstEdge[i] = 0xffff;
for (i = 0; i < ntris; ++i)
{
unsigned short* t = &tris[i*6];
for (j = 0; j < 3; ++j)
{
unsigned short v0 = t[j];
unsigned short v1 = t[(j+1) % 3];
if (v0 < v1)
{
struct Edge* edge = &edges[edgeCount];
edge->vert[0] = v0;
edge->vert[1] = v1;
edge->poly[0] = (unsigned short)i;
edge->polyEdge[0] = (unsigned short)j;
edge->poly[1] = (unsigned short)i;
edge->polyEdge[1] = 0;
// Insert edge
nextEdge[edgeCount] = firstEdge[v0];
firstEdge[v0] = (unsigned short)edgeCount;
edgeCount++;
}
}
}
for (i = 0; i < ntris; ++i)
{
unsigned short* t = &tris[i*6];
for (j = 0; j < 3; ++j)
{
unsigned short v0 = t[j];
unsigned short v1 = t[(j+1) % 3];
if (v0 > v1)
{
unsigned short e;
for (e = firstEdge[v1]; e != 0xffff; e = nextEdge[e])
{
struct Edge* edge = &edges[e];
if (edge->vert[1] == v0 && edge->poly[0] == edge->poly[1])
{
edge->poly[1] = (unsigned short)i;
edge->polyEdge[1] = (unsigned short)j;
break;
}
}
}
}
}
// Store adjacency
for (i = 0; i < ntris; ++i)
{
unsigned short* t = &tris[i*6];
t[3] = 0xffff;
t[4] = 0xffff;
t[5] = 0xffff;
}
for (i = 0; i < edgeCount; ++i)
{
struct Edge* e = &edges[i];
if (e->poly[0] != e->poly[1])
{
unsigned short* t0 = &tris[e->poly[0]*6];
unsigned short* t1 = &tris[e->poly[1]*6];
t0[3+e->polyEdge[0]] = e->poly[1];
t1[3+e->polyEdge[1]] = e->poly[0];
}
}
free(firstEdge);
free(edges);
return 1;
cleanup:
if (firstEdge)
free(firstEdge);
if (edges)
free(edges);
return 0;
}
static int buildmapnode(struct Navmesh* nav)
{
if (!nav) return 0;
//分配map_node内存
nav->defaultMap = (mapnode*)calloc(nav->ntris*2,sizeof(*nav->defaultMap));
if (!nav->defaultMap) return 0;
for (int i = 0; i < nav->ntris; ++i)
{
float cx1, cy1, cx2, cy2, cx3, cy3;
unsigned short* tri = &nav->tris[i*6];
float* va = &nav->verts[tri[0]*2];
float* vb = &nav->verts[tri[1]*2];
float* vc = &nav->verts[tri[2]*2];
mapnode *tmp = &nav->defaultMap[i];
tmp->i = i;
tmp->links[0] = -1;
tmp->links[1] = -1;
tmp->links[2] = -1;
//重心
tmp->x = (va[0] + vb[0] + vc[0])/3;
tmp->y = (va[1] + vb[1] + vc[1])/3;
//中心点坐标
cx1 = (va[0] + vb[0])/2;
cy1 = (va[1] + vb[1])/2;
cx2 = (vc[0] + vb[0])/2;
cy2 = (vc[1] + vb[1])/2;
cx3 = (vc[0] + va[0])/2;
cy3 = (vc[1] + va[1])/2;
//三条边中心点距离
tmp->v[0] = sqrt((cx1 - cx2)*(cx1 - cx2) + (cy1 - cy2)*(cy1 - cy2));
tmp->v[1] = sqrt((cx2 - cx3)*(cx2 - cx3) + (cy2 - cy3)*(cy2 - cy3));
tmp->v[2] = sqrt((cx1 - cx3)*(cx1 - cx3) + (cy1 - cy3)*(cy1 - cy3));
//邻接三角形
for (int i = 0; i < 3; ++i)
{
const unsigned short nei = tri[3+i];
if (nei != 0xffff)
{
unsigned short* tri_nei = &nav->tris[nei*6];
float* na = &nav->verts[tri_nei[0]*2];
float* nb = &nav->verts[tri_nei[1]*2];
float* nc = &nav->verts[tri_nei[2]*2];
//mapnode *nei_node = &nav->map[nei];
if (requestLink(tmp, na, nb, nc, va[0], va[1], vb[0], vb[1], nei)) { tmp->links[0] = nei; continue; }
if (requestLink(tmp, na, nb, nc, vb[0], vb[1], vc[0], vc[1], nei)) { tmp->links[1] = nei; continue; }
if (requestLink(tmp, na, nb, nc, vc[0], vc[1], va[0], va[1], nei)) { tmp->links[2] = nei; continue; }
}
}
}
return 1;
}
static float closestPtPtSeg(const float* pt, const float* sp, const float* sq)
{
float dir[2],diff[3],t,d;
vsub(dir,sq,sp);
vsub(diff,pt,sp);
t = vdot(diff,dir);
if (t <= 0.0f) return 0;
d = vdot(dir,dir);
if (t >= d) return 1;
return t/d;
}
// navmeshCreateEx
struct Navmesh* navmeshCreateEx(std::vector<int> const& trisList, std::vector<std::pair<float, float> > const& pointList)
{
int i=0,j=0,z=0;
struct Navmesh* nav = 0;
int npts = pointList.size();
int ntrisL = trisList.size();
nav = (struct Navmesh*)malloc(sizeof(struct Navmesh));
if (!nav)
goto cleanup;
memset(nav, 0, sizeof(struct Navmesh));
nav->verts = (float*)malloc(sizeof(float)*npts*2);
if (!nav->verts)
goto cleanup;
for(std::vector<std::pair<float, float> >::size_type ix = 0; ix != npts; ++ix)
{
nav->verts[i] = pointList[ix].first;
nav->verts[i+1] = pointList[ix].second;
i=i+2;
}
nav->nverts = npts;
nav->tris = (unsigned short*)malloc(sizeof(unsigned short)*(ntrisL*2));
if (!nav->tris)
goto cleanup;
for(std::vector<int>::size_type jx = 0; jx != ntrisL; ++jx)
{
nav->tris[j] = (unsigned short)trisList[jx];
++z;
if(z==3)
{
j=j+4;
z=0;
}
else
{
++j;
}
}
nav->ntris = ntrisL/3;
if (nav->ntris < 0) nav->ntris = -nav->ntris;
if (!nav->ntris)
goto cleanup;
if (!buildadj(nav->tris, nav->ntris, nav->nverts))
goto cleanup;
if (!buildmapnode(nav))
goto cleanup;
nav->close_list = new dlist();
nav->open_list = new minheap(8192,_less,_clear);
return nav;
cleanup:
if (nav)
{
if (nav->verts)
free(nav->verts);
if (nav->tris)
free(nav->tris);
if (nav->defaultMap)
free(nav->defaultMap);
if (nav->close_list)
free(nav->close_list);
if (nav->open_list)
free(nav->open_list);
free(nav);
}
return 0;
}
// Find nearest triangle
int navmeshFindNearestTri(struct Navmesh* nav, const float* pos, float* nearest)
{
int i, j, besti = -1;
float t, d, p[2], bestd = FLT_MAX;
if (nearest)
vcpy(nearest, pos);
for (i = 0; i < nav->ntris; ++i)
{
const unsigned short* tri = &nav->tris[i*6];
const float* va = &nav->verts[tri[0]*2];
const float* vb = &nav->verts[tri[1]*2];
const float* vc = &nav->verts[tri[2]*2];
if (pntri(va,vb,vc,pos))
{
return i;
}
}
for (i = 0; i < nav->ntris; ++i)
{
const unsigned short* tri = &nav->tris[i*6];
for (j = 0; j < 3; ++j)
{
const float* va = &nav->verts[tri[j]*2];
const float* vb = &nav->verts[tri[(j+1)%3]*2];
if (tri[3+j] != 0xffff)
continue;
t = closestPtPtSeg(pos,va,vb);
vlerp(p,va,vb,t);
d = distpt(p,pos);
if (d < bestd)
{
if (nearest)
vcpy(nearest, p);
bestd = d;
besti = i;
}
}
}
return besti;
}
int navmeshFindPath(struct Navmesh* nav, const float* start, const float* end, unsigned short* path, const int maxpath)
{
// 根据地图大小适当调整宏
#define MAX_STACK 128*8
#define MAX_PARENT 128*8
int i, starti, endi, stack[MAX_STACK], nstack;
unsigned short parent[MAX_PARENT];
starti = navmeshFindNearestTri(nav, start, NULL);
endi = navmeshFindNearestTri(nav, end, NULL);
if (starti == -1 || endi == -1)
return 0;
if (starti == endi)
{
path[0] = (unsigned short)starti;
return 1;
}
memset(parent, 0xff, sizeof(unsigned short)*MAX_PARENT);
nstack = 0;
stack[nstack++] = endi;
parent[endi] = endi;
while (nstack)
{
unsigned short* tri;
unsigned short cur;
// Pop front.
cur = stack[0];
nstack--;
for (i = 0; i < nstack; ++i)
stack[i] = stack[i+1];
if (cur == starti)
{
// Trace and store back.
int npath = 0;
for (;;)
{
path[npath++] = cur;
if (npath >= maxpath) break;
if (parent[cur] == cur) break;
cur = parent[cur];
}
return npath;
}
tri = &nav->tris[cur*6];
for (i = 0; i < 3; ++i)
{
const unsigned short nei = tri[3+i];
if (nei == 0xffff) continue;
if (parent[nei] != 0xffff) continue;
parent[nei] = cur;
if (nstack < MAX_STACK)
stack[nstack++] = nei;
}
}
return 0;
}
void reset(minheap* open_list, dlist* close_list)
{
mapnode *n = NULL;
while(n = (mapnode*)close_list->Pop()){
n->G = n->H = n->F = n->z = 0;
n->parent = NULL;
}
open_list->Clear();
}
//计算到达相临节点需要的代价
double cost_2_neighbor(mapnode *from,int i)
{
int zz = i + from->z;
if (zz == 0 || zz == 1)
return from->v[0];
else if(zz == 2)
return from->v[2];
else if(zz == 3)
return from->v[1];
else
{
assert(0);
return 0.0f;
}
}
// H估值计算
double cost_2_goal(mapnode *from,const float* to)
{
float delta_x = from->x - to[0];
float delta_y = from->y - to[1];
return sqrt(delta_x*delta_x + delta_y*delta_y);
}
//记录路径从上一个节点进入该节点的边(如果从终点开始寻路即为穿出边)
void setZ(mapnode *node, int idx)
{
node->z = 0;
if (node->links[0] == idx)
node->z = 0;
else if(node->links[1] == idx)
node->z = 1;
else if(node->links[2] == idx)
node->z = 2;
else
assert(0);
}
int navmeshFindBestPath(struct Navmesh* nav, const float* start, const float* end, unsigned short* path)
{
int starti, endi, tpath[AGENT_MAX_PATH];
starti = navmeshFindNearestTri(nav, start, NULL);
endi = navmeshFindNearestTri(nav, end, NULL);
if (starti == -1 || endi == -1)
return 0;
if (starti == endi)
{
path[0] = (unsigned short)starti;
return 1;
}
mapnode *from = &nav->defaultMap[starti];
mapnode *to = &nav->defaultMap[endi];
nav->open_list->insert(to);
mapnode *current_node = NULL;
for(;;)
{
current_node = (mapnode*)nav->open_list->popmin();
if(!current_node){
reset(nav->open_list, nav->close_list);
return 0;
}
if(current_node->i == from->i){
int npath = 0;
while(current_node)
{
path[npath++] = current_node->i;
mapnode *t = current_node;
current_node = current_node->parent;
t->parent = NULL;
t->F = t->G = t->H = t->z = 0;
t->index = 0;
}
reset(nav->open_list, nav->close_list);
return npath;
}
//current插入到close表
nav->close_list->Push(current_node);
for(int i=0; i<3; ++i)
{
int nei = current_node->links[i];
if (nei == -1) continue;
mapnode *neighbor = &nav->defaultMap[nei];
if(neighbor->pre || neighbor->next){
continue;//在close表中,不做处理
}
if(neighbor->index)//在openlist中
{
double new_G = current_node->G + cost_2_neighbor(current_node,i);
if(new_G < neighbor->G)
{
//经过当前neighbor路径更佳,更新路径
neighbor->G = new_G;
neighbor->F = neighbor->G + neighbor->H;
neighbor->parent = current_node;
nav->open_list->change(neighbor);
setZ(neighbor, current_node->i);
}
continue;
}
neighbor->parent = current_node;
neighbor->G = current_node->G + cost_2_neighbor(current_node,i);
neighbor->H = cost_2_goal(neighbor,start);
neighbor->F = neighbor->G + neighbor->H;
nav->open_list->insert(neighbor);
setZ(neighbor, current_node->i);
}
}
return 0;
}
inline int vequal(const float* a, const float* b)
{
static const float eq = 0.001f*0.001f;
return distpt(a, b) < eq;
}
inline int pushPoint(float* pts, int npts, const int maxpts, const float* pt)
{
if (npts >= maxpts) return npts;
if (vequal(pt, &pts[(npts-1)*2])) return npts;
vcpy(&pts[npts*2], pt);
return npts+1;
}
static int stringPull(const float* portals, int nportals, float* pts, const int maxpts)
{
int npts = 0, i;
float portalApex[2], portalLeft[2], portalRight[2];
int apexIndex = 0, leftIndex = 0, rightIndex = 0;
vcpy(portalApex, &portals[0]);
vcpy(portalLeft, &portals[0]);
vcpy(portalRight, &portals[2]);
// Add start point.
vcpy(&pts[npts*2], portalApex);
npts++;
for (i = 1; i < nportals && npts < maxpts; ++i)
{
const float* left = &portals[i*4+0];
const float* right = &portals[i*4+2];
// Update right vertex.
if (triarea(portalApex, portalRight, right) <= 0.0f)
{
if (vequal(portalApex, portalRight) || triarea(portalApex, portalLeft, right) > 0.0f)
{
// Tighten the funnel.
vcpy(portalRight, right);
rightIndex = i;
}
else
{
// Right over left, insert left to path and restart scan from portal left point.
npts = pushPoint(pts, npts, maxpts, portalLeft);
// Make current left the new apex.
vcpy(portalApex, portalLeft);
apexIndex = leftIndex;
// Reset portal
vcpy(portalLeft, portalApex);
vcpy(portalRight, portalApex);
leftIndex = apexIndex;
rightIndex = apexIndex;
// Restart scan
i = apexIndex;
continue;
}
}
// Update left vertex.
if (triarea(portalApex, portalLeft, left) >= 0.0f)
{
if (vequal(portalApex, portalLeft) || triarea(portalApex, portalRight, left) < 0.0f)
{
// Tighten the funnel.
vcpy(portalLeft, left);
leftIndex = i;
}
else
{
// Left over right, insert right to path and restart scan from portal right point.
npts = pushPoint(pts, npts, maxpts, portalRight);
// Make current right the new apex.
vcpy(portalApex, portalRight);
apexIndex = rightIndex;
// Reset portal
vcpy(portalLeft, portalApex);
vcpy(portalRight, portalApex);
leftIndex = apexIndex;
rightIndex = apexIndex;
// Restart scan
i = apexIndex;
continue;
}
}
}
// Append last point to path.
npts = pushPoint(pts, npts, maxpts, &portals[(nportals-1)*4+0]);
return npts;
}
static void getPortalPoints(struct Navmesh* nav, const unsigned short a, const unsigned short b,
float* left, float* right)
{
const unsigned short* ta = &nav->tris[a*6];
int i;
for (i = 0; i < 3; ++i)
{
if (ta[3+i] == b)
{
const float* va = &nav->verts[ta[i]*2];
const float* vb = &nav->verts[ta[(i+1)%3]*2];
vcpy(right, va);
vcpy(left, vb);
}
}
}
int navmeshStringPull(struct Navmesh* nav, const float* start, const float* end,
const unsigned short* path, const int npath,
float* pts, const int maxpts)
{
#define MAX_PORTALS 128
float portals[MAX_PORTALS*4];
int nportals = 0, i;
// Start portal
vcpy(&portals[nportals*4+0], start);
vcpy(&portals[nportals*4+2], start);
nportals++;
// Portal between navmesh polygons
for (i = 0; i < npath-1; ++i)
{
getPortalPoints(nav, path[i], path[i+1], &portals[nportals*4+0], &portals[nportals*4+2]);
nportals++;
}
// End portal
vcpy(&portals[nportals*4+0], end);
vcpy(&portals[nportals*4+2], end);
nportals++;
return stringPull(portals, nportals, pts, maxpts);
}
// Deletes navmesh.
void navmeshDelete(struct Navmesh* nav)
{
if (!nav)
return;
if (nav->verts)
free(nav->verts);
if (nav->tris)
free(nav->tris);
if (nav->defaultMap)
free(nav->defaultMap);
if (nav->close_list)
free(nav->close_list);
if (nav->open_list)
free(nav->open_list);
free(nav);
}
int posValid(const float* p)
{
return p[0] < FLT_MAX && p[1] < FLT_MAX;
}
void agentInit(struct NavmeshAgent* agent)
{
vset(agent->pos, FLT_MAX, FLT_MAX);
vset(agent->oldpos, FLT_MAX, FLT_MAX);
vset(agent->target, FLT_MAX, FLT_MAX);
agent->npath = 0;
agent->ncorners = 0;
}
void agentFindPath(struct NavmeshAgent* agent, struct Navmesh* nav)
{
agent->npath = 0;
if (posValid(agent->target))
agent->npath = navmeshFindBestPath(nav, agent->pos, agent->target, agent->path);
//agent->npath = navmeshFindPath(nav, agent->pos, agent->target, agent->path, AGENT_MAX_PATH);
}
int FindPath(struct Navmesh* nav, struct NavmeshAgent* agent)
{
//获得联通的三角形
agentFindPath(agent, nav);
//拉线获得路径
agent->ncorners = 0;
if (agent->npath)
agent->ncorners = navmeshStringPull(nav, agent->pos, agent->target, agent->path, agent->npath, agent->corners, MAX_CORNERS);
return agent->ncorners;
}