Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ amethyst = ["amethyst_core"]

[dependencies]
log = "0.4.6"
specs = "0.15.0"
specs-hierarchy = { git = "https://github.com/cedric-h/specs-hierarchy.git", branch = "update-specs" }
specs = "0.16.0"
specs-hierarchy = "0.6.0"
shrev = "1.1.1"
nalgebra = "0.18.0"
ncollide3d = "0.19"
nphysics3d = "0.11.1"
nalgebra = "0.19.0"
ncollide3d = "0.21.0"
nphysics3d = "0.13.1"
amethyst_core = { git = "https://github.com/amethyst/amethyst", optional = true }
objekt = "0.1.2"

Expand All @@ -52,4 +52,3 @@ path = "examples/collision.rs"
[[example]]
name = "events"
path = "examples/events.rs"

32 changes: 32 additions & 0 deletions src/amethyst.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use amethyst_core::{SystemBundle, Transform};
use amethyst_error::Error;
use specs::{DispatcherBuilder, World};

use crate::{nalgebra::Isometry3, register_physics_systems, Position};

impl Position<f32> for Transform {
fn isometry(&self) -> &Isometry3<f32> {
self.isometry()
}

fn isometry_mut(&mut self) -> &mut Isometry3<f32> {
self.isometry_mut()
}

fn set_isometry(&mut self, isometry: &Isometry3<f32>) -> &mut Self {
self.set_isometry(*isometry)
}
}

pub struct PhysicsBundle;

impl<'a, 'b> SystemBundle<'a, 'b> for PhysicsBundle {
fn build(
self,
_world: &mut World,
dispatcher: &mut DispatcherBuilder<'a, 'b>,
) -> Result<(), Error> {
register_physics_systems::<f32, Transform>(dispatcher);
Ok(())
}
}
4 changes: 2 additions & 2 deletions src/bodies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
nalgebra::{Isometry3, Matrix3, Point3, RealField},
nphysics::{
algebra::{Force3, ForceType, Velocity3},
object::{Body, BodyHandle, BodyPart, BodyStatus, RigidBody, RigidBodyDesc},
object::{Body, BodyPart, BodyStatus, DefaultBodyHandle, RigidBody, RigidBodyDesc},
},
};

