org.jbox2d.dynamics.World Java Examples

The following examples show how to use org.jbox2d.dynamics.World. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DominoTower.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void makeDomino(float x, float y, boolean horizontal, World world) {

    PolygonShape sd = new PolygonShape();
    sd.setAsBox(.5f * dwidth, .5f * dheight);
    FixtureDef fd = new FixtureDef();
    fd.shape = sd;
    fd.density = ddensity;
    BodyDef bd = new BodyDef();
    bd.type = BodyType.DYNAMIC;
    fd.friction = dfriction;
    fd.restitution = 0.65f;
    bd.position = new Vec2(x, y);
    bd.angle = horizontal ? (float) (Math.PI / 2.0) : 0f;
    Body myBody = getWorld().createBody(bd);
    myBody.createFixture(fd);
  }
 
Example #2
Source File: JBox2DTest.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Test
public void testNewWorld() {
    World world = new World(new Vec2(0, -9.8f));

    BodyDef axisDef = new BodyDef();
    axisDef.type = BodyType.STATIC;
    axisDef.position = new Vec2(3, 3);
    Body axis = world.createBody(axisDef);

    CircleShape axisShape = new CircleShape();
    axisShape.setRadius(0.02f);
    axisShape.m_p.set(0, 0);

    //FixtureDef axisFixture = new FixtureDef();
    //axisFixture.shape = axisShape;
    //axis.createFixture(axisFixture);

}
 
Example #3
Source File: JBox2DSimulation.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public Scene() {
    world = new World(new Vec2(0, -9.8f));
    initAxis();
    initReel();
    joinReelToAxis();
    initBalls();
    lastCalculated = System.currentTimeMillis();
    startTime = lastCalculated;
}
 
Example #4
Source File: Box2d.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private Box2d() {
  // Define the gravity vector.
  Vec2 gravity = new Vec2(0f, -9.8f);

  // Initialise the World.
  world = new World(gravity);

  // Create the ground (Something for dynamic bodies to collide with).
  {
    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.position.set(10, -40);
    groundBodyDef.type = BodyType.STATIC;

    // Create the Body in the World.
    Body ground = world.createBody(groundBodyDef);

    // Create the fixtures (physical aspects) of the ground body.
    FixtureDef groundEdgeFixtureDef = new FixtureDef();
    groundEdgeFixtureDef.density = 1.0f;
    groundEdgeFixtureDef.friction = 1.0f;
    groundEdgeFixtureDef.restitution = 0.4f;

    PolygonShape groundEdge = new PolygonShape();
    groundEdgeFixtureDef.shape = groundEdge;

    // Bottom Edge.
    groundEdge.setAsBox(100, 1);
    ground.createFixture(groundEdgeFixtureDef);
  }

  for (int i = 0; i < NUM_BALLS; i++) {
    spawnBall();
  }
}
 
Example #5
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void init(World world, boolean deserialized) {
  m_world = world;
  pointCount = 0;
  stepCount = 0;
  bombSpawning = false;
  model.getDebugDraw().setViewportTransform(camera.getTransform());

  world.setDestructionListener(destructionListener);
  world.setParticleDestructionListener(particleDestructionListener);
  world.setContactListener(this);
  world.setDebugDraw(model.getDebugDraw());
  title = getTestName();
  initTest(deserialized);
}
 
