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
|
using UnityEngine;
using System;
using System.Collections.Generic;
namespace Pathfinding {
/// <summary>
/// Radius path modifier for offsetting paths.
///
/// The radius modifier will offset the path to create the effect
/// of adjusting it to the characters radius.
/// It gives good results on navmeshes which have not been offset with the
/// character radius during scan. Especially useful when characters with different
/// radiuses are used on the same navmesh. It is also useful when using
/// rvo local avoidance with the RVONavmesh since the RVONavmesh assumes the
/// navmesh has not been offset with the character radius.
///
/// This modifier assumes all paths are in the XZ plane (i.e Y axis is up).
///
/// It is recommended to use the Funnel Modifier on the path as well.
///
/// [Open online documentation to see images]
///
/// See: RVONavmesh
/// See: modifiers
///
/// Also check out the howto page "Using Modifiers".
///
/// Since: Added in 3.2.6
/// </summary>
[AddComponentMenu("Pathfinding/Modifiers/Radius Offset Modifier")]
[HelpURL("https://arongranberg.com/astar/documentation/stable/radiusmodifier.html")]
public class RadiusModifier : MonoModifier {
#if UNITY_EDITOR
[UnityEditor.MenuItem("CONTEXT/Seeker/Add Radius Modifier")]
public static void AddComp (UnityEditor.MenuCommand command) {
(command.context as Component).gameObject.AddComponent(typeof(RadiusModifier));
}
#endif
public override int Order { get { return 41; } }
/// <summary>
/// Radius of the circle segments generated.
/// Usually similar to the character radius.
/// </summary>
public float radius = 1f;
/// <summary>
/// Detail of generated circle segments.
/// Measured as steps per full circle.
///
/// It is more performant to use a low value.
/// For movement, using a high value will barely improve path quality.
/// </summary>
public float detail = 10;
/// <summary>
/// Calculates inner tangents for a pair of circles.
///
/// Add a to sigma to get the first tangent angle, subtract a from sigma to get the second tangent angle.
///
/// Returns: True on success. False when the circles are overlapping.
/// </summary>
/// <param name="p1">Position of first circle</param>
/// <param name="p2">Position of the second circle</param>
/// <param name="r1">Radius of the first circle</param>
/// <param name="r2">Radius of the second circle</param>
/// <param name="a">Angle from the line joining the centers of the circles to the inner tangents.</param>
/// <param name="sigma">World angle from p1 to p2 (in XZ space)</param>
bool CalculateCircleInner (Vector3 p1, Vector3 p2, float r1, float r2, out float a, out float sigma) {
float dist = (p1-p2).magnitude;
if (r1+r2 > dist) {
a = 0;
sigma = 0;
return false;
}
a = (float)Math.Acos((r1+r2)/dist);
sigma = (float)Math.Atan2(p2.z-p1.z, p2.x-p1.x);
return true;
}
/// <summary>
/// Calculates outer tangents for a pair of circles.
///
/// Add a to sigma to get the first tangent angle, subtract a from sigma to get the second tangent angle.
///
/// Returns: True on success. False on failure (more specifically when |r1-r2| > |p1-p2| )
/// </summary>
/// <param name="p1">Position of first circle</param>
/// <param name="p2">Position of the second circle</param>
/// <param name="r1">Radius of the first circle</param>
/// <param name="r2">Radius of the second circle</param>
/// <param name="a">Angle from the line joining the centers of the circles to the inner tangents.</param>
/// <param name="sigma">World angle from p1 to p2 (in XZ space)</param>
bool CalculateCircleOuter (Vector3 p1, Vector3 p2, float r1, float r2, out float a, out float sigma) {
float dist = (p1-p2).magnitude;
if (Math.Abs(r1 - r2) > dist) {
a = 0;
sigma = 0;
return false;
}
a = (float)Math.Acos((r1-r2)/dist);
sigma = (float)Math.Atan2(p2.z-p1.z, p2.x-p1.x);
return true;
}
[System.Flags]
enum TangentType {
OuterRight = 1 << 0,
InnerRightLeft = 1 << 1,
InnerLeftRight = 1 << 2,
OuterLeft = 1 << 3,
Outer = OuterRight | OuterLeft,
Inner = InnerRightLeft | InnerLeftRight
}
TangentType CalculateTangentType (Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4) {
bool l1 = VectorMath.RightOrColinearXZ(p1, p2, p3);
bool l2 = VectorMath.RightOrColinearXZ(p2, p3, p4);
return (TangentType)(1 << ((l1 ? 2 : 0) + (l2 ? 1 : 0)));
}
TangentType CalculateTangentTypeSimple (Vector3 p1, Vector3 p2, Vector3 p3) {
bool l2 = VectorMath.RightOrColinearXZ(p1, p2, p3);
bool l1 = l2;
return (TangentType)(1 << ((l1 ? 2 : 0) + (l2 ? 1 : 0)));
}
public override void Apply (Path p) {
List<Vector3> vs = p.vectorPath;
List<Vector3> res = Apply(vs);
if (res != vs) {
Pathfinding.Util.ListPool<Vector3>.Release(ref p.vectorPath);
p.vectorPath = res;
}
}
float[] radi = new float[10];
float[] a1 = new float[10];
float[] a2 = new float[10];
bool[] dir = new bool[10];
/// <summary>Apply this modifier on a raw Vector3 list</summary>
public List<Vector3> Apply (List<Vector3> vs) {
if (vs == null || vs.Count < 3) return vs;
/// <summary>TODO: Do something about these allocations</summary>
if (radi.Length < vs.Count) {
radi = new float[vs.Count];
a1 = new float[vs.Count];
a2 = new float[vs.Count];
dir = new bool[vs.Count];
}
for (int i = 0; i < vs.Count; i++) {
radi[i] = radius;
}
radi[0] = 0;
radi[vs.Count-1] = 0;
int count = 0;
for (int i = 0; i < vs.Count-1; i++) {
count++;
if (count > 2*vs.Count) {
Debug.LogWarning("Could not resolve radiuses, the path is too complex. Try reducing the base radius");
break;
}
TangentType tt;
if (i == 0) {
tt = CalculateTangentTypeSimple(vs[i], vs[i+1], vs[i+2]);
} else if (i == vs.Count-2) {
tt = CalculateTangentTypeSimple(vs[i-1], vs[i], vs[i+1]);
} else {
tt = CalculateTangentType(vs[i-1], vs[i], vs[i+1], vs[i+2]);
}
//DrawCircle (vs[i], radi[i], Color.yellow);
if ((tt & TangentType.Inner) != 0) {
//Angle to tangent
float a;
//Angle to other circle
float sigma;
//Calculate angles to the next circle and angles for the inner tangents
if (!CalculateCircleInner(vs[i], vs[i+1], radi[i], radi[i+1], out a, out sigma)) {
//Failed, try modifying radiuses
float magn = (vs[i+1]-vs[i]).magnitude;
radi[i] = magn*(radi[i]/(radi[i]+radi[i+1]));
radi[i+1] = magn - radi[i];
radi[i] *= 0.99f;
radi[i+1] *= 0.99f;
i -= 2;
continue;
}
if (tt == TangentType.InnerRightLeft) {
a2[i] = sigma-a;
a1[i+1] = sigma-a + (float)Math.PI;
dir[i] = true;
} else {
a2[i] = sigma+a;
a1[i+1] = sigma+a + (float)Math.PI;
dir[i] = false;
}
} else {
float sigma;
float a;
//Calculate angles to the next circle and angles for the outer tangents
if (!CalculateCircleOuter(vs[i], vs[i+1], radi[i], radi[i+1], out a, out sigma)) {
//Failed, try modifying radiuses
if (i == vs.Count-2) {
//The last circle has a fixed radius at 0, don't modify it
radi[i] = (vs[i+1]-vs[i]).magnitude;
radi[i] *= 0.99f;
i -= 1;
} else {
if (radi[i] > radi[i+1]) {
radi[i+1] = radi[i] - (vs[i+1]-vs[i]).magnitude;
} else {
radi[i+1] = radi[i] + (vs[i+1]-vs[i]).magnitude;
}
radi[i+1] *= 0.99f;
}
i -= 1;
continue;
}
if (tt == TangentType.OuterRight) {
a2[i] = sigma-a;
a1[i+1] = sigma-a;
dir[i] = true;
} else {
a2[i] = sigma+a;
a1[i+1] = sigma+a;
dir[i] = false;
}
}
}
List<Vector3> res = Pathfinding.Util.ListPool<Vector3>.Claim();
res.Add(vs[0]);
if (detail < 1) detail = 1;
float step = (float)(2*Math.PI)/detail;
for (int i = 1; i < vs.Count-1; i++) {
float start = a1[i];
float end = a2[i];
float rad = radi[i];
if (dir[i]) {
if (end < start) end += (float)Math.PI*2;
for (float t = start; t < end; t += step) {
res.Add(new Vector3((float)Math.Cos(t), 0, (float)Math.Sin(t))*rad + vs[i]);
}
} else {
if (start < end) start += (float)Math.PI*2;
for (float t = start; t > end; t -= step) {
res.Add(new Vector3((float)Math.Cos(t), 0, (float)Math.Sin(t))*rad + vs[i]);
}
}
}
res.Add(vs[vs.Count-1]);
return res;
}
}
}
|