在 Flutter 中使用 Flame Forge2D 创建 L 形主体

问题描述 投票:0回答:1

我目前正在开展一个项目,需要使用 Flame Forge2D 创建 L 形身体。我使用具有以下顶点的 PolygonShape 类定义了形状:

final List<Vector2> vertices = [
  Vector2(-4, -4),
  Vector2(0, -4),
  Vector2(0, 0),
  Vector2(4, 0),
  Vector2(4, 4),
  Vector2(-4, 4),
];

但是,当我创建具有此形状的主体时,整个区域都会被填充,并且我希望只填充 L 形区域。

PolygonShape Issue

这是我当前代码的片段:

class LShapedBody extends BodyComponent {
  final Vector2 _position;
  final double width;
  final double height;

  LShapedBody(this._position, this.width, this.height);

  @override
  Body createBody() {
    final shape = PolygonShape();

    final List<Vector2> vertices = [
      Vector2(-1, -1),
      Vector2(0, -1),
      Vector2(0, 0),
      Vector2(1, 0),
      Vector2(1, 1),
      Vector2(-1, 1),
    ];

    shape.set(vertices);
    final fixtureDef = FixtureDef(shape);

    final bodyDef = BodyDef(type: BodyType.static, position: _position);
    return world.createBody(bodyDef)..createFixture(fixtureDef);
  }
}

我尝试过调整顶点,但没有成功达到预期的结果。如果有人有使用 Flame Forge2D 和创建非矩形体的经验,您能指导我如何正确定义顶点或需要任何其他步骤吗?

flutter dart flame forge2d
1个回答
0
投票

Box2D/Forge2D 中的多边形需要是凸的,而 L 则不是。

您在这里要做的就是创建固定装置并将它们连接到身体上,一个用于 L 的上部,一个用于躺着的部分。所以基本上只有两个矩形。

© www.soinside.com 2019 - 2024. All rights reserved.