godot-steering-ai-framework/project/src/Behaviors/GSTMatchOrientation.gd

49 lines
1.7 KiB
GDScript3
Raw Normal View History

2020-01-29 17:04:04 +01:00
# Calculates an angular acceleration to match an agent's orientation to that of
# its target. Attempts to make the agent arrive with zero remaining angular
# velocity.
class_name GSTMatchOrientation
extends GSTSteeringBehavior
2020-01-29 17:04:04 +01:00
# The target orientation for the behavior to try and match rotations to.
var target: GSTAgentLocation
2020-01-29 17:04:04 +01:00
# The amount of distance in radians for the behavior to consider itself close
# enough to be matching the target agent's rotation.
var alignment_tolerance: float
2020-01-29 17:04:04 +01:00
# The amount of distance in radians from the goal to start slowing down.
var deceleration_radius: float
# The amount of time to reach the target velocity
var time_to_reach: float = 0.1
func _init(agent: GSTSteeringAgent, target: GSTAgentLocation).(agent) -> void:
self.target = target
func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation: float) -> void:
var rotation := wrapf(desired_orientation - agent.orientation, -PI, PI)
var rotation_size := abs(rotation)
if rotation_size <= alignment_tolerance:
acceleration.set_zero()
else:
var desired_rotation := agent.angular_speed_max
2020-01-29 17:04:04 +01:00
if rotation_size <= deceleration_radius:
desired_rotation *= rotation_size / deceleration_radius
2020-01-29 17:04:04 +01:00
desired_rotation *= rotation / rotation_size
2020-01-29 17:04:04 +01:00
acceleration.angular = (desired_rotation - agent.angular_velocity) / time_to_reach
2020-01-29 17:04:04 +01:00
var limited_acceleration := abs(acceleration.angular)
if limited_acceleration > agent.angular_acceleration_max:
acceleration.angular *= agent.angular_acceleration_max / limited_acceleration
2020-01-29 17:04:04 +01:00
2019-12-20 19:17:27 +01:00
acceleration.linear = Vector3.ZERO
2020-01-29 17:04:04 +01:00
func _calculate_steering(acceleration: GSTTargetAcceleration) -> void:
_match_orientation(acceleration, target.orientation)