2

所以我看到了一个非常奇怪的问题。

使用展示中的 PlayN peas 演示,我组合了一个简单的应用程序 - 我有一个动态实体掉到一个静态实体上,我无法弄清楚它为什么不弹跳。

在做了一些打印输出后,我发现碰撞正在发生 - y 位置保持不变,但 y 速度只是不断增加

这是我在动态实体中的更新代码

  @Override
  public void paint(float alpha) {
    float x = (body.getPosition().x * alpha) + (previousX * (1f - alpha));
    float y = (body.getPosition().y * alpha) + (previousY * (1f - alpha));
    float w = (body.getAngle() * alpha) + (previousRotationAngle * (1f - alpha));
    if (body.m_type == BodyType.DYNAMIC) {
      System.out.println("body x = " + body.getPosition().x + ", y = " + body.getPosition().y);
      System.out.println("velocity x = " + body.m_linearVelocity.x + ", y = "
      + body.m_linearVelocity.y);
    }
    imageLayer.setTranslation(x, y);
    imageLayer.setRotation(w);
  }

  @Override
  public void update(float delta) {
    previousX = body.getPosition().x;
    previousY = body.getPosition().y;
    previousRotationAngle = body.getAngle();
  }

这是打印输出 - 你可以看到速度不断上升,但 y 位置在 8.46 处保持不变

  [java] body x = 17.024979, y = 0.5199691
 [java] velocity x = 0.0, y = 1.2983631
 [java] body x = 17.024979, y = 0.79601336
 [java] velocity x = 0.0, y = 2.562787
 [java] body x = 17.024979, y = 0.8908453
 [java] velocity x = 0.0, y = 2.8736944
 [java] body x = 17.024979, y = 0.99586946
 [java] velocity x = 0.0, y = 3.18255
 [java] body x = 17.024979, y = 0.99586946
 [java] velocity x = 0.0, y = 3.18255
 [java] body x = 17.024979, y = 1.1110185
 [java] velocity x = 0.0, y = 3.489367
 [java] body x = 17.024979, y = 1.2362258
 [java] velocity x = 0.0, y = 3.794159
 [java] body x = 17.024979, y = 1.2362258
 [java] velocity x = 0.0, y = 3.794159
 [java] body x = 17.024979, y = 1.3714248
 [java] velocity x = 0.0, y = 4.096939
 [java] body x = 17.024979, y = 1.5165496
 [java] velocity x = 0.0, y = 4.3977213
 [java] body x = 17.024979, y = 1.6715348
 [java] velocity x = 0.0, y = 4.696518
 [java] body x = 17.024979, y = 1.6715348
 [java] velocity x = 0.0, y = 4.696518
 [java] body x = 17.024979, y = 1.8363149
 [java] velocity x = 0.0, y = 4.993343
 [java] body x = 17.024979, y = 2.0108259
 [java] velocity x = 0.0, y = 5.2882085
 [java] body x = 17.024979, y = 2.195003
 [java] velocity x = 0.0, y = 5.581128
 [java] body x = 17.024979, y = 2.388783
 [java] velocity x = 0.0, y = 5.8721147
 [java] body x = 17.024979, y = 2.388783
 [java] velocity x = 0.0, y = 5.8721147
 [java] body x = 17.024979, y = 2.5921018
 [java] velocity x = 0.0, y = 6.1611805
 [java] body x = 17.024979, y = 2.5921018
 [java] velocity x = 0.0, y = 6.1611805
 [java] body x = 17.024979, y = 2.804897
 [java] velocity x = 0.0, y = 6.4483385
 [java] body x = 17.024979, y = 3.0271058
 [java] velocity x = 0.0, y = 6.733601
 [java] body x = 17.024979, y = 3.0271058
 [java] velocity x = 0.0, y = 6.733601
 [java] body x = 17.024979, y = 3.2586663
 [java] velocity x = 0.0, y = 7.016981
 [java] body x = 17.024979, y = 3.4995165
 [java] velocity x = 0.0, y = 7.298491
 [java] body x = 17.024979, y = 3.4995165
 [java] velocity x = 0.0, y = 7.298491
 [java] body x = 17.024979, y = 3.7495952
 [java] velocity x = 0.0, y = 7.5781426
 [java] body x = 17.024979, y = 4.0088415
 [java] velocity x = 0.0, y = 7.8559484
 [java] body x = 17.024979, y = 4.277195
 [java] velocity x = 0.0, y = 8.131921
 [java] body x = 17.024979, y = 4.5545955
 [java] velocity x = 0.0, y = 8.406072
 [java] body x = 17.024979, y = 4.5545955
 [java] velocity x = 0.0, y = 8.406072
 [java] body x = 17.024979, y = 4.840983
 [java] velocity x = 0.0, y = 8.678413
 [java] body x = 17.024979, y = 4.840983
 [java] velocity x = 0.0, y = 8.678413
 [java] body x = 17.024979, y = 5.1362987
 [java] velocity x = 0.0, y = 8.948957
 [java] body x = 17.024979, y = 5.440483
 [java] velocity x = 0.0, y = 9.217716
 [java] body x = 17.024979, y = 5.440483
 [java] velocity x = 0.0, y = 9.217716
 [java] body x = 17.024979, y = 5.753478
 [java] velocity x = 0.0, y = 9.484701
 [java] body x = 17.024979, y = 6.0752254
 [java] velocity x = 0.0, y = 9.749924
 [java] body x = 17.024979, y = 6.0752254
 [java] velocity x = 0.0, y = 9.749924
 [java] body x = 17.024979, y = 6.4056673
 [java] velocity x = 0.0, y = 10.013396
 [java] body x = 17.024979, y = 6.7447467
 [java] velocity x = 0.0, y = 10.275129
 [java] body x = 17.024979, y = 7.0924063
 [java] velocity x = 0.0, y = 10.535135
 [java] body x = 17.024979, y = 7.0924063
 [java] velocity x = 0.0, y = 10.535135
 [java] body x = 17.024979, y = 7.4485893
 [java] velocity x = 0.0, y = 10.793425
 [java] body x = 17.024979, y = 7.8132396
 [java] velocity x = 0.0, y = 11.05001
 [java] body x = 17.024979, y = 7.8132396
 [java] velocity x = 0.0, y = 11.05001
 [java] body x = 17.024979, y = 8.186301
 [java] velocity x = 0.0, y = 11.304901
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 11.55811
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 11.55811
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 11.8096485
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 12.059526
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 12.059526
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 12.307755
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 12.554346
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 12.554346
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 12.799309
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 13.042655
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 13.042655
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 13.284395
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 13.52454
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 13.7631
 [java] body x = 17.024979, y = 8.465522
 [java] velocity x = 0.0, y = 13.7631
 [java] body x = 17.024979, y = 8.465522

这是一张图片 - 花是我测试的动态实体 - 它显然停在砖块上,但我不明白为什么速度只是继续上升

在此处输入图像描述


我不知道还有什么其他代码在这里会有所帮助——我的设置与展示中的豌豆演示几乎相同——以至于我从展示演示中删除了一些东西并且无法在那。

我在这里 100% 不知所措,因为这是一个物理问题,它有点阻碍了我的所有进步。豌豆演示完全有效

任何帮助将不胜感激 - 我已经尝试了 4 个小时,现在尝试了我能想到的所有事情,但没有任何效果


编辑添加了主要的世界级(这绝对是丑陋的 - 请不要评判我)

public class LevelWorld implements ContactListener, Paintable, Updatable, Destroyable {

  private static final float PHYSICS_UNITS_PER_PIXEL = PhysicsScale.PHYSICS_UNITS_PER_PIXEL
  .getScaleFactor();

  private final playn.core.GroupLayer worldLayer;

  private final Context context;
  private final World world;
  private final float frameRate;

  private final List<playn.core.GroupLayer> groupLayers;
  private final List<Entity> entities;
  private final Stack<Contact> contacts;
  private final Map<Body, PhysicsEntity> bodies;

  public LevelWorld(Level level, Context context) {
    worldLayer = graphics().createGroupLayer();
    worldLayer.setScale(1.F / PHYSICS_UNITS_PER_PIXEL);
    this.context = context;
    final Integer fps = (Integer) context.get(FPS);
    this.frameRate = 1.F / fps.floatValue();
    final Vec2 gravity = new Vec2(0.F, 10.0F);
    world = new World(gravity, true);
    world.setWarmStarting(true);
    world.setAutoClearForces(true);
    groupLayers = new ArrayList<playn.core.GroupLayer>();
    entities = new ArrayList<Entity>();
    contacts = new Stack<Contact>();
    bodies = new HashMap<Body, PhysicsEntity>();
    for (final GroupLayer groupLayer : level) {
      final playn.core.GroupLayer gl = graphics().createGroupLayer();
      gl.setDepth(groupLayer.getDepth());
      gl.setScale(groupLayer.getScaleFactor());
      groupLayers.add(gl);
      for (final Entity entity : groupLayer.getEntities()) {
        if (entity instanceof PhysicsEntity) {
          final FixtureDef fixtureDef = new FixtureDef();
          final BodyDef bodyDef = new BodyDef();
          if (PhysicsType.DYNAMIC.equals(entity.getEntityMetadata().getPhysicsType())) {
            bodyDef.type = BodyType.DYNAMIC;
          } else if (PhysicsType.STATIC.equals(entity.getEntityMetadata().getPhysicsType())) {
            bodyDef.type = BodyType.STATIC;
          }
          final Body body = world.createBody(bodyDef);
          fixtureDef.shape = entity.getEntityMetadata().getShape();
          fixtureDef.density = 0.4f;
          fixtureDef.friction = 0.1f;
          fixtureDef.restitution = 0.5f;
          body.setLinearDamping(0.2f);
          body.createFixture(fixtureDef);
          body.setTransform(new Vec2(entity.getPosition().x, entity.getPosition().y),
          entity.getRotationAngle());
          ((PhysicsEntity) entity).setBody(body);
          bodies.put(body, (PhysicsEntity) entity);
        }
        gl.add(entity.getImageLayer());
      }
      entities.addAll(groupLayer.getEntities());
      worldLayer.add(gl);
    }
    graphics().rootLayer().add(worldLayer);
    world.setContactListener(this); // kinda sketchy
  }

