1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use nalgebra::{Isometry3, RealField};
use specs::{
    Component,
    Join,
    ReadExpect,
    ReadStorage,
    Resources,
    System,
    SystemData,
    WriteStorage,
};
use std::marker::PhantomData;

use crate::{
    bodies::{PhysicsBody, Position},
    Physics,
};

/// The `SyncPositionsFromPhysicsSystem` synchronised the updated position of
/// the `RigidBody`s in the nphysics `World` with their Specs counterparts. This
/// affects the `Position` `Component` related to the `Entity`.
pub struct SyncPositionsFromPhysicsSystem<N, P> {
    n_marker: PhantomData<N>,
    p_marker: PhantomData<P>,
}

impl<'s, N, P> System<'s> for SyncPositionsFromPhysicsSystem<N, P>
where
    N: RealField,
    P: Component + Position<N> + Send + Sync,
{
    type SystemData = (
        ReadExpect<'s, Physics<N>>,
        ReadStorage<'s, PhysicsBody<N>>,
        WriteStorage<'s, P>,
    );

    fn run(&mut self, data: Self::SystemData) {
        let (physics, physics_bodies, mut positions) = data;

        // iterate over all PhysicBody components joined with their Positions
        for (physics_body, position) in (&physics_bodies, &mut positions).join() {
            // if a RigidBody exists in the nphysics World we fetch it and update the
            // Position component accordingly
            if let Some(rigid_body) = physics.world.rigid_body(physics_body.handle.unwrap()) {
                let isometry: &Isometry3<N> = rigid_body.position();

                position.set_position(
                    isometry.translation.vector.x,
                    isometry.translation.vector.y,
                    isometry.translation.vector.z,
                );
            }
        }
    }

    fn setup(&mut self, res: &mut Resources) {
        info!("SyncPositionsFromPhysicsSystem.setup");
        Self::SystemData::setup(res);

        // initialise required resources
        res.entry::<Physics<N>>().or_insert_with(Physics::default);
    }
}

impl<N, P> Default for SyncPositionsFromPhysicsSystem<N, P>
where
    N: RealField,
    P: Component + Position<N> + Send + Sync,
{
    fn default() -> Self {
        Self {
            n_marker: PhantomData,
            p_marker: PhantomData,
        }
    }
}