1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
extern crate log;
extern crate simple_logger;
use specs::world::{Builder, World};
use specs_physics::{
colliders::Shape,
nalgebra::Isometry3,
nphysics::object::BodyStatus,
physics_dispatcher,
PhysicsBodyBuilder,
PhysicsColliderBuilder,
PhysicsParent,
SimplePosition,
};
fn main() {
// initialise the logger for system logs
simple_logger::init().unwrap();
// initialise the Specs world; this will contain our Resources and Entities
let mut world = World::new();
// create the dispatcher containing all relevant Systems; alternatively to using
// the convenience function you can add all required Systems by hand
let mut dispatcher = physics_dispatcher::<f32, SimplePosition<f32>>();
dispatcher.setup(&mut world.res);
// create an Entity containing the required Components; this Entity will be the
// parent
let parent = world
.create_entity()
.with(SimplePosition::<f32>(Isometry3::<f32>::translation(
1.0, 1.0, 1.0,
)))
.with(PhysicsBodyBuilder::<f32>::from(BodyStatus::Dynamic).build())
.with(PhysicsColliderBuilder::<f32>::from(Shape::Rectangle(1.0, 1.0, 1.0)).build())
.build();
// create the child Entity; if this Entity has its own PhysicsBody it'll more or
// less be its own object in the nphysics World, however if it's just a
// PhysicsCollider the parent/child hierarchy will actually take effect and the
// collider will be attached to the parent
let _child = world
.create_entity()
.with(SimplePosition::<f32>(Isometry3::<f32>::translation(
1.0, 1.0, 1.0,
)))
.with(
PhysicsColliderBuilder::<f32>::from(Shape::Rectangle(1.0, 1.0, 1.0))
.sensor(true)
.build(),
)
.with(PhysicsParent { entity: parent })
.build();
// execute the dispatcher
dispatcher.dispatch(&world.res);
}