  @Override
  public void beginContact(Contact contact) {
    System.out.println("BEGIN CONTACT HAPPENED");
    contacts.push(contact);
  }

  @Override
  public void endContact(Contact contact) {

  }

  @Override
  public void preSolve(Contact contact, Manifold oldManifold) {

  }

  @Override
  public void postSolve(Contact contact, ContactImpulse impulse) {

  }

  @Override
  public void destroy() {
    worldLayer.destroy();
  }

  @Override
  public void update(float delta) {
    // worldLayer.setOrigin(worldLayer.originX() + (delta / 250.F),
    // worldLayer.originY());
    for (final Entity entity : entities) {
      entity.update(delta);
    }
    world.step(0.033F, 10, 10);
    // processContacts();
  }

  @Override
  public void paint(float alpha) {
    for (final Entity entity : entities) {
      entity.paint(alpha);
    }
  }

  private void processContacts() {
    while (!contacts.isEmpty()) {
      final Contact contact = contacts.pop();
      final PhysicsEntity a = bodies.get(contact.m_fixtureA.m_body);
      final PhysicsEntity b = bodies.get(contact.m_fixtureB.m_body);

      if (a != null && b != null) {
        if (a instanceof PhysicsEntity.IsContactListener) {
          ((PhysicsEntity.IsContactListener) a).contact(b);
        }
        if (b instanceof PhysicsEntity.IsContactListener) {
          ((PhysicsEntity.IsContactListener) b).contact(a);
        }
      }
    }
  }
}
4

1 回答 1

2

万一有人遇到这种情况 - 经过一整天半的时间试图弄清楚这一点并创建一个单独的简单项目,将 1 个动态实体放到一个静态实体上并看到它做同样的事情。

如果你看到这种情况发生在实体停止移动但速度不断增加的地方——你输入的碰撞形状是向后的——我有 4 个顶点按 0、1、2、3 的顺序放入形状中——如果我把它们放进去作为 3,2,1,0 它会工作得很好。我想如果您遇到相同类型的问题,请尝试颠倒您创建形状的顺序并查看碰撞是否有效。

于 2012-11-12T00:47:52.307 回答