added gravity to the player; resized player

Commit 2eb9dc58c7a3 · mgecumshooter · 2026-10-21 18:02 +0300

Changeset
2eb9dc58c7a30f899ea2590f68dea636f84b40c7

View source at this commit

Comments

No comments yet.

Log in to comment

Diff

diff --git a/src/main.rs b/src/main.rs
--- a/src/main.rs
+++ b/src/main.rs
@@ -70,11 +70,11 @@
 
     // let test2 = [&test_chunk];
 
-    let mut world = World::new(4);
+    let mut world = World::new(5);
 
     let mut plr = Player::new(
         Vector3::new(0f32, 10f32, 0f32),
-        Cube::new(Vector3::new(5f32, 10f32, 5f32)),
+        Cube::new(Vector3::new(2f32, 6f32, 2f32)),
     );
 
     // main game loop
@@ -106,6 +106,9 @@
                 tx.draw_mode3D(cam, |mut d3, _| {
                     d3.draw_grid(100, 1f32);
                     for chunk in world.chunks.values() {
+			if !chunk.is_visible(&cam) {
+			    continue;
+			}
                         if let Some(ref mesh) = chunk.mesh {
                             d3.draw_mesh(
                                 mesh,
diff --git a/src/player/mod.rs b/src/player/mod.rs
--- a/src/player/mod.rs
+++ b/src/player/mod.rs
@@ -5,9 +5,11 @@
 use crate::voxel::*;
 use crate::chunk::Chunk;
 
-const MOVE_SPEED: f32 = 5f32;
+const MOVE_SPEED: f32 = 10f32;
 const MOUSE_SENS: f32 = 0.05f32;
 const PITCH_LIMIT: f32 = 89f32;
+const GRAVITY: f32 = 15f32;
+const TERMINAL_GRAVITY: f32 = 40f32;
 
 pub enum Axis {
     X,
@@ -20,6 +22,7 @@
     pos: Vector3,
     yaw: f32,
     pitch: f32,
+    vel_y: f32,
 }
 
 impl Player {
@@ -29,6 +32,7 @@
 	    body: body,
 	    yaw: 0f32,
 	    pitch: 0f32,
+	    vel_y: 0f32,
 	}
     }
 
@@ -58,9 +62,9 @@
 	)
     }
 
-    pub fn move_axis (&mut self, velocity: f32, axis: Axis, world: &Vec<Aabb>) {
+    pub fn move_axis (&mut self, velocity: f32, axis: Axis, world: &Vec<Aabb>) -> bool {
 	if velocity == 0f32 {
-	    return;
+	    return false;
 	}
 
 	match axis {
@@ -70,9 +74,11 @@
 	}
 	
 	let plr = self.get_aabb();
+	let mut hit = false;
 
 	for voxel in world {
 	    if plr.overlaps(&voxel) {
+		hit = true;
 		match axis {
 		    Axis::X => {
 			if velocity > 0f32 {
@@ -83,7 +89,7 @@
 		    }
 		    Axis::Y => {
 			if velocity > 0f32 {
-			    self.pos.y = voxel.min.y - self.body.width;
+			    self.pos.y = voxel.min.y - self.body.height;
 			} else {
 			    self.pos.y = voxel.max.y;
 			}
@@ -98,6 +104,7 @@
 		}
 	    }
 	}
+	hit
     }
 
     pub fn get_surroundings(&self, world: &[&Chunk]) -> Vec<Aabb> {
@@ -159,6 +166,15 @@
 	let dt = rl.get_frame_time();
 	self.move_axis(wish.x * MOVE_SPEED * dt, Axis::X, &obstacles);
 	self.move_axis(wish.z * MOVE_SPEED * dt, Axis::Z, &obstacles);
+
+	self.vel_y = (self.vel_y - GRAVITY * dt).min(TERMINAL_GRAVITY);
+	if self.move_axis(self.vel_y * dt, Axis::Y, &obstacles) {
+	    self.vel_y = 0f32;
+	}
+
+	if self.vel_y == 0f32 && rl.is_key_down(KeyboardKey::KEY_SPACE) {
+	    self.vel_y = 12.5f32;
+	}
     }
 
     pub fn apply_to_camera (&self, cam: &mut Camera3D) {