Skip to content

Commit a5f9289

Browse files
authored
Fix tickrate-dependent physics in Voxel demo (#1224)
Fix mouse sensitivity being dependent on window size by using `InputEventMouseMotion.screen_relative`.
1 parent 5f4a9e4 commit a5f9289

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

3d/voxel/player/player.gd

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ extends CharacterBody3D
33
const EYE_HEIGHT_STAND = 1.6
44
const EYE_HEIGHT_CROUCH = 1.4
55

6-
const MOVEMENT_SPEED_GROUND = 0.6
7-
const MOVEMENT_SPEED_AIR = 0.11
6+
const MOVEMENT_SPEED_GROUND = 70.0
7+
const MOVEMENT_SPEED_AIR = 13.0
88
const MOVEMENT_SPEED_CROUCH_MODIFIER = 0.5
9-
const MOVEMENT_FRICTION_GROUND = 0.9
10-
const MOVEMENT_FRICTION_AIR = 0.98
9+
const MOVEMENT_FRICTION_GROUND = 12.5
10+
const MOVEMENT_FRICTION_AIR = 2.25
11+
const MOVEMENT_JUMP_VELOCITY = 7.25
1112

1213
var _mouse_motion := Vector2()
1314
var _selected_block := 6
@@ -81,7 +82,7 @@ func _physics_process(delta: float) -> void:
8182
camera_attributes.dof_blur_far_transition = Settings.fog_distance * 0.125
8283
# Crouching.
8384
var crouching := Input.is_action_pressed(&"crouch")
84-
head.transform.origin.y = lerpf(head.transform.origin.y, EYE_HEIGHT_CROUCH if crouching else EYE_HEIGHT_STAND, 16 * delta)
85+
head.transform.origin.y = lerpf(head.transform.origin.y, EYE_HEIGHT_CROUCH if crouching else EYE_HEIGHT_STAND, 1.0 - exp(-delta * 16.0))
8586

8687
# Keyboard movement.
8788
var movement_vec2 := Input.get_vector("move_left", "move_right", "move_forward", "move_back")
@@ -96,17 +97,18 @@ func _physics_process(delta: float) -> void:
9697
movement *= MOVEMENT_SPEED_CROUCH_MODIFIER
9798

9899
# Gravity.
99-
velocity.y -= gravity * delta
100+
if not is_on_floor():
101+
velocity.y -= gravity * delta
100102

101-
velocity += Vector3(movement.x, 0, movement.z)
103+
velocity += Vector3(movement.x, 0, movement.z) * delta
102104
# Apply horizontal friction.
103-
velocity.x *= MOVEMENT_FRICTION_GROUND if is_on_floor() else MOVEMENT_FRICTION_AIR
104-
velocity.z *= MOVEMENT_FRICTION_GROUND if is_on_floor() else MOVEMENT_FRICTION_AIR
105+
var friction_delta := exp(-(MOVEMENT_FRICTION_GROUND if is_on_floor() else MOVEMENT_FRICTION_AIR) * delta)
106+
velocity = Vector3(velocity.x * friction_delta, velocity.y, velocity.z * friction_delta)
105107
move_and_slide()
106108

107109
# Jumping, applied next frame.
108110
if is_on_floor() and Input.is_action_pressed(&"jump"):
109-
velocity.y = 7.5
111+
velocity.y = MOVEMENT_JUMP_VELOCITY
110112

111113

112114
func _input(event: InputEvent) -> void:

0 commit comments

Comments
 (0)