rotation from physics
This commit is contained in:
3212
resources/ball.obj
3212
resources/ball.obj
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::ColliderHandle;
|
||||||
use rapier3d::geometry::Collider as r3dCollider;
|
use rapier3d::geometry::Collider as r3dCollider;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
use cgmath::Deg;
|
||||||
|
|
||||||
// a component is any type that is 'static, sized, send and sync
|
// a component is any type that is 'static, sized, send and sync
|
||||||
|
|
||||||
@@ -19,7 +20,7 @@ pub struct Position {
|
|||||||
pub x: f32,
|
pub x: f32,
|
||||||
pub y: f32,
|
pub y: f32,
|
||||||
pub z: f32,
|
pub z: f32,
|
||||||
pub rot: cgmath::Quaternion<f32>,
|
pub rot: cgmath::Euler<Deg<f32>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
|||||||
26
src/main.rs
26
src/main.rs
@@ -124,7 +124,7 @@ fn main() {
|
|||||||
resources.insert(renderer);
|
resources.insert(renderer);
|
||||||
|
|
||||||
let (physics_state, physics_pipeline) =
|
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_state);
|
||||||
resources.insert(physics_pipeline);
|
resources.insert(physics_pipeline);
|
||||||
|
|
||||||
@@ -289,11 +289,11 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
|
|||||||
x: 1.0,
|
x: 1.0,
|
||||||
y: 5.0,
|
y: 5.0,
|
||||||
z: 2.0,
|
z: 2.0,
|
||||||
rot: Quaternion::from(Euler {
|
rot: Euler {
|
||||||
x: Deg(90.0),
|
x: Deg(90.0),
|
||||||
y: Deg(45.0),
|
y: Deg(45.0),
|
||||||
z: Deg(15.0),
|
z: Deg(15.0),
|
||||||
}), //mx: cgmath::Matrix4::from(transform),
|
}, //mx: cgmath::Matrix4::from(transform),
|
||||||
},
|
},
|
||||||
monkey_mesh,
|
monkey_mesh,
|
||||||
Color {
|
Color {
|
||||||
@@ -306,15 +306,15 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
|
|||||||
|
|
||||||
let mut dynamic_ball_body = RigidBodyBuilder::new_dynamic()
|
let mut dynamic_ball_body = RigidBodyBuilder::new_dynamic()
|
||||||
.position(Isometry3::new(
|
.position(Isometry3::new(
|
||||||
Vector3::new(0.0, 0.0, 5.0),
|
Vector3::new(0.0, 0.0, 0.0),
|
||||||
Vector::y() * PI,
|
Vector::y(),
|
||||||
))
|
))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let mut static_floor_body = RigidBodyBuilder::new_static()
|
let mut static_floor_body = RigidBodyBuilder::new_static()
|
||||||
.position(Isometry3::new(
|
.position(Isometry3::new(
|
||||||
Vector3::new(0.0, -8.0, 0.0),
|
Vector3::new(0.0, -8.0, 0.0),
|
||||||
Vector::y() * PI,
|
Vector::y(),
|
||||||
))
|
))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@@ -328,11 +328,11 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
|
|||||||
x: 0.0,
|
x: 0.0,
|
||||||
y: -8.0,
|
y: -8.0,
|
||||||
z: 0.0,
|
z: 0.0,
|
||||||
rot: Quaternion::from(Euler {
|
rot: Euler {
|
||||||
x: Deg(0.0),
|
x: Deg(0.0),
|
||||||
y: Deg(0.0),
|
y: Deg(0.0),
|
||||||
z: Deg(0.0),
|
z: Deg(0.0),
|
||||||
}),
|
},
|
||||||
},
|
},
|
||||||
plane_mesh,
|
plane_mesh,
|
||||||
Color {
|
Color {
|
||||||
@@ -355,14 +355,14 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
|
|||||||
|
|
||||||
let ball_mesh: Entity = world.push((
|
let ball_mesh: Entity = world.push((
|
||||||
Position {
|
Position {
|
||||||
x: 2.0,
|
x: 0.0,
|
||||||
y: 2.0,
|
y: 0.0,
|
||||||
z: 3.0,
|
z: 0.0,
|
||||||
rot: Quaternion::from(Euler {
|
rot: Euler {
|
||||||
x: Deg(25.0),
|
x: Deg(25.0),
|
||||||
y: Deg(45.0),
|
y: Deg(45.0),
|
||||||
z: Deg(15.0),
|
z: Deg(15.0),
|
||||||
}),
|
},
|
||||||
},
|
},
|
||||||
ball_mesh,
|
ball_mesh,
|
||||||
Color {
|
Color {
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|||||||
@@ -87,6 +87,8 @@ pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) {
|
|||||||
|
|
||||||
let frame = renderer.get_current_frame();
|
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();
|
let mut query = <(&mut Camera)>::query();
|
||||||
for (camera) in query.iter_mut(world) {
|
for (camera) in query.iter_mut(world) {
|
||||||
let matrix = camera.calc_matrix(renderer.camera_projection);
|
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
|
// Update the entity uniforms
|
||||||
for (pos, mesh, color) in query.iter_mut(world) {
|
for (pos, mesh, color) in query.iter_mut(world) {
|
||||||
// let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(2.0));
|
let d = Decomposed {
|
||||||
// pos.mx = pos.mx * rotation;
|
scale: 1.0,
|
||||||
|
rot: Quaternion::from(pos.rot),
|
||||||
//let p = cgmath::Matrix4::from(pos.rot);
|
disp: Vector3::new(pos.x, pos.y, pos.z)
|
||||||
let q = cgmath::Matrix4::from_translation(Vector3::new(pos.x, pos.y, pos.z));
|
};
|
||||||
|
let m = Matrix4::from(d);
|
||||||
let z = q; // p + q;
|
|
||||||
|
|
||||||
let data = EntityUniforms {
|
let data = EntityUniforms {
|
||||||
model: z.into(),
|
model: m.into(),
|
||||||
color: [
|
color: [
|
||||||
color.r as f32,
|
color.r as f32,
|
||||||
color.g as f32,
|
color.g as f32,
|
||||||
|
|||||||
Reference in New Issue
Block a user