camera scrolls right as the player moves

main
_ 2021-12-23 01:19:53 +00:00
parent c8baadc24c
commit a3fcff78f9
4 changed files with 52 additions and 8 deletions

9
Cargo.lock generated
View File

@ -426,9 +426,9 @@ checksum = "3c6015bb2d300661a0014e29025374a32a68e8ef7e4effe547baa00eef38b8e3"
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "0.4.6" version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]] [[package]]
name = "jpeg-decoder" name = "jpeg-decoder"
@ -647,6 +647,7 @@ dependencies = [
"rmp-serde", "rmp-serde",
"sdl2", "sdl2",
"serde", "serde",
"serde_json",
"tokio", "tokio",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
@ -1201,9 +1202,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.60" version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1500e84d27fe482ed1dc791a56eddc2f230046a040fa908c08bda1d9fb615779" checksum = "bcbd0344bc6533bc7ec56df11d42fb70f1b912351c0825ccb7211b59d8af7cf5"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",

View File

@ -27,6 +27,7 @@ rand = "0.6.5"
rcgen = "0.8.11" rcgen = "0.8.11"
rmp-serde = "0.15.4" rmp-serde = "0.15.4"
serde = { version = "1.0.125", features = ["derive"] } serde = { version = "1.0.125", features = ["derive"] }
serde_json = "1.0.73"
sdl2 = "0.32.2" sdl2 = "0.32.2"
tokio = { version = "1.5.0", features = ["full"] } tokio = { version = "1.5.0", features = ["full"] }
tracing = "0.1.22" tracing = "0.1.22"

View File

@ -177,8 +177,28 @@ impl Graphics {
level: &crate::LoadedLevel, level: &crate::LoadedLevel,
camera: &crate::Camera, camera: &crate::Camera,
) { ) {
use serde::Deserialize;
use uniforms as u; use uniforms as u;
#[derive (Default, Deserialize)]
struct Config {
camera: CameraCfg,
}
#[derive (Default, Deserialize)]
struct CameraCfg {
#[serde(default)]
rotate_mix: f32,
#[serde(default)]
translate_mix: f32,
}
/*
let cfg: Option <Config> = std::fs::read_to_string ("config.json").ok ()
.map (|s| serde_json::from_str (s.as_str ()).ok ())
.flatten ();
let cfg = cfg.unwrap_or_else (Config::default);
*/
let white = color_from_255 ((255.0, 255.0, 255.0)); let white = color_from_255 ((255.0, 255.0, 255.0));
let black = color_from_255 ((0.0, 0.0, 0.0)); let black = color_from_255 ((0.0, 0.0, 0.0));
let shadow_blue = color_from_255 ((68.0, 36.0, 52.0)); let shadow_blue = color_from_255 ((68.0, 36.0, 52.0));
@ -195,9 +215,17 @@ impl Graphics {
_ _
) = camera.transform.clone ().decomposed (); ) = camera.transform.clone ().decomposed ();
let view_mat = proj_mat * let cam_trans = Vec3::from (cam_trans);
Mat4::from_quat (Quat::from_array (cam_rot).inverse ()); let cam_trans = Vec3::new (cam_trans.x.max (state.player.pos.x + 4.0), cam_trans.y, cam_trans.z);
//Mat4::from_translation (cam_trans.into ()) *
let view_mat =
Mat4::from_quat (Quat::from_array (cam_rot).inverse ()) *
Mat4::from_translation (-cam_trans);
// println! ("Camera is at {:?}", view_mat.inverse ().transform_point3 ((0.0, 0.0, 0.0).into ()));
let view_mat = proj_mat * view_mat;
let world_model_mat = Mat4::IDENTITY; let world_model_mat = Mat4::IDENTITY;
self.passes [0].with (gl_state, || { self.passes [0].with (gl_state, || {

View File

@ -1,5 +1,6 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
time::Instant,
}; };
use anyhow::Result; use anyhow::Result;
@ -177,6 +178,9 @@ async fn main () -> Result <()> {
game_state.reset_level (&level); game_state.reset_level (&level);
game_state.phys_tris = phys_tris; game_state.phys_tris = phys_tris;
let mut next_mpf_print = 60;
let mut last_mpf_instant = Instant::now ();
'running: loop { 'running: loop {
let _frames_to_do = time_step.step (); let _frames_to_do = time_step.step ();
let mut player_wants_to_jump = false; let mut player_wants_to_jump = false;
@ -292,13 +296,23 @@ async fn main () -> Result <()> {
}); });
} }
dbg! (game_state.player.pos); // dbg! (game_state.player.pos);
window.gl_make_current (&gl_ctx).unwrap (); window.gl_make_current (&gl_ctx).unwrap ();
graphics.draw (&game_state, &mut gl_state, &level, &camera); graphics.draw (&game_state, &mut gl_state, &level, &camera);
graphics.frames += 1; graphics.frames += 1;
if graphics.frames == next_mpf_print {
let now = Instant::now ();
let mpf = (now - last_mpf_instant).as_millis () / 60;
dbg! (mpf);
next_mpf_print += 60;
last_mpf_instant = now;
}
window.gl_swap_window (); window.gl_swap_window ();
tokio::time::sleep (Duration::from_millis (15)).await; tokio::time::sleep (Duration::from_millis (15)).await;