#New to Bevy, Camera2D not showing up, not sure what I'm doing wrong

17 messages · Page 1 of 1 (latest)

hasty nebula
#

Giving Bevy a try for the first time.

https://taintedcoders.com/bevy/tutorials/pong-tutorial#creating-a-camera
Trying to follow Tainted coders Pong tutorial, running into an issue where the camera seemingly isn't being added to the project?

The tutorial describes: "Instead of a completely black window we get a lighter background." But nothing changed for me so I'm doing something wrong

I'm sure this is a very trivial fix or a typo somewhere, but I'm not sure where I'm going wrong.

Help is appreciated!

Code below:

use bevy::{math::VectorSpace, prelude::*};

#[derive(Component, Default)]
#[require(Transform)]
struct Position(Vec2);

#[derive(Component)]
#[require(Position)]
struct Ball;

const BALL_SIZE: f32 = 5.;

fn project_positions(mut positionables: Query<(&mut Transform, &Position)>) {
    for (mut transform, position) in &mut positionables {
        transform.translation = position.0.extend(0.)
    }
}

fn spawn_camera(mut commands: Commands) {
    commands.spawn_empty().insert(Camera2d); // <-- this is the line that is seemingly not working
}

fn spawn_ball(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    println!("spawning ball");
    let shape = Circle::new(BALL_SIZE);
    let color = Color::srgb(1., 0., 0.);
    let mesh = meshes.add(shape);
    let material = materials.add(color);
    commands
        .spawn_empty()
        .insert(Position(Vec2::ZERO))
        .insert(Ball);
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, (spawn_ball, spawn_camera))
        .add_systems(Update, (project_positions))
        .run();
}

pseudo hamlet
#

I think it's because you didn't add the mesh/material to the ball

#

You've added them as assets, but you need to add them to the ball entity so that Bevy knows how to display it to the screen

#

It seems you're just missing out on this part of the tutorial:

commands.spawn((
    Ball,
    Mesh2d(mesh),
    MeshMaterial2d(material)
  ));
hasty nebula
#

thanks for the help :)

twilit pasture
#

you can spawn with a tuple with up to 16 components, and it is faster than using .insert

hasty nebula
#

so spawn_empty() makes a new entity/ new "row" in the ECS table
and .insert() gives it different components?

pseudo hamlet
#

That seems like a nice tutorial, but I don't get why they're using spawn_empty() and insert instead of just spawn yeah 🤔

#

Yeah, insert "inserts" extra components, overriding if they are already present
spawn_empty creates a new entity without giving it any components, and spawn lets you create a new entity and give it components in one go

copper quiver
#

Say that there are two archetypes (I think this is what you mean by row), one for entities with A, and one for entities with A and B. If you have an entity with component A, and then you add B, it is then moved to the archetype with both A and B

hasty nebula
#

oh okay so bevy groups entities with the same components together?

copper quiver
#

Yeah that's a good way to think about it. The underlying representation gets quite complicated...

twilit pasture
restive plover
#

I will add a note about that though, It's nicer to just use spawn, I rarely am using insert unless I'm adding to existing entities