Fix door!
This commit is contained in:
+25
-14
@@ -7,7 +7,7 @@ use std::mem::ManuallyDrop;
|
||||
use std::pin::Pin;
|
||||
|
||||
use crate::mesh::{ Mesh, Terrain, Helicopter };
|
||||
use crate::toolbox::{self, open_door, AnimCTX};
|
||||
use crate::toolbox::{self, AnimCTX};
|
||||
|
||||
|
||||
unsafe fn draw_scene(node: &SceneNode,
|
||||
@@ -90,7 +90,6 @@ impl World {
|
||||
let lunar_mesh = &meshes[&Nodes::LunarSurface];
|
||||
let mut lunar_node = SceneNode::from_vao(lunar_mesh.vao_id, lunar_mesh.index_count);
|
||||
lunar_node.reference_point = glm::vec3(0.0, 0.0, 0.0);
|
||||
lunar_node.position.y = -10.0;
|
||||
nodes.insert(Nodes::LunarSurface, vec![lunar_node]);
|
||||
|
||||
nodes.insert(Nodes::HeliRoot, Vec::new());
|
||||
@@ -101,7 +100,7 @@ impl World {
|
||||
|
||||
for _i in 0..num_helicopters {
|
||||
let mut heli_root = SceneNode::new();
|
||||
heli_root.reference_point = glm::vec3(0.0, 0.0, 0.0);
|
||||
heli_root.reference_point = glm::vec3(20.0, 10.0, 0.0);
|
||||
|
||||
let mut heli_body = SceneNode::from_vao(meshes[&Nodes::HeliBody].vao_id, meshes[&Nodes::HeliBody].index_count);
|
||||
heli_body.reference_point = glm::vec3(0.0, 0.0, 0.0);
|
||||
@@ -164,19 +163,31 @@ impl World {
|
||||
self.nodes.get_mut(&Nodes::HeliTailRotor).unwrap()[i].rotation.x = elapsed * rotor_speed;
|
||||
}
|
||||
|
||||
for ctx in &mut self.anim_ctxs {
|
||||
let door_transform = open_door(elapsed, ctx);
|
||||
self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0].position.x = door_transform.x;
|
||||
self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0].position.y = door_transform.y;
|
||||
self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0].position.z = door_transform.y;
|
||||
self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0].rotation.x = door_transform.pitch;
|
||||
self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0].rotation.y = door_transform.yaw;
|
||||
self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0].rotation.z = door_transform.roll;
|
||||
}
|
||||
|
||||
// Build scene graph on the fly or store scene_root separately
|
||||
unsafe {
|
||||
draw_scene(&self.nodes[&Nodes::SceneRoot][0], &transform_thus_far, glm::identity(), &shader, elapsed);
|
||||
}
|
||||
|
||||
let mut i = 0;
|
||||
while i < self.anim_ctxs.len() {
|
||||
let transform = (self.anim_ctxs[i].anim_func)(elapsed, &mut self.anim_ctxs[i]);
|
||||
|
||||
self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0].position.x = transform.x;
|
||||
self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0].position.y = transform.y;
|
||||
self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0].position.z = transform.z;
|
||||
self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0].rotation.x = transform.pitch;
|
||||
self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0].rotation.y = transform.yaw;
|
||||
self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0].rotation.z = transform.roll;
|
||||
|
||||
unsafe {
|
||||
draw_scene(&self.nodes.get_mut(&Nodes::HeliDoor).unwrap()[0], &transform_thus_far, glm::identity(), &shader, elapsed);
|
||||
}
|
||||
|
||||
if transform.y <= -1000.0f32 {
|
||||
self.anim_ctxs.remove(i);
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
-5
@@ -11,7 +11,6 @@ extern crate nalgebra_glm as glm;
|
||||
use std::ptr;
|
||||
use std::thread;
|
||||
use std::sync::{Mutex, Arc, RwLock};
|
||||
use rand::random;
|
||||
|
||||
mod shader;
|
||||
mod util;
|
||||
@@ -24,6 +23,8 @@ use glutin::event::{Event, WindowEvent, DeviceEvent, KeyboardInput, ElementState
|
||||
use glutin::event_loop::ControlFlow;
|
||||
|
||||
|
||||
use crate::draw::Nodes;
|
||||
use crate::scene_graph::SceneNode;
|
||||
use crate::toolbox::AnimCTX;
|
||||
|
||||
// initial window size
|
||||
@@ -177,10 +178,22 @@ fn main() {
|
||||
camera_position.y += trans_speed;
|
||||
}
|
||||
VirtualKeyCode::E => {
|
||||
world.anim_ctxs.push(AnimCTX{
|
||||
stime : elapsed,
|
||||
rand_seed: rand::random::<f32>()
|
||||
});
|
||||
|
||||
let ptr = &world.nodes[&Nodes::HeliDoor][0];
|
||||
let parentptr = &world.nodes[&Nodes::HeliRoot][0];
|
||||
|
||||
if let Some(pos) = world.nodes[&Nodes::HeliRoot][0].children.iter().position(|&c| ptr::eq(c, ptr.as_ref().get_ref())) {
|
||||
|
||||
world.anim_ctxs.push(AnimCTX{
|
||||
stime : elapsed,
|
||||
rand_seed: rand::random::<f32>(),
|
||||
anim_func: toolbox::open_door,
|
||||
start_pos: parentptr.position,
|
||||
start_rot: parentptr.rotation
|
||||
});
|
||||
|
||||
world.nodes.get_mut(&Nodes::HeliRoot).unwrap()[0].children.remove(pos);
|
||||
}
|
||||
}
|
||||
|
||||
VirtualKeyCode::Down => {
|
||||
|
||||
@@ -16,6 +16,7 @@ use std::pin::Pin;
|
||||
// having what I arbitrarily decided to be the required level of "simplicity of use".
|
||||
pub type Node = ManuallyDrop<Pin<Box<SceneNode>>>;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub struct SceneNode {
|
||||
pub position : glm::Vec3, // Where I should be in relation to my parent
|
||||
pub rotation : glm::Vec3, // How I should be rotated, around the X, the Y and the Z axes
|
||||
|
||||
+13
-8
@@ -1,7 +1,8 @@
|
||||
extern crate nalgebra_glm as glm;
|
||||
use std::f64::consts::PI;
|
||||
|
||||
use glm::min2;
|
||||
use glm::Vec3;
|
||||
use tobj::Mesh;
|
||||
|
||||
pub struct Heading {
|
||||
pub x : f32,
|
||||
@@ -49,7 +50,10 @@ pub struct FullHeading {
|
||||
|
||||
pub struct AnimCTX {
|
||||
pub stime : f32,
|
||||
pub rand_seed: f32
|
||||
pub rand_seed: f32,
|
||||
pub anim_func: fn(f32, &AnimCTX) -> FullHeading,
|
||||
pub start_pos: Vec3,
|
||||
pub start_rot: Vec3,
|
||||
}
|
||||
|
||||
pub fn open_door(time: f32, ctx: &AnimCTX) -> FullHeading {
|
||||
@@ -59,13 +63,14 @@ pub fn open_door(time: f32, ctx: &AnimCTX) -> FullHeading {
|
||||
let x_speed = 5.0f32;
|
||||
let y_speed = 3.0f32;
|
||||
let sim_speed = 5.0f32;
|
||||
let xpos = 5.0f32 * ctx.rand_seed * x_speed * t;
|
||||
let ypos: f32 = -0.5* 9.81f32 * y_speed * t.powf(2.0f32);
|
||||
let zpos: f32 = ctx.rand_seed * 5.0f32 * t;
|
||||
|
||||
let tilt = ctx.rand_seed * sim_speed * y_speed / 12.0f32 * t;
|
||||
let roll = ctx.rand_seed * sim_speed * (x_speed * t) / 5.0f32;
|
||||
let yaw = tilt;
|
||||
let xpos: f32 = ctx.start_pos.x + 5.0f32 * ctx.rand_seed * x_speed * t;
|
||||
let ypos: f32 = ctx.start_pos.y + -0.5* 9.81f32 * y_speed * t.powf(2.0f32);
|
||||
let zpos: f32 = ctx.start_pos.z + ctx.rand_seed * 5.0f32 * t;
|
||||
|
||||
let tilt = ctx.start_rot.x + ctx.rand_seed * sim_speed * y_speed / 12.0f32 * t;
|
||||
let roll = ctx.start_rot.y + ctx.rand_seed * sim_speed * (x_speed * t) / 5.0f32;
|
||||
let yaw = ctx.start_rot.z + tilt;
|
||||
|
||||
FullHeading {
|
||||
x : xpos as f32,
|
||||
|
||||
Reference in New Issue
Block a user