// ---------------------------------------------------------------------------- // evasion of another vehicle public Vector3 steerForEvasion ( Vehicle menace, float maxPredictionTime) { // offset from this to menace, that distance, unit vector toward menace Vector3 offset = menace.Position - Position; float distance = offset.magnitude; float roughTime = distance / menace.Speed; float predictionTime = ((roughTime > maxPredictionTime) ? maxPredictionTime : roughTime); Vector3 target = menace.predictFuturePosition (predictionTime); return steerForFlee (target); }
public Vector3 steerForPursuit (Vehicle quarry, float maxPredictionTime) { // offset from this to quarry, that distance, unit vector toward quarry Vector3 offset = quarry.Position - Position; float distance = offset.magnitude; Vector3 unitOffset = offset / distance; // how parallel are the paths of "this" and the quarry // (1 means parallel, 0 is pependicular, -1 is anti-parallel) float parallelness = Vector3.Dot(Forward, quarry.Forward); // how "forward" is the direction to the quarry // (1 means dead ahead, 0 is directly to the side, -1 is straight back) float forwardness = Vector3.Dot(Forward, unitOffset); float directTravelTime = distance / Speed; int f = intervalComparison (forwardness, -0.707f, 0.707f); int p = intervalComparison (parallelness, -0.707f, 0.707f); float timeFactor = 0; // to be filled in below Color color = Color.black; // to be filled in below (xxx just for debugging) // Break the pursuit into nine cases, the cross product of the // quarry being [ahead, aside, or behind] us and heading // [parallel, perpendicular, or anti-parallel] to us. switch (f) { case +1: switch (p) { case +1: // ahead, parallel timeFactor = 4; color = Color.black; break; case 0: // ahead, perpendicular timeFactor = 1.8f; color = Color.gray; break; case -1: // ahead, anti-parallel timeFactor = 0.85f; color = Color.white; break; } break; case 0: switch (p) { case +1: // aside, parallel timeFactor = 1; color = Color.red; break; case 0: // aside, perpendicular timeFactor = 0.8f; color = Color.yellow; break; case -1: // aside, anti-parallel timeFactor = 4; color = Color.green; break; } break; case -1: switch (p) { case +1: // behind, parallel timeFactor = 0.5f; color = Color.cyan; break; case 0: // behind, perpendicular timeFactor = 2; color = Color.blue; break; case -1: // behind, anti-parallel timeFactor = 2; color = Color.magenta; break; } break; } // estimated time until intercept of quarry float et = directTravelTime * timeFactor; // xxx experiment, if kept, this limit should be an argument float etl = (et > maxPredictionTime) ? maxPredictionTime : et; // estimated position of quarry at intercept Vector3 target = quarry.predictFuturePosition (etl); // annotation #if DEBUG annotationLine (Position, target, gaudyPursuitAnnotation ? color : Color.gray); #endif return steerForSeek (target); }