Fix agents trying to use body instead of weakref

Fixes #43
This commit is contained in:
Francois Belair 2020-05-08 12:40:48 -04:00
parent a2d0258ff1
commit 083450e019
5 changed files with 46 additions and 20 deletions

View File

@ -11,6 +11,7 @@ This document lists new features, improvements, changes, and bug fixes in every
### Fixes
- KinematicBody2DAgents and KinematicBody3DAgents that moved fast enough no longer reverse velocity suddenly during a frame where no acceleration is applied.
- RigidBody2DAgent should no longer crash due to a missing reference.
## Godot Steering AI Framework 2.1.0 ##

View File

@ -103,6 +103,7 @@ func _apply_orientation_steering(angular_acceleration: float, delta: float) -> v
func _set_body(value: KinematicBody2D) -> void:
body = value
_body_ref = weakref(body)
_last_position = value.global_position

View File

@ -20,6 +20,7 @@ var _body_ref: WeakRef
func _init(_body: KinematicBody, _movement_type: int = MovementType.SLIDE) -> void:
body = _body
if not _body.is_inside_tree():
yield(_body, "ready")
@ -27,7 +28,9 @@ func _init(_body: KinematicBody, _movement_type: int = MovementType.SLIDE) -> vo
self.movement_type = _movement_type
# warning-ignore:return_value_discarded
_body.get_tree().connect("physics_frame", self, "_on_SceneTree_physics_frame")
_body.get_tree().connect(
"physics_frame", self, "_on_SceneTree_physics_frame"
)
# Moves the agent's `body` by target `acceleration`.
@ -49,10 +52,14 @@ func _apply_sliding_steering(accel: Vector3, delta: float) -> void:
var _body: KinematicBody = _body_ref.get_ref()
if not _body:
return
var velocity := GSAIUtils.clampedv3(linear_velocity + accel * delta, linear_speed_max)
var velocity := GSAIUtils.clampedv3(
linear_velocity + accel * delta, linear_speed_max
)
if apply_linear_drag:
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
velocity = velocity.linear_interpolate(
Vector3.ZERO, linear_drag_percentage
)
velocity = _body.move_and_slide(velocity)
if calculate_velocities:
linear_velocity = velocity
@ -62,10 +69,14 @@ func _apply_collide_steering(accel: Vector3, delta: float) -> void:
var _body: KinematicBody = _body_ref.get_ref()
if not _body:
return
var velocity := GSAIUtils.clampedv3(linear_velocity + accel * delta, linear_speed_max)
var velocity := GSAIUtils.clampedv3(
linear_velocity + accel * delta, linear_speed_max
)
if apply_linear_drag:
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
velocity = velocity.linear_interpolate(
Vector3.ZERO, linear_drag_percentage
)
# warning-ignore:return_value_discarded
_body.move_and_collide(velocity * delta)
if calculate_velocities:
@ -76,10 +87,14 @@ func _apply_position_steering(accel: Vector3, delta: float) -> void:
var _body: KinematicBody = _body_ref.get_ref()
if not _body:
return
var velocity := GSAIUtils.clampedv3(linear_velocity + accel * delta, linear_speed_max)
var velocity := GSAIUtils.clampedv3(
linear_velocity + accel * delta, linear_speed_max
)
if apply_linear_drag:
velocity = velocity.linear_interpolate(Vector3.ZERO, linear_drag_percentage)
velocity = velocity.linear_interpolate(
Vector3.ZERO, linear_drag_percentage
)
_body.global_position += velocity * delta
if calculate_velocities:
linear_velocity = velocity
@ -89,7 +104,7 @@ func _apply_orientation_steering(angular_acceleration: float, delta: float) -> v
var _body: KinematicBody = _body_ref.get_ref()
if not _body:
return
var velocity = clamp(
angular_velocity + angular_acceleration * delta,
-angular_acceleration_max,
@ -103,6 +118,7 @@ func _apply_orientation_steering(angular_acceleration: float, delta: float) -> v
func _set_body(value: KinematicBody) -> void:
body = value
_body_ref = weakref(value)
_last_position = value.transform.origin
@ -116,7 +132,7 @@ func _on_SceneTree_physics_frame() -> void:
var _body: KinematicBody = _body_ref.get_ref()
if not _body:
return
var current_position := _body.transform.origin
var current_orientation := _body.rotation.y
@ -136,11 +152,15 @@ func _on_SceneTree_physics_frame() -> void:
)
angular_velocity = clamp(
_last_orientation - current_orientation, -angular_speed_max, angular_speed_max
_last_orientation - current_orientation,
-angular_speed_max,
angular_speed_max
)
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_orientation = current_orientation

View File

@ -12,6 +12,7 @@ var _body_ref: WeakRef
func _init(_body: RigidBody2D) -> void:
body = _body
if not _body.is_inside_tree():
yield(_body, "ready")
@ -26,7 +27,7 @@ func _apply_steering(acceleration: GSAITargetAcceleration, _delta: float) -> voi
var _body: RigidBody2D = _body_ref.get_ref()
if not _body:
return
_applied_steering = true
_body.apply_central_impulse(GSAIUtils.to_vector2(acceleration.linear))
_body.apply_torque_impulse(acceleration.angular)
@ -36,6 +37,7 @@ func _apply_steering(acceleration: GSAITargetAcceleration, _delta: float) -> voi
func _set_body(value: RigidBody2D) -> void:
body = value
_body_ref = weakref(value)
_last_position = value.global_position
@ -49,9 +51,9 @@ func _on_SceneTree_frame() -> void:
var _body: RigidBody2D = _body_ref.get_ref()
if not _body:
return
var current_position := body.global_position
var current_orientation := body.rotation
var current_position := _body.global_position
var current_orientation := _body.rotation
position = GSAIUtils.to_vector3(current_position)
orientation = current_orientation
@ -60,5 +62,5 @@ func _on_SceneTree_frame() -> void:
if _applied_steering:
_applied_steering = false
else:
linear_velocity = GSAIUtils.to_vector3(body.linear_velocity)
angular_velocity = body.angular_velocity
linear_velocity = GSAIUtils.to_vector3(_body.linear_velocity)
angular_velocity = _body.angular_velocity

View File

@ -11,6 +11,7 @@ var _last_position: Vector3
var _body_ref: WeakRef
func _init(_body: RigidBody) -> void:
body = _body
if not _body.is_inside_tree():
yield(_body, "ready")
@ -35,6 +36,7 @@ func _apply_steering(acceleration: GSAITargetAcceleration, _delta: float) -> voi
func _set_body(value: RigidBody) -> void:
body = value
_body_ref = weakref(value)
_last_position = value.transform.origin