aboutsummaryrefslogtreecommitdiff
path: root/examples2d/s2d_high_mass_ratio_1.rs
blob: b211a5e309c0b44cded08087269f1b785ac344ae (plain)
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
use rapier2d::prelude::*;
use rapier_testbed2d::Testbed;

pub fn init_world(testbed: &mut Testbed) {
    /*
     * World
     */
    let mut bodies = RigidBodySet::new();
    let mut colliders = ColliderSet::new();
    let impulse_joints = ImpulseJointSet::new();
    let multibody_joints = MultibodyJointSet::new();

    let extent = 1.0;
    let friction = 0.5;

    /*
     * Ground
     */
    let ground_width = 66.0 * extent;

    let rigid_body = RigidBodyBuilder::fixed();
    let ground_handle = bodies.insert(rigid_body);
    let collider = ColliderBuilder::segment(
        point![-0.5 * 2.0 * ground_width, 0.0],
        point![0.5 * 2.0 * ground_width, 0.0],
    )
    .friction(friction);
    colliders.insert_with_parent(collider, ground_handle, &mut bodies);

    /*
     * Create the cubes
     */

    for j in 0..3 {
        let mut count = 10;
        let offset = -20.0 * extent + 2.0 * (count as f32 + 1.0) * extent * j as f32;
        let mut y = extent;

        while count > 0 {
            for i in 0..count {
                let coeff = i as f32 - 0.5 * count as f32;
                let yy = if count == 1 { y + 2.0 } else { y };
                let position = vector![2.0 * coeff * extent + offset, yy];
                let rigid_body = RigidBodyBuilder::dynamic().translation(position);
                let parent = bodies.insert(rigid_body);

                let collider = ColliderBuilder::cuboid(extent, extent)
                    .density(if count == 1 {
                        (j as f32 + 1.0) * 100.0
                    } else {
                        1.0
                    })
                    .friction(friction);
                colliders.insert_with_parent(collider, parent, &mut bodies);
            }

            count -= 1;
            y += 2.0 * extent;
        }
    }
    /*
     * Set up the testbed.
     */
    testbed.set_world(bodies, colliders, impulse_joints, multibody_joints);
    testbed.look_at(point![0.0, 2.5], 20.0);
}