Skip to content

Commit 91778a8

Browse files
committed
Fix tickrate-dependent physics in Voxel demo
- Fix mouse sensitivity being dependent on window size by using `InputEventMouseMotion.screen_relative`.
1 parent 9acb552 commit 91778a8

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

3d/voxel/player/player.gd

Lines changed: 13 additions & 11 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,23 +97,24 @@ 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:
113115
if event is InputEventMouseMotion:
114116
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
115-
_mouse_motion += event.relative
117+
_mouse_motion += event.screen_relative
116118

117119

118120
func chunk_pos() -> Vector3i:

3d/voxel/project.godot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ pick_block={
148148
[physics]
149149

150150
common/physics_ticks_per_second=120
151-
3d/default_gravity=20.0
152151
3d/physics_engine="Jolt Physics"
152+
3d/default_gravity=20.0
153153
common/physics_interpolation=true
154154

155155
[rendering]

0 commit comments

Comments
 (0)