Expand Down Expand Up @@ -73,7 +73,7 @@ impl Position<f32> for amethyst_core::Transform {
/// both worlds.
#[derive(Clone, Copy, Debug)]
pub struct PhysicsBody<N: RealField> {
pub(crate) handle: Option<BodyHandle>,
pub(crate) handle: Option<DefaultBodyHandle>,
pub gravity_enabled: bool,
pub body_status: BodyStatus,
pub velocity: Velocity3<N>,
Expand Down
18 changes: 10 additions & 8 deletions src/colliders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use specs::{Component, DenseVecStorage, FlaggedStorage};
use crate::{
nalgebra::{DMatrix, Isometry3, Point2, Point3, RealField, Unit, Vector3},
ncollide::{
pipeline::CollisionGroups,
shape::{
Ball,
Capsule,
Expand All @@ -19,11 +20,10 @@ use crate::{
TriMesh,
Triangle,
},
world::CollisionGroups,
},
nphysics::{
material::{BasicMaterial, MaterialHandle},
object::ColliderHandle,
object::DefaultColliderHandle,
},
};

Expand Down Expand Up @@ -138,16 +138,18 @@ impl<N: RealField> Shape<N> {
#[derive(Clone)]
pub struct PhysicsCollider<N: RealField> {
/// The handle to the collider in the physics world.
pub(crate) handle: Option<ColliderHandle>,
pub(crate) handle: Option<DefaultColliderHandle>,
/// The shape of this collider.
pub shape: Shape<N>,
/// The position/rotation offset of the collider from the entity it is attached to.
/// The position/rotation offset of the collider from the entity it is
/// attached to.
pub offset_from_parent: Isometry3<N>,
pub density: N,
/// The physics material of which this collider is composed.
/// Defines properties like bounciness and others.
pub material: MaterialHandle<N>,
/// Margin between the detection zone of what is "near" the collider and the actual collider.
/// Margin between the detection zone of what is "near" the collider and the
/// actual collider.
pub margin: N,
/// Collision groups this collider is part of.
/// Defines with which other colliders this collider can interact.
Expand All @@ -156,8 +158,8 @@ pub struct PhysicsCollider<N: RealField> {
pub linear_prediction: N,
/// Prediction amount of the angular momentum.
pub angular_prediction: N,
/// Whether this collider is a sensor and only emits events without interacting (true) or
/// if it is a regular collider (false).
/// Whether this collider is a sensor and only emits events without
/// interacting (true) or if it is a regular collider (false).
pub sensor: bool,
}

Expand Down Expand Up @@ -210,7 +212,7 @@ impl<N: RealField> PhysicsCollider<N> {
/// use specs_physics::{
/// colliders::Shape,
/// nalgebra::{Isometry3, Vector3},
/// ncollide::world::CollisionGroups,
/// ncollide::pipeline::CollisionGroups,
/// nphysics::material::{BasicMaterial, MaterialHandle},
/// PhysicsColliderBuilder,
/// };
Expand Down
74 changes: 47 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
//! use specs_physics::{
//! colliders::Shape,
//! nalgebra::{Isometry3, Vector3},
//! ncollide::world::CollisionGroups,
//! ncollide::pipeline::CollisionGroups,
//! nphysics::material::{BasicMaterial, MaterialHandle},
//! PhysicsColliderBuilder,
//! };
Expand Down Expand Up @@ -151,11 +151,11 @@
//! `SyncBodiesToPhysicsSystem` as [Collider][] can depend on [RigidBody][].
//!
//! 3. `specs_physics::systems::SyncParametersToPhysicsSystem` - handles the
//! modification of the [nphysics][] `World`s parameters.
//! modification of the [nphysics][] `DefaultMechanicalWorld`s parameters.
//!
//! 4. `specs_physics::systems::PhysicsStepperSystem` - handles the progression
//! of the [nphysics][] `World` and causes objects to actually move and
//! change their position. This `System` is the backbone for collision
//! of the [nphysics][] `DefaultMechanicalWorld` and causes objects to actually
//! move and change their position. This `System` is the backbone for collision
//! detection.
//!
//! 5. `specs_physics::systems::SyncBodiesFromPhysicsSystem` -
Expand Down Expand Up @@ -215,9 +215,12 @@
//! arguments like so:
//!
//! ```
//! # #[cfg(feature = "amethyst")]
//! # {
//! use amethyst_core::Transform;
//! use specs_physics::systems::SyncBodiesToPhysicsSystem;
//! SyncBodiesToPhysicsSystem::<f32, Transform>::default();
//! # }
//! ```
//!
//! Alternatively to building your own `Dispatcher`, you can always fall back on
Expand Down Expand Up @@ -267,10 +270,18 @@ use self::{
nalgebra::{RealField, Vector3},
nphysics::{
counters::Counters,
force_generator::DefaultForceGeneratorSet,
joint::DefaultJointConstraintSet,
material::MaterialsCoefficientsTable,
object::{BodyHandle, ColliderHandle},
object::{
DefaultBodyHandle,
DefaultBodySet,
DefaultColliderHandle,
DefaultColliderSet,
Ground,
},
solver::IntegrationParameters,
world::World,
world::{DefaultGeometricalWorld, DefaultMechanicalWorld},
},
systems::{
PhysicsStepperSystem,
Expand All @@ -281,25 +292,31 @@ use self::{
},
};

#[cfg(feature = "amethyst")]
pub mod amethyst;
pub mod bodies;
pub mod colliders;
pub mod events;
pub mod parameters;
pub mod systems;

/// Resource holding the internal fields where physics computation occurs.
/// Some inspection methods are exposed to allow debugging.
pub struct Physics<N: RealField> {
/// Core structure where physics computation and synchronization occurs.
/// Also contains ColliderWorld.
pub(crate) world: World<N>,
pub(crate) mechanical_world: DefaultMechanicalWorld<N>,
pub(crate) geometrical_world: DefaultGeometricalWorld<N>,
pub(crate) bodies: DefaultBodySet<N>,
pub(crate) colliders: DefaultColliderSet<N>,
pub(crate) joint_constraints: DefaultJointConstraintSet<N>,
pub(crate) force_generators: DefaultForceGeneratorSet<N>,
pub(crate) ground: DefaultBodyHandle,

/// Hashmap of Entities to internal Physics bodies.
/// Necessary for reacting to removed Components.
pub(crate) body_handles: HashMap<Index, BodyHandle>,
pub(crate) body_handles: HashMap<Index, DefaultBodyHandle>,
/// Hashmap of Entities to internal Collider handles.
/// Necessary for reacting to removed Components.
pub(crate) collider_handles: HashMap<Index, ColliderHandle>,
pub(crate) collider_handles: HashMap<Index, DefaultColliderHandle>,
}

// Some non-mutating methods for diagnostics and testing
Expand All @@ -312,45 +329,47 @@ impl<N: RealField> Physics<N> {
/// Reports the internal value for the timestep.
/// See also `TimeStep` for setting this value.
pub fn timestep(&self) -> N {
self.world.timestep()
self.mechanical_world.timestep()
}

/// Reports the internal value for the gravity.
/// See also `Gravity` for setting this value.
pub fn gravity(&self) -> &Vector3<N> {
self.world.gravity()
}

/// Reports the internal value for prediction distance in collision
/// detection. This cannot change and will normally be `0.002m`
pub fn prediction(&self) -> N {
self.world.prediction()
&self.mechanical_world.gravity
}

/// Retrieves the performance statistics for the last simulated timestep.
/// Profiling is disabled by default.
/// See also `PhysicsProfilingEnabled` for enabling performance counters.
pub fn performance_counters(&self) -> &Counters {
self.world.performance_counters()
&self.mechanical_world.counters
}

/// Retrieves the internal parameters for integration.
/// See also `PhysicsIntegrationParameters` for setting these parameters.
pub fn integration_parameters(&self) -> &IntegrationParameters<N> {
self.world.integration_parameters()
&self.mechanical_world.integration_parameters
}

/// Retrieves the internal lookup table for friction and restitution
/// constants. Exposing this for modification is TODO.
pub fn materials_coefficients_table(&self) -> &MaterialsCoefficientsTable<N> {
self.world.materials_coefficients_table()
&self.mechanical_world.material_coefficients
}
}

impl<N: RealField> Default for Physics<N> {
fn default() -> Self {
let mut bodies = DefaultBodySet::new();
let ground = bodies.insert(Ground::new());
Self {
world: World::new(),
mechanical_world: DefaultMechanicalWorld::new(Vector3::zeros()),
geometrical_world: DefaultGeometricalWorld::new(),
bodies,
ground,
colliders: DefaultColliderSet::new(),
joint_constraints: DefaultJointConstraintSet::new(),
force_generators: DefaultForceGeneratorSet::new(),
body_handles: HashMap::new(),
collider_handles: HashMap::new(),
}
Expand Down Expand Up @@ -427,8 +446,9 @@ where
);

// add PhysicsStepperSystem after all other Systems that write data to the
// nphysics World and has to depend on them; this System is used to progress the
// nphysics World for all existing objects
// nphysics DefaultMechanicalWorld and has to depend on them; this System is
// used to progress the nphysics DefaultMechanicalWorld for all existing
// objects
dispatcher_builder.add(
PhysicsStepperSystem::<N>::default(),
"physics_stepper_system",
Expand All @@ -440,8 +460,8 @@ where
);

// add SyncBodiesFromPhysicsSystem last as it handles the
// synchronisation between nphysics World bodies and the Position
// components; this depends on the PhysicsStepperSystem
// synchronisation between nphysics DefaultMechanicalWorld bodies and the
// Position components; this depends on the PhysicsStepperSystem
dispatcher_builder.add(
SyncBodiesFromPhysicsSystem::<N, P>::default(),
"sync_bodies_from_physics_system",
Expand Down
Loading