specs-physics

[Fork] Integration of Amethyst, SPECS and Nphysics together.
git clone https://git.jojolepro.com/specs-physics.git
Log | Files | Refs | README | LICENSE

commit 3219ceccd2c0648d93542a28ad08766e906ca76f
parent bab0ab3ccb599c700ca47642a9eeb742ac603dc2
Author: bamlin <b.amling@tarent.de>
Date:   Sat,  1 Jun 2019 15:06:14 +0200

only update timestep when values differ; also added another convenience function for registering systems to a given dispatcher builder

Diffstat:
MCargo.toml | 2+-
Mexamples/basic.rs | 4++--
Mexamples/hierarchy.rs | 6++++--
Mexamples/positions.rs | 4++--
Msrc/lib.rs | 19++++++++++++++-----
Msrc/systems/physics_stepper.rs | 11++++++++++-
6 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "specs-physics" -version = "0.1.0" +version = "0.1.1" authors = ["Benjamin Amling <benjamin@amling.net>"] repository = "https://github.com/bamling/specs-physics.git" homepage = "https://github.com/bamling/specs-physics.git" diff --git a/examples/basic.rs b/examples/basic.rs @@ -4,7 +4,7 @@ extern crate simple_logger; use specs::{world::Builder, Component, DenseVecStorage, FlaggedStorage, World}; use specs_physics::{ body::{BodyStatus, Position}, - dispatcher, + physics_dispatcher, PhysicsBodyBuilder, PhysicsColliderBuilder, Shape, @@ -43,7 +43,7 @@ fn main() { // create the dispatcher containing all relevant Systems; alternatively to using // the convenience function you can add all required Systems by hand - let mut dispatcher = dispatcher::<f32, Pos>(); + let mut dispatcher = physics_dispatcher::<f32, Pos>(); dispatcher.setup(&mut world.res); // create an Entity containing the required Components diff --git a/examples/hierarchy.rs b/examples/hierarchy.rs @@ -4,9 +4,10 @@ extern crate simple_logger; use specs::{world::Builder, Component, DenseVecStorage, FlaggedStorage, World}; use specs_physics::{ body::{BodyStatus, Position}, - dispatcher, + physics_dispatcher, PhysicsBodyBuilder, PhysicsColliderBuilder, + PhysicsParent, Shape, }; @@ -43,7 +44,7 @@ fn main() { // create the dispatcher containing all relevant Systems; alternatively to using // the convenience function you can add all required Systems by hand - let mut dispatcher = dispatcher::<f32, Pos>(); + let mut dispatcher = physics_dispatcher::<f32, Pos>(); dispatcher.setup(&mut world.res); // create an Entity containing the required Components; this Entity will be the @@ -75,6 +76,7 @@ fn main() { .sensor(true) .build(), ) + .with(PhysicsParent { entity: parent }) .build(); // execute the dispatcher diff --git a/examples/positions.rs b/examples/positions.rs @@ -6,7 +6,7 @@ use nalgebra::Vector3; use specs::{world::Builder, Component, DenseVecStorage, FlaggedStorage, World}; use specs_physics::{ body::{BodyStatus, Position}, - dispatcher, + physics_dispatcher, PhysicsBodyBuilder, PhysicsColliderBuilder, Shape, @@ -45,7 +45,7 @@ fn main() { // create the dispatcher containing all relevant Systems; alternatively to using // the convenience function you can add all required Systems by hand - let mut dispatcher = dispatcher::<f32, Pos>(); + let mut dispatcher = physics_dispatcher::<f32, Pos>(); dispatcher.setup(&mut world.res); // create an Entity containing the required Components; for this examples sake diff --git a/src/lib.rs b/src/lib.rs @@ -89,15 +89,26 @@ impl Parent for PhysicsParent { } /// Convenience function for configuring and building a `Dispatcher` with all -/// required physics related `System`s. This also serves as a blueprint on how -/// to properly set up the `System`s and have them depend on each other. -pub fn dispatcher<'a, 'b, N, P>() -> Dispatcher<'a, 'b> +/// required physics related `System`s. +pub fn physics_dispatcher<'a, 'b, N, P>() -> Dispatcher<'a, 'b> where N: RealField, P: Component<Storage = FlaggedStorage<P, DenseVecStorage<P>>> + Position<N> + Send + Sync, { let mut dispatcher_builder = DispatcherBuilder::new(); + register_physics_systems::<N, P>(&mut dispatcher_builder); + dispatcher_builder.build() +} + +/// Convenience function for registering all required physics related `System`s +/// to the given `DispatcherBuilder`. This also serves as a blueprint on how +///// to properly set up the `System`s and have them depend on each other. +pub fn register_physics_systems<N, P>(dispatcher_builder: &mut DispatcherBuilder) +where + N: RealField, + P: Component<Storage = FlaggedStorage<P, DenseVecStorage<P>>> + Position<N> + Send + Sync, +{ // add SyncBodiesToPhysicsSystem first since we have to start with bodies; // colliders can exist without a body but in most cases have a body parent dispatcher_builder.add( @@ -144,6 +155,4 @@ where "sync_positions_from_physics_system", &["physics_stepper_system"], ); - - dispatcher_builder.build() } diff --git a/src/systems/physics_stepper.rs b/src/systems/physics_stepper.rs @@ -19,7 +19,16 @@ impl<'s, N: RealField> System<'s> for PhysicsStepperSystem<N> { // accordingly; this should not be required if the Systems are executed in a // fixed interval if let Some(time_step) = time_step { - physics.world.set_timestep(time_step.0); + // only update timestep if it actually differs from the current nphysics World + // one; keep in mind that changing the Resource will destabilize the simulation + if physics.world.timestep() != time_step.0 { + warn!( + "TimeStep and world.timestep() differ, changing worlds timestep from {} to: {:?}", + physics.world.timestep(), + time_step.0 + ); + physics.world.set_timestep(time_step.0); + } } physics.world.step();