comparison ai/steer.d @ 21:cad384ad349e

avoid
author zzzzrrr <mason.green@gmail.com>
date Thu, 26 Mar 2009 07:02:56 -0400
parents 6efd0830715b
children 4fce5596d1f6
comparison
equal deleted inserted replaced
20:6efd0830715b 21:cad384ad349e
180 //avoidance += m_forward * m_maxForce * 0.75f; 180 //avoidance += m_forward * m_maxForce * 0.75f;
181 } 181 }
182 182
183 return avoidance; 183 return avoidance;
184 } 184 }
185
186 bzVec2 avoid(float minTimeToCollision, bzBody obstacles) {
187
188 float avoidMargin = 1.0f;
189 float maxLookahead = minTimeToCollision * m_speed;
190
191 // Make sure we're moving
192 if (m_velocity.length > 0)
193 {
194 for (bzBody o = obstacles; o; o = o.next) {
195
196 if(o is m_body) continue;
197
198 // Find the distance from the line we're moving along to the obstacle.
199 bzVec2 movementNormal = m_velocity;
200 movementNormal.normalize();
201 bzVec2 characterToObstacle = o.position - m_position;
202
203 real distanceSquared = bzDot(characterToObstacle, movementNormal);
204 distanceSquared = characterToObstacle.length -
205 distanceSquared*distanceSquared;
206
207 // Check for collision
208 // Find radius of obstacle
209 float oRad = 0;
210 for (bzShape shape = o.shapeList; shape; shape = shape.next) {
211 if(shape.sweepRadius > oRad) {
212 oRad = shape.sweepRadius;
213 }
214 }
215
216 real radius = oRad + avoidMargin;
217 if (distanceSquared < radius*radius)
218 {
219 // Find how far along our movement vector the closest pass is
220 real distanceToClosest = bzDot(characterToObstacle, movementNormal);
221
222 // Make sure this isn't behind us and is closer than our lookahead.
223 if (distanceToClosest > 0 && distanceToClosest < maxLookahead)
224 {
225 // Find the closest point
226 bzVec2 closestPoint =
227 o.position + movementNormal*distanceToClosest;
228
229 // Find the point of avoidance
230 bzVec2 target =
231 o.position +
232 (closestPoint - o.position).length *
233 (oRad + avoidMargin);
234
235 return target;
236 }
237 }
238 }
239 }
240 return bzVec2.zeroVect;
241
242 }
185 243
186 void findNextIntersectionWithSphere(bzBody obs, 244 void findNextIntersectionWithSphere(bzBody obs,
187 inout PathIntersection intersection, float mdc) { 245 inout PathIntersection intersection, float mdc) {
188 246
189 // This routine is based on the Paul Bourke's derivation in: 247 // This routine is based on the Paul Bourke's derivation in: