/** * Creates a fixture from a shape and attach it to this body. This is a convenience function. Use * FixtureDef if you need to set parameters like friction, restitution, user data, or filtering. * If the density is non-zero, this function automatically updates the mass of the body. * * @param shape the shape to be cloned. * @param density the shape density (set to zero for static bodies). * @warning This function is locked during callbacks. */ public final Fixture createFixture(Shape shape, float density) { fixDef.shape = shape; fixDef.density = density; return createFixture(fixDef); }
/** Creates a fixture from a shape and attach it to this body. This is a convenience function. Use b2FixtureDef if you need to * set parameters like friction, restitution, user data, or filtering. If the density is non-zero, this function automatically * updates the mass of the body. * @param shape the shape to be cloned. * @param density the shape density (set to zero for static bodies). * @warning This function is locked during callbacks. */ public Fixture createFixture (Shape shape, float density) { org.jbox2d.dynamics.Fixture f = body.createFixture(shape.shape, density); Fixture fixture = new Fixture(this, f); fixtures.add(fixture); world.fixtures.put(f, fixture); return fixture; }
/** Creates a fixture and attach it to this body. Use this function if you need to set some fixture parameters, like friction. * Otherwise you can create the fixture directly from a shape. If the density is non-zero, this function automatically updates * the mass of the body. Contacts are not created until the next time step. * @param def the fixture definition. * @warning This function is locked during callbacks. */ public Fixture createFixture (FixtureDef def) { org.jbox2d.dynamics.FixtureDef fd = def.toJBox2d(); org.jbox2d.dynamics.Fixture f = body.createFixture(fd); Fixture fixture = new Fixture(this, f); fixtures.add(fixture); world.fixtures.put(f, fixture); return fixture; }
ballDef.position.y = 3 + y; Body body = world.createBody(ballDef); body.createFixture(fixtureDef); body.createFixture(fixtureDef); body.createFixture(fixtureDef); body.createFixture(fixtureDef);
private void initReel() { BodyDef reelDef = new BodyDef(); reelDef.type = BodyType.DYNAMIC; reelDef.position = new Vec2(3, 3); reel = world.createBody(reelDef); FixtureDef fixture = new FixtureDef(); fixture.friction = 0.5f; fixture.restitution = 0.4f; fixture.density = 1; int parts = 30; for (int i = 0; i < parts; ++i) { PolygonShape shape = new PolygonShape(); double angle1 = i / (double) parts * 2 * Math.PI; double x1 = 2.7 * Math.cos(angle1); double y1 = 2.7 * Math.sin(angle1); double angle2 = (i + 1) / (double) parts * 2 * Math.PI; double x2 = 2.7 * Math.cos(angle2); double y2 = 2.7 * Math.sin(angle2); double angle = (angle1 + angle2) / 2; double x = 0.01 * Math.cos(angle); double y = 0.01 * Math.sin(angle); shape.set(new Vec2[] { new Vec2((float) x1, (float) y1), new Vec2((float) x2, (float) y2), new Vec2((float) (x2 - x), (float) (y2 - y)), new Vec2((float) (x1 - x), (float) (y1 - y)) }, 4); fixture.shape = shape; reel.createFixture(fixture); } }
private void initAxis() { BodyDef axisDef = new BodyDef(); axisDef.type = BodyType.STATIC; axisDef.position = new Vec2(3, 3); 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); }
/** * Creates a fixture from a shape and attach it to this body. This is a convenience function. Use * FixtureDef if you need to set parameters like friction, restitution, user data, or filtering. * If the density is non-zero, this function automatically updates the mass of the body. * * @param shape the shape to be cloned. * @param density the shape density (set to zero for static bodies). * @warning This function is locked during callbacks. */ public final Fixture createFixture(Shape shape, float density) { fixDef.shape = shape; fixDef.density = density; return createFixture(fixDef); }
public void createCircle() { float radius = 2.0f; CircleShape shape = new CircleShape(); shape.m_p.setZero(); shape.m_radius = radius; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; fd.friction = 0.0f; Vec2 p = new Vec2((float)Math.random(), 3.0f + (float)Math.random()); BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position = p; //bd.allowSleep = false; Body body = getWorld().createBody(bd); body.createFixture(fd); }
void Create(int index) { BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; float x = MathUtils.randomFloat(-2.0f, 2.0f); bd.position.set(x, 10.0f); bd.angle = MathUtils.randomFloat(-MathUtils.PI, MathUtils.PI); if (index == 4) { bd.angularDamping = 0.02f; } m_bodies[m_bodyIndex] = getWorld().createBody(bd); if (index < 4) { FixtureDef fd = new FixtureDef(); fd.shape = m_polygons[index]; fd.density = 1.0f; fd.friction = 0.3f; m_bodies[m_bodyIndex].createFixture(fd); } else { FixtureDef fd = new FixtureDef(); fd.shape = m_circle; fd.density = 1.0f; fd.friction = 0.3f; m_bodies[m_bodyIndex].createFixture(fd); } m_bodyIndex = (m_bodyIndex + 1) % k_maxBodies; }
@Override public synchronized void step(TestbedSettings settings) { super.step(settings); if (m_count < MAX_NUM) { BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position.set(0.0f, 10.0f); Body body = m_world.createBody(bd); PolygonShape shape = new PolygonShape(); shape.setAsBox(0.125f, 0.125f); body.createFixture(shape, 1.0f); ++m_count; } }
@Override public void keyPressed(char key, int argKeyCode) { switch (key) { case 'c': if (m_fixture2 == null) { CircleShape shape = new CircleShape(); shape.m_radius = 3.0f; shape.m_p.set(0.5f, -4.0f); m_fixture2 = m_body.createFixture(shape, 10.0f); m_body.setAwake(true); } break; case 'd': if (m_fixture2 != null) { m_body.destroyFixture(m_fixture2); m_fixture2 = null; m_body.setAwake(true); } break; } }
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); }
@Override public void initTest(boolean argDeserialized) { // Ground body { BodyDef bd = new BodyDef(); Body ground = getWorld().createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); ground.createFixture(shape, 0.0f); } // Breakable dynamic body { BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position.set(0.0f, 40.0f); bd.angle = 0.25f * MathUtils.PI; m_body1 = getWorld().createBody(bd); m_shape1 = new PolygonShape(); m_shape1.setAsBox(0.5f, 0.5f, new Vec2(-0.5f, 0.0f), 0.0f); m_piece1 = m_body1.createFixture(m_shape1, 1.0f); m_shape2 = new PolygonShape(); m_shape2.setAsBox(0.5f, 0.5f, new Vec2(0.5f, 0.0f), 0.0f); m_piece2 = m_body1.createFixture(m_shape2, 1.0f); } m_break = false; m_broke = false; }
@Override public void initTest(boolean argDeserialized) { { BodyDef bd = new BodyDef(); Body ground = getWorld().createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); ground.createFixture(shape, 0.0f); } BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position.set(0.0f, 10.0f); m_body = getWorld().createBody(bd); PolygonShape shape = new PolygonShape(); shape.setAsBox(4.0f, 4.0f, new Vec2(0.0f, 0.0f), 0.0f); m_fixture1 = m_body.createFixture(shape, 10.0f); m_fixture2 = null; }
@Override public void initTest(boolean deserialized) { if (deserialized) { return; } Body bodies[] = new Body[e_count]; { BodyDef bd = new BodyDef(); Body ground = getWorld().createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); ground.createFixture(shape, 0.0f); } { CircleShape shape = new CircleShape(); shape.m_radius = 1.0f; for (int i = 0; i < e_count; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position.set(0.0f, 4.0f + 3.0f * i); bodies[i] = getWorld().createBody(bd); bodies[i].createFixture(shape, 1.0f); // m_bodies[i].setLinearVelocity(new Vec2(0.0f, -100.0f)); } } }
@Override public void keyPressed(char argKeyChar, int argKeyCode) { switch (argKeyChar) { case ',': if (m_bullet != null) { getWorld().destroyBody(m_bullet); m_bullet = null; } { CircleShape shape = new CircleShape(); shape.m_radius = 0.25f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.restitution = 0.05f; BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.bullet = true; bd.position.set(-31.0f, 5.0f); m_bullet = getWorld().createBody(bd); m_bullet.createFixture(fd); m_bullet.setLinearVelocity(new Vec2(400.0f, 0.0f)); } break; } }
public Fixture deserializeFixture(Body argBody, PbFixture argFixture) { PbFixture f = argFixture; FixtureDef fd = new FixtureDef(); fd.density = f.getDensity(); fd.filter.categoryBits = f.getFilter().getCategoryBits(); fd.filter.groupIndex = f.getFilter().getGroupIndex(); fd.filter.maskBits = f.getFilter().getMaskBits(); fd.friction = f.getFriction(); fd.isSensor = f.getSensor(); fd.restitution = f.getRestitution(); fd.shape = deserializeShape(f.getShape()); Fixture fixture = argBody.createFixture(fd); if (listener != null && f.hasTag()) { listener.processFixture(fixture, f.getTag()); } return fixture; }
@Override public void initTest(boolean deserialized) { { BodyDef bd = new BodyDef(); Body ground = m_world.createBody(bd); ChainShape shape = new ChainShape(); Vec2[] vertices = new Vec2[] {new Vec2(-20, 0), new Vec2(20, 0), new Vec2(20, 40), new Vec2(-20, 40)}; shape.createLoop(vertices, 4); ground.createFixture(shape, 0.0f); } m_world.setParticleRadius(0.15f); m_world.setParticleDamping(0.2f); { PolygonShape shape = new PolygonShape(); shape.setAsBox(8, 10, new Vec2(-12, 10.1f), 0); ParticleGroupDef pd = new ParticleGroupDef(); pd.shape = shape; m_world.createParticleGroup(pd); } }
private void launchBomb(Vec2 position, Vec2 velocity) { if (bomb != null) { m_world.destroyBody(bomb); bomb = null; } // todo optimize this BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position.set(position); bd.bullet = true; bomb = m_world.createBody(bd); bomb.setLinearVelocity(velocity); CircleShape circle = new CircleShape(); circle.m_radius = 0.3f; FixtureDef fd = new FixtureDef(); fd.shape = circle; fd.density = 20f; fd.restitution = 0; Vec2 minV = new Vec2(position); Vec2 maxV = new Vec2(position); minV.subLocal(new Vec2(.3f, .3f)); maxV.addLocal(new Vec2(.3f, .3f)); aabb.lowerBound.set(minV); aabb.upperBound.set(maxV); bomb.createFixture(fd); }
void Break() { // Create two bodies from one. Body body1 = m_piece1.getBody(); Vec2 center = body1.getWorldCenter(); body1.destroyFixture(m_piece2); m_piece2 = null; BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position = body1.getPosition(); bd.angle = body1.getAngle(); Body body2 = getWorld().createBody(bd); m_piece2 = body2.createFixture(m_shape2, 1.0f); // Compute consistent velocities for new bodies based on // cached velocity. Vec2 center1 = body1.getWorldCenter(); Vec2 center2 = body2.getWorldCenter(); Vec2 velocity1 = m_velocity.add(Vec2.cross(m_angularVelocity, center1.sub(center))); Vec2 velocity2 = m_velocity.add(Vec2.cross(m_angularVelocity, center2.sub(center))); body1.setAngularVelocity(m_angularVelocity); body1.setLinearVelocity(velocity1); body2.setAngularVelocity(m_angularVelocity); body2.setLinearVelocity(velocity2); }