Skip to main content

default_chain_def

Function default_chain_def 

Source
pub fn default_chain_def() -> ChainDef
Expand description

Initialize a chain definition with the default values. (b2DefaultChainDef)

Examples found in repository?
examples/benchmark/scenes/machines.rs (line 66)
39pub fn create_spinner(world: &mut World) {
40    let ground_id;
41    {
42        let body_def = default_body_def();
43        ground_id = create_body(world, &body_def);
44
45        let q = make_rot(-2.0 * PI / SPINNER_POINT_COUNT as f32);
46        let mut p = Vec2 { x: 40.0, y: 0.0 };
47        let mut points: Vec<Vec2> = Vec::with_capacity(SPINNER_POINT_COUNT);
48        for _ in 0..SPINNER_POINT_COUNT {
49            points.push(Vec2 {
50                x: p.x,
51                y: p.y + 32.0,
52            });
53            p = rotate_vector(q, p);
54        }
55
56        // C: b2SurfaceMaterial material = { 0 }; material.friction = 0.1f;
57        let material = SurfaceMaterial {
58            friction: 0.1,
59            restitution: 0.0,
60            rolling_resistance: 0.0,
61            tangent_speed: 0.0,
62            user_material_id: 0,
63            custom_color: 0,
64        };
65
66        let mut chain_def = default_chain_def();
67        chain_def.points = points;
68        chain_def.is_loop = true;
69        chain_def.materials = vec![material];
70
71        create_chain(world, ground_id, &chain_def);
72    }
73
74    {
75        let mut body_def = default_body_def();
76        body_def.type_ = BodyType::Dynamic;
77        body_def.position = to_pos(Vec2 { x: 0.0, y: 12.0 });
78        body_def.enable_sleep = false;
79
80        let spinner_id = create_body(world, &body_def);
81
82        let box_shape = make_rounded_box(0.4, 20.0, 0.2);
83        let mut shape_def = default_shape_def();
84        shape_def.material.friction = 0.0;
85        create_polygon_shape(world, spinner_id, &shape_def, &box_shape);
86
87        let motor_speed = 5.0;
88        let max_motor_torque = f32::MAX;
89        let mut joint_def = default_revolute_joint_def();
90        joint_def.base.body_id_a = ground_id;
91        joint_def.base.body_id_b = spinner_id;
92        joint_def.base.local_frame_a.p = body_get_local_point(world, ground_id, body_def.position);
93        joint_def.enable_motor = true;
94        joint_def.motor_speed = motor_speed;
95        joint_def.max_motor_torque = max_motor_torque;
96
97        let joint_id = create_revolute_joint(world, &joint_def);
98        SPINNER_ID.with(|id| id.set(joint_id));
99    }
100
101    let capsule = Capsule {
102        center1: Vec2 { x: -0.25, y: 0.0 },
103        center2: Vec2 { x: 0.25, y: 0.0 },
104        radius: 0.25,
105    };
106    let circle = Circle {
107        center: Vec2 { x: 0.0, y: 0.0 },
108        radius: 0.35,
109    };
110    let square = make_square(0.35);
111
112    let mut body_def = default_body_def();
113    body_def.type_ = BodyType::Dynamic;
114    let mut shape_def = default_shape_def();
115    shape_def.material.friction = 0.1;
116    shape_def.material.restitution = 0.1;
117    shape_def.density = 0.25;
118
119    let body_count: i32 = if BENCHMARK_DEBUG { 499 } else { 2 * 3038 };
120
121    let mut x = -23.0;
122    let mut y = 2.0;
123    for i in 0..body_count {
124        body_def.position = to_pos(Vec2 { x, y });
125        let body_id = create_body(world, &body_def);
126
127        let remainder = i % 3;
128        if remainder == 0 {
129            create_capsule_shape(world, body_id, &shape_def, &capsule);
130        } else if remainder == 1 {
131            create_circle_shape(world, body_id, &shape_def, &circle);
132        } else if remainder == 2 {
133            create_polygon_shape(world, body_id, &shape_def, &square);
134        }
135
136        x += 0.5;
137
138        if x >= 23.0 {
139            x = -23.0;
140            y += 0.5;
141        }
142    }
143}