Skip to main content

make_box

Function make_box 

Source
pub fn make_box(half_width: f32, half_height: f32) -> Polygon
Expand description

Make a box (rectangle) polygon, bypassing the need for a convex hull. (b2MakeBox)

Examples found in repository?
examples/demo_scenes.rs (line 15)
10fn add_static_box(world: &mut World, x: f32, y: f32, hx: f32, hy: f32) -> i32 {
11    let mut body_def = default_body_def();
12    body_def.position = m::to_pos(m::Vec2 { x, y });
13    let id = create_body(world, &body_def);
14    let def = default_shape_def();
15    let poly = make_box(hx, hy);
16    create_polygon_shape(world, id, &def, &poly);
17    get_body_full_id(world, id)
18}
19
20fn add_box(world: &mut World, x: f32, y: f32, hx: f32, hy: f32) -> i32 {
21    let mut body_def = default_body_def();
22    body_def.type_ = BodyType::Dynamic;
23    body_def.position = m::to_pos(m::Vec2 { x, y });
24    let id = create_body(world, &body_def);
25    let mut def = default_shape_def();
26    def.density = 1.0;
27    def.material.friction = 0.3;
28    let poly = make_box(hx, hy);
29    create_polygon_shape(world, id, &def, &poly);
30    get_body_full_id(world, id)
31}
More examples
Hide additional examples
examples/benchmark/scenes/pyramids.rs (line 92)
82pub fn create_large_pyramid(world: &mut World) {
83    world_enable_sleeping(world, false);
84
85    let base_count: i32 = if BENCHMARK_DEBUG { 20 } else { 100 };
86
87    {
88        let mut body_def = default_body_def();
89        body_def.position = to_pos(Vec2 { x: 0.0, y: -1.0 });
90        let ground_id = create_body(world, &body_def);
91
92        let box_shape = make_box(100.0, 1.0);
93        let shape_def = default_shape_def();
94        create_polygon_shape(world, ground_id, &shape_def, &box_shape);
95    }
96
97    let mut body_def = default_body_def();
98    body_def.type_ = BodyType::Dynamic;
99
100    let mut shape_def = default_shape_def();
101    shape_def.density = 1.0;
102
103    let a = 0.5;
104    let box_shape = make_square(a);
105
106    let shift = 1.0 * a;
107
108    for i in 0..base_count {
109        let y = (2.0 * i as f32 + 1.0) * shift;
110
111        for j in i..base_count {
112            let x = (i as f32 + 1.0) * shift + 2.0 * (j - i) as f32 * shift - a * base_count as f32;
113
114            body_def.position = to_pos(Vec2 { x, y });
115
116            let body_id = create_body(world, &body_def);
117            create_polygon_shape(world, body_id, &shape_def, &box_shape);
118        }
119    }
120}
examples/benchmark/scenes/machines.rs (line 156)
152pub fn create_smash(world: &mut World) {
153    world_set_gravity(world, VEC2_ZERO);
154
155    {
156        let box_shape = make_box(4.0, 4.0);
157
158        let mut body_def = default_body_def();
159        body_def.type_ = BodyType::Dynamic;
160        body_def.position = to_pos(Vec2 { x: -20.0, y: 0.0 });
161        body_def.linear_velocity = Vec2 { x: 40.0, y: 0.0 };
162        let body_id = create_body(world, &body_def);
163
164        let mut shape_def = default_shape_def();
165        shape_def.density = 8.0;
166        create_polygon_shape(world, body_id, &shape_def, &box_shape);
167    }
168
169    let d = 0.4;
170    let box_shape = make_square(0.5 * d);
171
172    let mut body_def = default_body_def();
173    body_def.type_ = BodyType::Dynamic;
174    body_def.is_awake = false;
175
176    let shape_def = default_shape_def();
177
178    let columns: i32 = if BENCHMARK_DEBUG { 20 } else { 120 };
179    let rows: i32 = if BENCHMARK_DEBUG { 10 } else { 80 };
180
181    for i in 0..columns {
182        for j in 0..rows {
183            body_def.position = to_pos(Vec2 {
184                x: i as f32 * d + 30.0,
185                y: (j as f32 - rows as f32 / 2.0) * d,
186            });
187            let body_id = create_body(world, &body_def);
188            create_polygon_shape(world, body_id, &shape_def, &box_shape);
189        }
190    }
191}
192
193// (CreateTumbler)
194pub fn create_tumbler(world: &mut World) {
195    let ground_id;
196    {
197        let body_def = default_body_def();
198        ground_id = create_body(world, &body_def);
199    }
200
201    {
202        let mut body_def = default_body_def();
203        body_def.type_ = BodyType::Dynamic;
204        body_def.position = to_pos(Vec2 { x: 0.0, y: 10.0 });
205        let body_id = create_body(world, &body_def);
206
207        let mut shape_def = default_shape_def();
208        shape_def.density = 50.0;
209
210        let polygon = make_offset_box(0.5, 10.0, Vec2 { x: 10.0, y: 0.0 }, ROT_IDENTITY);
211        create_polygon_shape(world, body_id, &shape_def, &polygon);
212        let polygon = make_offset_box(0.5, 10.0, Vec2 { x: -10.0, y: 0.0 }, ROT_IDENTITY);
213        create_polygon_shape(world, body_id, &shape_def, &polygon);
214        let polygon = make_offset_box(10.0, 0.5, Vec2 { x: 0.0, y: 10.0 }, ROT_IDENTITY);
215        create_polygon_shape(world, body_id, &shape_def, &polygon);
216        let polygon = make_offset_box(10.0, 0.5, Vec2 { x: 0.0, y: -10.0 }, ROT_IDENTITY);
217        create_polygon_shape(world, body_id, &shape_def, &polygon);
218
219        let motor_speed = 25.0;
220
221        let mut joint_def = default_revolute_joint_def();
222        joint_def.base.body_id_a = ground_id;
223        joint_def.base.body_id_b = body_id;
224        joint_def.base.local_frame_a.p = Vec2 { x: 0.0, y: 10.0 };
225        joint_def.base.local_frame_b.p = Vec2 { x: 0.0, y: 0.0 };
226        joint_def.motor_speed = (PI / 180.0) * motor_speed;
227        joint_def.max_motor_torque = 1e8;
228        joint_def.enable_motor = true;
229
230        create_revolute_joint(world, &joint_def);
231    }
232
233    let grid_count: i32 = if BENCHMARK_DEBUG { 20 } else { 45 };
234
235    let polygon = make_box(0.125, 0.125);
236    let mut body_def = default_body_def();
237    body_def.type_ = BodyType::Dynamic;
238    let shape_def = default_shape_def();
239
240    let mut y = -0.2 * grid_count as f32 + 10.0;
241    for _ in 0..grid_count {
242        let mut x = -0.2 * grid_count as f32;
243
244        for _ in 0..grid_count {
245            body_def.position = to_pos(Vec2 { x, y });
246            let body_id = create_body(world, &body_def);
247
248            create_polygon_shape(world, body_id, &shape_def, &polygon);
249
250            x += 0.4;
251        }
252
253        y += 0.4;
254    }
255}