在 Flame Engine 中检测与屏幕底部的碰撞?

Detect collision with bottom of screen in Flame Engine?

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

颤振版本:2.2.3
Flame版本:1.0.0-releasecandidate.13

MyCollidable的onCollision方法class

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

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

代码应该是这样的:

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