Files
godot-demo-projects/2d/physics_tests/utils/characterbody_controller.gd
Hugo Locurcio a531235db3 Use Jolt Physics in all 3D demos that use physics, improve physics tests demos (#1195)
- Add options for physics ticks per second, time scale, max physics steps per frame
  and physics interpolation to the 2D and 3D physics tests demos.
  - Physics ticks per second are always multiplied by time scale so that
    time scale does not affect the physics simulation quality.
- Enable 4× MSAA for better debug shape display. Remove meshes/lights as
  the debug collision fill make these unnecessary.
  - Switch to the Mobile rendering method in the 2D physics tests demo
    to allow for 2D MSAA, as it's not implemented in Compatibility yet.
- Improve collision shapes color in the 2D and 3D physics tests demos
  for better visibility. Each PhysicsBody type now has its own collision
  shape color.
2025-04-21 21:59:31 +02:00

61 lines
1.6 KiB
GDScript

extends CharacterBody2D
var _initial_velocity := Vector2.ZERO
var _constant_velocity := Vector2.ZERO
var _motion_speed := 400.0
var _gravity_force := 50.0
var _jump_force := 1000.0
var _velocity := Vector2.ZERO
var _snap := 0.0
var _floor_max_angle := 45.0
var _stop_on_slope := false
var _move_on_floor_only := false
var _constant_speed := false
var _jumping := false
var _keep_velocity := false
func _physics_process(delta: float) -> void:
if _initial_velocity != Vector2.ZERO:
_velocity = _initial_velocity
_initial_velocity = Vector2.ZERO
_keep_velocity = true
elif _constant_velocity != Vector2.ZERO:
_velocity = _constant_velocity
elif not _keep_velocity:
_velocity.x = 0.0
# Handle horizontal controls.
if Input.is_action_pressed(&"character_left"):
if position.x > 0.0:
_velocity.x = -_motion_speed
_keep_velocity = false
_constant_velocity = Vector2.ZERO
elif Input.is_action_pressed(&"character_right"):
if position.x < 1024.0:
_velocity.x = _motion_speed
_keep_velocity = false
_constant_velocity = Vector2.ZERO
# Handle jump controls and gravity.
if is_on_floor():
if not _jumping and Input.is_action_just_pressed(&"character_jump"):
# Start jumping.
_jumping = true
_velocity.y = -_jump_force
else:
_velocity.y += _gravity_force * delta * 60.0
floor_snap_length = _snap
floor_stop_on_slope = _stop_on_slope
floor_block_on_wall = _move_on_floor_only
floor_constant_speed = _constant_speed
floor_max_angle = deg_to_rad(_floor_max_angle)
velocity = _velocity
move_and_slide()
_velocity = velocity
# Get next jump ready.
if _jumping:
_jumping = false