Mercurial > projects > openmelee
annotate ai/steer.d @ 19:08ddf9e71b88
steer to avoid
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Wed, 25 Mar 2009 14:44:47 -0400 |
parents | 7f74e064dad5 |
children | 6efd0830715b |
rev | line source |
---|---|
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
1 /* |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
2 * Copyright (c) 2009, Mason Green (zzzzrrr) |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
3 * http://www.dsource.org/projects/openmelee |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
4 * Based on OpenSteer, Copyright (c) 2002-2003, Sony Computer Entertainment America |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
5 * Original author: Craig Reynolds |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
6 * |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
7 * All rights reserved. |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
8 * |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
9 * Redistribution and use in source and binary forms, with or without modification, |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
10 * are permitted provided that the following conditions are met: |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
11 * |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
12 * * Redistributions of source code must retain the above copyright notice, |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
13 * this list of conditions and the following disclaimer. |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
14 * * Redistributions in binary form must reproduce the above copyright notice, |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
15 * this list of conditions and the following disclaimer in the documentation |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
16 * and/or other materials provided with the distribution. |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
17 * * Neither the name of the polygonal nor the names of its contributors may be |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
18 * used to endorse or promote products derived from this software without specific |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
19 * prior written permission. |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
20 * |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
32 */ |
18 | 33 module openmelee.ai.steer; |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
34 |
19 | 35 import tango.io.Stdout : Stdout; |
36 | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
37 import blaze.common.bzMath: bzDot, bzClamp, bzVec2; |
19 | 38 import blaze.collision.shapes.bzShape : bzShape; |
39 import tango.math.Math : sqrt; | |
15 | 40 import blaze.dynamics.bzBody: bzBody; |
41 | |
18 | 42 import openmelee.ships.ship : Ship, State; |
43 import openmelee.ai.utilities; | |
0 | 44 |
9 | 45 class Steer |
46 { | |
47 // Constructor: initializes state | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
48 this (Ship ship) |
0 | 49 { |
19 | 50 m_ship = ship; |
51 m_body = ship.rBody; | |
52 m_radius = 0.0f; | |
53 | |
54 // Find radius of body | |
55 for (bzShape s = m_body.shapeList; s; s = s.next) { | |
56 if(s.sweepRadius > m_radius) { | |
57 m_radius = s.sweepRadius; | |
58 } | |
59 } | |
15 | 60 } |
61 | |
62 struct PathIntersection | |
63 { | |
64 bool intersect; | |
65 float distance; | |
66 bzVec2 surfacePoint; | |
67 bzVec2 surfaceNormal; | |
68 bzBody obstacle; | |
9 | 69 } |
0 | 70 |
9 | 71 // reset state |
72 void reset () { | |
73 // initial state of wander behavior | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
74 m_wanderSide = 0; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
75 m_wanderUp = 0; |
9 | 76 } |
77 | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
78 void update() { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
79 m_position = m_ship.state.position; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
80 m_velocity = m_ship.state.velocity; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
81 m_speed = m_ship.state.speed; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
82 m_maxForce = m_ship.state.maxForce; |
12 | 83 m_forward = m_ship.state.forward; |
9 | 84 } |
0 | 85 |
9 | 86 // -------------------------------------------------- steering behaviors |
0 | 87 |
9 | 88 bzVec2 steerForWander (float dt) { |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
89 // random walk m_wanderSide and m_wanderUp between -1 and +1 |
9 | 90 float speed = 12 * dt; // maybe this (12) should be an argument? |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
91 m_wanderSide = scalarRandomWalk (m_wanderSide, speed, -1, +1); |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
92 m_wanderUp = scalarRandomWalk (m_wanderUp, speed, -1, +1); |
0 | 93 |
9 | 94 // return a pure lateral steering vector: (+/-Side) + (+/-Up) |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
95 return (m_side * m_wanderSide) + (m_up * m_wanderUp); |
9 | 96 } |
0 | 97 |
9 | 98 // Seek behavior |
99 bzVec2 steerForSeek (bzVec2 target) { | |
100 bzVec2 desiredVelocity = target - m_position; | |
101 return desiredVelocity - m_velocity; | |
102 } | |
0 | 103 |
9 | 104 // Flee behavior |
105 bzVec2 steerForFlee (bzVec2 target) { | |
106 bzVec2 desiredVelocity = m_position - target; | |
107 return desiredVelocity - m_velocity; | |
108 } | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
109 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
110 /* |
9 | 111 // xxx proposed, experimental new seek/flee [cwr 9-16-02] |
112 bzVec2 xxxsteerForFlee (bzVec2 target) { | |
113 bzVec2 offset = m_position - target; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
114 bzVec2 desiredVelocity = bzClamp(offset.truncateLength (maxSpeed ()); |
9 | 115 return desiredVelocity - m_velocity; |
116 } | |
0 | 117 |
9 | 118 bzVec2 xxxsteerForSeek (bzVec2 target) { |
119 // bzVec2 offset = target - position; | |
120 bzVec2 offset = target - m_position; | |
121 bzVec2 desiredVelocity = offset.truncateLength (maxSpeed ()); //xxxnew | |
122 return desiredVelocity - m_velocity; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
123 } |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
124 */ |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
125 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
126 /* |
9 | 127 // ------------------------------------------------------------------------ |
128 // Obstacle Avoidance behavior | |
129 // | |
130 // Returns a steering force to avoid a given obstacle. The purely | |
131 // lateral steering force will turn our vehicle towards a silhouette edge | |
132 // of the obstacle. Avoidance is required when (1) the obstacle | |
133 // intersects the vehicle's current path, (2) it is in front of the | |
134 // vehicle, and (3) is within minTimeToCollision seconds of travel at the | |
135 // vehicle's current velocity. Returns a zero vector value (bzVec2::zero) | |
136 // when no avoidance is required. | |
137 bzVec2 steerToAvoidObstacle (float minTimeToCollision, Obstacle obstacle) { | |
0 | 138 |
9 | 139 bzVec2 avoidance = obstacle.steerToAvoid (this, minTimeToCollision); |
140 return avoidance; | |
141 } | |
142 | |
15 | 143 // avoids all obstacles in an ObstacleGroup |
144 */ | |
145 | |
146 bzVec2 steerToAvoidObstacles (float minTimeToCollision, bzBody obstacles) { | |
0 | 147 |
9 | 148 bzVec2 avoidance; |
149 PathIntersection nearest, next; | |
15 | 150 float minDistanceToCollision = minTimeToCollision * m_speed; |
19 | 151 |
9 | 152 next.intersect = false; |
153 nearest.intersect = false; | |
0 | 154 |
9 | 155 // test all obstacles for intersection with my forward axis, |
156 // select the one whose point of intersection is nearest | |
19 | 157 for (bzBody o = obstacles; o; o = o.next) { |
158 | |
159 if(o is m_body) continue; | |
160 | |
15 | 161 // This code which presumes the obstacle is spherical |
19 | 162 findNextIntersectionWithSphere(o, next); |
0 | 163 |
15 | 164 if (!nearest.intersect || (next.intersect && next.distance < nearest.distance)) { |
165 nearest = next; | |
166 } | |
9 | 167 } |
19 | 168 |
9 | 169 // when a nearest intersection was found |
15 | 170 if (nearest.intersect && (nearest.distance < minDistanceToCollision)) { |
19 | 171 Stdout("Distance: ")(nearest.distance).newline; |
9 | 172 // compute avoidance steering force: take offset from obstacle to me, |
173 // take the component of that which is lateral (perpendicular to my | |
174 // forward direction), set length to maxForce, add a bit of forward | |
175 // component (in capture the flag, we never want to slow down) | |
15 | 176 bzVec2 offset = m_position - nearest.obstacle.position; |
19 | 177 avoidance = perpendicularComponent(offset, m_forward); |
15 | 178 avoidance.normalize; |
19 | 179 //avoidance *= m_maxForce; |
180 //avoidance += m_forward * m_maxForce * 0.75f; | |
0 | 181 } |
182 | |
9 | 183 return avoidance; |
184 } | |
15 | 185 |
19 | 186 void findNextIntersectionWithSphere(bzBody obs, |
187 inout PathIntersection intersection) { | |
188 | |
189 // This routine is based on the Paul Bourke's derivation in: | |
190 // Intersection of a Line and a Sphere (or circle) | |
191 // http://www.swin.edu.au/astronomy/pbourke/geometry/sphereline/ | |
192 | |
193 float b, c, d, p, q, s; | |
194 bzVec2 lc; | |
195 | |
196 // initialize pathIntersection object | |
197 intersection.intersect = false; | |
198 intersection.obstacle = obs; | |
199 | |
200 // find "local center" (lc) of sphere in boid's coordinate space | |
201 lc = m_body.localPoint(obs.position); | |
202 | |
203 // computer line-sphere intersection parameters | |
204 b = 0; | |
205 | |
206 // Find radius of obstacle | |
207 float obsRadius = 0; | |
208 for (bzShape shape = obs.shapeList; shape; shape = shape.next) { | |
209 if(shape.sweepRadius > obsRadius) { | |
210 obsRadius = shape.sweepRadius; | |
211 } | |
212 } | |
213 | |
214 c = square(lc.x) + square(lc.y) - square(obsRadius + m_radius); | |
215 d = (b * b) - (4 * c); | |
216 | |
217 // when the path does not intersect the sphere | |
218 if (d < 0) return; | |
219 | |
220 // otherwise, the path intersects the sphere in two points with | |
221 // parametric coordinates of "p" and "q". | |
222 // (If "d" is zero the two points are coincident, the path is tangent) | |
223 s = sqrt(d); | |
224 p = (-b + s) * 0.5f; | |
225 q = (-b - s) * 0.5f; | |
226 | |
227 // both intersections are behind us, so no potential collisions | |
228 if ((p < 0) && (q < 0)) return; | |
229 | |
230 // at least one intersection is in front of us | |
231 intersection.intersect = true; | |
232 intersection.distance = | |
233 ((p > 0) && (q > 0)) ? | |
234 // both intersections are in front of us, find nearest one | |
235 ((p < q) ? p : q) : | |
236 // otherwise only one intersections is in front, select it | |
237 ((p > 0) ? p : q); | |
238 } | |
239 | |
15 | 240 /* |
9 | 241 // ------------------------------------------------------------------------ |
242 // Unaligned collision avoidance behavior: avoid colliding with other | |
243 // nearby vehicles moving in unconstrained directions. Determine which | |
244 // (if any) other other vehicle we would collide with first, then steers | |
245 // to avoid the site of that potential collision. Returns a steering | |
246 // force vector, which is zero length if there is no impending collision. | |
247 | |
248 bzVec2 steerToAvoidNeighbors (float minTimeToCollision, AVGroup others) { | |
0 | 249 |
9 | 250 // first priority is to prevent immediate interpenetration |
251 bzVec2 separation = steerToAvoidCloseNeighbors (0, others); | |
252 if (separation != bzVec2::zero) return separation; | |
0 | 253 |
9 | 254 // otherwise, go on to consider potential future collisions |
255 float steer = 0; | |
15 | 256 Ship threat; |
9 | 257 |
258 // Time (in seconds) until the most immediate collision threat found | |
259 // so far. Initial value is a threshold: don't look more than this | |
260 // many frames into the future. | |
261 float minTime = minTimeToCollision; | |
0 | 262 |
9 | 263 // xxx solely for annotation |
264 bzVec2 xxxThreatPositionAtNearestApproach; | |
265 bzVec2 xxxOurPositionAtNearestApproach; | |
0 | 266 |
9 | 267 // for each of the other vehicles, determine which (if any) |
268 // pose the most immediate threat of collision. | |
269 for (AVIterator i = others.begin(); i != others.end(); i++) | |
270 { | |
15 | 271 Ship other = i; |
272 if (other !is this) | |
0 | 273 { |
9 | 274 // avoid when future positions are this close (or less) |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
275 float collisionDangerThreshold = radius * 2; |
9 | 276 |
277 // predicted time until nearest approach of "this" and "other" | |
278 float time = predictNearestApproachTime (other); | |
0 | 279 |
9 | 280 // If the time is in the future, sooner than any other |
281 // threatened collision... | |
282 if ((time >= 0) (time < minTime)) | |
283 { | |
284 // if the two will be close enough to collide, | |
285 // make a note of it | |
286 if (computeNearestApproachPositions (other, time) | |
287 < collisionDangerThreshold) | |
0 | 288 { |
9 | 289 minTime = time; |
290 threat = other; | |
291 xxxThreatPositionAtNearestApproach | |
292 = hisPositionAtNearestApproach; | |
293 xxxOurPositionAtNearestApproach | |
294 = ourPositionAtNearestApproach; | |
0 | 295 } |
296 } | |
297 } | |
9 | 298 } |
0 | 299 |
9 | 300 // if a potential collision was found, compute steering to avoid |
301 if (threat) | |
302 { | |
303 // parallel: +1, perpendicular: 0, anti-parallel: -1 | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
304 float parallelness = m_forward.dot(threat.forward); |
9 | 305 float angle = 0.707f; |
306 | |
307 if (parallelness < -angle) | |
0 | 308 { |
9 | 309 // anti-parallel "head on" paths: |
310 // steer away from future threat position | |
311 bzVec2 offset = xxxThreatPositionAtNearestApproach - m_position; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
312 float sideDot = offset.dot(m_side()); |
9 | 313 steer = (sideDot > 0) ? -1.0f : 1.0f; |
314 } | |
315 else | |
316 { | |
317 if (parallelness > angle) | |
0 | 318 { |
9 | 319 // parallel paths: steer away from threat |
320 bzVec2 offset = threat.position - m_position; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
321 float sideDot = bzDot(offset, m_side); |
0 | 322 steer = (sideDot > 0) ? -1.0f : 1.0f; |
323 } | |
324 else | |
325 { | |
9 | 326 // perpendicular paths: steer behind threat |
327 // (only the slower of the two does this) | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
328 if (threat.speed() <= speed) |
0 | 329 { |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
330 float sideDot = bzDot(m_side, threat.velocity); |
0 | 331 steer = (sideDot > 0) ? -1.0f : 1.0f; |
332 } | |
333 } | |
334 } | |
335 } | |
336 | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
337 return m_side() * steer; |
9 | 338 } |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
339 */ |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
340 |
9 | 341 // Given two vehicles, based on their current positions and velocities, |
342 // determine the time until nearest approach | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
343 float predictNearestApproachTime (State other) { |
9 | 344 |
345 // imagine we are at the origin with no velocity, | |
346 // compute the relative velocity of the other vehicle | |
347 bzVec2 myVelocity = m_velocity; | |
348 bzVec2 otherVelocity = other.velocity; | |
349 bzVec2 relVelocity = otherVelocity - myVelocity; | |
350 float relSpeed = relVelocity.length; | |
351 | |
352 // for parallel paths, the vehicles will always be at the same distance, | |
353 // so return 0 (aka "now") since "there is no time like the present" | |
354 if (relSpeed == 0) return 0; | |
355 | |
356 // Now consider the path of the other vehicle in this relative | |
357 // space, a line defined by the relative position and velocity. | |
358 // The distance from the origin (our vehicle) to that line is | |
359 // the nearest approach. | |
360 | |
361 // Take the unit tangent along the other vehicle's path | |
362 bzVec2 relTangent = relVelocity / relSpeed; | |
363 | |
364 // find distance from its path to origin (compute offset from | |
365 // other to us, find length of projection onto path) | |
366 bzVec2 relPosition = m_position - other.position; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
367 float projection = bzDot(relTangent, relPosition); |
9 | 368 |
369 return projection / relSpeed; | |
370 } | |
0 | 371 |
9 | 372 // Given the time until nearest approach (predictNearestApproachTime) |
373 // determine position of each vehicle at that time, and the distance | |
374 // between them | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
375 float computeNearestApproachPositions (State other, float time) { |
0 | 376 |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
377 bzVec2 myTravel = m_forward * m_speed * time; |
9 | 378 bzVec2 otherTravel = other.forward * other.speed * time; |
379 | |
380 bzVec2 myFinal = m_position + myTravel; | |
381 bzVec2 otherFinal = other.position + otherTravel; | |
0 | 382 |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
383 return (myFinal - otherFinal).length; |
9 | 384 } |
385 | |
386 // ------------------------------------------------------------------------ | |
387 // pursuit of another vehicle ( version with ceiling on prediction time) | |
388 | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
389 bzVec2 steerForPursuit (State quarry) { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
390 return steerForPursuit (quarry, float.max); |
9 | 391 } |
392 | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
393 bzVec2 steerForPursuit (State quarry, float maxPredictionTime) { |
0 | 394 |
9 | 395 // offset from this to quarry, that distance, unit vector toward quarry |
396 bzVec2 offset = quarry.position - m_position; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
397 float distance = offset.length; |
9 | 398 bzVec2 unitOffset = offset / distance; |
399 | |
400 // how parallel are the paths of "this" and the quarry | |
401 // (1 means parallel, 0 is pependicular, -1 is anti-parallel) | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
402 float parallelness = bzDot(m_forward , quarry.forward); |
9 | 403 |
404 // how "forward" is the direction to the quarry | |
405 // (1 means dead ahead, 0 is directly to the side, -1 is straight back) | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
406 float forwardness = bzDot(m_forward , unitOffset); |
0 | 407 |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
408 float directTravelTime = distance / m_speed; |
9 | 409 int f = intervalComparison (forwardness, -0.707f, 0.707f); |
410 int p = intervalComparison (parallelness, -0.707f, 0.707f); | |
411 | |
412 float timeFactor = 0; // to be filled in below | |
0 | 413 |
9 | 414 // Break the pursuit into nine cases, the cross product of the |
415 // quarry being [ahead, aside, or behind] us and heading | |
416 // [parallel, perpendicular, or anti-parallel] to us. | |
417 switch (f) | |
418 { | |
419 case +1: | |
420 switch (p) | |
0 | 421 { |
9 | 422 case +1: // ahead, parallel |
423 timeFactor = 4; | |
0 | 424 break; |
9 | 425 case 0: // ahead, perpendicular |
426 timeFactor = 1.8f; | |
0 | 427 break; |
9 | 428 case -1: // ahead, anti-parallel |
429 timeFactor = 0.85f; | |
0 | 430 break; |
431 } | |
9 | 432 break; |
433 case 0: | |
434 switch (p) | |
435 { | |
436 case +1: // aside, parallel | |
437 timeFactor = 1; | |
438 break; | |
439 case 0: // aside, perpendicular | |
440 timeFactor = 0.8f; | |
441 break; | |
442 case -1: // aside, anti-parallel | |
443 timeFactor = 4; | |
444 break; | |
445 } | |
446 break; | |
447 case -1: | |
448 switch (p) | |
449 { | |
450 case +1: // behind, parallel | |
451 timeFactor = 0.5f; | |
452 break; | |
453 case 0: // behind, perpendicular | |
454 timeFactor = 2; | |
455 break; | |
456 case -1: // behind, anti-parallel | |
457 timeFactor = 2; | |
458 break; | |
459 } | |
460 break; | |
461 } | |
0 | 462 |
9 | 463 // estimated time until intercept of quarry |
464 float et = directTravelTime * timeFactor; | |
465 | |
466 // xxx experiment, if kept, this limit should be an argument | |
467 float etl = (et > maxPredictionTime) ? maxPredictionTime : et; | |
468 | |
469 // estimated position of quarry at intercept | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
470 bzVec2 target = quarry.predictFuturePosition(etl); |
0 | 471 |
12 | 472 return target; //steerForSeek (target); |
9 | 473 } |
474 | |
475 // ------------------------------------------------------------------------ | |
476 // evasion of another vehicle | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
477 bzVec2 steerForEvasion (State menace, float maxPredictionTime) { |
0 | 478 |
9 | 479 // offset from this to menace, that distance, unit vector toward menace |
480 bzVec2 offset = menace.position - m_position; | |
481 float distance = offset.length; | |
0 | 482 |
9 | 483 float roughTime = distance / menace.speed; |
484 float predictionTime = ((roughTime > maxPredictionTime) ? maxPredictionTime : roughTime); | |
485 bzVec2 target = menace.predictFuturePosition (predictionTime); | |
486 | |
487 return steerForFlee (target); | |
488 } | |
0 | 489 |
490 | |
9 | 491 // ------------------------------------------------------------------------ |
492 // tries to maintain a given speed, returns a maxForce-clipped steering | |
493 // force along the forward/backward axis | |
494 bzVec2 steerForTargetSpeed (float targetSpeed) { | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
495 float mf = m_maxForce; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
496 float speedError = targetSpeed - m_speed; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
497 return m_forward * bzClamp(speedError, -mf, +mf); |
9 | 498 } |
0 | 499 |
500 | |
9 | 501 // ----------------------------------------------------------- utilities |
502 bool isAhead (bzVec2 target) {return isAhead (target, 0.707f);}; | |
503 bool isAside (bzVec2 target) {return isAside (target, 0.707f);}; | |
504 bool isBehind (bzVec2 target) {return isBehind (target, -0.707f);}; | |
0 | 505 |
9 | 506 bool isAhead (bzVec2 target, float cosThreshold) |
507 { | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
508 bzVec2 targetDirection = target - m_position; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
509 targetDirection.normalize(); |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
510 return bzDot(m_forward, targetDirection) > cosThreshold; |
9 | 511 } |
0 | 512 |
9 | 513 bool isAside (bzVec2 target, float cosThreshold) |
514 { | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
515 bzVec2 targetDirection = target - m_position; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
516 targetDirection.normalize(); |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
517 float dp = bzDot(m_forward, targetDirection); |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
518 return (dp < cosThreshold) && (dp > -cosThreshold); |
9 | 519 } |
0 | 520 |
9 | 521 bool isBehind (bzVec2 target, float cosThreshold) |
522 { | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
523 bzVec2 targetDirection = target - m_position; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
524 targetDirection.normalize(); |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
525 return bzDot(m_forward, targetDirection) < cosThreshold; |
9 | 526 } |
527 | |
528 private: | |
529 | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
530 Ship m_ship; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
531 |
9 | 532 bzVec2 m_position; |
533 bzVec2 m_velocity; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
534 bzVec2 m_up; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
535 bzVec2 m_side; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
536 bzVec2 m_forward; |
19 | 537 float m_radius; |
538 bzBody m_body; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
539 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
540 float m_speed = 0; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
541 float m_maxForce = 0; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
542 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
543 // Wander behavior |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
544 float m_wanderSide; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
545 float m_wanderUp; |
9 | 546 } |