I’m trying to implement a simple seek & avoid system for a 3d game wherein planes fly around. Using https://gamedevelopment.tutsplus.com/series/understanding-steering-behaviors–gamedev-12732 as a reference.
During testing I found that if a plane was flying directly at a target it should avoid, the avoidance force (a Vector3 that points in direction from the plane’s target to the plane) is in the complete opposite direction of its velocity (also a Vector3), thus it doesn’t steer at all. I could detect this and rotate the avoidance force a bit, but that seems like a poor solution.
func _avoid() -> Vector3:
var avoidance_force := Vector3.ZERO
var ray_start = host.get_position()
var ray_length = host.get_view_distance()
var collision = host.get_world().direct_space_state.intersect_ray(
ray_start + -host.global_transform.basis.z * ray_length,
(host), 31, true, true)
if collision.collider.name == 'Avoidance':
var collision_dist = (host.get_position() + host.get_velocity() - collision.position).length()
avoidance_force = (collision.position - collision.collider.global_transform.origin).normalized()
# if direction_change == -1, then the plane is flying directly at its target
# var direction_change = avoidance_force.dot(host.get_velocity().normalized())