Skip to content

Instantly share code, notes, and snippets.

@InfiniteCoder01
Created July 7, 2023 20:43
Show Gist options
  • Save InfiniteCoder01/19cc9c196154d4347b59307e24531f45 to your computer and use it in GitHub Desktop.
Save InfiniteCoder01/19cc9c196154d4347b59307e24531f45 to your computer and use it in GitHub Desktop.
use raylib::prelude::*;
pub fn update_camera(rl: &RaylibHandle, camera: &mut Camera3D, mouse_delta: Vector2) {
let speed = 5.0;
let sensivity = 0.003;
{
// Rotate
camera.target = (camera.target - camera.position).rotate_by(Quaternion::from_axis_angle(
camera.up,
-mouse_delta.x * sensivity,
)) + camera.position;
let forward = camera.target - camera.position;
let right = forward.cross(camera.up);
let max_angle_up = (-mouse_delta.y * sensivity)
.min(
Quaternion::from_vec3_pair(camera.up, forward)
.to_axis_angle()
.1,
)
.max(
Quaternion::from_vec3_pair(-camera.up, forward)
.to_axis_angle()
.1
* -1.0,
);
camera.target =
forward.rotate_by(Quaternion::from_axis_angle(right, max_angle_up)) + camera.position;
}
{
// Move
let mut forward = camera.target - camera.position;
forward.y = 0.0;
forward.normalize();
let right = forward.cross(camera.up);
let mut motion = Vector3::zero();
motion += forward
* (rl.is_key_down(KeyboardKey::KEY_W) as i32
- rl.is_key_down(KeyboardKey::KEY_S) as i32) as f32;
motion += right
* (rl.is_key_down(KeyboardKey::KEY_D) as i32
- rl.is_key_down(KeyboardKey::KEY_A) as i32) as f32;
motion += camera.up
* (rl.is_key_down(KeyboardKey::KEY_SPACE) as i32
- rl.is_key_down(KeyboardKey::KEY_LEFT_SHIFT) as i32) as f32;
motion *= rl.get_frame_time() * speed;
camera.position += motion;
camera.target += motion;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment