Skip to content

Commit 1e75a04

Browse files
AnneKitsunetopokel
authored andcommitted
Falling sphere owo
1 parent e37e2af commit 1e75a04

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ edition = "2018"
1111
nphysics3d = "0.9"
1212
ncollide3d = "0.17"
1313
nalgebra = "0.16"
14+
num-traits = "0.2"
1415

1516
[dependencies.amethyst]
1617
git = "https://github.com/amethyst/amethyst"

src/lib.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
extern crate amethyst;
22
pub extern crate ncollide3d as ncollide;
33
pub extern crate nphysics3d as nphysics;
4+
extern crate num_traits;
45

56
use self::ncollide::events::ContactEvent;
67
use self::nphysics::math::{Inertia, Point, Vector, Velocity};
78
use self::nphysics::object::{Body, BodyHandle, RigidBody};
89
use amethyst::core::nalgebra::base::Matrix3;
910
use amethyst::core::nalgebra::try_convert;
10-
use amethyst::core::{GlobalTransform, Time};
11+
use amethyst::core::nalgebra::Vector3;
12+
use amethyst::core::{GlobalTransform, Time, Transform};
1113
use amethyst::ecs::prelude::*;
1214
use amethyst::ecs::*;
1315
use amethyst::shrev::EventChannel;
@@ -96,6 +98,7 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
9698
Entities<'a>,
9799
WriteStorage<'a, GlobalTransform>,
98100
WriteStorage<'a, PhysicsBody>,
101+
ReadStorage<'a, Transform>,
99102
);
100103

101104
fn run(&mut self, data: Self::SystemData) {
@@ -106,6 +109,7 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
106109
entities,
107110
mut transforms,
108111
mut physics_bodies,
112+
locals,
109113
) = data;
110114

111115
// Clear bitsets
@@ -150,6 +154,7 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
150154
}
151155
ComponentEvent::Inserted(id) => {
152156
self.inserted_physics_bodies.add(*id);
157+
println!("I'm in!");
153158
}
154159
ComponentEvent::Removed(id) => {
155160
physical_world.remove_bodies(&[physics_bodies
@@ -175,8 +180,10 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
175180
.join()
176181
{
177182
if self.inserted_transforms.contains(id) || self.inserted_physics_bodies.contains(id) {
183+
println!("heya I'm new here!");
178184
match body {
179185
PhysicsBody::RigidBody(ref mut rigid_body) => {
186+
// Just inserted. Remove old one and insert new.
180187
if rigid_body.handle.is_some()
181188
&& physical_world
182189
.rigid_body(rigid_body.handle.unwrap())
@@ -207,6 +214,7 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
207214
} else if self.modified_transforms.contains(id)
208215
|| self.modified_physics_bodies.contains(id)
209216
{
217+
println!("oh shit, I'm changed!");
210218
match body {
211219
PhysicsBody::RigidBody(ref mut rigid_body) => {
212220
let mut physical_body = physical_world
@@ -237,7 +245,7 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
237245
//contact_events.iter_write(physical_world.contact_events());
238246

239247
// Apply the updated values of the simulated world to our Components
240-
for (mut transform, mut body) in (&mut transforms, &mut physics_bodies).join() {
248+
for (mut transform, mut body, local) in (&mut transforms, &mut physics_bodies, locals.maybe()).join() {
241249
let updated_body = physical_world.body(body.handle().unwrap());
242250

243251
if updated_body.is_ground() || !updated_body.is_active() || updated_body.is_static() {
@@ -249,7 +257,11 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
249257
PhysicsBody::RigidBody(ref mut rigid_body),
250258
Body::RigidBody(ref updated_rigid_body),
251259
) => {
252-
updated_rigid_body.position();
260+
println!("super power: change mehhh! new pos: {:?}", updated_rigid_body.position());
261+
262+
// TODO: Might get rid of the scale!!!
263+
transform.0 = updated_rigid_body.position().to_homogeneous().prepend_nonuniform_scaling(local.map(|tr| tr.scale()).unwrap_or(&Vector3::new(1.0, 1.0, 1.0)));
264+
253265
rigid_body.velocity = *updated_rigid_body.velocity();
254266
let inertia = updated_rigid_body.inertia();
255267
rigid_body.mass = inertia.linear;
@@ -268,6 +280,14 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
268280
_ => {}
269281
};
270282
}
283+
284+
// Now that we changed them all, let's remove all those pesky events that were generated.
285+
transforms
286+
.channel()
287+
.read(&mut self.transforms_reader_id.as_mut().unwrap()).for_each(|_|());
288+
physics_bodies
289+
.channel()
290+
.read(&mut self.physics_bodies_reader_id.as_mut().unwrap()).for_each(|_|());
271291
}
272292

273293
// TODO: resources need set up here. Including initializing the physics world.
@@ -296,6 +316,7 @@ mod tests {
296316
use amethyst::prelude::*;
297317
use amethyst::renderer::*;
298318
use amethyst::{Application, GameData, GameDataBuilder, SimpleState, StateData};
319+
use num_traits::identities::One;
299320

300321
struct GameState;
301322

@@ -360,7 +381,11 @@ mod tests {
360381
.with(material)
361382
.with(Transform::from(Vector3::new(0.0, 0.0, -10.0)))
362383
.with(GlobalTransform::default())
363-
.with(PhysicsBody::new_rigidbody_with_velocity(Velocity::linear(0.0, 2.0, 0.0), 10.0, Matrix3::identity(), Point::zero()))
384+
.with(PhysicsBody::new_rigidbody_with_velocity(
385+
Velocity::linear(0.0, 2.0, 0.0),
386+
10.0,
387+
Matrix3::one(),
388+
Point::new(0.0, 0.0, 0.0)))
364389
.build();
365390
}
366391
}

0 commit comments

Comments
 (0)