make collision detection not suck
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Sijmen 2021-06-07 04:14:27 +02:00
parent c8e02c6f86
commit 191f9b1f28
Signed by: vijfhoek
GPG key ID: 82D05C89B28B0DAE

View file

@ -360,14 +360,23 @@ impl WorldState {
VirtualKeyCode::D => self.right_pressed = pressed, VirtualKeyCode::D => self.right_pressed = pressed,
VirtualKeyCode::F2 if pressed => self.creative = !self.creative, VirtualKeyCode::F2 if pressed => self.creative = !self.creative,
VirtualKeyCode::Space => { VirtualKeyCode::Space => {
// TODO aaaaaaaaaaaaaaaaaa
self.up_speed = if pressed { self.up_speed = if pressed {
if self.creative { if self.creative {
1.0 1.0
} else { } else {
0.6 if self.up_speed.abs() < 0.05 {
0.6
} else {
self.up_speed
}
} }
} else { } else {
0.0 if self.creative {
0.0
} else {
self.up_speed
}
} }
} }
VirtualKeyCode::LShift if self.creative => { VirtualKeyCode::LShift if self.creative => {
@ -378,10 +387,10 @@ impl WorldState {
} }
} }
fn check_collision(&self, position: Point3<f32>) -> bool { fn check_collision(&self, position: Point3<f32>) -> Option<Aabb> {
let aabb = Aabb { let aabb = Aabb {
min: position + Vector3::new(-0.3, -1.8, -0.3), min: position + Vector3::new(-0.3, -1.62, -0.3),
max: position + Vector3::new(0.3, 0.0, 0.3), max: position + Vector3::new(0.3, 0.18, 0.3),
}; };
for corner in &aabb.get_corners() { for corner in &aabb.get_corners() {
@ -392,11 +401,11 @@ impl WorldState {
); );
if block.is_some() { if block.is_some() {
return true; return Some(aabb);
} }
} }
false None
} }
fn update_position(&mut self, dt: Duration) { fn update_position(&mut self, dt: Duration) {
@ -423,33 +432,33 @@ impl WorldState {
// y component (jumping) // y component (jumping)
new_position.y += self.up_speed * speed * dt_seconds; new_position.y += self.up_speed * speed * dt_seconds;
while self.check_collision(new_position) { if let Some(aabb) = self.check_collision(new_position) {
self.up_speed = 0.0; if self.up_speed < 0.0 {
new_position.y = aabb.min.y.ceil() + 1.62;
if velocity.y <= -0.0 { } else if self.up_speed > 0.0 {
new_position.y = (new_position.y - 1.8).floor() + 2.8; new_position.y = aabb.max.y.floor() - 0.1801;
} else {
new_position.y = (new_position.y - 0.0001).ceil() - 1.0001;
} }
self.up_speed = 0.0;
} }
// x component // x component
new_position.x += velocity.x; new_position.x += velocity.x;
while self.check_collision(new_position) { if let Some(aabb) = self.check_collision(new_position) {
if velocity.x <= -0.0 { if velocity.x < 0.0 {
new_position.x = (new_position.x - 0.2999).floor() + 1.3; new_position.x = aabb.min.x.ceil() + 0.3;
} else { } else if velocity.x > 0.0 {
new_position.x = (new_position.x + 0.3).ceil() - 1.3001; new_position.x = aabb.max.x.floor() - 0.3001;
} }
} }
// z component // z component
new_position.z += velocity.z; new_position.z += velocity.z;
while self.check_collision(new_position) { if let Some(aabb) = self.check_collision(new_position) {
if velocity.z <= -0.0 { if velocity.z < 0.0 {
new_position.z = (new_position.z - 0.2999).floor() + 1.3; new_position.z = aabb.min.z.ceil() + 0.3;
} else { } else if velocity.z > 0.0 {
new_position.z = (new_position.z + 0.3).ceil() - 1.3001; new_position.z = aabb.max.z.floor() - 0.3001;
} }
} }