I'm trying to very basic collision detection working where I prevent one sprite controlled by the player from moving through the other. Avian seemed like a good choice for this so I've brought it in however I'm unclear what the correct approach is to actually preventing collision with is. This is what I've got:
use avian2d::prelude::*;
use bevy::prelude::*;
#[derive(Component)]
struct Player;
fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
commands.spawn((
Player,
SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(100.0, 100.0)),
..default()
},
..default()
},
RigidBody::Kinematic,
Collider::round_rectangle(90., 90., 10.),
));
commands.spawn((
SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(500.0, 100.0)),
..default()
},
transform: Transform::from_xyz(0., -200., 0.),
..default()
},
RigidBody::Static,
Collider::round_rectangle(490., 90., 10.),
));
}
fn character_movement(
mut characters: Query<(&mut Transform, &Sprite, &Player)>,
input: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
) {
for (mut transform, _, _) in &mut characters {
if input.pressed(KeyCode::KeyW) {
transform.translation.y += 100.0 * time.delta_seconds();
}
if input.pressed(KeyCode::KeyS) {
transform.translation.y -= 100.0 * time.delta_seconds();
}
/* etc.... */
}
}
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
PhysicsPlugins::default(),
PhysicsDebugPlugin::default(),
))
.add_systems(Startup, setup)
.add_systems(Update, character_movement)
.run();
}