Some name changes and constats for the controller

master
Wynd 2024-11-06 13:23:50 +02:00
parent 5afea588bb
commit 79c10f6d95
2 changed files with 40 additions and 33 deletions

View File

@ -5,25 +5,31 @@ use crate::camera::Camera;
const MAX_ZOOM_LEVEL: f32 = 15.0; const MAX_ZOOM_LEVEL: f32 = 15.0;
pub struct CameraController { pub struct CameraController {
speed: f32, zoom_speed: f32,
forward: f32, movement_speed: f32,
zoom: f32,
cursor_start: (f32, f32), cursor_start: (f32, f32),
movement: (f32, f32), movement: (f32, f32),
is_moving: bool, is_moving: bool,
movement_level: (f32, f32),
zoom_level: f32, movement_total: (f32, f32),
zoom_total: f32,
} }
impl CameraController { impl CameraController {
pub fn new(speed: f32) -> Self { pub fn new(zoom_speed: f32, movement_speed: f32) -> Self {
Self { Self {
speed, zoom_speed,
forward: 0.0, movement_speed,
zoom: 0.0,
cursor_start: (0.0, 0.0), cursor_start: (0.0, 0.0),
movement: (0.0, 0.0), movement: (0.0, 0.0),
is_moving: false, is_moving: false,
movement_level: (0.0, 0.0),
zoom_level: 1.0, movement_total: (0.0, 0.0),
zoom_total: 1.0,
} }
} }
@ -45,12 +51,12 @@ impl CameraController {
self.cursor_start = (position.x as f32, position.y as f32); self.cursor_start = (position.x as f32, position.y as f32);
} }
let (x, y) = match self.is_moving { self.movement = match self.is_moving {
true => { true => {
let x = -self.cursor_start.0 + position.x as f32; let x = (-self.cursor_start.0 + position.x as f32) / 1000.0;
let x = x * 0.000001; let x = x * self.movement_speed;
let y = self.cursor_start.1 - position.y as f32; let y = (self.cursor_start.1 - position.y as f32) / 1000.0;
let y = y * 0.000001; let y = y * self.movement_speed;
(x, y) (x, y)
} }
false => { false => {
@ -70,17 +76,15 @@ impl CameraController {
} }
}; };
self.movement = (x, y);
true true
} }
WindowEvent::MouseWheel { delta, .. } => match delta { WindowEvent::MouseWheel { delta, .. } => match delta {
MouseScrollDelta::LineDelta(_, y) => { MouseScrollDelta::LineDelta(_, y) => {
self.zoom_level += y; self.zoom_total += y;
if self.zoom_level >= 0.0 && self.zoom_level <= MAX_ZOOM_LEVEL { if self.zoom_total >= 0.0 && self.zoom_total <= MAX_ZOOM_LEVEL {
self.forward = *y; self.zoom = *y;
} }
self.zoom_level = self.zoom_level.clamp(0.0, MAX_ZOOM_LEVEL); self.zoom_total = self.zoom_total.clamp(0.0, MAX_ZOOM_LEVEL);
true true
} }
_ => false, _ => false,
@ -95,34 +99,34 @@ impl CameraController {
let forward = camera.target - camera.eye; let forward = camera.target - camera.eye;
let forward_norm = forward.normalize(); let forward_norm = forward.normalize();
if self.forward > 0.0 { if self.zoom > 0.0 {
camera.eye += forward_norm * self.speed; camera.eye += forward_norm * self.zoom_speed;
} }
else if self.forward < 0.0 { else if self.zoom < 0.0 {
camera.eye -= forward_norm * self.speed; camera.eye -= forward_norm * self.zoom_speed;
} }
self.movement_level.0 += self.movement.0; self.movement_total.0 += self.movement.0;
self.movement_level.1 += self.movement.1; self.movement_total.1 += self.movement.1;
if self.movement_level.0 > -1.0 && self.movement_level.0 < 1.0 { if self.movement_total.0 > -1.0 && self.movement_total.0 < 1.0 {
camera.eye.x += self.movement.0; camera.eye.x += self.movement.0;
camera.target.x += self.movement.0; camera.target.x += self.movement.0;
} }
if self.movement_level.1 > -1.0 && self.movement_level.1 < 1.0 { if self.movement_total.1 > -1.0 && self.movement_total.1 < 1.0 {
camera.eye.y += self.movement.1; camera.eye.y += self.movement.1;
camera.target.y += self.movement.1; camera.target.y += self.movement.1;
} }
self.movement_level.0 = self.movement_level.0.clamp(-1.0, 1.0); self.movement_total.0 = self.movement_total.0.clamp(-1.0, 1.0);
self.movement_level.1 = self.movement_level.1.clamp(-1.0, 1.0); self.movement_total.1 = self.movement_total.1.clamp(-1.0, 1.0);
self.forward = 0.0; self.zoom = 0.0;
} }
pub fn reset_camera(&mut self, camera: &mut Camera) { pub fn reset_camera(&mut self, camera: &mut Camera) {
camera.reset_camera(); camera.reset_camera();
self.zoom_level = 1.0; self.zoom_total = 1.0;
} }
} }

View File

@ -15,6 +15,9 @@ use crate::{
vertex::Vertex, vertex::Vertex,
}; };
const ZOOM_SPEED: f32 = 0.1;
const MOVE_SPEED: f32 = 0.0005;
const VERTICES: &[Vertex] = &[ const VERTICES: &[Vertex] = &[
Vertex { Vertex {
position: [-1.0, 1.0], position: [-1.0, 1.0],
@ -112,7 +115,7 @@ impl<'a> State<'a> {
}; };
let (camera, camera_info) = Camera::new(&config, &device); let (camera, camera_info) = Camera::new(&config, &device);
let camera_controller = CameraController::new(0.1); let camera_controller = CameraController::new(ZOOM_SPEED, MOVE_SPEED);
let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl"));