1
1
extern crate amethyst;
2
2
pub extern crate ncollide3d as ncollide;
3
3
pub extern crate nphysics3d as nphysics;
4
+ extern crate num_traits;
4
5
5
6
use self :: ncollide:: events:: ContactEvent ;
6
7
use self :: nphysics:: math:: { Inertia , Point , Vector , Velocity } ;
7
8
use self :: nphysics:: object:: { Body , BodyHandle , RigidBody } ;
8
9
use amethyst:: core:: nalgebra:: base:: Matrix3 ;
9
10
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 } ;
11
13
use amethyst:: ecs:: prelude:: * ;
12
14
use amethyst:: ecs:: * ;
13
15
use amethyst:: shrev:: EventChannel ;
@@ -96,6 +98,7 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
96
98
Entities < ' a > ,
97
99
WriteStorage < ' a , GlobalTransform > ,
98
100
WriteStorage < ' a , PhysicsBody > ,
101
+ ReadStorage < ' a , Transform > ,
99
102
) ;
100
103
101
104
fn run ( & mut self , data : Self :: SystemData ) {
@@ -106,6 +109,7 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
106
109
entities,
107
110
mut transforms,
108
111
mut physics_bodies,
112
+ locals,
109
113
) = data;
110
114
111
115
// Clear bitsets
@@ -150,6 +154,7 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
150
154
}
151
155
ComponentEvent :: Inserted ( id) => {
152
156
self . inserted_physics_bodies . add ( * id) ;
157
+ println ! ( "I'm in!" ) ;
153
158
}
154
159
ComponentEvent :: Removed ( id) => {
155
160
physical_world. remove_bodies ( & [ physics_bodies
@@ -175,8 +180,10 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
175
180
. join ( )
176
181
{
177
182
if self . inserted_transforms . contains ( id) || self . inserted_physics_bodies . contains ( id) {
183
+ println ! ( "heya I'm new here!" ) ;
178
184
match body {
179
185
PhysicsBody :: RigidBody ( ref mut rigid_body) => {
186
+ // Just inserted. Remove old one and insert new.
180
187
if rigid_body. handle . is_some ( )
181
188
&& physical_world
182
189
. rigid_body ( rigid_body. handle . unwrap ( ) )
@@ -207,6 +214,7 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
207
214
} else if self . modified_transforms . contains ( id)
208
215
|| self . modified_physics_bodies . contains ( id)
209
216
{
217
+ println ! ( "oh shit, I'm changed!" ) ;
210
218
match body {
211
219
PhysicsBody :: RigidBody ( ref mut rigid_body) => {
212
220
let mut physical_body = physical_world
@@ -237,7 +245,7 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
237
245
//contact_events.iter_write(physical_world.contact_events());
238
246
239
247
// 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 ( ) {
241
249
let updated_body = physical_world. body ( body. handle ( ) . unwrap ( ) ) ;
242
250
243
251
if updated_body. is_ground ( ) || !updated_body. is_active ( ) || updated_body. is_static ( ) {
@@ -249,7 +257,11 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
249
257
PhysicsBody :: RigidBody ( ref mut rigid_body) ,
250
258
Body :: RigidBody ( ref updated_rigid_body) ,
251
259
) => {
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
+
253
265
rigid_body. velocity = * updated_rigid_body. velocity ( ) ;
254
266
let inertia = updated_rigid_body. inertia ( ) ;
255
267
rigid_body. mass = inertia. linear ;
@@ -268,6 +280,14 @@ impl<'a> System<'a> for Dumb3dPhysicsSystem {
268
280
_ => { }
269
281
} ;
270
282
}
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 ( |_|( ) ) ;
271
291
}
272
292
273
293
// TODO: resources need set up here. Including initializing the physics world.
@@ -296,6 +316,7 @@ mod tests {
296
316
use amethyst:: prelude:: * ;
297
317
use amethyst:: renderer:: * ;
298
318
use amethyst:: { Application , GameData , GameDataBuilder , SimpleState , StateData } ;
319
+ use num_traits:: identities:: One ;
299
320
300
321
struct GameState ;
301
322
@@ -360,7 +381,11 @@ mod tests {
360
381
. with ( material)
361
382
. with ( Transform :: from ( Vector3 :: new ( 0.0 , 0.0 , -10.0 ) ) )
362
383
. 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 ) ) )
364
389
. build ( ) ;
365
390
}
366
391
}
0 commit comments