sean christe
sean christe

Reputation: 909

PlayN JBox2D Y velocity increases but collision happens

So i'm seeing a really strange problem.

using the PlayN peas demo from the showcase i put together a simple app - i have a dynamic entity dropping onto a static entity and i cannot figure out why it isn't bouncing.

After doing some printouts i find that the collision is happening - the y position stays the same but the y velocity just keeps increasing

here's my update code in the dynamic entity

  @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();
  }

here's the printout - as you can see the velocity keeps going up but the y position stays the same right at 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

and here's an image - the flower was my test dynamic entity - it clearly stopped on the brick but i can't figure out why the velocity is just continuing to go up

enter image description here


i don't know what other code would be helpful here - i have a setup that is virtually identical to the Peas Demo from the showcase - to the point where I've deleted things from the Showcase demo and can't recreate the problem in that.

I am 100% at a loss here and since it's a physics problem it's kind of hindering all my progress. The peas demo completely works

Any help would be greatly appreciated - i have tried for 4 hours now trying all the things i can think of and nothing is working


edit added main world class (this is definitely pretty ugly - please don't judge me)

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

Upvotes: 2

Views: 238

Answers (1)

sean christe
sean christe

Reputation: 909

In case anybody runs into this - After a whole day and a half of trying to figure this out and creating a separate simple project dropping 1 dynamic entity onto a static entity and seeing it do the same thing.

If you see this happen where the Entity stops moving but the velocity keeps increasing - the Collision Shape you put in is BACKWARDS - i had 4 vertices being put into the shape in order of 0,1,2,3 - had I put them in as 3,2,1,0 it would have worked fine. I suppose if you hit this same type of problem try reversing the order your creating your shape and see if the collision works.

Upvotes: 2

Related Questions