2d Collision #2619
-
I'm trying to implement my own collision for a basic 2d platformer. Below is my current (broken) collision system. In the fn collision(
mut player_query: Query<(&Player, &Transform, &Sprite, &mut Gravity)>,
collider_query: Query<(&Collider, &Transform, &Sprite)>,
) {
if let Ok((_player, player_transform, player_sprite, mut gravity)) = player_query.single_mut() {
let player_size = player_sprite.size;
for (_collider, collider_transform, collider_sprite) in collider_query.iter() {
let collision = collide(
player_transform.translation,
player_size,
collider_transform.translation,
collider_sprite.size,
);
// TODO: this isn't how I should stop the Y-velocity of player
// causing issues with jumping
// not preventing player from colliding into the sides of the an object
if let Some(collision) = collision {
match collision {
// placeholders
Collision::Left => gravity.0 = 0.0,
Collision::Right => gravity.0 = 0.0,
Collision::Bottom => gravity.0 = 0.0,
Collision::Top => gravity.0 = 0.0,
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Here's a snippet from my draft of the next version of the Bevy book. In your case, ensuring the queries are mutually exclusive using a Running multiple queries at onceAs the logic in your systems become more complex, you may find that you want to access data from two different queries at once. fn defense_aura_system(aura_query: Query<&Transform, With<Aura>>, target_query: Query<(&mut Defense, &Transform), With<Creature>>){
// Give all allies near an aura-generator a bonus to their defense
} But as you use this pattern more, you may encounter an error that looks something like:
What went wrong? It worked just fine before! Well, it turns out that Rust, in its infinite wisdom, Of course, you already knew that, and have carefully thought about the architecture of your system, designing something like: fn camera_follow_system(player_query: Query<&Transform, With<Player>>, camera_query: Query<&mut Transform, With<Camera>>){
let player_transform = player_query.single().unwrap();
let camera_query = camera_query.single_mut.unwrap();
// Insert logic here
} You know that there's never going to be an entity that has both fn camera_follow_system(player_query: Query<&Transform, With<Player>>, camera_query: Query<&mut Transform, (With<Camera>, Without<Player>)>){
let player_transform = player_query.single().unwrap();
let camera_query = camera_query.single_mut.unwrap();
// Insert logic here
} The other way to get around this issue is to use a fn camera_follow_system(queries: QuerySet<Query<&Transform, With<Player>>, Query<&mut Transform, With<Camera>>){
let player_transform = queries.0.single().unwrap();
let camera_query = queries.1.single_mut.unwrap();
// Insert logic here
} |
Beta Was this translation helpful? Give feedback.
Here's a snippet from my draft of the next version of the Bevy book. In your case, ensuring the queries are mutually exclusive using a
Without
filter should do the trick.Running multiple queries at once
As the logic in your systems become more complex, you may find that you want to access data from two different queries at once.
In most cases, simply adding a second query as another system parameter works perfectly fine:
But as you use this pattern more, you may encounter an error that loo…