有没有办法在 bevy 游戏引擎中检测 Sprites 的碰撞?
Is there a way of detecting collision of Sprites in the bevy game engine?
目前我正在使用两个物体(彗星和飞船)的位置来检测它们是否发生碰撞。
fn collision_detection(
comets: Query<&Transform, With<Comet>>,
mut ships: Query<(&Transform, &mut ShipStatus), With<Ship>>,
) {
for comet in comets.iter() {
for (ship, mut status) in ships.iter_mut() {
if comet.translation.x < ship.translation.x + 80.0 && comet.translation.y < ship.translation.y + 80.0
&& comet.translation.x > ship.translation.x - 80.0 && comet.translation.y > ship.translation.y - 80.0 {
status.dead = true;
}
}
}
}
但必须有更好的方法来做到这一点。
Bevy 目前没有完整的碰撞系统。除非它在 introductory page 创建后的几个月内得到扩展:
Physics
Many games require collision detection and physics. I'm planning on building a plug-able physics interface with nphysics
/ ncollide
as the first backend.
幸运的是,这个简单的示例实际上是由 bevy::sprite::collide_aabb::collide
提供的,它可以进行简单的 AABB 碰撞检测。
use bevy::sprite::collide_aabb::collide;
let ship_size = Vec2::new(80.0, 80.0);
let comet_size = Vec2::new(0.0, 0.0);
for comet in comets.iter() {
for (ship, mut status) in ships.iter_mut() {
if collide(ship.translation, ship_size, comet.translation, comet_size).is_some() {
status.dead = true;
}
}
}
目前我正在使用两个物体(彗星和飞船)的位置来检测它们是否发生碰撞。
fn collision_detection(
comets: Query<&Transform, With<Comet>>,
mut ships: Query<(&Transform, &mut ShipStatus), With<Ship>>,
) {
for comet in comets.iter() {
for (ship, mut status) in ships.iter_mut() {
if comet.translation.x < ship.translation.x + 80.0 && comet.translation.y < ship.translation.y + 80.0
&& comet.translation.x > ship.translation.x - 80.0 && comet.translation.y > ship.translation.y - 80.0 {
status.dead = true;
}
}
}
}
但必须有更好的方法来做到这一点。
Bevy 目前没有完整的碰撞系统。除非它在 introductory page 创建后的几个月内得到扩展:
Physics
Many games require collision detection and physics. I'm planning on building a plug-able physics interface with
nphysics
/ncollide
as the first backend.
幸运的是,这个简单的示例实际上是由 bevy::sprite::collide_aabb::collide
提供的,它可以进行简单的 AABB 碰撞检测。
use bevy::sprite::collide_aabb::collide;
let ship_size = Vec2::new(80.0, 80.0);
let comet_size = Vec2::new(0.0, 0.0);
for comet in comets.iter() {
for (ship, mut status) in ships.iter_mut() {
if collide(ship.translation, ship_size, comet.translation, comet_size).is_some() {
status.dead = true;
}
}
}