-
Hi, So I borrowed the "spawn" logic from the breakout example and created the following function: pub fn spawn_ball(
commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
asset_server: &Res<AssetServer>,
) {
let mesh_2d_handle: Handle<Mesh> = meshes.add(RegularPolygon::new(BALL_DIAMETER, 6));
// let ball_handle: Handle<Mesh> = meshes.add(Circle::default());
commands.spawn((
MaterialMesh2dBundle {
mesh: mesh_2d_handle.into(),
material: materials.add(BALL_COLOR),
transform: Transform::from_translation(BALL_STARTING_POSITION)
.with_scale(Vec2::splat(BALL_DIAMETER).extend(1.)),
..default()
},
Ball,
Velocity(INITIAL_BALL_DIRECTION.normalize() * BALL_SPEED),
));
} Now I'm trying to create a "system" function that will increase or decrease the amount of side of the polygon: pub fn update_ball_shape(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut query: Query<&mut Mesh2dHandle, With<Ball>>,
time: Res<Time>,
) {
let mut ball_mesh_handle = query.single_mut();
let mut new_sides: u32 = ball_mesh_handle.0.(Some Magic Step).sides;
if keyboard_input.pressed(KeyCode::Digit1) {
new_sides -= 1;
}
if keyboard_input.pressed(KeyCode::Digit2) {
new_sides += 1;
}
let mesh_2d_handle: Handle<Mesh> = meshes.add(RegularPolygon::new(BALL_DIAMETER, new_sides));
ball_mesh_handle = mesh_2d_handle.into();
} But I'm having a hard time wrapping my head around on How should I query for the I know that on the example above the Any tips? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
After the mesh is created, the original // `RegularPolygon` doesn't impl `Component`, so we need to wrap it in our own type.
#[derive(Component)]
struct BallShape(RegularPolygon);
// Create a regular polygon with a circumcircle radius of 0.5, or diameter of 1.0.
// This will be scaled by `BALL_DIAMETER` later.
let shape = RegularPolygon::new(0.5, 6);
let mesh_2d_handle = meshes.add(Mesh::from(shape));
commands.spawn((
/* snipped for brevity */
Ball,
BallShape(shape)
)); You could then add it to the query in your fn update_ball_shape(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut query: Query<(&mut Mesh2dHandle, &mut BallShape), With<Ball>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
let (mut ball_mesh_handle, mut ball_shape) = query.single_mut();
// Watch out, this will crash if `sides` is < 3. You will want to clamp this value.
if keyboard_input.just_pressed(KeyCode::Digit1) {
ball_shape.0.sides -= 1;
}
// Also note: `just_pressed` is used here instead of `pressed`` so that sides isn't
// incremented every frame while the button is held.
if keyboard_input.just_pressed(KeyCode::Digit2) {
ball_shape.0.sides += 1;
}
*ball_mesh_handle = meshes
.add(RegularPolygon::new(BALL_DIAMETER, ball_shape.0.sides))
.into();
} |
Beta Was this translation helpful? Give feedback.
After the mesh is created, the original
RegularPolygon
is discarded. If you want to keep it around to access it later, a handy place to store it would be an additional component to yourBall
entity.You could then add it to the query in your
update…