From 083450e019bba9a90fa427fdcdc2d6f0cfbb5f25 Mon Sep 17 00:00:00 2001 From: Francois Belair Date: Fri, 8 May 2020 12:40:48 -0400 Subject: [PATCH] Fix agents trying to use body instead of weakref Fixes #43 --- CHANGELOG.md | 1 + .../src/Agents/GSAIKinematicBody2DAgent.gd | 1 + .../src/Agents/GSAIKinematicBody3DAgent.gd | 48 +++++++++++++------ project/src/Agents/GSAIRigidBody2DAgent.gd | 14 +++--- project/src/Agents/GSAIRigidBody3DAgent.gd | 2 + 5 files changed, 46 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af5961b..0dadfba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ## diff --git a/project/src/Agents/GSAIKinematicBody2DAgent.gd b/project/src/Agents/GSAIKinematicBody2DAgent.gd index a956bdf..a41c500 100644 --- a/project/src/Agents/GSAIKinematicBody2DAgent.gd +++ b/project/src/Agents/GSAIKinematicBody2DAgent.gd @@ -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 diff --git a/project/src/Agents/GSAIKinematicBody3DAgent.gd b/project/src/Agents/GSAIKinematicBody3DAgent.gd index 8fd9ef9..11ab734 100644 --- a/project/src/Agents/GSAIKinematicBody3DAgent.gd +++ b/project/src/Agents/GSAIKinematicBody3DAgent.gd @@ -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 diff --git a/project/src/Agents/GSAIRigidBody2DAgent.gd b/project/src/Agents/GSAIRigidBody2DAgent.gd index 5b4708f..455a99b 100644 --- a/project/src/Agents/GSAIRigidBody2DAgent.gd +++ b/project/src/Agents/GSAIRigidBody2DAgent.gd @@ -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 diff --git a/project/src/Agents/GSAIRigidBody3DAgent.gd b/project/src/Agents/GSAIRigidBody3DAgent.gd index 9cfe846..fcf7369 100644 --- a/project/src/Agents/GSAIRigidBody3DAgent.gd +++ b/project/src/Agents/GSAIRigidBody3DAgent.gd @@ -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