Example #6
Source File: Joint.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static Joint create(World world, JointDef def) {
  // Joint joint = null;
  switch (def.type) {
    case MOUSE:
      return new MouseJoint(world.getPool(), (MouseJointDef) def);
    case DISTANCE:
      return new DistanceJoint(world.getPool(), (DistanceJointDef) def);
    case PRISMATIC:
      return new PrismaticJoint(world.getPool(), (PrismaticJointDef) def);
    case REVOLUTE:
      return new RevoluteJoint(world.getPool(), (RevoluteJointDef) def);
    case WELD:
      return new WeldJoint(world.getPool(), (WeldJointDef) def);
    case FRICTION:
      return new FrictionJoint(world.getPool(), (FrictionJointDef) def);
    case WHEEL:
      return new WheelJoint(world.getPool(), (WheelJointDef) def);
    case GEAR:
      return new GearJoint(world.getPool(), (GearJointDef) def);
    case PULLEY:
      return new PulleyJoint(world.getPool(), (PulleyJointDef) def);
    case CONSTANT_VOLUME:
      return new ConstantVolumeJoint(world, (ConstantVolumeJointDef) def);
    case ROPE:
      return new RopeJoint(world.getPool(), (RopeJointDef) def);
    case MOTOR:
      return new MotorJoint(world.getPool(), (MotorJointDef) def);
    case UNKNOWN:
    default:
      return null;
  }
}
 
Example #7
Source File: JBox2DTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleBall() {
    World world = new World(new Vec2(0, -9.8f));

    float ballRadius = 0.15f;

    BodyDef ballDef = new BodyDef();
    ballDef.type = BodyType.DYNAMIC;
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.friction = 0.3f;
    fixtureDef.restitution = 0.3f;
    fixtureDef.density = 0.2f;
    CircleShape shape = new CircleShape();
    shape.m_radius = ballRadius;
    fixtureDef.shape = shape;

    int i=0;
    int j=0;

    float x = (j + 0.5f) * (ballRadius * 2 + 0.01f);
    float y = (i + 0.5f) * (ballRadius * 2 + 0.01f);
    ballDef.position.x = 3 + x;
    ballDef.position.y = 3 + y;
    Body theBall = world.createBody(ballDef);
    theBall.createFixture(fixtureDef);

    for (int k=0;k<100;k++) {
        world.step(0.01f, 20, 40);
    }

    Vec2 thePosition = theBall.getPosition();
    int theX = (int)(thePosition.x * 1000);
    int theY = (int) (thePosition.y * 1000);

    System.out.println("Finally ended at ");
    System.out.println(theX);
    System.out.println(theY);
}
 
Example #8
Source File: PbSerializer.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public SerializationResult serialize(World argWorld) {
  final PbWorld world = serializeWorld(argWorld).build();
  return new SerializationResult() {
    @Override
    public void writeTo(OutputStream argOutputStream) throws IOException {
      world.writeTo(argOutputStream);
    }

    @Override
    public Object getValue() {
      return world;
    }
  };
}
 
Example #9
Source File: PbDeserializer.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Joint deserializeJoint(World argWorld, InputStream argInput,
    Map<Integer, Body> argBodyMap, Map<Integer, Joint> jointMap) throws IOException {
  PbJoint joint = PbJoint.parseFrom(argInput);
  return deserializeJoint(argWorld, joint, argBodyMap, jointMap);
}
 
Example #10
Source File: PbDeserializer.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Body deserializeBody(World argWorld, PbBody argBody) {
  PbBody b = argBody;

  BodyDef bd = new BodyDef();
  bd.position.set(pbToVec(b.getPosition()));
  bd.angle = b.getAngle();
  bd.linearDamping = b.getLinearDamping();
  bd.angularDamping = b.getAngularDamping();
  bd.gravityScale = b.getGravityScale();
  // velocities are populated after fixture addition
  
  bd.bullet = b.getBullet();
  bd.allowSleep = b.getAllowSleep();
  bd.awake = b.getAwake();
  bd.active = b.getActive();
  bd.fixedRotation = b.getFixedRotation();

  switch (b.getType()) {
    case DYNAMIC:
      bd.type = BodyType.DYNAMIC;
      break;
    case KINEMATIC:
      bd.type = BodyType.KINEMATIC;
      break;
    case STATIC:
      bd.type = BodyType.STATIC;
      break;
    default:
      UnsupportedObjectException e =
          new UnsupportedObjectException("Unknown body type: " + argBody.getType(), Type.BODY);
      if (ulistener == null || ulistener.isUnsupported(e)) {
        throw e;
      }
      return null;
  }

  Body body = argWorld.createBody(bd);

  for (int i = 0; i < b.getFixturesCount(); i++) {
    deserializeFixture(body, b.getFixtures(i));
  }
  
  // adding fixtures can change this, so we put this here and set it directly in the body
  body.m_linearVelocity.set(pbToVec(b.getLinearVelocity()));
  body.m_angularVelocity = b.getAngularVelocity();

  if (listener != null && b.hasTag()) {
    listener.processBody(body, b.getTag());
  }
  return body;
}
 
