feat: instantiate and animate 5 helicopters with independent rotor rotations and paths
This commit is contained in:
65
src/main.rs
65
src/main.rs
@@ -251,21 +251,27 @@ fn main() {
|
||||
let mut terrain_node = SceneNode::from_vao(terrain_vao, terrain_mesh.index_count);
|
||||
root_node.add_child(&*terrain_node);
|
||||
|
||||
let mut heli_root = SceneNode::new();
|
||||
let body_node = SceneNode::from_vao(heli_body_vao, helicopter.body.index_count);
|
||||
let door_node = SceneNode::from_vao(heli_door_vao, helicopter.door.index_count);
|
||||
let mut main_rotor_node = SceneNode::from_vao(heli_main_rotor_vao, helicopter.main_rotor.index_count);
|
||||
let mut tail_rotor_node = SceneNode::from_vao(heli_tail_rotor_vao, helicopter.tail_rotor.index_count);
|
||||
// Set reference point for tail rotor (pivot around its hub)
|
||||
let mut tail_rotor_pin = tail_rotor_node.as_mut();
|
||||
unsafe { tail_rotor_pin.get_unchecked_mut().reference_point = glm::vec3(0.35, 2.3, 10.4); }
|
||||
|
||||
heli_root.add_child(&*body_node);
|
||||
heli_root.add_child(&*door_node);
|
||||
heli_root.add_child(&*main_rotor_node);
|
||||
heli_root.add_child(&*tail_rotor_node);
|
||||
|
||||
terrain_node.add_child(&*heli_root);
|
||||
// Instantiate multiple helicopters
|
||||
let heli_count = 5;
|
||||
let mut heli_roots = Vec::new();
|
||||
for i in 0..heli_count {
|
||||
let mut root = SceneNode::new();
|
||||
let body_node = SceneNode::from_vao(heli_body_vao, helicopter.body.index_count);
|
||||
let door_node = SceneNode::from_vao(heli_door_vao, helicopter.door.index_count);
|
||||
let mut main_rotor_node = SceneNode::from_vao(heli_main_rotor_vao, helicopter.main_rotor.index_count);
|
||||
let mut tail_rotor_node = SceneNode::from_vao(heli_tail_rotor_vao, helicopter.tail_rotor.index_count);
|
||||
// Set reference point for tail rotor (pivot around its hub)
|
||||
{
|
||||
let mut tr_pin = tail_rotor_node.as_mut();
|
||||
unsafe { tr_pin.get_unchecked_mut().reference_point = glm::vec3(0.35, 2.3, 10.4); }
|
||||
}
|
||||
root.add_child(&*body_node);
|
||||
root.add_child(&*door_node);
|
||||
root.add_child(&*main_rotor_node);
|
||||
root.add_child(&*tail_rotor_node);
|
||||
terrain_node.add_child(&*root);
|
||||
heli_roots.push(root);
|
||||
}
|
||||
|
||||
let vertices = vec![
|
||||
// triangle 1
|
||||
@@ -449,22 +455,25 @@ fn main() {
|
||||
let projection: glm::Mat4 =
|
||||
glm::perspective(window_aspect_ratio, std::f32::consts::PI / 4.0, 1.0, 1000.0);
|
||||
|
||||
// Animate helicopter root via simple_heading_animation
|
||||
let heading = simple_heading_animation(elapsed);
|
||||
{
|
||||
let mut hr_pin = heli_root.as_mut();
|
||||
unsafe {
|
||||
let node = hr_pin.get_unchecked_mut();
|
||||
// Animate and update multiple helicopters
|
||||
for (i, root) in heli_roots.iter_mut().enumerate() {
|
||||
let time_offset = elapsed + (i as f32) * 0.5;
|
||||
let heading = simple_heading_animation(time_offset);
|
||||
{
|
||||
let mut pin = root.as_mut();
|
||||
let node = unsafe { pin.get_unchecked_mut() };
|
||||
node.position = glm::vec3(heading.x, 0.0, heading.z);
|
||||
node.rotation = glm::vec3(heading.pitch, heading.yaw, heading.roll);
|
||||
}
|
||||
}
|
||||
// Update rotor rotations
|
||||
{
|
||||
let mut mr_pin = main_rotor_node.as_mut();
|
||||
unsafe { mr_pin.get_unchecked_mut().rotation.y = elapsed * 10.0; }
|
||||
let mut tr_pin = tail_rotor_node.as_mut();
|
||||
unsafe { tr_pin.get_unchecked_mut().rotation.x = elapsed * 10.0; }
|
||||
// Update rotors: child index 2 = main rotor, 3 = tail rotor
|
||||
{
|
||||
let mut pin = root.as_mut();
|
||||
let node = unsafe { pin.get_unchecked_mut() };
|
||||
unsafe {
|
||||
(*node.children[2]).rotation.y = elapsed * 10.0;
|
||||
(*node.children[3]).rotation.x = elapsed * 10.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draw scene via scene graph
|
||||
|
||||
Reference in New Issue
Block a user