【问题标题】:How to detect in which direction collision happened in Flutter Flame?如何检测Flutter Flame中发生碰撞的方向?
【发布时间】:2023-02-02 14:54:10
【问题描述】:

我有一个视频游戏,我可以在 4 个方向上移动:上、下、左和右。所以,基本上是“从上面看”。我在游戏中也有墙。如果角色撞到墙上——他不应该穿过它。

我如何在 onCollision/onCollisionStart 中实现碰撞检测,以便角色知道他何时可以向左、向右、向上和向下移动?

我尝试的方法:

  @override
  void onCollisionStart(
    Set<Vector2> intersectionPoints,
    PositionComponent other,
  ) {
    super.onCollisionStart(intersectionPoints, other);

    if (other is Wall) {
      // if character is going up
      if (current == MainHeroState.up) {
        upCollisionObject = other;
        return;
      }
      // if character is going down
      if (current == MainHeroState.down) {
        downCollisionObject = other;
        return;
      }
      // ...
    }
  }

然后在onCollisionEnd

  @override
  void onCollisionEnd(PositionComponent other) {
    super.onCollisionEnd(intersectionPoints, other);

    if (other is Wall) {
      // if the link to the object is the same as the link of the collided object
      if (other == upCollisionObject) {
        upCollisionObject = null;
        return;
      }
      if (other == downCollisionObject) {
        downCollisionObject = null;
        return;
      }
      // ...
    }
  }

但这种做法是不正确的,在很多情况下是行不通的。

【问题讨论】:

  • 请提供足够的代码,以便其他人可以更好地理解或重现问题。

标签: flutter dart collision-detection game-development flame


【解决方案1】:

你可能想看看这个: https://code.pieces.app/blog/build-a-pong-game-in-flutter-with-flame 在“桨碰撞更新”部分下。

【讨论】:

    猜你喜欢
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多