diff options
author | mikatomik <mikec_2007@hotmail.com> | 2022-09-12 06:56:30 -0400 |
---|---|---|
committer | mikatomik <mikec_2007@hotmail.com> | 2022-09-15 01:42:11 -0400 |
commit | 53e8605d35774aac8a9fe6abe04f8e38b53f3866 (patch) | |
tree | 8d63156125a307a9f145eea7973ee1f2c13f7b57 /character | |
parent | c2b38eae03b49b0ad75fbfa152e1aa563758edfd (diff) | |
download | game_jam49-53e8605d35774aac8a9fe6abe04f8e38b53f3866.tar.gz game_jam49-53e8605d35774aac8a9fe6abe04f8e38b53f3866.zip |
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.
Diffstat (limited to 'character')
-rw-r--r-- | character/fps_controller/fps_controller.gd | 15 |
1 files changed, 15 insertions, 0 deletions
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) |