Physics Support
- NEW CRATE: raidillon_physics. - Added new models to be able to test the physics support. - Added a new system "PhysicsSystem" to apply physics calculations to the ECS world. - NEW COMPONENT: RigidBodyComponent
This commit is contained in:
parent
4b97bd98d2
commit
db1b427e2a
13 changed files with 697 additions and 43 deletions
34
game/src/systems/physics.rs
Normal file
34
game/src/systems/physics.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use raidillon_core::scene::Scene;
|
||||
use raidillon_ecs::components::RigidBodyComponent;
|
||||
use raidillon_ecs::Transform;
|
||||
use raidillon_engine::EngineResources;
|
||||
use raidillon_engine::system::System;
|
||||
use raidillon_physics::Physics;
|
||||
use raidillon_platform::PlatformContext;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct PhysicsSystem;
|
||||
|
||||
impl System for PhysicsSystem {
|
||||
fn load_world(&mut self, res: &mut EngineResources, scene: &mut Scene) {
|
||||
let p = Physics::default();
|
||||
res.insert(p);
|
||||
}
|
||||
|
||||
fn fixed_update(&mut self, res: &mut EngineResources, scene: &mut Scene) {
|
||||
let pctx = res.get::<PlatformContext>().expect("PlatformContext missing").clone();
|
||||
let physics = res.get_mut::<Physics>().expect("Physics missing");
|
||||
physics.step(pctx.time_ctx.fixed_dt);
|
||||
|
||||
let mut query = scene.world.query::<(&mut Transform, &RigidBodyComponent)>();
|
||||
for (_ent, (tr, rb_component)) in query.iter() {
|
||||
if let Some(body) = physics.get_rigid_body(rb_component.0) {
|
||||
let pos = body.position();
|
||||
let translation = Physics::rapier_translation_to_glam(&pos.translation.vector);
|
||||
let rotation = Physics::rapier_rotation_to_glam(&pos.rotation);
|
||||
tr.translation = translation;
|
||||
tr.rotation = rotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue