-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcamera.rs
More file actions
131 lines (109 loc) · 3.92 KB
/
Copy pathcamera.rs
File metadata and controls
131 lines (109 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use crate::vec2::Vec2;
use crate::world::World;
use std::f64::consts::PI;
pub struct Camera {
pub position: Vec2,
pub direction: Vec2,
pub plane: Vec2,
pub move_speed: f64,
pub rot_speed: f64,
pub pitch: f64,
pub z_position: f64,
pub z_velocity: f64,
pub bob_phase: f64,
}
impl Camera {
pub fn new(position: Vec2, direction: Vec2) -> Self {
let direction = direction.normalize();
let plane = Vec2::new(0.0, 0.66);
Camera {
position,
direction,
plane,
move_speed: 0.15,
rot_speed: 0.08,
pitch: 0.0,
z_position: 0.0,
z_velocity: 0.0,
bob_phase: 0.0,
}
}
pub fn move_forward(&mut self, world: &World, delta: f64) {
let new_pos = self.position + self.direction * (self.move_speed * delta);
if !world.is_wall(new_pos.x as i32, self.position.y as i32) {
self.position.x = new_pos.x;
}
if !world.is_wall(self.position.x as i32, new_pos.y as i32) {
self.position.y = new_pos.y;
}
self.bob_phase += 0.2;
if self.pitch > 0.1 {
self.z_velocity += 0.05;
}
}
pub fn move_backward(&mut self, world: &World, delta: f64) {
let new_pos = self.position - self.direction * (self.move_speed * delta);
if !world.is_wall(new_pos.x as i32, self.position.y as i32) {
self.position.x = new_pos.x;
}
if !world.is_wall(self.position.x as i32, new_pos.y as i32) {
self.position.y = new_pos.y;
}
self.bob_phase += 0.2;
}
pub fn strafe_left(&mut self, world: &World, delta: f64) {
let right = Vec2::new(self.direction.y, -self.direction.x);
let new_pos = self.position - right * (self.move_speed * delta);
if !world.is_wall(new_pos.x as i32, self.position.y as i32) {
self.position.x = new_pos.x;
}
if !world.is_wall(self.position.x as i32, new_pos.y as i32) {
self.position.y = new_pos.y;
}
self.bob_phase += 0.2;
}
pub fn strafe_right(&mut self, world: &World, delta: f64) {
let right = Vec2::new(self.direction.y, -self.direction.x);
let new_pos = self.position + right * (self.move_speed * delta);
if !world.is_wall(new_pos.x as i32, self.position.y as i32) {
self.position.x = new_pos.x;
}
if !world.is_wall(self.position.x as i32, new_pos.y as i32) {
self.position.y = new_pos.y;
}
self.bob_phase += 0.2;
}
pub fn rotate(&mut self, angle: f64) {
let rot_angle = angle * self.rot_speed;
self.direction = self.direction.rotate(rot_angle);
self.plane = self.plane.rotate(rot_angle);
}
pub fn rotate_absolute(&mut self, angle: f64) {
self.direction = self.direction.rotate(angle);
self.plane = self.plane.rotate(angle);
}
pub fn look_up(&mut self, delta: f64) {
self.pitch = (self.pitch + delta * 0.05).clamp(-PI / 3.0, PI / 3.0);
}
pub fn look_down(&mut self, delta: f64) {
self.pitch = (self.pitch - delta * 0.05).clamp(-PI / 3.0, PI / 3.0);
}
pub fn update(&mut self, _delta_time: f64) {
self.z_velocity -= 0.02;
self.z_position += self.z_velocity;
if self.z_position < 0.0 {
self.z_position = 0.0;
self.z_velocity = 0.0;
}
self.z_velocity *= 0.95;
}
pub fn get_view_bob(&self) -> f64 {
(self.bob_phase.sin() * 0.08).clamp(-0.12, 0.12)
}
pub fn get_horizon_offset(&self) -> i32 {
let base_offset = (self.pitch * 150.0) as i32;
let bob_offset = (self.get_view_bob() * 20.0) as i32;
let jump_offset = (self.z_position * 50.0) as i32;
base_offset + bob_offset + jump_offset
}
}