delta time usage
This commit is contained in:
parent
6db338d79b
commit
1b26754f8f
16 changed files with 667 additions and 348 deletions
|
|
@ -6,12 +6,15 @@ fn main() -> int do
|
|||
enable_msaa_4x()
|
||||
|
||||
# Initialize graphics
|
||||
init_window(1280, 720, "Sui FPS Game - WASD to move, Mouse to look, SPACE to jump, SHIFT to sprint")
|
||||
defer close_window()
|
||||
set_target_fps(60)
|
||||
init_window(1280, 720, "Sui FPS Game - WASD to move, Mouse to look, SPACE to jump, SHIFT to sprint, F1 cursor, F2 hide FPS, F3 show FPS")
|
||||
defer close_window()
|
||||
# set_target_fps(60)
|
||||
|
||||
# Hide cursor and capture mouse for FPS controls
|
||||
disable_cursor()
|
||||
# Show FPS meter
|
||||
show_fps_meter(Vec2 { x: 10.0, y: 10.0 })
|
||||
|
||||
# Hide cursor and capture mouse for FPS controls
|
||||
disable_cursor()
|
||||
|
||||
# Initialize physics
|
||||
ode_init()
|
||||
|
|
@ -93,6 +96,9 @@ fn main() -> int do
|
|||
let mut yaw = 0.0
|
||||
let mut pitch = 0.0
|
||||
let mut mouse_sensitivity = 0.003
|
||||
|
||||
# FPS meter toggle
|
||||
let mut fps_meter_visible = 1
|
||||
|
||||
# Cube position variables
|
||||
let mut cube1_x = 0.0
|
||||
|
|
@ -105,115 +111,128 @@ fn main() -> int do
|
|||
let mut cube3_y = 0.0
|
||||
let mut cube3_z = 0.0
|
||||
|
||||
# Player position (initial values)
|
||||
let mut player_x = 0.0
|
||||
let mut player_y = 2.0
|
||||
let mut player_z = 0.0
|
||||
# Player position (initial values)
|
||||
let mut player_x = 0.0
|
||||
let mut player_y = 2.0
|
||||
let mut player_z = 0.0
|
||||
|
||||
# Main game loop
|
||||
while window_should_close() == false do
|
||||
# Input handling
|
||||
let delta_time = 0.01666 # ~60 FPS
|
||||
# Main game loop
|
||||
while window_should_close() == false do
|
||||
# Get actual frame delta time
|
||||
let delta_time = get_frame_time()
|
||||
|
||||
# WASD movement - set velocities on physics body
|
||||
let move_speed = 5.0 # Velocity applied to body
|
||||
let mut target_vx = 0.0
|
||||
let mut target_vz = 0.0
|
||||
# Mouse look
|
||||
let mouse_delta_x = get_mouse_delta_x()
|
||||
let mouse_delta_y = get_mouse_delta_y()
|
||||
|
||||
if is_key_down(KEY_W) != 0 do
|
||||
target_vx = target_vx + forward_x * move_speed
|
||||
target_vz = target_vz + forward_z * move_speed
|
||||
end
|
||||
yaw = yaw + (mouse_delta_x * mouse_sensitivity)
|
||||
pitch = pitch - (mouse_delta_y * mouse_sensitivity) # Negate Y for natural FPS controls
|
||||
|
||||
if is_key_down(KEY_S) != 0 do
|
||||
target_vx = target_vx - forward_x * move_speed
|
||||
target_vz = target_vz - forward_z * move_speed
|
||||
end
|
||||
# Clamp pitch to prevent flipping
|
||||
if pitch > 1.5 do
|
||||
pitch = 1.5
|
||||
end
|
||||
if pitch < -1.5 do
|
||||
pitch = -1.5
|
||||
end
|
||||
|
||||
if is_key_down(KEY_D) != 0 do
|
||||
target_vx = target_vx + right_x * move_speed
|
||||
target_vz = target_vz + right_z * move_speed
|
||||
end
|
||||
# Calculate camera direction from yaw and pitch
|
||||
let cos_yaw = cos(yaw)
|
||||
let sin_yaw = sin(yaw)
|
||||
let cos_pitch = cos(pitch)
|
||||
let sin_pitch = sin(pitch)
|
||||
|
||||
if is_key_down(KEY_A) != 0 do
|
||||
target_vx = target_vx - right_x * move_speed
|
||||
target_vz = target_vz - right_z * move_speed
|
||||
end
|
||||
# Update forward and right vectors FIRST (before movement)
|
||||
forward_x = sin_yaw
|
||||
forward_y = sin_pitch
|
||||
forward_z = -cos_yaw
|
||||
|
||||
# Get current velocity for Y (preserve gravity)
|
||||
let mut current_vx = 0.0
|
||||
let mut current_vy = 0.0
|
||||
let mut current_vz = 0.0
|
||||
ode_body_get_linear_vel(player_body, ¤t_vx, ¤t_vy, ¤t_vz)
|
||||
right_x = cos_yaw
|
||||
right_y = 0.0
|
||||
right_z = sin_yaw
|
||||
|
||||
# Jump - set upward velocity
|
||||
if is_jump_pressed() != 0 do
|
||||
# Get current position to check if on ground
|
||||
let mut dummy_x = 0.0
|
||||
let mut current_y = 0.0
|
||||
let mut dummy_z = 0.0
|
||||
ode_body_get_position(player_body, &dummy_x, ¤t_y, &dummy_z)
|
||||
if current_y <= 1.3 do # On ground (box bottom is at y - 0.8 from center)
|
||||
current_vy = 8.0 # Jump velocity
|
||||
end
|
||||
end
|
||||
# WASD movement - set velocities on physics body
|
||||
let move_speed = 5.0 # Units per second
|
||||
let mut target_vx = 0.0
|
||||
let mut target_vz = 0.0
|
||||
|
||||
# Set new velocity (keep Y velocity for gravity/jumping)
|
||||
ode_body_set_linear_vel(player_body, target_vx, current_vy, target_vz)
|
||||
if is_key_down(KEY_W) != 0 do
|
||||
target_vx = target_vx + forward_x * move_speed
|
||||
target_vz = target_vz + forward_z * move_speed
|
||||
end
|
||||
|
||||
# Get player position from physics body
|
||||
ode_body_get_position(player_body, &player_x, &player_y, &player_z)
|
||||
if is_key_down(KEY_S) != 0 do
|
||||
target_vx = target_vx - forward_x * move_speed
|
||||
target_vz = target_vz - forward_z * move_speed
|
||||
end
|
||||
|
||||
# Adjust mouse sensitivity
|
||||
if is_key_pressed(KEY_EQUAL) != 0 or is_key_pressed(KEY_KP_ADD) != 0 do # + key
|
||||
mouse_sensitivity = mouse_sensitivity + 0.001
|
||||
end
|
||||
if is_key_pressed(KEY_MINUS) != 0 or is_key_pressed(KEY_KP_SUBTRACT) != 0 do # - key
|
||||
mouse_sensitivity = mouse_sensitivity - 0.001
|
||||
if mouse_sensitivity < 0.001 do
|
||||
mouse_sensitivity = 0.001 # Minimum sensitivity
|
||||
end
|
||||
end
|
||||
if is_key_down(KEY_D) != 0 do
|
||||
target_vx = target_vx + right_x * move_speed
|
||||
target_vz = target_vz + right_z * move_speed
|
||||
end
|
||||
|
||||
# Show cursor with F1 (useful for debugging)
|
||||
if is_key_pressed(KEY_F1) != 0 do
|
||||
enable_cursor()
|
||||
end
|
||||
if is_key_down(KEY_A) != 0 do
|
||||
target_vx = target_vx - right_x * move_speed
|
||||
target_vz = target_vz - right_z * move_speed
|
||||
end
|
||||
|
||||
# Mouse look
|
||||
let mouse_delta_x = get_mouse_delta_x()
|
||||
let mouse_delta_y = get_mouse_delta_y()
|
||||
# Get current velocity for Y (preserve gravity)
|
||||
let mut current_vx = 0.0
|
||||
let mut current_vy = 0.0
|
||||
let mut current_vz = 0.0
|
||||
ode_body_get_linear_vel(player_body, ¤t_vx, ¤t_vy, ¤t_vz)
|
||||
|
||||
yaw = yaw + (mouse_delta_x * mouse_sensitivity)
|
||||
pitch = pitch - (mouse_delta_y * mouse_sensitivity) # Negate Y for natural FPS controls
|
||||
# Jump - set upward velocity
|
||||
if is_jump_pressed() != 0 do
|
||||
# Get current position to check if on ground
|
||||
let mut dummy_x = 0.0
|
||||
let mut current_y = 0.0
|
||||
let mut dummy_z = 0.0
|
||||
ode_body_get_position(player_body, &dummy_x, ¤t_y, &dummy_z)
|
||||
if current_y <= 1.3 do # On ground (box bottom is at y - 0.8 from center)
|
||||
current_vy = 8.0 # Jump velocity
|
||||
end
|
||||
end
|
||||
|
||||
# Clamp pitch to prevent flipping
|
||||
if pitch > 1.5 do
|
||||
pitch = 1.5
|
||||
end
|
||||
if pitch < -1.5 do
|
||||
pitch = -1.5
|
||||
end
|
||||
# Set new velocity (keep Y velocity for gravity/jumping)
|
||||
ode_body_set_linear_vel(player_body, target_vx, current_vy, target_vz)
|
||||
|
||||
# Update physics (just for cubes)
|
||||
ode_space_collide(world, space, contactgroup)
|
||||
ode_world_step(world, delta_time)
|
||||
ode_joint_group_empty(contactgroup)
|
||||
# Update physics (just for cubes)
|
||||
ode_space_collide(world, space, contactgroup)
|
||||
# Only step physics if delta_time is valid (guard against first frame which is 0)
|
||||
if delta_time > 0.0 do
|
||||
ode_world_step(world, delta_time)
|
||||
end
|
||||
ode_joint_group_empty(contactgroup)
|
||||
|
||||
# Calculate camera direction from yaw and pitch
|
||||
let cos_yaw = cos(yaw)
|
||||
let sin_yaw = sin(yaw)
|
||||
let cos_pitch = cos(pitch)
|
||||
let sin_pitch = sin(pitch)
|
||||
# Get player position from physics body
|
||||
ode_body_get_position(player_body, &player_x, &player_y, &player_z)
|
||||
|
||||
# Update forward and right vectors
|
||||
forward_x = sin_yaw
|
||||
forward_y = sin_pitch
|
||||
forward_z = -cos_yaw
|
||||
# Adjust mouse sensitivity
|
||||
if is_key_pressed(KEY_EQUAL) != 0 or is_key_pressed(KEY_KP_ADD) != 0 do # + key
|
||||
mouse_sensitivity = mouse_sensitivity + 0.001
|
||||
end
|
||||
if is_key_pressed(KEY_MINUS) != 0 or is_key_pressed(KEY_KP_SUBTRACT) != 0 do # - key
|
||||
mouse_sensitivity = mouse_sensitivity - 0.001
|
||||
if mouse_sensitivity < 0.001 do
|
||||
mouse_sensitivity = 0.001 # Minimum sensitivity
|
||||
end
|
||||
end
|
||||
|
||||
right_x = cos_yaw
|
||||
right_y = 0.0
|
||||
right_z = sin_yaw
|
||||
# Show cursor with F1 (useful for debugging)
|
||||
if is_key_pressed(KEY_F1) != 0 do
|
||||
enable_cursor()
|
||||
end
|
||||
|
||||
# Toggle FPS meter with F2 (hide) and F3 (show)
|
||||
if is_key_pressed(KEY_F2) != 0 do
|
||||
hide_fps_meter()
|
||||
fps_meter_visible = 0
|
||||
end
|
||||
if is_key_pressed(KEY_F3) != 0 do
|
||||
show_fps_meter(Vec2 { x: 10.0, y: 10.0 })
|
||||
fps_meter_visible = 1
|
||||
end
|
||||
|
||||
# Direction vectors are updated above
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue