【问题标题】:Is there a way of detecting collision of Sprites in the bevy game engine?有没有办法在 bevy 游戏引擎中检测 Sprites 的碰撞?
【发布时间】:2021-03-10 15:31:31
【问题描述】:

目前我使用两个物体(彗星和船)的位置来检测它们是否碰撞。

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;
            }
        }
    }
}

但是必须有更好的方法来做到这一点。

【问题讨论】:

    标签: rust game-development bevy


    【解决方案1】:

    Bevy 目前没有完整的碰撞系统。除非它在 ​​introductory page 创建后的几个月内进行了扩展:

    物理

    许多游戏都需要碰撞检测和物理。我正计划构建一个以nphysics / ncollide 作为第一个后端的可插拔物理接口。

    幸运的是,这个简单的例子实际上是由 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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 2022-08-19
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多