Example #11
Source File: PbDeserializer.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Body deserializeBody(World argWorld, InputStream argInput) throws IOException {
  PbBody body = PbBody.parseFrom(argInput);
  return deserializeBody(argWorld, body);
}
 
Example #12
Source File: PbDeserializer.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public World deserializeWorld(InputStream argInput) throws IOException {
  PbWorld world = PbWorld.parseFrom(argInput);
  return deserializeWorld(world);
}
 
Example #13
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void processWorld(World argWorld, Long argTag) {
  listener.processWorld(argWorld, argTag);
}
 
Example #14
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Long getTag(World argWorld) {
  return delegate.getTag(argWorld);
}
 
Example #15
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void init(World world, Shape shape, Vec2 velocity) {
  this.world = world;
  this.shape = shape;
  this.velocity = velocity;
}
 
Example #16
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void processWorld(World world, Long tag) {}
 
Example #17
Source File: PbSerializer.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public PbWorld.Builder serializeWorld(World argWorld) {
  final PbWorld.Builder builder = PbWorld.newBuilder();
  if (signer != null) {
    Long tag = signer.getTag(argWorld);
    if (tag != null) {
      builder.setTag(tag);
    }
  }

  builder.setGravity(vecToPb(argWorld.getGravity()));
  builder.setAutoClearForces(argWorld.getAutoClearForces());
  builder.setAllowSleep(argWorld.isAllowSleep());
  builder.setContinuousPhysics(argWorld.isContinuousPhysics());
  builder.setWarmStarting(argWorld.isWarmStarting());
  builder.setSubStepping(argWorld.isSubStepping());

  Body cbody = argWorld.getBodyList();
  int cnt = 0;
  HashMap<Body, Integer> bodies = new HashMap<Body, Integer>();

  while (cbody != null) {
    builder.addBodies(serializeBody(cbody));
    bodies.put(cbody, cnt);
    cnt++;
    cbody = cbody.m_next;
  }
  cnt = 0;
  HashMap<Joint, Integer> joints = new HashMap<Joint, Integer>();
  Joint cjoint = argWorld.getJointList();
  // first pass
  while (cjoint != null) {
    if (SerializationHelper.isIndependentJoint(cjoint.getType())) {
      builder.addJoints(serializeJoint(cjoint, bodies, joints));
      joints.put(cjoint, cnt);
      cnt++;
    }
    cjoint = cjoint.m_next;
  }

  // second pass for dependent joints
  cjoint = argWorld.getJointList();
  while (cjoint != null) {
    if (!SerializationHelper.isIndependentJoint(cjoint.getType())) {
      builder.addJoints(serializeJoint(cjoint, bodies, joints));
      joints.put(cjoint, cnt);
      cnt++;
    }
    cjoint = cjoint.m_next;
  }

  return builder;
}
 
Example #18
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Gets the current world
 */
public World getWorld() {
  return m_world;
}
 
Example #19
Source File: DefaultWorldCreator.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public World createWorld(Vec2 gravity) {
  return new World(gravity);
}
 
Example #20
Source File: JBox2DSimulation.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public World getWorld() {
    return world;
}
 
Example #21
Source File: JBox2DTest.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public World getWorld() {
    return world;
}
 
