mirror of
https://github.com/Relintai/godot-steering-ai-framework.git
synced 2024-11-14 04:57:19 +01:00
More cleanups.
This commit is contained in:
parent
7b9561875a
commit
a9df2d5842
@ -31,7 +31,7 @@ func _body_ready() -> void:
|
|||||||
# Moves the agent's `body` by target `acceleration`.
|
# Moves the agent's `body` by target `acceleration`.
|
||||||
# @tags - virtual
|
# @tags - virtual
|
||||||
func _apply_steering(acceleration: GSAITargetAcceleration, delta: float) -> void:
|
func _apply_steering(acceleration: GSAITargetAcceleration, delta: float) -> void:
|
||||||
_applied_steering = true
|
applied_steering = true
|
||||||
|
|
||||||
if movement_type == MovementType.COLLIDE:
|
if movement_type == MovementType.COLLIDE:
|
||||||
_apply_collide_steering(acceleration.linear, delta)
|
_apply_collide_steering(acceleration.linear, delta)
|
||||||
@ -122,10 +122,10 @@ func _set_body(value: KinematicBody2D) -> void:
|
|||||||
_body_ref = weakref(body)
|
_body_ref = weakref(body)
|
||||||
|
|
||||||
_last_position = value.global_position
|
_last_position = value.global_position
|
||||||
_last_orientation = value.rotation
|
last_orientation = value.rotation
|
||||||
|
|
||||||
position = GSAIUtils.to_vector3(_last_position)
|
position = GSAIUtils.to_vector3(_last_position)
|
||||||
orientation = _last_orientation
|
orientation = last_orientation
|
||||||
|
|
||||||
if !had_body:
|
if !had_body:
|
||||||
if !body.is_inside_tree():
|
if !body.is_inside_tree():
|
||||||
@ -146,18 +146,18 @@ func _on_SceneTree_physics_frame() -> void:
|
|||||||
orientation = current_orientation
|
orientation = current_orientation
|
||||||
|
|
||||||
if calculate_velocities:
|
if calculate_velocities:
|
||||||
if _applied_steering:
|
if applied_steering:
|
||||||
_applied_steering = false
|
applied_steering = false
|
||||||
else:
|
else:
|
||||||
linear_velocity = GSAIUtils.clampedv3(GSAIUtils.to_vector3(current_position - _last_position), linear_speed_max)
|
linear_velocity = GSAIUtils.clampedv3(GSAIUtils.to_vector3(current_position - _last_position), linear_speed_max)
|
||||||
|
|
||||||
if apply_linear_drag:
|
if apply_linear_drag:
|
||||||
linear_velocity = linear_velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
|
linear_velocity = linear_velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
|
||||||
|
|
||||||
angular_velocity = clamp(_last_orientation - current_orientation, -angular_speed_max, angular_speed_max)
|
angular_velocity = clamp(last_orientation - current_orientation, -angular_speed_max, angular_speed_max)
|
||||||
|
|
||||||
if apply_angular_drag:
|
if apply_angular_drag:
|
||||||
angular_velocity = lerp(angular_velocity, 0, angular_drag_percentage)
|
angular_velocity = lerp(angular_velocity, 0, angular_drag_percentage)
|
||||||
|
|
||||||
_last_position = current_position
|
_last_position = current_position
|
||||||
_last_orientation = current_orientation
|
last_orientation = current_orientation
|
||||||
|
@ -31,7 +31,7 @@ func _body_ready() -> void:
|
|||||||
# Moves the agent's `body` by target `acceleration`.
|
# Moves the agent's `body` by target `acceleration`.
|
||||||
# @tags - virtual
|
# @tags - virtual
|
||||||
func _apply_steering(acceleration: GSAITargetAcceleration, delta: float) -> void:
|
func _apply_steering(acceleration: GSAITargetAcceleration, delta: float) -> void:
|
||||||
_applied_steering = true
|
applied_steering = true
|
||||||
if movement_type == MovementType.COLLIDE:
|
if movement_type == MovementType.COLLIDE:
|
||||||
_apply_collide_steering(acceleration.linear, delta)
|
_apply_collide_steering(acceleration.linear, delta)
|
||||||
elif movement_type == MovementType.SLIDE:
|
elif movement_type == MovementType.SLIDE:
|
||||||
@ -115,10 +115,10 @@ func _set_body(value: KinematicBody) -> void:
|
|||||||
_body_ref = weakref(value)
|
_body_ref = weakref(value)
|
||||||
|
|
||||||
_last_position = value.transform.origin
|
_last_position = value.transform.origin
|
||||||
_last_orientation = value.rotation.y
|
last_orientation = value.rotation.y
|
||||||
|
|
||||||
position = _last_position
|
position = _last_position
|
||||||
orientation = _last_orientation
|
orientation = last_orientation
|
||||||
|
|
||||||
if !had_body:
|
if !had_body:
|
||||||
if !body.is_inside_tree():
|
if !body.is_inside_tree():
|
||||||
@ -142,18 +142,18 @@ func _on_SceneTree_physics_frame() -> void:
|
|||||||
orientation = current_orientation
|
orientation = current_orientation
|
||||||
|
|
||||||
if calculate_velocities:
|
if calculate_velocities:
|
||||||
if _applied_steering:
|
if applied_steering:
|
||||||
_applied_steering = false
|
applied_steering = false
|
||||||
else:
|
else:
|
||||||
linear_velocity = GSAIUtils.clampedv3(current_position - _last_position, linear_speed_max)
|
linear_velocity = GSAIUtils.clampedv3(current_position - _last_position, linear_speed_max)
|
||||||
|
|
||||||
if apply_linear_drag:
|
if apply_linear_drag:
|
||||||
linear_velocity = linear_velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
|
linear_velocity = linear_velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
|
||||||
|
|
||||||
angular_velocity = clamp(_last_orientation - current_orientation,-angular_speed_max,angular_speed_max)
|
angular_velocity = clamp(last_orientation - current_orientation,-angular_speed_max,angular_speed_max)
|
||||||
|
|
||||||
if apply_angular_drag:
|
if apply_angular_drag:
|
||||||
angular_velocity = lerp(angular_velocity, 0, angular_drag_percentage)
|
angular_velocity = lerp(angular_velocity, 0, angular_drag_percentage)
|
||||||
|
|
||||||
_last_position = current_position
|
_last_position = current_position
|
||||||
_last_orientation = current_orientation
|
last_orientation = current_orientation
|
||||||
|
@ -23,7 +23,7 @@ func _apply_steering(acceleration : GSAITargetAcceleration, _delta : float) -> v
|
|||||||
if not _body:
|
if not _body:
|
||||||
return
|
return
|
||||||
|
|
||||||
_applied_steering = true
|
applied_steering = true
|
||||||
_body.apply_central_impulse(GSAIUtils.to_vector2(acceleration.linear))
|
_body.apply_central_impulse(GSAIUtils.to_vector2(acceleration.linear))
|
||||||
_body.apply_torque_impulse(acceleration.angular)
|
_body.apply_torque_impulse(acceleration.angular)
|
||||||
if calculate_velocities:
|
if calculate_velocities:
|
||||||
@ -41,10 +41,10 @@ func _set_body(value: RigidBody2D) -> void:
|
|||||||
_body_ref = weakref(value)
|
_body_ref = weakref(value)
|
||||||
|
|
||||||
_last_position = value.global_position
|
_last_position = value.global_position
|
||||||
_last_orientation = value.rotation
|
last_orientation = value.rotation
|
||||||
|
|
||||||
position = GSAIUtils.to_vector3(_last_position)
|
position = GSAIUtils.to_vector3(_last_position)
|
||||||
orientation = _last_orientation
|
orientation = last_orientation
|
||||||
|
|
||||||
if !had_body:
|
if !had_body:
|
||||||
if !body.is_inside_tree():
|
if !body.is_inside_tree():
|
||||||
@ -68,8 +68,8 @@ func _on_SceneTree_frame() -> void:
|
|||||||
orientation = current_orientation
|
orientation = current_orientation
|
||||||
|
|
||||||
if calculate_velocities:
|
if calculate_velocities:
|
||||||
if _applied_steering:
|
if applied_steering:
|
||||||
_applied_steering = false
|
applied_steering = false
|
||||||
else:
|
else:
|
||||||
linear_velocity = GSAIUtils.to_vector3(_body.linear_velocity)
|
linear_velocity = GSAIUtils.to_vector3(_body.linear_velocity)
|
||||||
angular_velocity = _body.angular_velocity
|
angular_velocity = _body.angular_velocity
|
||||||
|
@ -22,7 +22,7 @@ func _apply_steering(acceleration: GSAITargetAcceleration, _delta: float) -> voi
|
|||||||
if !_body:
|
if !_body:
|
||||||
return
|
return
|
||||||
|
|
||||||
_applied_steering = true
|
applied_steering = true
|
||||||
_body.apply_central_impulse(acceleration.linear)
|
_body.apply_central_impulse(acceleration.linear)
|
||||||
_body.apply_torque_impulse(Vector3.UP * acceleration.angular)
|
_body.apply_torque_impulse(Vector3.UP * acceleration.angular)
|
||||||
if calculate_velocities:
|
if calculate_velocities:
|
||||||
@ -40,10 +40,10 @@ func _set_body(value: RigidBody) -> void:
|
|||||||
_body_ref = weakref(value)
|
_body_ref = weakref(value)
|
||||||
|
|
||||||
_last_position = value.transform.origin
|
_last_position = value.transform.origin
|
||||||
_last_orientation = value.rotation.y
|
last_orientation = value.rotation.y
|
||||||
|
|
||||||
position = _last_position
|
position = _last_position
|
||||||
orientation = _last_orientation
|
orientation = last_orientation
|
||||||
|
|
||||||
if !had_body:
|
if !had_body:
|
||||||
if !body.is_inside_tree():
|
if !body.is_inside_tree():
|
||||||
@ -66,8 +66,8 @@ func _on_SceneTree_frame() -> void:
|
|||||||
orientation = current_orientation
|
orientation = current_orientation
|
||||||
|
|
||||||
if calculate_velocities:
|
if calculate_velocities:
|
||||||
if _applied_steering:
|
if applied_steering:
|
||||||
_applied_steering = false
|
applied_steering = false
|
||||||
else:
|
else:
|
||||||
linear_velocity = _body.linear_velocity
|
linear_velocity = _body.linear_velocity
|
||||||
angular_velocity = _body.angular_velocity.y
|
angular_velocity = _body.angular_velocity.y
|
||||||
|
@ -29,8 +29,8 @@ var linear_drag_percentage : float = 0.0
|
|||||||
# Does not apply to `RigidBody` and `RigidBody2D` nodes.
|
# Does not apply to `RigidBody` and `RigidBody2D` nodes.
|
||||||
var angular_drag_percentage : float = 0.0
|
var angular_drag_percentage : float = 0.0
|
||||||
|
|
||||||
var _last_orientation : float = 0.0
|
var last_orientation : float = 0.0
|
||||||
var _applied_steering : bool = false
|
var applied_steering : bool = false
|
||||||
|
|
||||||
func apply_steering(_acceleration : GSAITargetAcceleration, _delta : float) -> void:
|
func apply_steering(_acceleration : GSAITargetAcceleration, _delta : float) -> void:
|
||||||
call("_apply_steering", _acceleration, _delta)
|
call("_apply_steering", _acceleration, _delta)
|
||||||
|
@ -4,14 +4,13 @@
|
|||||||
class_name GSAIPriority
|
class_name GSAIPriority
|
||||||
extends GSAISteeringBehavior
|
extends GSAISteeringBehavior
|
||||||
|
|
||||||
var _behaviors : Array = Array()
|
|
||||||
|
|
||||||
# The index of the last behavior the container prioritized.
|
|
||||||
var _last_selected_index : int = 0
|
|
||||||
# If a behavior's acceleration is lower than this threshold, the container
|
# If a behavior's acceleration is lower than this threshold, the container
|
||||||
# considers it has an acceleration of zero.
|
# considers it has an acceleration of zero.
|
||||||
var zero_threshold : float = 0.0
|
var zero_threshold : float = 0.0
|
||||||
|
|
||||||
|
# The index of the last behavior the container prioritized.
|
||||||
|
var _last_selected_index : int = 0
|
||||||
|
var _behaviors : Array = Array()
|
||||||
|
|
||||||
# Appends a steering behavior as a child of this container.
|
# Appends a steering behavior as a child of this container.
|
||||||
func add_behavior(behavior: GSAISteeringBehavior) -> void:
|
func add_behavior(behavior: GSAISteeringBehavior) -> void:
|
||||||
|
@ -58,7 +58,7 @@ func calculate_distance(agent_current_position : Vector3) -> float:
|
|||||||
|
|
||||||
for i in range(_segments.size()):
|
for i in range(_segments.size()):
|
||||||
var segment: GSAISegment = _segments[i]
|
var segment: GSAISegment = _segments[i]
|
||||||
var distance_squared : float = calculate_point_segment_distance_squared(segment.begin, segment.end, agent_current_position)
|
var distance_squared : float = _calculate_point_segment_distance_squared(segment.begin, segment.end, agent_current_position)
|
||||||
|
|
||||||
if distance_squared < smallest_distance_squared:
|
if distance_squared < smallest_distance_squared:
|
||||||
_nearest_point_on_path = _nearest_point_on_segment
|
_nearest_point_on_path = _nearest_point_on_segment
|
||||||
@ -105,7 +105,7 @@ func get_end_point() -> Vector3:
|
|||||||
return _segments.back().end
|
return _segments.back().end
|
||||||
|
|
||||||
|
|
||||||
func calculate_point_segment_distance_squared(start: Vector3, end: Vector3, position: Vector3) -> float:
|
func _calculate_point_segment_distance_squared(start: Vector3, end: Vector3, position: Vector3) -> float:
|
||||||
_nearest_point_on_segment = start
|
_nearest_point_on_segment = start
|
||||||
var start_end : Vector3 = end - start
|
var start_end : Vector3 = end - start
|
||||||
var start_end_length_squared : float = start_end.length_squared()
|
var start_end_length_squared : float = start_end.length_squared()
|
||||||
|
Loading…
Reference in New Issue
Block a user