MCPcopy Create free account
hub / github.com/TanTanDev/first_voxel_engine / update_camera

Method update_camera

src/camera_controller.rs:91–131  ·  view source on GitHub ↗
(&mut self, camera: &mut Camera, dt: Duration)

Source from the content-addressed store, hash-verified

89 }
90
91 pub fn update_camera(&mut self, camera: &mut Camera, dt: Duration) {
92 let dt = dt.as_secs_f32();
93
94 // Move forward/backward and left/right
95 let (yaw_sin, yaw_cos) = camera.yaw.0.sin_cos();
96 let forward = Vector3::new(yaw_cos, 0.0, yaw_sin).normalize();
97 let right = Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize();
98 camera.position += forward * (self.amount_forward - self.amount_backward) * self.speed * dt;
99 camera.position += right * (self.amount_right - self.amount_left) * self.speed * dt;
100
101 // Move in/out (aka. "zoom")
102 // Note: this isn't an actual zoom. The camera's position
103 // changes when zooming. I've added this to make it easier
104 // to get closer to an object you want to focus on.
105 let (pitch_sin, pitch_cos) = camera.pitch.0.sin_cos();
106 let scrollward =
107 Vector3::new(pitch_cos * yaw_cos, pitch_sin, pitch_cos * yaw_sin).normalize();
108 camera.position += scrollward * self.scroll * self.speed * self.sensitivity * dt;
109 self.scroll = 0.0;
110
111 // Move up/down. Since we don't use roll, we can just
112 // modify the y coordinate directly.
113 camera.position.y += (self.amount_up - self.amount_down) * self.speed * dt;
114
115 // Rotate
116 camera.yaw += Rad(self.rotate_horizontal) * self.sensitivity * dt;
117 camera.pitch += Rad(-self.rotate_vertical) * self.sensitivity * dt;
118
119 // If process_mouse isn't called every frame, these values
120 // will not get set to zero, and the camera will rotate
121 // when moving in a non cardinal direction.
122 self.rotate_horizontal = 0.0;
123 self.rotate_vertical = 0.0;
124
125 // Keep the camera's angle from going too high/low.
126 if camera.pitch < -Rad(FRAC_PI_2) {
127 camera.pitch = -Rad(FRAC_PI_2);
128 } else if camera.pitch > Rad(FRAC_PI_2) {
129 camera.pitch = Rad(FRAC_PI_2);
130 }
131 }
132}

Callers 1

updateMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected