Skip to content

Commit

Permalink
Switch to property-based force application.
Browse files Browse the repository at this point in the history
  • Loading branch information
distransient committed Dec 17, 2018
1 parent beb2c22 commit 20d676a
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 190 deletions.
3 changes: 1 addition & 2 deletions examples/amethyst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use amethyst::renderer::{
};
use amethyst::{Application, GameData, GameDataBuilder, SimpleState, StateData};
use nphysics_ecs_dumb::bodies::DynamicBody;
use nphysics_ecs_dumb::forces::DefaultForceGenerators;
use nphysics_ecs_dumb::nphysics::math::{Point, Velocity};
use nphysics_ecs_dumb::systems::PhysicsBundle;
use num_traits::identities::One;
Expand Down Expand Up @@ -111,7 +110,7 @@ fn main() -> amethyst::Result<()> {
let game_data = GameDataBuilder::default()
.with_bundle(TransformBundle::new())?
.with_bundle(
PhysicsBundle::<DefaultForceGenerators>::new().with_dep(&["transform_system"]),
PhysicsBundle::new().with_dep(&["transform_system"]),
)?
.with_bundle(RenderBundle::new(pipe, Some(display_config)))?;

Expand Down
5 changes: 4 additions & 1 deletion src/bodies.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use amethyst::ecs::world::Index;
use amethyst::ecs::{Component, FlaggedStorage};
use nalgebra::Matrix3;
use nphysics3d::math::{Point, Velocity};
use nphysics3d::math::{Point, Velocity, Force};
use nphysics3d::object::BodyHandle;
use std::collections::HashMap;

Expand Down Expand Up @@ -37,6 +37,7 @@ impl DynamicBody {
mass,
angular_mass,
center_of_mass,
external_forces: Force::<f32>::zero(),
})
}

Expand All @@ -63,6 +64,8 @@ pub struct RigidPhysicsBody {
pub mass: f32,
pub angular_mass: Matrix3<f32>,
pub center_of_mass: Point<f32>,

pub external_forces: Force<f32>,
}

/// Multipart physics body, for use in `PhysicsBody` Component. Not implemented yet.
Expand Down
113 changes: 0 additions & 113 deletions src/forces.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub extern crate nphysics3d as nphysics;
extern crate num_traits;

pub mod bodies;
pub mod forces;
pub mod systems;

pub type World = self::nphysics::world::World<f32>;
Expand Down
27 changes: 3 additions & 24 deletions src/systems/mod.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,30 @@
mod physics_stepper;
mod sync_bodies_from_physics;
mod sync_bodies_to_physics;
mod sync_force_generators_to_physics;
mod sync_gravity_to_physics;

use crate::forces::ForceGenerators;
use amethyst::core::bundle::{Result, SystemBundle};
use amethyst::core::specs::DispatcherBuilder;
use core::marker::PhantomData;

pub use self::physics_stepper::PhysicsStepperSystem;
pub use self::sync_bodies_from_physics::SyncBodiesFromPhysicsSystem;
pub use self::sync_bodies_to_physics::SyncBodiesToPhysicsSystem;
pub use self::sync_force_generators_to_physics::SyncForceGeneratorsToPhysicsSystem;
pub use self::sync_gravity_to_physics::SyncGravityToPhysicsSystem;

// TODO: Implement contact events, use Entity id's instead of nphysics handles.
// contact_events.iter_write(physical_world.contact_events());

pub const SYNC_FORCE_GENERATORS_TO_PHYSICS_SYSTEM: &str = "sync_force_generators_to_physics_system";
pub const SYNC_BODIES_TO_PHYSICS_SYSTEM: &str = "sync_bodies_to_physics_system";
pub const SYNC_GRAVITY_TO_PHYSICS_SYSTEM: &str = "sync_gravity_to_physics_system";
pub const PHYSICS_STEPPER_SYSTEM: &str = "physics_stepper_system";
pub const SYNC_BODIES_FROM_PHYSICS_SYSTEM: &str = "sync_bodies_from_physics_system";

#[derive(Default)]
pub struct PhysicsBundle<'a, F>
where
F: ForceGenerators,
{
pub struct PhysicsBundle<'a> {
dep: &'a [&'a str],
phantom_force_generators: PhantomData<F>,
}

impl<'a, F> PhysicsBundle<'a, F>
where
F: ForceGenerators,
{
impl<'a> PhysicsBundle<'a> {
pub fn new() -> Self {
Default::default()
}
Expand All @@ -47,16 +35,8 @@ where
}
}

impl<'a, 'b, 'c, F: 'a> SystemBundle<'a, 'b> for PhysicsBundle<'c, F>
where
F: ForceGenerators,
{
impl<'a, 'b, 'c> SystemBundle<'a, 'b> for PhysicsBundle<'c> {
fn build(self, builder: &mut DispatcherBuilder<'a, 'b>) -> Result<()> {
builder.add(
SyncForceGeneratorsToPhysicsSystem::<F>::new(),
SYNC_FORCE_GENERATORS_TO_PHYSICS_SYSTEM,
self.dep,
);
builder.add(
SyncBodiesToPhysicsSystem::new(),
SYNC_BODIES_TO_PHYSICS_SYSTEM,
Expand All @@ -72,7 +52,6 @@ where
PhysicsStepperSystem::new(),
PHYSICS_STEPPER_SYSTEM,
&[
SYNC_FORCE_GENERATORS_TO_PHYSICS_SYSTEM,
SYNC_BODIES_TO_PHYSICS_SYSTEM,
SYNC_GRAVITY_TO_PHYSICS_SYSTEM,
],
Expand Down
2 changes: 2 additions & 0 deletions src/systems/sync_bodies_to_physics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl<'a> System<'a> for SyncBodiesToPhysicsSystem {
.unwrap();

physical_body.set_velocity(rigid_body.velocity);
physical_body.apply_force(&rigid_body.external_forces);
}
DynamicBody::Multibody(_) => {
// TODO
Expand All @@ -112,6 +113,7 @@ impl<'a> System<'a> for SyncBodiesToPhysicsSystem {

physical_body.set_position(try_convert(transform.0).unwrap());
physical_body.set_velocity(rigid_body.velocity);
physical_body.apply_force(&rigid_body.external_forces);

// if you changed the mass properties at all... too bad!
}
Expand Down
49 changes: 0 additions & 49 deletions src/systems/sync_force_generators_to_physics.rs

This file was deleted.

0 comments on commit 20d676a

Please sign in to comment.