So, I'm using Bevy (0.11) the Rapier (0.22) physics engine to have a bunch of blocks falling on each other, here is how I spawn the blocks:
commands.spawn((
Collider::cuboid(dim, dim),
MaterialMesh2dBundle {
mesh: meshes. Add(shape::Box::new(dim2, dim2, dim2).into()).into(),
material: materials.add(ColorMaterial::from(random_colour())),
transform: Transform::from_xyz(
left + (dim2 * i as f32) + 1.0,
half_height - dim,
0.0,
),
..default()
},
RigidBody::Dynamic,
Restitution::coefficient(0.5),
ColliderMassProperties::Mass(1000.0), // Increase the mass to stop the glitching (a bit)
GravityScale(10.0),
));
I want to be able to interact with them via mouse click/touch. I'm trying to use the Point projection to see if the mouse is over something like so
fn mouse_button_events(
mut mousebtn_evr: EventReader<MouseButtonInput>,
q_windows: Query<&Window, With<PrimaryWindow>>,
rapier_context: Res<RapierContext>,
) {
use bevy::input::ButtonState;
for ev in mousebtn_evr.iter() {
match ev.state {
ButtonState::Pressed => {
println!("Mouse button press: {:?}", ev.button);
let point = q_windows.single().cursor_position().unwrap();
let filter = QueryFilter::default();
rapier_context.intersections_with_point(point, filter, |entity| {
// Callback called on each collider with a shape containing the point.
println!("The entity {:?} contains the point.", entity);
// Return `false` instead if we want to stop searching for other colliders containing this point.
true
});
}
ButtonState::Released => {
println!("Mouse button release: {:?}", ev.button);
}
}
}
}
But it's not printing the entity id when something is clicked. Are the screen coordinates the same as the Rapier space coordinates? What am I don't wrong here?
Seems I was spot on about the different coordinate systems. Mouse events are in screen coordinates which have their origin in the top left while Rapier has it's origin in the center of the screen. So I just needed a conversion function...