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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
#[allow(unused)]
fn main() {
App::new()
.insert_resource(ClearColor(Color::srgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0),
RapierDebugRenderPlugin::default(),
))
.add_systems(Startup, (setup_graphics, setup_physics))
.run();
}
pub fn setup_graphics(mut commands: Commands) {
commands.spawn((Camera2d, Transform::from_xyz(0.0, -200.0, 0.0)));
}
pub fn setup_physics(mut commands: Commands) {
// Build the rigid body.
let rad = 4.0;
let numi = 40; // Num vertical nodes.
let numk = 40; // Num horizontal nodes.
let shift = 10.0;
let mut body_entities = Vec::new();
for k in 0..numk {
for i in 0..numi {
let fk = k as f32;
let fi = i as f32;
let rigid_body = if i == 0 && (k % 4 == 0 || k == numk - 1) {
RigidBody::Fixed
} else {
RigidBody::Dynamic
};
let child_entity = commands
.spawn((
Transform::from_xyz(fk * shift, -fi * shift, 0.0),
rigid_body,
Collider::cuboid(rad, rad),
))
.id();
// Vertical joint.
if i > 0 {
let parent_entity = *body_entities.last().unwrap();
let joint = RevoluteJointBuilder::new().local_anchor2(Vec2::new(0.0, shift));
commands.entity(child_entity).with_children(|cmd| {
// NOTE: we want to attach multiple impulse joints to this entity, so
// we need to add the components to children of the entity. Otherwise
// the second joint component would just overwrite the first one.
cmd.spawn(ImpulseJoint::new(parent_entity, joint));
});
}
// Horizontal joint.
if k > 0 {
let parent_index = body_entities.len() - numi;
let parent_entity = body_entities[parent_index];
let joint = RevoluteJointBuilder::new().local_anchor2(Vec2::new(-shift, 0.0));
commands.entity(child_entity).with_children(|cmd| {
// NOTE: we want to attach multiple impulse joints to this entity, so
// we need to add the components to children of the entity. Otherwise
// the second joint component would just overwrite the first one.
cmd.spawn(ImpulseJoint::new(parent_entity, joint));
});
}
body_entities.push(child_entity);
}
}
}