camera movement

This commit is contained in:
2019-07-28 16:20:08 +02:00
parent a8a34b90f4
commit 5795d0b635
4 changed files with 94 additions and 37 deletions

View File

@@ -1,5 +1,5 @@
use winit::{Event, WindowEvent};
use cgmath::{Matrix4, Rad, Point3, Vector3, Deg};
use winit::{Event, WindowEvent, DeviceEvent};
use cgmath::{Matrix4, Rad, Point3, Vector3, Deg, SquareMatrix, Quaternion, Rotation3, One, Rotation};
mod vulkan;
use crate::vulkan::{GameData, Game, LinePoint};
@@ -12,7 +12,8 @@ use crate::config::LogConfig;
struct TestGame {
input: InputState,
cam_pos: Point3<f32>,
cam_position: Vector3<f32>,
cam_rotation: Quaternion<f32>,
log_config: LogConfig,
}
@@ -22,6 +23,8 @@ impl Game for TestGame {
}
fn update(self: &mut Self, game_data: &mut GameData) {
game_data.push_constants.time = game_data.start_time.elapsed().unwrap().as_millis() as f32 / 1000.0;
// User interaction
if self.input.button_just_released("QUIT") {
game_data.shutdown = true;
@@ -31,18 +34,17 @@ impl Game for TestGame {
game_data.recreate_pipeline = true;
}
self.cam_pos.x += self.input.get_axis("FORWARD_AXIS") * 0.01;
self.cam_rotation = self.cam_rotation * Quaternion::from_angle_z(Deg(self.input.get_axis("look_horizontal") * 0.05));
self.cam_rotation = Quaternion::from_angle_x(Deg(self.input.get_axis("look_vertical") * 0.05)) * self.cam_rotation;
self.cam_position += self.cam_rotation.invert().rotate_vector(Vector3::new(
self.input.get_axis("move_sideways") * -0.05,
0.0,
self.input.get_axis("move_forward") * 0.05));
// Move game objects
game_data.push_constants.time = game_data.start_time.elapsed().unwrap().as_millis() as f32 / 1000.0;
let model = Matrix4::from_angle_z(Rad::from(Deg(game_data.push_constants.time * 100.0)));
let view = Matrix4::look_at(
self.cam_pos,
Point3::new(0.0, 0.0, 0.0),
Vector3::new(0.0, 0.0, 1.0)
);
let view = Matrix4::from(self.cam_rotation) * Matrix4::from_translation(self.cam_position);
let mut proj = cgmath::perspective(
Rad::from(Deg(45.0)),
@@ -99,6 +101,13 @@ impl Game for TestGame {
}
self.input.on_mouse_wheel_event(&delta, &modifiers);
},
Event::DeviceEvent { device_id, event: DeviceEvent::MouseMotion { delta: (delta_x, delta_y) } } => {
if self.log_config.input {
println!("MouseMotion {:?}, ({:?},{:?})", device_id, delta_x, delta_y);
}
self.input.mouse_delta_x += *delta_x;
self.input.mouse_delta_y += *delta_y;
}
_ => {}
}
@@ -108,7 +117,8 @@ impl Game for TestGame {
fn main() {
let mut game = TestGame {
input: InputState::new("config/input.toml"),
cam_pos: Point3::new(2.0, 2.0, 2.0),
cam_rotation: Quaternion::one(),
cam_position: Vector3::new(0.0, 0.0, -10.0),
log_config: LogConfig::from_file("config/log.toml"),
};