Example #22
Source File: JBox2DTest.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Test
public void testNewWorld2() {
    World world = new World(new Vec2(0, -9.8f));
    Assert.assertFalse(world.isLocked());
}
 
Example #23
Source File: Createbox2d.java    From Form-N-Fun with MIT License 4 votes vote down vote up
Createbox2d()
{
    world = new World(gravity);
    world.setWarmStarting(true);
    world.setContinuousPhysics(true);
}
 
Example #24
Source File: Createbox2d.java    From Form-N-Fun with MIT License 4 votes vote down vote up
/**
 *  returns world object
 */
public World getWorld() {
    return world;
}
 
Example #25
Source File: ParticleSystem.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ParticleSystem(World world) {
  m_world = world;
  m_timestamp = 0;
  m_allParticleFlags = 0;
  m_allGroupFlags = 0;
  m_density = 1;
  m_inverseDensity = 1;
  m_gravityScale = 1;
  m_particleDiameter = 1;
  m_inverseDiameter = 1;
  m_squaredDiameter = 1;

  m_count = 0;
  m_internalAllocatedCapacity = 0;
  m_maxCount = 0;

  m_proxyCount = 0;
  m_proxyCapacity = 0;

  m_contactCount = 0;
  m_contactCapacity = 0;

  m_bodyContactCount = 0;
  m_bodyContactCapacity = 0;

  m_pairCount = 0;
  m_pairCapacity = 0;

  m_triadCount = 0;
  m_triadCapacity = 0;

  m_groupCount = 0;

  m_pressureStrength = 0.05f;
  m_dampingStrength = 1.0f;
  m_elasticStrength = 0.25f;
  m_springStrength = 0.25f;
  m_viscousStrength = 0.25f;
  m_surfaceTensionStrengthA = 0.1f;
  m_surfaceTensionStrengthB = 0.2f;
  m_powderStrength = 0.5f;
  m_ejectionStrength = 0.5f;
  m_colorMixingStrength = 0.5f;

  m_flagsBuffer = new ParticleBufferInt();
  m_positionBuffer = new ParticleBuffer<Vec2>(Vec2.class);
  m_velocityBuffer = new ParticleBuffer<Vec2>(Vec2.class);
  m_colorBuffer = new ParticleBuffer<ParticleColor>(ParticleColor.class);
  m_userDataBuffer = new ParticleBuffer<Object>(Object.class);
}
 
Example #26
Source File: SettingsPerformanceTest.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void setupTest(int testNum) {
  World w = new World(new Vec2(0, -10));
  world.setupWorld(w);
}
 
Example #27
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Long getTag(World world) {
  return null;
}
 
Example #28
Source File: JbDeserializer.java    From jbox2d with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Deserializes a world
 * @param input
 * @return
 * @throws IOException
 * @throws UnsupportedObjectException if a read physics object is unsupported by this library
 * @see #setUnsupportedListener(UnsupportedListener)
 */
public World deserializeWorld(InputStream input) throws IOException, UnsupportedObjectException;
 
Example #29
Source File: JbDeserializer.java    From jbox2d with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
  * Deserializes a body
  * @param world
  * @param input
  * @return
  * @throws IOException
  * @throws UnsupportedObjectException if a read physics object is unsupported by this library
  * @see #setUnsupportedListener(UnsupportedListener)
  */
public Body deserializeBody(World world, InputStream input) throws IOException, UnsupportedObjectException;
 
Example #30
Source File: JbDeserializer.java    From jbox2d with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
  * Deserializes a joint
  * @param world
  * @param input
  * @param bodyMap
  * @param jointMap
  * @return
  * @throws IOException
  * @throws UnsupportedObjectException if a read physics object is unsupported by this library
  * @see #setUnsupportedListener(UnsupportedListener)
  */
public Joint deserializeJoint(World world, InputStream input, Map<Integer, 
							  Body> bodyMap, Map<Integer, Joint> jointMap) throws IOException, UnsupportedObjectException;