Skip to content

Commit

Permalink
add: scroll processing for OrbitController
Browse files Browse the repository at this point in the history
  • Loading branch information
Sufhal committed Sep 18, 2024
1 parent cc08d84 commit e1c69f5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
44 changes: 21 additions & 23 deletions src/modules/camera/orbit_controller.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cgmath::{Point3, Rad};
use winit::{dpi::PhysicalPosition, event::MouseScrollDelta};
use super::camera::Camera;

const VERTICAL_OFFSET: f32 = 2.0;
Expand Down Expand Up @@ -30,43 +31,40 @@ impl OrbitController {
self.rotate_vertical = delta_y as f32;
}

pub fn process_scroll(&mut self, delta: &MouseScrollDelta) {
let value = match delta {
MouseScrollDelta::LineDelta(_, scroll) => scroll * 100.0,
MouseScrollDelta::PixelDelta(PhysicalPosition {
y: scroll,
..
}) => *scroll as f32,
};
self.distance += value / 15.0;
self.distance = self.distance.clamp(2.0, 30.0);
dbg!(self.distance);
}

pub fn update_target(&mut self, mut target: [f32; 3]) {
target[1] += VERTICAL_OFFSET;
self.target = Point3::from(target);
}

pub fn update_camera(&mut self, camera: &mut Camera) {
// Mise à jour des angles yaw et pitch avec les rotations enregistrées
self.yaw += self.rotate_horizontal * 0.01; // Sensibilité de la rotation
self.pitch += self.rotate_vertical * 0.01; // Sensibilité de la rotation

// Limiter l'angle de pitch pour éviter de passer sous ou au-dessus du personnage
const PITCH_LIMIT: f32 = std::f32::consts::FRAC_PI_2 - 0.1; // Limiter à presque 90°
self.yaw += self.rotate_horizontal * 0.01;
self.pitch += self.rotate_vertical * 0.01;
const PITCH_LIMIT: f32 = std::f32::consts::FRAC_PI_2 - 0.1;
self.pitch = self.pitch.clamp(-PITCH_LIMIT, PITCH_LIMIT);

// Calculer la position de la caméra en coordonnées sphériques
// let x = self.distance * self.yaw.cos() * self.pitch.cos();
// let y = self.distance * self.pitch.sin();
// let z = self.distance * self.yaw.sin() * self.pitch.cos();

let horizontal_distance = self.distance * self.pitch.cos(); // Distance projetée sur le plan XZ
let x = horizontal_distance * self.yaw.cos(); // Composante X du déplacement
let z = horizontal_distance * self.yaw.sin(); // Composante Z du déplacement
let y = self.distance * self.pitch.sin(); // Composante verticale du déplacement (rotation verticale)


// Mettre à jour la position de la caméra en fonction de la cible (personnage)
let horizontal_distance = self.distance * self.pitch.cos();
let x = horizontal_distance * self.yaw.cos();
let z = horizontal_distance * self.yaw.sin();
let y = self.distance * self.pitch.sin();
camera.position = Point3::new(
self.target.x + x,
self.target.y + y,
self.target.z + z,
);

// Mettre à jour l'orientation de la caméra (yaw et pitch)
camera.yaw = Rad(self.yaw + std::f32::consts::PI);
camera.pitch = Rad(-self.pitch);

// Réinitialiser les rotations après l'update
self.rotate_horizontal = 0.0;
self.rotate_vertical = 0.0;
}
Expand Down
7 changes: 0 additions & 7 deletions src/modules/character/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,6 @@ impl Character {
scene
);
self.has_moved = true;
// // Mettre à jour la rotation du personnage pour qu'il fasse face à la direction du mouvement
// if movement.magnitude() > 0.001 {
// let new_rotation = movement.y.atan2(movement.x);
// let rotation = Quaternion::from_arc(Vector3::new(0.0, 0.0, 1.0), normalized_direction, None);
// self.rotate_quaternion(rotation, scene);
// self.rotate(new_rotation, scene);
// }
}

}
Expand Down
8 changes: 7 additions & 1 deletion src/modules/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,13 @@ impl<'a> State<'a> {
}
},
WindowEvent::MouseWheel { delta, .. } => {
self.camera_controller.process_scroll(delta);
if self.camera_controller.enabled {
self.camera_controller.process_scroll(delta);
} else {
if let Some(actor) = &mut self.actor {
actor.orbit_controller.process_scroll(delta);
}
}
true
}
WindowEvent::MouseInput {
Expand Down

0 comments on commit e1c69f5

Please sign in to comment.