From 53e8605d35774aac8a9fe6abe04f8e38b53f3866 Mon Sep 17 00:00:00 2001 From: mikatomik Date: Mon, 12 Sep 2022 06:56:30 -0400 Subject: Update FPS controller to play nice with rigid bodies Infinite inertia set to false, the handle_collision func is now individually applying impulses to rigid bodies we collide with so we stop yeeting boxes off the ship just by bumping them or jumping on top of them. --- character/fps_controller/fps_controller.gd | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'character') diff --git a/character/fps_controller/fps_controller.gd b/character/fps_controller/fps_controller.gd index c522e67..9c4e7c3 100644 --- a/character/fps_controller/fps_controller.gd +++ b/character/fps_controller/fps_controller.gd @@ -8,6 +8,7 @@ export var accel : float = 0.2 export var gravity : int = -1 export var term_velocity : int = -35 export var jump_strength : int = 20 +export var inertia : int = 5 var velocity : Vector3 @@ -28,6 +29,7 @@ func _process(_delta): func _physics_process(_delta): handle_movement() handle_jumping() + handle_collision() func get_input(): var dir : Vector3 = Vector3.ZERO @@ -68,3 +70,16 @@ func _input(event): func handle_jumping(): if Input.is_action_just_pressed("jump") and is_on_floor(): velocity.y = jump_strength + +func handle_collision(): + for index in get_slide_count(): + var collision = get_slide_collision(index) + var collider = collision.collider + if (collider.get_class() == "RigidBody"): + var col_force_vec = -collision.normal * inertia # rotate the force along collision normal + # collision pos + # > RigidBody.add_force(force, pos) > The position uses the rotation of the global coordinate system, but is centered at the object's origin. + # > KinematicCollision.position() > The point of collision, in global coordinates. + # So to add force at collision position, substract colliders pos from collision.position + var col_pos = collision.position - collider.transform.origin + collider.add_force(col_force_vec, col_pos) -- cgit v1.2.3