【问题标题】:Detect collision with bottom of screen in Flame Engine?在火焰引擎中检测与屏幕底部的碰撞?
【发布时间】:2021-09-05 20:33:17
【问题描述】:

按照示例here,是否可以检测与屏幕特定一侧(例如屏幕底部)的碰撞?

Flutter 版本:2.2.3
火焰版本:1.0.0-releasecandidate.13

MyCollidable 类的onCollision 方法

@override
  void onCollision(Set<Vector2> intersectionPoints, Collidable other) {
    if (other is ScreenCollidable) {
      _isWallHit = true;
      // Detect which side?
      return;
    }
  }

【问题讨论】:

    标签: flutter dart flame


    【解决方案1】:

    对此没有内置方法,但是您可以使用游戏的大小很容易地计算出您正在与哪一侧发生碰撞并将碰撞点转换为屏幕坐标(如果您不需要此步骤没有改变缩放级别或移动相机)。

    代码是这样的:

    class MyCollidable extends PositionComponent
        with Hitbox, Collidable, HasGameRef {
      
      ...
    
      void onCollision(Set<Vector2> intersectionPoints, Collidable other) {
        if (other is ScreenCollidable) {
          _isWallHit = true;
          final firstPoint = intersectionPoints.first;
          // If you don't move/zoom the camera this step can be skipped
          final screenPoint = gameRef.projectVector(firstPoint);
          final screenSize = gameRef.size;
          if (screenPoint.x == 0) {
            // Left wall (or one of the leftmost corners)
          } else if (screenPoint.y == 0) {
            // Top wall (or one of the upper corners)
          } else if (screenPoint.x == screenSize.x) {
            // Right wall (or one of the rightmost corners)
          } else if (screenPoint.y == screenSize.y) {
            // Bottom wall (or one of the bottom corners)
          }
          return;
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 2015-01-02
      相关资源
      最近更新 更多