Skip to main content

compute_cos_sin

Function compute_cos_sin 

Source
pub fn compute_cos_sin(radians: f32) -> CosSin
Expand description

Compute the cosine and sine of an angle in radians. Implemented for cross-platform determinism.

Examples found in repository?
examples/benchmark/scenes/machines.rs (line 430)
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}
467
468// (StepJunkyard)
469pub fn step_junkyard(world: &mut World, step_count: i32) -> f32 {
470    let time_step = 1.0 / 60.0;
471    let time = time_step * step_count as f32;
472    let cs = compute_cos_sin(0.2 * time);
473    let target = WorldTransform {
474        p: to_pos(Vec2 {
475            x: 60.0 * cs.sine,
476            y: 0.0,
477        }),
478        q: ROT_IDENTITY,
479    };
480    let pusher_id = JUNKYARD_PUSHER_ID.with(|id| id.get());
481    body_set_target_transform(world, pusher_id, target, time_step, true);
482    0.0
483}