commit c50bb7eb1edd29f7fefe5b205cd30a7039d3ea9a
parent 953e4e3cae1af90d0c838698c56e649381bd567b
Author: kel <distransient@protonmail.com>
Date: Wed, 28 Nov 2018 15:02:51 -0500
rustfmt
Diffstat:
M | .gitignore | | | 1 | + |
M | src/lib.rs | | | 152 | ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
2 files changed, 93 insertions(+), 60 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,4 +1,5 @@
/target
**/*.rs.bk
*.iml
+/.idea
cargo.lock
\ No newline at end of file
diff --git a/src/lib.rs b/src/lib.rs
@@ -1,15 +1,15 @@
extern crate amethyst;
-pub extern crate nphysics3d as nphysics;
pub extern crate ncollide3d as ncollide;
+pub extern crate nphysics3d as nphysics;
use self::ncollide::events::ContactEvent;
-use self::nphysics::math::{Inertia, Point, Velocity, Vector};
+use self::nphysics::math::{Inertia, Point, Vector, Velocity};
use self::nphysics::object::{Body, BodyHandle, RigidBody};
-use amethyst::ecs::*;
-use amethyst::ecs::prelude::*;
-use amethyst::core::{GlobalTransform, Time};
-use amethyst::core::nalgebra::try_convert;
use amethyst::core::nalgebra::base::Matrix3;
+use amethyst::core::nalgebra::try_convert;
+use amethyst::core::{GlobalTransform, Time};
+use amethyst::ecs::prelude::*;
+use amethyst::ecs::*;
use amethyst::shrev::EventChannel;
pub type World = self::nphysics::world::World<f32>;
@@ -74,7 +74,7 @@ pub struct Dumb3dPhysicsSystem {
physics_bodies_reader_id: Option<ReaderId<ComponentEvent>>,
}
-impl <'a> System<'a> for Dumb3dPhysicsSystem {
+impl<'a> System<'a> for Dumb3dPhysicsSystem {
type SystemData = (
WriteExpect<'a, World>,
Write<'a, EventChannel<ContactEvent>>,
@@ -102,32 +102,48 @@ impl <'a> System<'a> for Dumb3dPhysicsSystem {
// Get change flag events for transforms, removing deleted ones from the physics world.
{
- let events = transforms.channel().read(
- &mut self.transforms_reader_id.as_mut().unwrap());
+ let events = transforms
+ .channel()
+ .read(&mut self.transforms_reader_id.as_mut().unwrap());
for event in events {
match event {
- ComponentEvent::Modified(id) => { self.modified_transforms.add(*id); },
- ComponentEvent::Inserted(id) => { self.inserted_transforms.add(*id); },
+ ComponentEvent::Modified(id) => {
+ self.modified_transforms.add(*id);
+ }
+ ComponentEvent::Inserted(id) => {
+ self.inserted_transforms.add(*id);
+ }
ComponentEvent::Removed(id) => {
- physical_world.remove_bodies(&[
- physics_bodies.get(entities.entity(*id)).unwrap().handle().unwrap() ]);
- },
+ physical_world.remove_bodies(&[physics_bodies
+ .get(entities.entity(*id))
+ .unwrap()
+ .handle()
+ .unwrap()]);
+ }
};
}
}
// Get change flag events for physics bodies, removing deleted ones from the physics world.
{
- let events = physics_bodies.channel().read(
- &mut self.physics_bodies_reader_id.as_mut().unwrap());
+ let events = physics_bodies
+ .channel()
+ .read(&mut self.physics_bodies_reader_id.as_mut().unwrap());
for event in events {
match event {
- ComponentEvent::Modified(id) => { self.modified_physics_bodies.add(*id); },
- ComponentEvent::Inserted(id) => { self.inserted_physics_bodies.add(*id); },
+ ComponentEvent::Modified(id) => {
+ self.modified_physics_bodies.add(*id);
+ }
+ ComponentEvent::Inserted(id) => {
+ self.inserted_physics_bodies.add(*id);
+ }
ComponentEvent::Removed(id) => {
- physical_world.remove_bodies(&[
- physics_bodies.get(entities.entity(*id)).unwrap().handle().unwrap() ]);
- },
+ physical_world.remove_bodies(&[physics_bodies
+ .get(entities.entity(*id))
+ .unwrap()
+ .handle()
+ .unwrap()]);
+ }
};
}
}
@@ -140,53 +156,60 @@ impl <'a> System<'a> for Dumb3dPhysicsSystem {
&self.modified_transforms
| &self.inserted_transforms
| &self.modified_physics_bodies
- | &self.inserted_physics_bodies
- ).join() {
-
- if self.inserted_transforms.contains(id)
- || self.inserted_physics_bodies.contains(id) {
+ | &self.inserted_physics_bodies,
+ )
+ .join()
+ {
+ if self.inserted_transforms.contains(id) || self.inserted_physics_bodies.contains(id) {
match body {
PhysicsBody::RigidBody(ref mut rigid_body) => {
if rigid_body.handle.is_some()
- && physical_world.rigid_body(rigid_body.handle.unwrap()).is_some() {
- physical_world.remove_bodies(&[ rigid_body.handle.unwrap() ]);
+ && physical_world
+ .rigid_body(rigid_body.handle.unwrap())
+ .is_some()
+ {
+ physical_world.remove_bodies(&[rigid_body.handle.unwrap()]);
}
rigid_body.handle = Some(physical_world.add_rigid_body(
try_convert(transform.0).unwrap(),
Inertia::new(rigid_body.mass, rigid_body.angular_mass),
- rigid_body.center_of_mass));
+ rigid_body.center_of_mass,
+ ));
- let mut physical_body = physical_world.rigid_body_mut(
- rigid_body.handle.unwrap()).unwrap();
+ let mut physical_body = physical_world
+ .rigid_body_mut(rigid_body.handle.unwrap())
+ .unwrap();
physical_body.set_velocity(rigid_body.velocity);
- },
+ }
PhysicsBody::Multibody(x) => {
// TODO
- },
+ }
PhysicsBody::Ground(x) => {
// TODO
- },
+ }
}
} else if self.modified_transforms.contains(id)
- || self.modified_physics_bodies.contains(id) {
+ || self.modified_physics_bodies.contains(id)
+ {
match body {
PhysicsBody::RigidBody(ref mut rigid_body) => {
- let mut physical_body = physical_world.rigid_body_mut(
- rigid_body.handle.unwrap()).unwrap();
+ let mut physical_body = physical_world
+ .rigid_body_mut(rigid_body.handle.unwrap())
+ .unwrap();
physical_body.set_position(try_convert(transform.0).unwrap());
physical_body.set_velocity(rigid_body.velocity);
// if you changed the mass properties at all... too bad!
- },
+ }
PhysicsBody::Multibody(x) => {
// TODO
- },
+ }
PhysicsBody::Ground(x) => {
// TODO
- },
+ }
}
}
}
@@ -200,7 +223,7 @@ impl <'a> System<'a> for Dumb3dPhysicsSystem {
//contact_events.iter_write(physical_world.contact_events());
// Apply the updated values of the simulated world to our Components
- for (mut transform, mut body) in (&mut transforms,&mut physics_bodies).join() {
+ for (mut transform, mut body) in (&mut transforms, &mut physics_bodies).join() {
let updated_body = physical_world.body(body.handle().unwrap());
if updated_body.is_ground() || !updated_body.is_active() || updated_body.is_static() {
@@ -208,14 +231,17 @@ impl <'a> System<'a> for Dumb3dPhysicsSystem {
}
match (body, updated_body) {
- (PhysicsBody::RigidBody(ref mut rigid_body), Body::RigidBody(ref updated_rigid_body)) => {
+ (
+ PhysicsBody::RigidBody(ref mut rigid_body),
+ Body::RigidBody(ref updated_rigid_body),
+ ) => {
updated_rigid_body.position();
rigid_body.velocity = *updated_rigid_body.velocity();
let inertia = updated_rigid_body.inertia();
rigid_body.mass = inertia.linear;
rigid_body.angular_mass = inertia.angular;
rigid_body.center_of_mass = updated_rigid_body.center_of_mass();
- },
+ }
(PhysicsBody::Multibody(multibody), Body::Multibody(updated_multibody)) => {
match updated_multibody.links().next() {
Some(link) => link.position(),
@@ -234,7 +260,8 @@ impl <'a> System<'a> for Dumb3dPhysicsSystem {
fn setup(&mut self, res: &mut Resources) {
Self::SystemData::setup(res);
- res.entry::<Gravity>().or_insert_with(|| Gravity::new(0.0, -9.80665, 0.0));
+ res.entry::<Gravity>()
+ .or_insert_with(|| Gravity::new(0.0, -9.80665, 0.0));
res.entry::<World>().or_insert_with(|| World::new());
let mut transform_storage: WriteStorage<GlobalTransform> = SystemData::fetch(&res);
@@ -247,14 +274,14 @@ impl <'a> System<'a> for Dumb3dPhysicsSystem {
#[cfg(test)]
mod tests {
- use amethyst::{GameData, StateData, SimpleState, GameDataBuilder, Application};
+ use super::*;
use amethyst::assets::Handle;
+ use amethyst::core::nalgebra::Vector3;
+ use amethyst::core::transform::bundle::TransformBundle;
+ use amethyst::core::Transform;
use amethyst::prelude::Builder;
use amethyst::renderer::*;
- use amethyst::core::Transform;
- use amethyst::core::transform::bundle::TransformBundle;
- use amethyst::core::nalgebra::Vector3;
- use super::*;
+ use amethyst::{Application, GameData, GameDataBuilder, SimpleState, StateData};
struct GameState;
@@ -265,11 +292,14 @@ mod tests {
data.world.register::<Handle<Texture>>();
// Create a texture for using.
- let texture = data.world.read_resource::<amethyst::assets::Loader>()
+ let texture = data
+ .world
+ .read_resource::<amethyst::assets::Loader>()
.load_from_data::<Texture, ()>(
[170.0, 170.0, 255.0, 1.0].into(),
(),
- &data.world.read_resource());
+ &data.world.read_resource(),
+ );
// Get resolution of the screen.
let (x, y) = {
@@ -283,7 +313,8 @@ mod tests {
camera_transform.yaw_local(-3.142);
// Add Camera
- data.world.create_entity()
+ data.world
+ .create_entity()
.with(Camera::standard_3d(x, y))
.with(camera_transform)
.build();
@@ -291,8 +322,9 @@ mod tests {
// Add Light
data.world.add_resource(AmbientColor(Rgba::from([0.01; 3])));
- data.world.create_entity()
- .with(Light::Point(PointLight{
+ data.world
+ .create_entity()
+ .with(Light::Point(PointLight {
intensity: 3.0,
color: Rgba::white(),
radius: 5.0,
@@ -302,7 +334,8 @@ mod tests {
.build();
// Add Sphere (todo: add many, add rigidbodies and colliders)
- data.world.create_entity()
+ data.world
+ .create_entity()
.with(Shape::Sphere(32, 32).generate::<Vec<PosNormTex>>(None))
.with(texture)
.with(GlobalTransform::default())
@@ -318,12 +351,12 @@ mod tests {
.with_basic_renderer(
"./resources/display.ron",
DrawShaded::<PosNormTex>::new(),
- false)?
+ false,
+ )?
.with_bundle(TransformBundle::new())?
.with(Dumb3dPhysicsSystem::default(), "physics", &[]);
- let mut application
- = Application::new("./", GameState, game_data,);
+ let mut application = Application::new("./", GameState, game_data);
assert_eq!(application.is_ok(), true);
@@ -331,4 +364,4 @@ mod tests {
Ok(())
}
-}-
\ No newline at end of file
+}