rotation from physics

master
mitchellhansen 4 years ago
parent b18ea7371a
commit 2c9ce75b8b

File diff suppressed because it is too large Load Diff

@ -4,6 +4,7 @@ use rapier3d::dynamics::{RigidBody, RigidBodyHandle};
use rapier3d::geometry::ColliderHandle;
use rapier3d::geometry::Collider as r3dCollider;
use std::time::{Duration, Instant};
use cgmath::Deg;
// a component is any type that is 'static, sized, send and sync
@ -19,7 +20,7 @@ pub struct Position {
pub x: f32,
pub y: f32,
pub z: f32,
pub rot: cgmath::Quaternion<f32>,
pub rot: cgmath::Euler<Deg<f32>>,
}
#[derive(Clone, Copy, Debug, PartialEq)]

@ -124,7 +124,7 @@ fn main() {
resources.insert(renderer);
let (physics_state, physics_pipeline) =
PhysicsState::build(rapier3d::math::Vector::new(0.0, -9.81, 0.0));
PhysicsState::build(rapier3d::math::Vector::new(0.0, -9.81, 0.05));
resources.insert(physics_state);
resources.insert(physics_pipeline);
@ -289,11 +289,11 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
x: 1.0,
y: 5.0,
z: 2.0,
rot: Quaternion::from(Euler {
rot: Euler {
x: Deg(90.0),
y: Deg(45.0),
z: Deg(15.0),
}), //mx: cgmath::Matrix4::from(transform),
}, //mx: cgmath::Matrix4::from(transform),
},
monkey_mesh,
Color {
@ -306,15 +306,15 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
let mut dynamic_ball_body = RigidBodyBuilder::new_dynamic()
.position(Isometry3::new(
Vector3::new(0.0, 0.0, 5.0),
Vector::y() * PI,
Vector3::new(0.0, 0.0, 0.0),
Vector::y(),
))
.build();
let mut static_floor_body = RigidBodyBuilder::new_static()
.position(Isometry3::new(
Vector3::new(0.0, -8.0, 0.0),
Vector::y() * PI,
Vector::y(),
))
.build();
@ -328,11 +328,11 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
x: 0.0,
y: -8.0,
z: 0.0,
rot: Quaternion::from(Euler {
rot: Euler {
x: Deg(0.0),
y: Deg(0.0),
z: Deg(0.0),
}),
},
},
plane_mesh,
Color {
@ -355,14 +355,14 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
let ball_mesh: Entity = world.push((
Position {
x: 2.0,
y: 2.0,
z: 3.0,
rot: Quaternion::from(Euler {
x: 0.0,
y: 0.0,
z: 0.0,
rot: Euler {
x: Deg(25.0),
y: Deg(45.0),
z: Deg(15.0),
}),
},
},
ball_mesh,
Color {

@ -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;
}
}

@ -87,6 +87,8 @@ pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) {
let frame = renderer.get_current_frame();
// Update the camera uniform buffers, need to make it support selection of
// cameras
let mut query = <(&mut Camera)>::query();
for (camera) in query.iter_mut(world) {
let matrix = camera.calc_matrix(renderer.camera_projection);
@ -104,16 +106,14 @@ pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) {
// Update the entity uniforms
for (pos, mesh, color) in query.iter_mut(world) {
// let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(2.0));
// pos.mx = pos.mx * rotation;
//let p = cgmath::Matrix4::from(pos.rot);
let q = cgmath::Matrix4::from_translation(Vector3::new(pos.x, pos.y, pos.z));
let z = q; // p + q;
let d = Decomposed {
scale: 1.0,
rot: Quaternion::from(pos.rot),
disp: Vector3::new(pos.x, pos.y, pos.z)
};
let m = Matrix4::from(d);
let data = EntityUniforms {
model: z.into(),
model: m.into(),
color: [
color.r as f32,
color.g as f32,

Loading…
Cancel
Save