pub fn make_offset_box(
half_width: f32,
half_height: f32,
center: Vec2,
rotation: Rot,
) -> PolygonExpand description
Make an offset box, bypassing the need for a convex hull. (b2MakeOffsetBox)
Examples found in repository?
examples/benchmark/scenes/rain.rs (line 85)
61pub fn create_rain(world: &mut World) {
62 RAIN_DATA.with(|rd| rd.borrow_mut().reset());
63
64 let grid_size = 0.5;
65 let grid_count = if BENCHMARK_DEBUG { 200 } else { 500 };
66 RAIN_DATA.with(|rd| {
67 let mut rd = rd.borrow_mut();
68 rd.grid_size = grid_size;
69 rd.grid_count = grid_count;
70 });
71
72 {
73 let body_def = default_body_def();
74 let ground_id = create_body(world, &body_def);
75
76 let shape_def = default_shape_def();
77 let mut y = 0.0;
78 let width = grid_size;
79 let height = grid_size;
80
81 for _ in 0..RAIN_ROW_COUNT {
82 let mut x = -0.5 * grid_count as f32 * grid_size;
83 for _ in 0..=grid_count {
84 let box_shape =
85 make_offset_box(0.5 * width, 0.5 * height, Vec2 { x, y }, ROT_IDENTITY);
86 create_polygon_shape(world, ground_id, &shape_def, &box_shape);
87
88 x += grid_size;
89 }
90
91 y += 45.0;
92 }
93 }
94
95 RAIN_DATA.with(|rd| {
96 let mut rd = rd.borrow_mut();
97 rd.column_count = 0;
98 rd.column_index = 0;
99 });
100}More examples
examples/benchmark/scenes/machines.rs (line 210)
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}
256
257// (CreateWasher)
258pub fn create_washer(world: &mut World) {
259 let kinematic = true;
260
261 {
262 let body_def = default_body_def();
263 // groundId is only used to anchor the (disabled) revolute joint branch.
264 let _ground_id = create_body(world, &body_def);
265 }
266
267 {
268 let motor_speed = 25.0;
269
270 let mut body_def = default_body_def();
271 body_def.position = to_pos(Vec2 { x: 0.0, y: 10.0 });
272
273 if kinematic {
274 body_def.type_ = BodyType::Kinematic;
275 body_def.angular_velocity = (PI / 180.0) * motor_speed;
276 body_def.linear_velocity = Vec2 {
277 x: 0.001,
278 y: -0.002,
279 };
280 } else {
281 body_def.type_ = BodyType::Dynamic;
282 }
283
284 let body_id = create_body(world, &body_def);
285
286 let shape_def = default_shape_def();
287
288 let r0 = 14.0;
289 let r1 = 16.0;
290 let r2 = 18.0;
291
292 let angle = PI / 18.0;
293 let q = Rot {
294 c: angle.cos(),
295 s: angle.sin(),
296 };
297 let qo = Rot {
298 c: (0.1 * angle).cos(),
299 s: (0.1 * angle).sin(),
300 };
301 let mut u1 = Vec2 { x: 1.0, y: 0.0 };
302 for i in 0..36 {
303 let u2 = if i == 35 {
304 Vec2 { x: 1.0, y: 0.0 }
305 } else {
306 rotate_vector(q, u1)
307 };
308
309 {
310 let a1 = inv_rotate_vector(qo, u1);
311 let a2 = rotate_vector(qo, u2);
312
313 let p1 = mul_sv(r1, a1);
314 let p2 = mul_sv(r2, a1);
315 let p3 = mul_sv(r1, a2);
316 let p4 = mul_sv(r2, a2);
317
318 let points = [p1, p2, p3, p4];
319 let hull = compute_hull(&points);
320
321 let polygon = make_polygon(&hull, 0.0);
322 create_polygon_shape(world, body_id, &shape_def, &polygon);
323 }
324
325 if i % 9 == 0 {
326 let p1 = mul_sv(r0, u1);
327 let p2 = mul_sv(r1, u1);
328 let p3 = mul_sv(r0, u2);
329 let p4 = mul_sv(r1, u2);
330
331 let points = [p1, p2, p3, p4];
332 let hull = compute_hull(&points);
333
334 let polygon = make_polygon(&hull, 0.0);
335 create_polygon_shape(world, body_id, &shape_def, &polygon);
336 }
337
338 u1 = u2;
339 }
340 }
341
342 let grid_count: i32 = if BENCHMARK_DEBUG { 20 } else { 90 };
343 let a = 0.1;
344
345 let polygon = make_square(a);
346 let mut body_def = default_body_def();
347 body_def.type_ = BodyType::Dynamic;
348 let mut shape_def = default_shape_def();
349 shape_def.enable_hit_events = true;
350
351 let mut y = -1.1 * a * grid_count as f32 + 10.0;
352 for _ in 0..grid_count {
353 let mut x = -1.1 * a * grid_count as f32;
354
355 for _ in 0..grid_count {
356 body_def.position = to_pos(Vec2 { x, y });
357 let body_id = create_body(world, &body_def);
358
359 create_polygon_shape(world, body_id, &shape_def, &polygon);
360
361 x += 2.1 * a;
362 }
363
364 y += 2.1 * a;
365 }
366}
367
368// (CreateJunkyard)
369pub fn create_junkyard(world: &mut World) {
370 {
371 let grid_size = 1.0;
372
373 let body_def = default_body_def();
374 let ground_id = create_body(world, &body_def);
375
376 let shape_def = default_shape_def();
377
378 let y = 0.0;
379 let mut x = -80.0 * grid_size;
380 for _ in 0..161 {
381 let box_shape = make_offset_box(
382 0.55 * grid_size,
383 0.5 * grid_size,
384 Vec2 { x, y },
385 ROT_IDENTITY,
386 );
387 create_polygon_shape(world, ground_id, &shape_def, &box_shape);
388 x += grid_size;
389 }
390
391 let mut y = grid_size;
392 let x = -80.0 * grid_size;
393 for _ in 0..50 {
394 let box_shape = make_offset_box(
395 0.5 * grid_size,
396 0.55 * grid_size,
397 Vec2 { x, y },
398 ROT_IDENTITY,
399 );
400 create_polygon_shape(world, ground_id, &shape_def, &box_shape);
401 y += grid_size;
402 }
403
404 let mut y = grid_size;
405 let x = 80.0 * grid_size;
406 for _ in 0..50 {
407 let box_shape = make_offset_box(
408 0.5 * grid_size,
409 0.55 * grid_size,
410 Vec2 { x, y },
411 ROT_IDENTITY,
412 );
413 create_polygon_shape(world, ground_id, &shape_def, &box_shape);
414 y += grid_size;
415 }
416 }
417
418 let column_count = 200;
419 let row_count: i32 = if BENCHMARK_DEBUG { 2 } else { 40 };
420
421 let radius = 0.25;
422 let polygon;
423 {
424 // Fibonacci sphere algorithm
425 let phi = PI * (5.0f32.sqrt() - 1.0);
426 let mut points = [Vec2 { x: 0.0, y: 0.0 }; 5];
427
428 for (i, point) in points.iter_mut().enumerate() {
429 let theta = phi * i as f32;
430 let cs = compute_cos_sin(theta);
431 point.x = radius * cs.cosine;
432 point.y = radius * cs.sine;
433 }
434
435 let hull = compute_hull(&points);
436 polygon = make_polygon(&hull, 0.0);
437 }
438
439 let mut body_def = default_body_def();
440 body_def.type_ = BodyType::Dynamic;
441 let shape_def = default_shape_def();
442
443 let mut side = -0.1;
444 let y_start = 15.0;
445
446 for i in 0..column_count {
447 let x = 1.5 * (2.0 * i as f32 - column_count as f32) * radius;
448
449 for j in 0..row_count {
450 let y = 4.0 * j as f32 * radius + y_start;
451
452 body_def.position = to_pos(Vec2 { x: x + side, y });
453 side = -side;
454
455 let body_id = create_body(world, &body_def);
456 create_polygon_shape(world, body_id, &shape_def, &polygon);
457 }
458 }
459
460 body_def.type_ = BodyType::Kinematic;
461 body_def.position = to_pos(VEC2_ZERO);
462 let pusher_id = create_body(world, &body_def);
463 JUNKYARD_PUSHER_ID.with(|id| id.set(pusher_id));
464 let box_shape = make_offset_box(2.0, 4.0, Vec2 { x: 0.0, y: 4.0 }, ROT_IDENTITY);
465 create_polygon_shape(world, pusher_id, &shape_def, &box_shape);
466}examples/benchmark/scenes/pyramids.rs (lines 212-217)
200pub fn create_compounds(world: &mut World) {
201 {
202 let grid_size = 1.0;
203
204 let body_def = default_body_def();
205 let ground_id = create_body(world, &body_def);
206
207 let shape_def = default_shape_def();
208
209 let y = 0.0;
210 let mut x = -40.0 * grid_size;
211 for _ in 0..81 {
212 let box_shape = make_offset_box(
213 0.55 * grid_size,
214 0.5 * grid_size,
215 Vec2 { x, y },
216 ROT_IDENTITY,
217 );
218 create_polygon_shape(world, ground_id, &shape_def, &box_shape);
219 x += grid_size;
220 }
221
222 let mut y = grid_size;
223 let x = -40.0 * grid_size;
224 for _ in 0..100 {
225 let box_shape = make_offset_box(
226 0.5 * grid_size,
227 0.55 * grid_size,
228 Vec2 { x, y },
229 ROT_IDENTITY,
230 );
231 create_polygon_shape(world, ground_id, &shape_def, &box_shape);
232 y += grid_size;
233 }
234
235 let mut y = grid_size;
236 let x = 40.0 * grid_size;
237 for _ in 0..100 {
238 let box_shape = make_offset_box(
239 0.5 * grid_size,
240 0.55 * grid_size,
241 Vec2 { x, y },
242 ROT_IDENTITY,
243 );
244 create_polygon_shape(world, ground_id, &shape_def, &box_shape);
245 y += grid_size;
246 }
247
248 let segment = Segment {
249 point1: Vec2 {
250 x: -800.0,
251 y: -80.0,
252 },
253 point2: Vec2 { x: 800.0, y: -80.0 },
254 };
255 create_segment_shape(world, ground_id, &shape_def, &segment);
256 }
257
258 let column_count: i32 = if BENCHMARK_DEBUG { 10 } else { 20 };
259 let row_count: i32 = if BENCHMARK_DEBUG { 40 } else { 150 };
260
261 let left_points = [
262 Vec2 { x: -1.0, y: 0.0 },
263 Vec2 { x: 0.5, y: 1.0 },
264 Vec2 { x: 0.0, y: 2.0 },
265 ];
266 let left_hull = compute_hull(&left_points);
267 let left = make_polygon(&left_hull, 0.0);
268
269 let right_points = [
270 Vec2 { x: 1.0, y: 0.0 },
271 Vec2 { x: -0.5, y: 1.0 },
272 Vec2 { x: 0.0, y: 2.0 },
273 ];
274 let right_hull = compute_hull(&right_points);
275 let right = make_polygon(&right_hull, 0.0);
276
277 let mut body_def = default_body_def();
278 body_def.type_ = BodyType::Dynamic;
279
280 let mut shape_def = default_shape_def();
281 shape_def.density = 1.0;
282 shape_def.material.friction = 0.5;
283
284 // Match the sample exactly: centery is computed before shift is reset for
285 // the compound branch.
286 let shift = 2.0;
287 let extray = 0.25;
288 let mut side = 0.25;
289 let centerx = shift * column_count as f32 / 2.0 - 1.0;
290 let centery = 1.15 / 2.0;
291 let y_start = 5.0;
292
293 for i in 0..column_count {
294 let x = i as f32 * shift - centerx;
295
296 for j in 0..row_count {
297 let y = j as f32 * (shift + extray) + centery + y_start;
298
299 body_def.position = to_pos(Vec2 { x: x + side, y });
300 side = -side;
301
302 let body_id = create_body(world, &body_def);
303 create_polygon_shape(world, body_id, &shape_def, &left);
304 create_polygon_shape(world, body_id, &shape_def, &right);
305 }
306 }
307}