|
|
@ -1,15 +1,17 @@
|
|
|
|
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use cgmath::{Euler, Quaternion};
|
|
|
|
use legion::world::SubWorld;
|
|
|
|
use legion::world::SubWorld;
|
|
|
|
use legion::IntoQuery;
|
|
|
|
use legion::IntoQuery;
|
|
|
|
|
|
|
|
use legion::*;
|
|
|
|
|
|
|
|
use nalgebra::Quaternion as naQuaternion;
|
|
|
|
use rapier3d::dynamics::{IntegrationParameters, JointSet, RigidBodySet};
|
|
|
|
use rapier3d::dynamics::{IntegrationParameters, JointSet, RigidBodySet};
|
|
|
|
use rapier3d::geometry::{BroadPhase, ColliderSet, NarrowPhase};
|
|
|
|
use rapier3d::geometry::{BroadPhase, ColliderSet, NarrowPhase};
|
|
|
|
use rapier3d::pipeline::PhysicsPipeline;
|
|
|
|
use rapier3d::pipeline::PhysicsPipeline;
|
|
|
|
use legion::*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use crate::camera::{Camera, CameraController};
|
|
|
|
|
|
|
|
use crate::components::{Collider, LoopState, Mesh, Physics, Position};
|
|
|
|
use crate::render::{EntityUniforms, Renderer};
|
|
|
|
use crate::render::{EntityUniforms, Renderer};
|
|
|
|
use cgmath::Quaternion;
|
|
|
|
|
|
|
|
use crate::components::{Collider, Physics, Mesh, Position, LoopState};
|
|
|
|
|
|
|
|
use crate::camera::{CameraController, Camera};
|
|
|
|
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct PhysicsState {
|
|
|
|
pub struct PhysicsState {
|
|
|
|
gravity: rapier3d::math::Vector<f32>,
|
|
|
|
gravity: rapier3d::math::Vector<f32>,
|
|
|
@ -47,7 +49,6 @@ pub fn run_physics(
|
|
|
|
#[resource] physics_state: &mut PhysicsState,
|
|
|
|
#[resource] physics_state: &mut PhysicsState,
|
|
|
|
#[resource] physics_pipeline: &mut PhysicsPipeline,
|
|
|
|
#[resource] physics_pipeline: &mut PhysicsPipeline,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
|
|
// Make sure all the entities we care about are added to the system
|
|
|
|
// Make sure all the entities we care about are added to the system
|
|
|
|
let mut query = <(&mut Collider, &mut Physics, &mut Mesh)>::query();
|
|
|
|
let mut query = <(&mut Collider, &mut Physics, &mut Mesh)>::query();
|
|
|
|
for (collider, physics, mesh) in query.iter_mut(world) {
|
|
|
|
for (collider, physics, mesh) in query.iter_mut(world) {
|
|
|
@ -56,11 +57,15 @@ pub fn run_physics(
|
|
|
|
let handle = physics_state.bodies.insert(physics.rigid_body.clone());
|
|
|
|
let handle = physics_state.bodies.insert(physics.rigid_body.clone());
|
|
|
|
physics.rigid_body_handle = Some(handle);
|
|
|
|
physics.rigid_body_handle = Some(handle);
|
|
|
|
physics.rigid_body_handle.unwrap()
|
|
|
|
physics.rigid_body_handle.unwrap()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Some(handle) => handle,
|
|
|
|
Some(handle) => handle,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
if collider.collider_handle == None {
|
|
|
|
if collider.collider_handle == None {
|
|
|
|
let handle = physics_state.colliders.insert(collider.collider.clone(), rigid_body_handle, &mut physics_state.bodies);
|
|
|
|
let handle = physics_state.colliders.insert(
|
|
|
|
|
|
|
|
collider.collider.clone(),
|
|
|
|
|
|
|
|
rigid_body_handle,
|
|
|
|
|
|
|
|
&mut physics_state.bodies,
|
|
|
|
|
|
|
|
);
|
|
|
|
collider.collider_handle = Some(handle);
|
|
|
|
collider.collider_handle = Some(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -77,17 +82,14 @@ pub fn run_physics(
|
|
|
|
&mut physics_state.joints,
|
|
|
|
&mut physics_state.joints,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
&event_handler
|
|
|
|
&event_handler,
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[system]
|
|
|
|
#[system]
|
|
|
|
#[write_component(Camera)]
|
|
|
|
#[write_component(Camera)]
|
|
|
|
#[write_component(CameraController)]
|
|
|
|
#[write_component(CameraController)]
|
|
|
|
pub fn update_camera(
|
|
|
|
pub fn update_camera(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) {
|
|
|
|
world: &mut SubWorld,
|
|
|
|
|
|
|
|
#[resource] loop_state: &mut LoopState,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
let mut query = <(&mut Camera, &mut CameraController)>::query();
|
|
|
|
let mut query = <(&mut Camera, &mut CameraController)>::query();
|
|
|
|
for (mut camera, controller) in query.iter_mut(world) {
|
|
|
|
for (mut camera, controller) in query.iter_mut(world) {
|
|
|
|
controller.update_camera(&mut camera, loop_state.step_size)
|
|
|
|
controller.update_camera(&mut camera, loop_state.step_size)
|
|
|
@ -104,25 +106,32 @@ pub fn update_models(
|
|
|
|
#[resource] physics_state: &mut PhysicsState,
|
|
|
|
#[resource] physics_state: &mut PhysicsState,
|
|
|
|
#[resource] physics_pipeline: &mut PhysicsPipeline,
|
|
|
|
#[resource] physics_pipeline: &mut PhysicsPipeline,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
|
|
// Make sure all the entities we care about are added to the system
|
|
|
|
// Make sure all the entities we care about are added to the system
|
|
|
|
let mut query = <(&mut Collider, &mut Physics, &mut Mesh, &mut Position)>::query();
|
|
|
|
let mut query = <(&mut Collider, &mut Physics, &mut Mesh, &mut Position)>::query();
|
|
|
|
for (collider, physics, mesh, position) in query.iter_mut(world) {
|
|
|
|
for (collider, physics, mesh, position) in query.iter_mut(world) {
|
|
|
|
let pos = physics_state.bodies.get(physics.rigid_body_handle.unwrap()).unwrap().position();
|
|
|
|
let pos = physics_state
|
|
|
|
|
|
|
|
.bodies
|
|
|
|
|
|
|
|
.get(physics.rigid_body_handle.unwrap())
|
|
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
|
|
.position();
|
|
|
|
position.x = pos.translation.x;
|
|
|
|
position.x = pos.translation.x;
|
|
|
|
position.y = pos.translation.y;
|
|
|
|
position.y = pos.translation.y;
|
|
|
|
position.z = pos.translation.z;
|
|
|
|
position.z = pos.translation.z;
|
|
|
|
|
|
|
|
|
|
|
|
position.rot.s = pos.rotation.w;
|
|
|
|
let q = Quaternion::new(
|
|
|
|
position.rot.v.x = pos.rotation.i;
|
|
|
|
pos.rotation.w,
|
|
|
|
position.rot.v.y = pos.rotation.j;
|
|
|
|
pos.rotation.i,
|
|
|
|
position.rot.v.z = pos.rotation.k;
|
|
|
|
pos.rotation.j,
|
|
|
|
|
|
|
|
pos.rotation.k,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
let rotation = Euler::from(q);
|
|
|
|
|
|
|
|
position.rot.x = rotation.x.into();
|
|
|
|
|
|
|
|
position.rot.y = rotation.y.into();
|
|
|
|
|
|
|
|
position.rot.z = rotation.z.into();
|
|
|
|
|
|
|
|
|
|
|
|
// mx.x = pos.rotation.i;
|
|
|
|
// mx.x = pos.rotation.i;
|
|
|
|
// mx.y = pos.rotation.j;
|
|
|
|
// mx.y = pos.rotation.j;
|
|
|
|
// mx.z = pos.rotation.k;
|
|
|
|
// mx.z = pos.rotation.k;
|
|
|
|
// mx.w = pos.rotation.w;
|
|
|
|
// mx.w = pos.rotation.w;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|