1
1
extern crate amethyst;
2
- pub extern crate nphysics3d as nphysics;
3
2
pub extern crate ncollide3d as ncollide;
3
+ pub extern crate nphysics3d as nphysics;
4
4
5
5
use self :: ncollide:: events:: ContactEvent ;
6
- use self :: nphysics:: math:: { Inertia , Point , Velocity , Vector } ;
6
+ use self :: nphysics:: math:: { Inertia , Point , Vector , Velocity } ;
7
7
use self :: nphysics:: object:: { Body , BodyHandle , RigidBody } ;
8
- use amethyst:: ecs:: * ;
9
- use amethyst:: ecs:: prelude:: * ;
10
- use amethyst:: core:: { GlobalTransform , Time } ;
11
- use amethyst:: core:: nalgebra:: try_convert;
12
8
use amethyst:: core:: nalgebra:: base:: Matrix3 ;
9
+ use amethyst:: core:: nalgebra:: try_convert;
10
+ use amethyst:: core:: { GlobalTransform , Time } ;
11
+ use amethyst:: ecs:: prelude:: * ;
12
+ use amethyst:: ecs:: * ;
13
13
use amethyst:: shrev:: EventChannel ;
14
14
15
15
pub type World = self :: nphysics:: world:: World < f32 > ;
@@ -74,7 +74,7 @@ pub struct Dumb3dPhysicsSystem {
74
74
physics_bodies_reader_id : Option < ReaderId < ComponentEvent > > ,
75
75
}
76
76
77
- impl < ' a > System < ' a > for Dumb3dPhysicsSystem {
77
+ impl < ' a > System < ' a > for Dumb3dPhysicsSystem {
78
78
type SystemData = (
79
79
WriteExpect < ' a , World > ,
80
80
Write < ' a , EventChannel < ContactEvent > > ,
@@ -102,32 +102,48 @@ impl <'a> System<'a> for Dumb3dPhysicsSystem {
102
102
103
103
// Get change flag events for transforms, removing deleted ones from the physics world.
104
104
{
105
- let events = transforms. channel ( ) . read (
106
- & mut self . transforms_reader_id . as_mut ( ) . unwrap ( ) ) ;
105
+ let events = transforms
106
+ . channel ( )
107
+ . read ( & mut self . transforms_reader_id . as_mut ( ) . unwrap ( ) ) ;
107
108
for event in events {
108
109
match event {
109
- ComponentEvent :: Modified ( id) => { self . modified_transforms . add ( * id) ; } ,
110
- ComponentEvent :: Inserted ( id) => { self . inserted_transforms . add ( * id) ; } ,
110
+ ComponentEvent :: Modified ( id) => {
111
+ self . modified_transforms . add ( * id) ;
112
+ }
113
+ ComponentEvent :: Inserted ( id) => {
114
+ self . inserted_transforms . add ( * id) ;
115
+ }
111
116
ComponentEvent :: Removed ( id) => {
112
- physical_world. remove_bodies ( & [
113
- physics_bodies. get ( entities. entity ( * id) ) . unwrap ( ) . handle ( ) . unwrap ( ) ] ) ;
114
- } ,
117
+ physical_world. remove_bodies ( & [ physics_bodies
118
+ . get ( entities. entity ( * id) )
119
+ . unwrap ( )
120
+ . handle ( )
121
+ . unwrap ( ) ] ) ;
122
+ }
115
123
} ;
116
124
}
117
125
}
118
126
119
127
// Get change flag events for physics bodies, removing deleted ones from the physics world.
120
128
{
121
- let events = physics_bodies. channel ( ) . read (
122
- & mut self . physics_bodies_reader_id . as_mut ( ) . unwrap ( ) ) ;
129
+ let events = physics_bodies
130
+ . channel ( )
131
+ . read ( & mut self . physics_bodies_reader_id . as_mut ( ) . unwrap ( ) ) ;
123
132
for event in events {
124
133
match event {
125
- ComponentEvent :: Modified ( id) => { self . modified_physics_bodies . add ( * id) ; } ,
126
- ComponentEvent :: Inserted ( id) => { self . inserted_physics_bodies . add ( * id) ; } ,
134
+ ComponentEvent :: Modified ( id) => {
135
+ self . modified_physics_bodies . add ( * id) ;
136
+ }
137
+ ComponentEvent :: Inserted ( id) => {
138
+ self . inserted_physics_bodies . add ( * id) ;
139
+ }
127
140
ComponentEvent :: Removed ( id) => {
128
- physical_world. remove_bodies ( & [
129
- physics_bodies. get ( entities. entity ( * id) ) . unwrap ( ) . handle ( ) . unwrap ( ) ] ) ;
130
- } ,
141
+ physical_world. remove_bodies ( & [ physics_bodies
142
+ . get ( entities. entity ( * id) )
143
+ . unwrap ( )
144
+ . handle ( )
145
+ . unwrap ( ) ] ) ;
146
+ }
131
147
} ;
132
148
}
133
149
}
@@ -140,53 +156,60 @@ impl <'a> System<'a> for Dumb3dPhysicsSystem {
140
156
& self . modified_transforms
141
157
| & self . inserted_transforms
142
158
| & self . modified_physics_bodies
143
- | & self . inserted_physics_bodies
144
- ) . join ( ) {
145
-
146
- if self . inserted_transforms . contains ( id )
147
- || self . inserted_physics_bodies . contains ( id) {
159
+ | & self . inserted_physics_bodies ,
160
+ )
161
+ . join ( )
162
+ {
163
+ if self . inserted_transforms . contains ( id ) || self . inserted_physics_bodies . contains ( id) {
148
164
match body {
149
165
PhysicsBody :: RigidBody ( ref mut rigid_body) => {
150
166
if rigid_body. handle . is_some ( )
151
- && physical_world. rigid_body ( rigid_body. handle . unwrap ( ) ) . is_some ( ) {
152
- physical_world. remove_bodies ( & [ rigid_body. handle . unwrap ( ) ] ) ;
167
+ && physical_world
168
+ . rigid_body ( rigid_body. handle . unwrap ( ) )
169
+ . is_some ( )
170
+ {
171
+ physical_world. remove_bodies ( & [ rigid_body. handle . unwrap ( ) ] ) ;
153
172
}
154
173
155
174
rigid_body. handle = Some ( physical_world. add_rigid_body (
156
175
try_convert ( transform. 0 ) . unwrap ( ) ,
157
176
Inertia :: new ( rigid_body. mass , rigid_body. angular_mass ) ,
158
- rigid_body. center_of_mass ) ) ;
177
+ rigid_body. center_of_mass ,
178
+ ) ) ;
159
179
160
- let mut physical_body = physical_world. rigid_body_mut (
161
- rigid_body. handle . unwrap ( ) ) . unwrap ( ) ;
180
+ let mut physical_body = physical_world
181
+ . rigid_body_mut ( rigid_body. handle . unwrap ( ) )
182
+ . unwrap ( ) ;
162
183
163
184
physical_body. set_velocity ( rigid_body. velocity ) ;
164
- } ,
185
+ }
165
186
PhysicsBody :: Multibody ( x) => {
166
187
// TODO
167
- } ,
188
+ }
168
189
PhysicsBody :: Ground ( x) => {
169
190
// TODO
170
- } ,
191
+ }
171
192
}
172
193
} else if self . modified_transforms . contains ( id)
173
- || self . modified_physics_bodies . contains ( id) {
194
+ || self . modified_physics_bodies . contains ( id)
195
+ {
174
196
match body {
175
197
PhysicsBody :: RigidBody ( ref mut rigid_body) => {
176
- let mut physical_body = physical_world. rigid_body_mut (
177
- rigid_body. handle . unwrap ( ) ) . unwrap ( ) ;
198
+ let mut physical_body = physical_world
199
+ . rigid_body_mut ( rigid_body. handle . unwrap ( ) )
200
+ . unwrap ( ) ;
178
201
179
202
physical_body. set_position ( try_convert ( transform. 0 ) . unwrap ( ) ) ;
180
203
physical_body. set_velocity ( rigid_body. velocity ) ;
181
204
182
205
// if you changed the mass properties at all... too bad!
183
- } ,
206
+ }
184
207
PhysicsBody :: Multibody ( x) => {
185
208
// TODO
186
- } ,
209
+ }
187
210
PhysicsBody :: Ground ( x) => {
188
211
// TODO
189
- } ,
212
+ }
190
213
}
191
214
}
192
215
}
@@ -200,22 +223,25 @@ impl <'a> System<'a> for Dumb3dPhysicsSystem {
200
223
//contact_events.iter_write(physical_world.contact_events());
201
224
202
225
// Apply the updated values of the simulated world to our Components
203
- for ( mut transform, mut body) in ( & mut transforms, & mut physics_bodies) . join ( ) {
226
+ for ( mut transform, mut body) in ( & mut transforms, & mut physics_bodies) . join ( ) {
204
227
let updated_body = physical_world. body ( body. handle ( ) . unwrap ( ) ) ;
205
228
206
229
if updated_body. is_ground ( ) || !updated_body. is_active ( ) || updated_body. is_static ( ) {
207
230
continue ;
208
231
}
209
232
210
233
match ( body, updated_body) {
211
- ( PhysicsBody :: RigidBody ( ref mut rigid_body) , Body :: RigidBody ( ref updated_rigid_body) ) => {
234
+ (
235
+ PhysicsBody :: RigidBody ( ref mut rigid_body) ,
236
+ Body :: RigidBody ( ref updated_rigid_body) ,
237
+ ) => {
212
238
updated_rigid_body. position ( ) ;
213
239
rigid_body. velocity = * updated_rigid_body. velocity ( ) ;
214
240
let inertia = updated_rigid_body. inertia ( ) ;
215
241
rigid_body. mass = inertia. linear ;
216
242
rigid_body. angular_mass = inertia. angular ;
217
243
rigid_body. center_of_mass = updated_rigid_body. center_of_mass ( ) ;
218
- } ,
244
+ }
219
245
( PhysicsBody :: Multibody ( multibody) , Body :: Multibody ( updated_multibody) ) => {
220
246
match updated_multibody. links ( ) . next ( ) {
221
247
Some ( link) => link. position ( ) ,
@@ -234,7 +260,8 @@ impl <'a> System<'a> for Dumb3dPhysicsSystem {
234
260
fn setup ( & mut self , res : & mut Resources ) {
235
261
Self :: SystemData :: setup ( res) ;
236
262
237
- res. entry :: < Gravity > ( ) . or_insert_with ( || Gravity :: new ( 0.0 , -9.80665 , 0.0 ) ) ;
263
+ res. entry :: < Gravity > ( )
264
+ . or_insert_with ( || Gravity :: new ( 0.0 , -9.80665 , 0.0 ) ) ;
238
265
res. entry :: < World > ( ) . or_insert_with ( || World :: new ( ) ) ;
239
266
240
267
let mut transform_storage: WriteStorage < GlobalTransform > = SystemData :: fetch ( & res) ;
@@ -247,14 +274,14 @@ impl <'a> System<'a> for Dumb3dPhysicsSystem {
247
274
248
275
#[ cfg( test) ]
249
276
mod tests {
250
- use amethyst :: { GameData , StateData , SimpleState , GameDataBuilder , Application } ;
277
+ use super :: * ;
251
278
use amethyst:: assets:: Handle ;
279
+ use amethyst:: core:: nalgebra:: Vector3 ;
280
+ use amethyst:: core:: transform:: bundle:: TransformBundle ;
281
+ use amethyst:: core:: Transform ;
252
282
use amethyst:: prelude:: Builder ;
253
283
use amethyst:: renderer:: * ;
254
- use amethyst:: core:: Transform ;
255
- use amethyst:: core:: transform:: bundle:: TransformBundle ;
256
- use amethyst:: core:: nalgebra:: Vector3 ;
257
- use super :: * ;
284
+ use amethyst:: { Application , GameData , GameDataBuilder , SimpleState , StateData } ;
258
285
259
286
struct GameState ;
260
287
@@ -265,11 +292,14 @@ mod tests {
265
292
data. world . register :: < Handle < Texture > > ( ) ;
266
293
267
294
// Create a texture for using.
268
- let texture = data. world . read_resource :: < amethyst:: assets:: Loader > ( )
295
+ let texture = data
296
+ . world
297
+ . read_resource :: < amethyst:: assets:: Loader > ( )
269
298
. load_from_data :: < Texture , ( ) > (
270
299
[ 170.0 , 170.0 , 255.0 , 1.0 ] . into ( ) ,
271
300
( ) ,
272
- & data. world . read_resource ( ) ) ;
301
+ & data. world . read_resource ( ) ,
302
+ ) ;
273
303
274
304
// Get resolution of the screen.
275
305
let ( x, y) = {
@@ -283,16 +313,18 @@ mod tests {
283
313
camera_transform. yaw_local ( -3.142 ) ;
284
314
285
315
// Add Camera
286
- data. world . create_entity ( )
316
+ data. world
317
+ . create_entity ( )
287
318
. with ( Camera :: standard_3d ( x, y) )
288
319
. with ( camera_transform)
289
320
. build ( ) ;
290
321
}
291
322
292
323
// Add Light
293
324
data. world . add_resource ( AmbientColor ( Rgba :: from ( [ 0.01 ; 3 ] ) ) ) ;
294
- data. world . create_entity ( )
295
- . with ( Light :: Point ( PointLight {
325
+ data. world
326
+ . create_entity ( )
327
+ . with ( Light :: Point ( PointLight {
296
328
intensity : 3.0 ,
297
329
color : Rgba :: white ( ) ,
298
330
radius : 5.0 ,
@@ -302,7 +334,8 @@ mod tests {
302
334
. build ( ) ;
303
335
304
336
// Add Sphere (todo: add many, add rigidbodies and colliders)
305
- data. world . create_entity ( )
337
+ data. world
338
+ . create_entity ( )
306
339
. with ( Shape :: Sphere ( 32 , 32 ) . generate :: < Vec < PosNormTex > > ( None ) )
307
340
. with ( texture)
308
341
. with ( GlobalTransform :: default ( ) )
@@ -318,17 +351,17 @@ mod tests {
318
351
. with_basic_renderer (
319
352
"./resources/display.ron" ,
320
353
DrawShaded :: < PosNormTex > :: new ( ) ,
321
- false ) ?
354
+ false ,
355
+ ) ?
322
356
. with_bundle ( TransformBundle :: new ( ) ) ?
323
357
. with ( Dumb3dPhysicsSystem :: default ( ) , "physics" , & [ ] ) ;
324
358
325
- let mut application
326
- = Application :: new ( "./" , GameState , game_data, ) ;
359
+ let mut application = Application :: new ( "./" , GameState , game_data) ;
327
360
328
361
assert_eq ! ( application. is_ok( ) , true ) ;
329
362
330
363
application. ok ( ) . unwrap ( ) . run ( ) ;
331
364
332
365
Ok ( ( ) )
333
366
}
334
- }
367
+ }
0 commit comments