Skip to main content

spawn_object

Function spawn_object 

Source
pub fn spawn_object(world: &mut World, object: Object) -> Entity
Expand description

Spawns an Object: mesh, color, and optional physics body in one call.

Examples found in repository?
examples/fps_walk.rs (lines 8-17)
3fn main() {
4    let mut app = open();
5    spawn_floor(&mut app.world, 40.0);
6    for index in 0..30 {
7        let angle = index as f32 * 0.7;
8        spawn_object(
9            &mut app.world,
10            Object {
11                position: vec3(angle.sin() * 15.0, 1.0, (angle * 1.3).cos() * 15.0),
12                scale: vec3(1.0, 2.0, 1.0),
13                color: STEEL,
14                body: Body::Static,
15                ..Object::default()
16            },
17        );
18    }
19    first_person(&mut app.world, vec3(0.0, 1.2, 8.0));
20    while frame(&mut app) {}
21}
More examples
Hide additional examples
examples/day_night.rs (lines 22-30)
3fn main() {
4    let mut app = open();
5    show_grid(&mut app.world, false);
6    set_bloom(&mut app.world, true);
7    set_fog(
8        &mut app.world,
9        Some(Fog {
10            color: [0.7, 0.75, 0.85],
11            start: 30.0,
12            end: 120.0,
13        }),
14    );
15    spawn_floor(&mut app.world, 60.0);
16
17    for index in 0..14 {
18        let angle = index as f32 * 0.55;
19        let height = 3.0 + (index as f32 * 1.7).sin().abs() * 7.0;
20        let distance = 9.0 + index as f32;
21        let center = vec3(angle.cos() * distance, height * 0.5, angle.sin() * distance);
22        spawn_object(
23            &mut app.world,
24            Object {
25                position: center,
26                scale: vec3(2.0, height, 2.0),
27                color: rgb(0.35, 0.37, 0.42),
28                ..Object::default()
29            },
30        );
31        let window_glow = center + vec3(0.0, height * 0.5 + 0.5, 0.0);
32        point_light(&mut app.world, window_glow, [1.0, 0.7, 0.3], 4.0);
33    }
34
35    orbit_camera(&mut app.world, vec3(0.0, 4.0, 0.0), 35.0);
36    while frame(&mut app) {
37        let hour = (8.0 + elapsed_seconds(&app.world)) % 24.0;
38        set_time_of_day(&mut app.world, hour);
39    }
40}
examples/poster.rs (lines 13-22)
3fn main() {
4    std::fs::create_dir_all("screenshots").expect("failed to create the screenshots directory");
5    render_image(1920, 1080, "screenshots/poster.png", |world| {
6        set_background(world, Background::Nebula);
7        show_grid(world, false);
8        set_bloom(world, true);
9        spawn_floor(world, 14.0);
10
11        for index in 0..7 {
12            let angle = index as f32 / 7.0 * std::f32::consts::TAU;
13            let column = spawn_object(
14                world,
15                Object {
16                    shape: Shape::Cylinder,
17                    position: vec3(angle.cos() * 7.0, 1.5, angle.sin() * 7.0),
18                    scale: vec3(1.0, 3.0, 1.0),
19                    color: STEEL,
20                    ..Object::default()
21                },
22            );
23            set_metallic_roughness(world, column, 0.9, 0.3);
24        }
25
26        let centerpiece = spawn_torus(world, vec3(0.0, 2.2, 0.0));
27        set_scale(world, centerpiece, vec3(1.6, 1.6, 1.6));
28        set_emissive(world, centerpiece, [0.3, 0.8, 1.0], 9.0);
29        point_light(world, vec3(0.0, 4.5, 0.0), [0.3, 0.8, 1.0], 8.0);
30
31        fixed_camera(world, vec3(11.0, 6.0, 11.0), vec3(0.0, 1.8, 0.0));
32    });
33    println!("wrote screenshots/poster.png");
34}
examples/physics_playground.rs (lines 9-21)
3fn main() {
4    let mut app = open();
5    spawn_floor(&mut app.world, 20.0);
6
7    for level in 0..6 {
8        for column in 0..(6 - level) {
9            spawn_object(
10                &mut app.world,
11                Object {
12                    position: vec3(
13                        column as f32 + level as f32 * 0.5 - 3.0,
14                        level as f32 + 0.5,
15                        0.0,
16                    ),
17                    color: GREEN,
18                    body: Body::Dynamic { mass: 1.0 },
19                    ..Object::default()
20                },
21            );
22        }
23    }
24
25    let counter = spawn_text(&mut app.world, "shots: 0", ScreenAnchor::TopLeft);
26    spawn_text(
27        &mut app.world,
28        "space fires a ball, click pops a block upward",
29        ScreenAnchor::BottomLeft,
30    );
31
32    let mut shots = 0;
33    while frame(&mut app) {
34        if key_pressed(&app.world, KeyCode::Space) {
35            let muzzle = camera_position(&app.world);
36            let ball = spawn_object(
37                &mut app.world,
38                Object {
39                    shape: Shape::Sphere,
40                    position: muzzle,
41                    scale: vec3(0.3, 0.3, 0.3),
42                    color: ORANGE,
43                    body: Body::Dynamic { mass: 3.0 },
44                },
45            );
46            let velocity = camera_forward(&app.world) * 25.0;
47            set_velocity(&mut app.world, ball, velocity);
48            shots += 1;
49            set_text(&mut app.world, counter, &format!("shots: {shots}"));
50        }
51        if let Some(target) = clicked_entity(&app.world) {
52            push(&mut app.world, target, vec3(0.0, 6.0, 0.0));
53            let pop_position = position(&app.world, target);
54            emit_burst(&mut app.world, pop_position, ORANGE, 60);
55            shake_camera(&mut app.world, 0.05, 0.2);
56        }
57    }
58}
examples/juice.rs (lines 12-21)
3fn main() {
4    let mut app = open();
5    show_grid(&mut app.world, false);
6    set_bloom(&mut app.world, true);
7    spawn_floor(&mut app.world, 12.0);
8
9    let mut gems = Vec::new();
10    for index in 0..9 {
11        let angle = index as f32 / 9.0 * std::f32::consts::TAU;
12        let gem = spawn_object(
13            &mut app.world,
14            Object {
15                shape: Shape::Sphere,
16                position: vec3(angle.cos() * 5.0, 0.6, angle.sin() * 5.0),
17                scale: vec3(0.6, 0.6, 0.6),
18                color: TEAL,
19                ..Object::default()
20            },
21        );
22        gems.push(gem);
23    }
24
25    let door = spawn_object(
26        &mut app.world,
27        Object {
28            position: vec3(0.0, 1.5, -8.0),
29            scale: vec3(4.0, 3.0, 0.4),
30            color: STEEL,
31            ..Object::default()
32        },
33    );
34
35    emit_fire(&mut app.world, vec3(0.0, 0.2, 0.0));
36    point_light(&mut app.world, vec3(0.0, 1.2, 0.0), [1.0, 0.6, 0.2], 5.0);
37    spawn_text(
38        &mut app.world,
39        "click a gem, O slides the door, B bursts",
40        ScreenAnchor::BottomLeft,
41    );
42
43    let mut door_open = false;
44    while frame(&mut app) {
45        if let Some(target) = clicked_entity(&app.world)
46            && gems.contains(&target)
47        {
48            animate_scale(
49                &mut app.world,
50                target,
51                vec3(1.0, 1.0, 1.0),
52                0.25,
53                EasingFunction::BackOut,
54            );
55            animate_color(&mut app.world, target, GOLD, 0.25, EasingFunction::QuadOut);
56            let burst_position = position(&app.world, target);
57            emit_burst(&mut app.world, burst_position, GOLD, 80);
58            shake_camera(&mut app.world, 0.08, 0.3);
59        }
60        if key_pressed(&app.world, KeyCode::KeyO) {
61            door_open = !door_open;
62            let target_height = if door_open { 4.6 } else { 1.5 };
63            animate_position(
64                &mut app.world,
65                door,
66                vec3(0.0, target_height, -8.0),
67                0.8,
68                EasingFunction::CubicInOut,
69            );
70        }
71        if key_pressed(&app.world, KeyCode::KeyB) {
72            let center = camera_position(&app.world) + camera_forward(&app.world) * 6.0;
73            emit_burst(&mut app.world, center, PINK, 200);
74            shake_camera(&mut app.world, 0.2, 0.5);
75        }
76    }
77}