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