public void addAreaObject(String worldname, String objname, LayoutArea area, ObjectProps props) { Box2dData data = this.mBox2dWorlds.get(worldname); if (data == null) { return; } // Dynamic Body BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DYNAMIC; bodyDef.position.set(area.mLocation[0], area.mLocation[1]); Body body = data.mWorld.createBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); PolygonShape dynamicBox = new PolygonShape(); dynamicBox.setAsBox(area.mBounds[0] / 2, area.mBounds[1] / 2); fixtureDef.shape = dynamicBox; fixtureDef.density = props.mDensity; fixtureDef.friction = props.mFriction; fixtureDef.restitution = props.mRestitution; // kinematicBody fixtureDef.filter.groupIndex = props.mGroupIndex; body.createFixture(fixtureDef); BodyData bodydata = new BodyData(); bodydata.mArea = area; bodydata.mBody = body; bodydata.mProps = props; area.assignPsyData(bodydata); data.mAreaModels.add(bodydata); }
@Override public void create() { { PolygonDef sd = new PolygonDef(); sd.setAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.position = new Vec2(0.0f, -10.0f); m_world.createStaticBody(bd).createShape(sd); } { PolygonDef sd = new PolygonDef(); float w = 4.0f; float h = 0.25f; sd.setAsBox(w, h); sd.density = 1.0f; sd.friction = 0.3f; sd.restitution = 0.0f; BodyDef bd = new BodyDef(); int numSlats = 8; float lastCMX = 0.0f; float eps = 0.14f; for (int i = 0; i < numSlats; ++i) { float newX = lastCMX + w - eps; lastCMX = (i * lastCMX + newX) / (i + 1); bd.position = new Vec2(newX, .25f + 2 * h * (numSlats - i - 1)); Body myBody = m_world.createDynamicBody(bd); myBody.createShape(sd); myBody.setMassFromShapes(); } } }
private Body initPhysicsBody(World world, float x, float y) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DYNAMIC; bodyDef.position = new Vec2(0, 0); Body body = world.createBody(bodyDef); PolygonShape shape = new PolygonShape(); Transform fx = new Transform(); fx.position.set(100f, 100f); shape.centroid(fx); shape.setAsBox( sprite.layer().width() * GameScreen.M_PER_PIXEL / 2, sprite.layer().height() * GameScreen.M_PER_PIXEL / 2); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = 0.4f; fixtureDef.friction = 0.1f; fixtureDef.restitution = 0f; body.createFixture(fixtureDef); body.setLinearDamping(0.2f); body.setTransform(new Vec2(x, y), 0f); // MassData md = body.getMassData(); // massD.center.set(2f, 0); body.setMassData(massD); return body; }
/** 创建多边形物体 */ private void createBody() { // 定义物体的形状是多边形形状 PolygonShape shape = new PolygonShape(); shape.set(vecs, edge); // 设置多边形物体的一些固定的物理属性 FixtureDef fd = new FixtureDef(); fd.shape = shape; // 固定设置参数 fd.density = 5.0f; // 密度 fd.friction = 0.3f; // 摩擦系数 fd.restitution = restitution; // 恢复系数 BodyDef bd = new BodyDef(); bd.position.set(x, y); bd.type = BodyType.DYNAMIC; bd.angle = angle; bd.allowSleep = true; bd.setAwake(true); // 根据bodyDef创建物体到世界中 body = world.createBody(bd); // 设置好物体的固定属性 body.createFixture(fd); }
@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)); } } }
public void initTest(boolean argDeserialized) { if (argDeserialized) { return; } { // Floor FixtureDef fd = new FixtureDef(); PolygonShape sd = new PolygonShape(); sd.setAsBox(50.0f, 10.0f); fd.shape = sd; BodyDef bd = new BodyDef(); bd.position = new Vec2(0.0f, -10.0f); getWorld().createBody(bd).createFixture(fd); } { // Platforms for (int i = 0; i < 4; i++) { FixtureDef fd = new FixtureDef(); PolygonShape sd = new PolygonShape(); sd.setAsBox(25.0f, 0.125f); fd.shape = sd; BodyDef bd = new BodyDef(); bd.position = new Vec2(0.0f, 5f + 5f * i); getWorld().createBody(bd).createFixture(fd); } } { FixtureDef fd = new FixtureDef(); PolygonShape sd = new PolygonShape(); sd.setAsBox(0.125f, 2f); fd.shape = sd; fd.density = 25.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; float friction = .5f; int numPerRow = 25; for (int i = 0; i < 4; ++i) { for (int j = 0; j < numPerRow; j++) { fd.friction = friction; bd.position = new Vec2(-14.75f + j * (29.5f / (numPerRow - 1)), 7.3f + 5f * i); if (i == 2 && j == 0) { bd.angle = -0.1f; bd.position.x += .1f; } else if (i == 3 && j == numPerRow - 1) { bd.angle = .1f; bd.position.x -= .1f; } else bd.angle = 0f; Body myBody = getWorld().createBody(bd); myBody.createFixture(fd); } } } }
@Override protected void bodyDefB4CreationCallback(BodyDef def) { super.bodyDefB4CreationCallback(def); // def.linearDamping = 0.15f; def.linearDamping = 0.25f; def.isBullet = true; def.angularDamping = 0.9f; // def.fixedRotation = true; }
public void create() { if (firstTime) { setCamera(0f, 10f, 10f); firstTime = false; } { PolygonDef sd = new PolygonDef(); sd.setAsBox(50.0f, 10.0f, new Vec2(0.0f, -10.0f), 0.0f); BodyDef bd = new BodyDef(); bd.position.set(0.0f, 0.0f); Body ground = m_world.createStaticBody(bd); ground.createShape(sd); sd.setAsBox(0.1f, 10.0f, new Vec2(20.0f, 10.0f), 0.0f); ground.createShape(sd); } float[] xs = {0.0f, -10.0f, -5.0f, 5.0f, 10.0f}; for (int j = 0; j < xs.length; ++j) { PolygonDef sd = new PolygonDef(); sd.setAsBox(0.5f, 0.5f); sd.density = 1.0f; sd.friction = 0.3f; for (int i = 0; i < 12; ++i) { BodyDef bd = new BodyDef(); // For this test we are using continuous physics for all boxes. // This is a stress test, you normally wouldn't do this for // performance reasons. bd.isBullet = true; bd.allowSleep = true; // float32 x = b2Random(-0.1f, 0.1f); // float32 x = i % 2 == 0 ? -0.025f : 0.025f; bd.position.set(xs[j] + parent.random(-.05f, .05f), 0.752f + 1.54f * i); // bd.position.Set(xs[j], 2.51f + 4.02f * i); Body body = m_world.createDynamicBody(bd); body.createShape(sd); body.setMassFromShapes(); } } }
@Override Body initPhysicsBody(World world, float x, float y, float width, float height, float angle) { FixtureDef fixtureDef = new FixtureDef(); BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DYNAMIC; bodyDef.position = new Vec2(0, 0); Body body = world.createBody(bodyDef); CircleShape circleShape = new CircleShape(); circleShape.m_radius = RADIUS; fixtureDef.shape = circleShape; fixtureDef.density = 0.4f; fixtureDef.friction = 1f; fixtureDef.restitution = 0.0f; circleShape.m_p.set(0, 0); body.createFixture(fixtureDef); // body.setLinearDamping(0.2f); body.setTransform(new Vec2(x, y), angle); return body; }
public void initWorld() { Vec2 gravity = new Vec2(0.0f, 9.8f); world = new World(gravity); // ground FixtureDef groundFixtureDef = new FixtureDef(); PolygonShape groundShape = new PolygonShape(); groundShape.setAsBox(20f, 0.1f); groundFixtureDef.shape = groundShape; groundFixtureDef.density = 25.0f; groundFixtureDef.filter = new Filter(); groundFixtureDef.filter.categoryBits = 0x0001; BodyDef groundBodyDef = new BodyDef(); groundBodyDef.position = new Vec2(0.0f, 3f); groundBodyDef.angle = 0.0f; groundBodyDef.type = BodyType.STATIC; groundBody = world.createBody(groundBodyDef); groundFixture = groundBody.createFixture(groundFixtureDef); }
private void checkBounds() { for (int i = 0; i < liquid.length; ++i) { if (liquid[i].getMemberWorldCenter().y < -10.0f) { m_world.destroyBody(liquid[i]); float massPerParticle = totalMass / nParticles; CircleDef pd = new CircleDef(); pd.density = 1.0f; pd.filter.groupIndex = -10; pd.radius = .05f; pd.restitution = 0.4f; pd.friction = 0.0f; float cx = 0.0f + parent.random(-0.6f, 0.6f); float cy = 15.0f + parent.random(-2.3f, 2.0f); BodyDef bd = new BodyDef(); bd.position = new Vec2(cx, cy); bd.fixedRotation = true; Body b = m_world.createBody(bd); b.createShape(pd).setUserData(LIQUID_INT); MassData md = new MassData(); md.mass = massPerParticle; md.I = 1.0f; b.setMass(md); b.allowSleeping(false); liquid[i] = b; } } if (bod.getMemberWorldCenter().y < -15.0f) { m_world.destroyBody(bod); PolygonDef polyDef = new PolygonDef(); polyDef.setAsBox(parent.random(0.3f, 0.7f), parent.random(0.3f, 0.7f)); polyDef.density = 1.0f; BodyDef bodyDef = new BodyDef(); bodyDef.position = new Vec2(0.0f, 25.0f); bod = m_world.createBody(bodyDef); bod.createShape(polyDef); bod.setMassFromShapes(); } }
public void addDynObject( String worldname, String objname, GameonModelRef ref, ObjectProps props) { Box2dData data = this.mBox2dWorlds.get(worldname); if (data == null) { return; } // Dynamic Body BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DYNAMIC; bodyDef.position.set(ref.mPosition[0], ref.mPosition[1]); Body body = data.mWorld.createBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); if (props.mShape == SHAPE_BOX) { PolygonShape dynamicBox = new PolygonShape(); dynamicBox.setAsBox(ref.mScale[0] / 2, ref.mScale[1] / 2); fixtureDef.shape = dynamicBox; } else { CircleShape dynamicCircle = new CircleShape(); dynamicCircle.m_radius = ref.mScale[0] / 2; fixtureDef.shape = dynamicCircle; } fixtureDef.density = props.mDensity; fixtureDef.friction = props.mFriction; fixtureDef.restitution = props.mRestitution; // kinematicBody fixtureDef.filter.groupIndex = props.mGroupIndex; body.createFixture(fixtureDef); BodyData bodydata = new BodyData(); bodydata.mRef = ref; bodydata.mBody = body; bodydata.mProps = props; ref.assignPsyData(bodydata); data.mDynModels.add(bodydata); }
public PhysicsObject(float x, float y, int w, int h, BodyType t) { pos.x = x; pos.y = y; height = h; width = w; shape = new PolygonShape(); shape.setAsBox( (width / 2) / PhysicsInvaders.PTM_RATIO, (height / 2) / PhysicsInvaders.PTM_RATIO); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 100; fd.friction = 0.9f; fd.restitution = 0.1f; BodyDef bodyDef = new BodyDef(); bodyDef.position.set(pos.x / PhysicsInvaders.PTM_RATIO, pos.y / PhysicsInvaders.PTM_RATIO); bodyDef.type = t; body = PhysicsInvaders.world.createBody(bodyDef); body.createFixture(fd); }
// Constructor Box(PApplet parent, PBox2D box2d, float x, float y, float w_, float h_, boolean lock) { this.parent = parent; this.box2d = box2d; w = w_; h = h_; // Define and create the body BodyDef bd = new BodyDef(); bd.position.set(box2d.coordPixelsToWorld(new Vec2(x, y))); if (lock) bd.type = BodyType.STATIC; else bd.type = BodyType.DYNAMIC; body = box2d.createBody(bd); // Define the shape -- a (this is what we use for a rectangle) PolygonShape sd = new PolygonShape(); float box2dW = box2d.scalarPixelsToWorld(w / 2); float box2dH = box2d.scalarPixelsToWorld(h / 2); sd.setAsBox(box2dW, box2dH); // Define a fixture FixtureDef fd = new FixtureDef(); fd.shape = sd; // Parameters that affect physics fd.density = 1; fd.friction = 0.3f; fd.restitution = 0.5f; body.createFixture(fd); // Give it some initial random velocity body.setLinearVelocity(new Vec2(parent.random(-5, 5), parent.random(2, 5))); body.setAngularVelocity(parent.random(-5, 5)); }
/** * Create a new paddle. * * @param world The world the paddle will be added to. * @param y The height of the paddle (starts centered on the x) * @param mode The keys that will move this paddle */ public Paddle(World world, int y, MovementMode mode) { Mode = mode; // Respond to the keyboard KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); manager.addKeyEventDispatcher(this); // Add the physics body BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position.set(world.Bounds.width / 2, y); bd.linearDamping = 0.0f; bd.angularDamping = 0.01f; FixtureDef fd = new FixtureDef(); fd.shape = new PolygonShape(); ((PolygonShape) fd.shape).setAsBox(SIZE.width / 2, SIZE.height / 2); fd.density = 0.1f; fd.friction = 0.0f; fd.restitution = 1.0f; j2dBody = world.j2dWorld.createBody(bd); j2dBody.createFixture(fd); j2dBody.setUserData(this); // Limit it to only x movement BodyDef gbd = new BodyDef(); gbd.type = BodyType.STATIC; bd.position.set(world.Bounds.width / 2, y); PrismaticJointDef pjd = new PrismaticJointDef(); pjd.collideConnected = true; pjd.initialize( j2dBody, world.j2dWorld.createBody(gbd), j2dBody.getWorldCenter(), new Vec2(1.0f, 0.0f)); world.j2dWorld.createJoint(pjd); }
/** * Rozbije objekt. Upravi objekt world tak, ze vymaze triesteny objekt a nahradi ho fragmentami na * zaklade nastaveneho materialu a clenskych premennych. * * @param dt casova dlzka framu */ public void smash(float dt) { if (contact == null) { // riesi sa staticky prvok, ktory ma priliz maly obsah b1.setType(BodyType.DYNAMIC); return; } World w = b1.m_world; Shape s = f1.m_shape; Polygon p = f1.m_polygon; if (p == null) { switch (s.m_type) { case POLYGON: PolygonShape ps = (PolygonShape) s; Vec2[] vertices = ps.m_vertices; p = new Polygon(); for (int i = 0; i < ps.m_count; ++i) { p.add(vertices[ps.m_count - i - 1]); } break; case CIRCLE: CircleShape cs = (CircleShape) s; p = new Polygon(); float radius = cs.m_radius; double u = Math.PI * 2 / CIRCLEVERTICES; radius = (float) Math.sqrt(u / Math.sin(u)) * radius; // upravim radius tak, aby bola zachovana velkost obsahu Vec2 center = cs.m_p; for (int i = 0; i < CIRCLEVERTICES; ++i) { double j = u * i; // uhol float sin = (float) Math.sin(j); float cos = (float) Math.cos(j); Vec2 v = new Vec2(sin, cos).mulLocal(radius).addLocal(center); p.add(v); } break; default: throw new RuntimeException("Dany typ tvaru nepodporuje stiepenie"); } } float mConst = f1.m_material.m_rigidity / normalImpulse; // sila v zavislosti na pevnosti telesa boolean fixA = f1 == contact.m_fixtureA; // true, ak f2 je v objekte contact ako m_fixtureA float oldAngularVelocity = fixA ? contact.m_angularVelocity_bodyA : contact.m_angularVelocity_bodyB; Vec2 oldLinearVelocity = fixA ? contact.m_linearVelocity_bodyA : contact.m_linearVelocity_bodyB; b1.setAngularVelocity( (b1.m_angularVelocity - oldAngularVelocity) * mConst + oldAngularVelocity); b1.setLinearVelocity( b1.m_linearVelocity.sub(oldLinearVelocity).mulLocal(mConst).addLocal(oldLinearVelocity)); if (!w.isFractured(f2) && b2.m_type == BodyType.DYNAMIC && !b2.m_fractureTransformUpdate) { // ak sa druhy objekt nerozbija, tak sa jej nahodia // povodne hodnoty (TREBA MODIFIKOVAT POHYB OBJEKTU, // KTORY SPOSOBUJE ROZPAD) oldAngularVelocity = !fixA ? contact.m_angularVelocity_bodyA : contact.m_angularVelocity_bodyB; oldLinearVelocity = !fixA ? contact.m_linearVelocity_bodyA : contact.m_linearVelocity_bodyB; b2.setAngularVelocity( (b2.m_angularVelocity - oldAngularVelocity) * mConst + oldAngularVelocity); b2.setLinearVelocity( b2.m_linearVelocity.sub(oldLinearVelocity).mulLocal(mConst).addLocal(oldLinearVelocity)); b2.setTransform( b2.m_xf0.p.add(b2.m_linearVelocity.mul(dt)), b2.m_xf0.q.getAngle()); // osetruje jbox2d od posuvania telesa pri rieseni kolizie b2.m_fractureTransformUpdate = true; } Vec2 localPoint = Transform.mulTrans(b1.m_xf, point); Vec2 b1Vec = b1.getLinearVelocityFromWorldPoint(point); Vec2 b2Vec = b2.getLinearVelocityFromWorldPoint(point); Vec2 localVector = b2Vec.subLocal(b1Vec); localVector.mulLocal(dt); Polygon[] fragment; try { fragment = m.split(p, localPoint, localVector, normalImpulse); // rodeli to } catch (RuntimeException ex) { return; } if (fragment.length == 1) { // nerozbilo to na ziadne fragmenty return; } // definuje tela fragmentov - tie maju vsetky rovnaku definiciu (preberaju parametre z povodneho // objektu) BodyDef bodyDef = new BodyDef(); bodyDef.position.set(b1.m_xf.p); // pozicia bodyDef.angle = b1.m_xf.q.getAngle(); // otocenie bodyDef.fixedRotation = b1.isFixedRotation(); bodyDef.angularDamping = b1.m_angularDamping; bodyDef.allowSleep = b1.isSleepingAllowed(); FixtureDef fd = new FixtureDef(); fd.friction = f1.m_friction; // trenie fd.restitution = f1.m_restitution; // odrazivost fd.isSensor = f1.m_isSensor; fd.density = f1.m_density; // odstrani fragmentacne predmety/cele teleso ArrayList<Fixture> fixtures = new ArrayList<>(); if (f1.m_polygon != null) { for (Fixture f = b1.m_fixtureList; f != null; f = f.m_next) { if (f.m_polygon == f1.m_polygon) { fixtures.add(f); } } } else { fixtures.add(f1); } for (Fixture f : fixtures) { b1.destroyFixture(f); } if (b1.m_fixtureCount == 0) { w.destroyBody(b1); } // prida fragmenty do simulacie MyList<Body> newbodies = new MyList<>(); for (Polygon pg : fragment) { // vytvori tela, prida fixtury, poriesi konvexnu dekompoziciu if (pg.isCorrect()) { if (pg instanceof Fragment) { Polygon[] convex = pg.convexDecomposition(); bodyDef.type = BodyType.DYNAMIC; for (Polygon pgx : convex) { Body f_body = w.createBody(bodyDef); pgx.flip(); PolygonShape ps = new PolygonShape(); ps.set(pgx.getArray(), pgx.size()); fd.shape = ps; fd.polygon = null; fd.material = f1.m_material.m_fragments; // rekurzivne stiepenie f_body.createFixture(fd); f_body.setAngularVelocity(b1.m_angularVelocity); f_body.setLinearVelocity(b1.getLinearVelocityFromLocalPoint(f_body.getLocalCenter())); newbodies.add(f_body); } } else { fd.material = f1.m_material.m_fragments; // rekurzivne stiepenie bodyDef.type = b1.getType(); Body f_body = w.createBody(bodyDef); PolygonFixture pf = new PolygonFixture(pg); f_body.createFixture(pf, fd); f_body.setLinearVelocity(b1.getLinearVelocityFromLocalPoint(f_body.getLocalCenter())); f_body.setAngularVelocity(b1.m_angularVelocity); newbodies.add(f_body); } } } // zavola sa funkcia z fraction listeneru (pokial je nadefinovany) FractureListener fl = w.getContactManager().m_fractureListener; if (fl != null) { fl.action(m, normalImpulse, newbodies); } }
@Override public void initTest(boolean argDeserialized) { Body ground = null; { BodyDef bd = new BodyDef(); ground = getWorld().createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vec2(50.0f, 0.0f), new Vec2(-50.0f, 0.0f)); ground.createFixture(shape, 0.0f); } { CircleShape circle1 = new CircleShape(); circle1.m_radius = 1.0f; PolygonShape box = new PolygonShape(); box.setAsBox(0.5f, 5.0f); CircleShape circle2 = new CircleShape(); circle2.m_radius = 2.0f; BodyDef bd1 = new BodyDef(); bd1.type = BodyType.STATIC; bd1.position.set(10.0f, 9.0f); Body body1 = m_world.createBody(bd1); body1.createFixture(circle1, 5.0f); BodyDef bd2 = new BodyDef(); bd2.type = BodyType.DYNAMIC; bd2.position.set(10.0f, 8.0f); Body body2 = m_world.createBody(bd2); body2.createFixture(box, 5.0f); BodyDef bd3 = new BodyDef(); bd3.type = BodyType.DYNAMIC; bd3.position.set(10.0f, 6.0f); Body body3 = m_world.createBody(bd3); body3.createFixture(circle2, 5.0f); RevoluteJointDef jd1 = new RevoluteJointDef(); jd1.initialize(body2, body1, bd1.position); Joint joint1 = m_world.createJoint(jd1); RevoluteJointDef jd2 = new RevoluteJointDef(); jd2.initialize(body2, body3, bd3.position); Joint joint2 = m_world.createJoint(jd2); GearJointDef jd4 = new GearJointDef(); jd4.bodyA = body1; jd4.bodyB = body3; jd4.joint1 = joint1; jd4.joint2 = joint2; jd4.ratio = circle2.m_radius / circle1.m_radius; m_world.createJoint(jd4); } { CircleShape circle1 = new CircleShape(); circle1.m_radius = 1.0f; CircleShape circle2 = new CircleShape(); circle2.m_radius = 2.0f; PolygonShape box = new PolygonShape(); box.setAsBox(0.5f, 5.0f); BodyDef bd1 = new BodyDef(); bd1.type = BodyType.DYNAMIC; bd1.position.set(-3.0f, 12.0f); Body body1 = m_world.createBody(bd1); body1.createFixture(circle1, 5.0f); RevoluteJointDef jd1 = new RevoluteJointDef(); jd1.bodyA = ground; jd1.bodyB = body1; ground.getLocalPointToOut(bd1.position, jd1.localAnchorA); body1.getLocalPointToOut(bd1.position, jd1.localAnchorB); jd1.referenceAngle = body1.getAngle() - ground.getAngle(); m_joint1 = (RevoluteJoint) m_world.createJoint(jd1); BodyDef bd2 = new BodyDef(); bd2.type = BodyType.DYNAMIC; bd2.position.set(0.0f, 12.0f); Body body2 = m_world.createBody(bd2); body2.createFixture(circle2, 5.0f); RevoluteJointDef jd2 = new RevoluteJointDef(); jd2.initialize(ground, body2, bd2.position); m_joint2 = (RevoluteJoint) m_world.createJoint(jd2); BodyDef bd3 = new BodyDef(); bd3.type = BodyType.DYNAMIC; bd3.position.set(2.5f, 12.0f); Body body3 = m_world.createBody(bd3); body3.createFixture(box, 5.0f); PrismaticJointDef jd3 = new PrismaticJointDef(); jd3.initialize(ground, body3, bd3.position, new Vec2(0.0f, 1.0f)); jd3.lowerTranslation = -5.0f; jd3.upperTranslation = 5.0f; jd3.enableLimit = true; m_joint3 = (PrismaticJoint) m_world.createJoint(jd3); GearJointDef jd4 = new GearJointDef(); jd4.bodyA = body1; jd4.bodyB = body2; jd4.joint1 = m_joint1; jd4.joint2 = m_joint2; jd4.ratio = circle2.m_radius / circle1.m_radius; m_joint4 = (GearJoint) m_world.createJoint(jd4); GearJointDef jd5 = new GearJointDef(); jd5.bodyA = body2; jd5.bodyB = body3; jd5.joint1 = m_joint2; jd5.joint2 = m_joint3; jd5.ratio = 1f / circle2.m_radius; m_joint5 = (GearJoint) m_world.createJoint(jd5); } }
/** * Create a new body * * @param shape The shape the body should have * @param x The x axis location of the body * @param y The y axis location of the body * @param staticBody True if this body should be static */ public Body(Shape shape, float x, float y, boolean staticBody) { jboxBodyDef = new BodyDef(); jboxBodyDef.position = new Vec2(x, y); this.staticBody = staticBody; this.shape = shape; }
/** * Set the linear damping to apply to this body. Higher value slows the body acceleration. Maximum * is 1.0 * * @param damping The amount to dampen the movement by */ public void setDamping(float damping) { if (jboxBody == null) { jboxBodyDef.linearDamping = damping; } }
@Override public void create() { if (firstTime) { setCamera(0f, 2f, 35f); firstTime = false; } // m_world.setGravity(new Vec2(0.0f,0.0f)); Body ground = null; { PolygonDef sd = new PolygonDef(); sd.setAsBox(5.0f, 0.5f); BodyDef bd = new BodyDef(); bd.position.set(0.0f, 0.0f); ground = m_world.createBody(bd); ground.createShape(sd); sd.setAsBox(1.0f, 0.2f, new Vec2(0.0f, 4.0f), -0.2f); ground.createShape(sd); sd.setAsBox(1.5f, 0.2f, new Vec2(-1.2f, 5.2f), -1.5f); ground.createShape(sd); sd.setAsBox(0.5f, 50.0f, new Vec2(5.0f, 0.0f), 0.0f); ground.createShape(sd); sd.setAsBox(0.5f, 3.0f, new Vec2(-8.0f, 0.0f), 0.0f); ground.createShape(sd); sd.setAsBox(2.0f, 0.1f, new Vec2(-6.0f, -2.8f), 0.1f); ground.createShape(sd); CircleDef cd = new CircleDef(); cd.radius = 0.5f; cd.localPosition.set(-0.5f, -4.0f); ground.createShape(cd); } liquid = new Body[nParticles]; float massPerParticle = totalMass / nParticles; // PointDef pd = new PointDef(); // pd.mass = massPerParticle; // pd.restitution = 0.0f; // pd.filter.groupIndex = -10; CircleDef pd = new CircleDef(); pd.density = 1.0f; pd.filter.groupIndex = -10; pd.radius = .05f; pd.restitution = 0.4f; pd.friction = 0.0f; float cx = 0.0f; float cy = 25.0f; for (int i = 0; i < nParticles; ++i) { BodyDef bd = new BodyDef(); bd.position = new Vec2( parent.random(cx - boxWidth * .5f, cx + boxWidth * .5f), parent.random(cy - boxHeight * .5f, cy + boxHeight * .5f)); bd.fixedRotation = true; Body b = m_world.createBody(bd); b.createShape(pd).setUserData(LIQUID_INT); MassData md = new MassData(); md.mass = massPerParticle; md.I = 1.0f; b.setMass(md); b.allowSleeping(false); liquid[i] = b; } PolygonDef polyDef = new PolygonDef(); polyDef.setAsBox(parent.random(0.3f, 0.7f), parent.random(0.3f, 0.7f)); polyDef.density = 1.0f; BodyDef bodyDef = new BodyDef(); bodyDef.position = new Vec2(0.0f, 25.0f); bod = m_world.createBody(bodyDef); bod.createShape(polyDef); bod.setMassFromShapes(); }
@Override protected void configureBodyDef(BodyDef bd) { super.configureBodyDef(bd); bd.type = BodyType.DYNAMIC; }
public void start() { Vec2 gravity = new Vec2(0, 0); World world = new World(gravity); BodyDef groundBodyDef = new BodyDef(); groundBodyDef.position.set(0, -25); Body groundBody = world.createBody(groundBodyDef); PolygonShape groundBox = new PolygonShape(); groundBox.setAsBox(900, 10); groundBody.createFixture(groundBox, 0); // Dynamic Body BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DYNAMIC; bodyDef.position.set(0, 0); Body body = world.createBody(bodyDef); PolygonShape dynamicBox = new PolygonShape(); dynamicBox.setAsBox(12, 12); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = dynamicBox; fixtureDef.density = 1f; fixtureDef.friction = 0.3f; body.createFixture(fixtureDef); // Setup world float timeStep = 1.0f / 60.0f; int velocityIterations = 6; int positionIterations = 2; body.setLinearVelocity(new Vec2(15.0f, -15.0f)); body.setLinearDamping(2f); Vec2 f = body.getWorldVector(new Vec2(0.0f, -30.0f)); Vec2 p = body.getWorldPoint(body.getLocalCenter().add(new Vec2(-.2f, 0f))); // body.applyForce(new Vec2(-200,-200),new Vec2(-200,200)); try { Display.setDisplayMode(new DisplayMode(screen_width, screen_height)); Display.create(); } catch (LWJGLException e) { e.printStackTrace(); System.exit(0); } lastFPS = getTime(); // standardBall[0] = new Ball(400,200,0,12); // standardBall[1] = new Ball(100,210,2,12); // standardBall[0].addImpulse(-10,0); standardBall[0] = new Ball(200, 300, 0, 12); standardBall[1] = new Ball(415, 100, 2, 12); standardBall[0].addImpulse(10, -9.99f); standardBall[2] = new Ball(620, 270, 2, 12); standardBall[3] = new Ball(640, 290, 2, 12); standardBall[4] = new Ball(660, 310, 2, 12); standardBall[5] = new Ball(680, 330, 2, 12); standardBall[6] = new Ball(620, 230, 2, 12); standardBall[7] = new Ball(640, 210, 2, 12); standardBall[8] = new Ball(660, 190, 2, 12); standardBall[9] = new Ball(680, 170, 2, 12); standardBall[10] = new Ball(640, 250, 1, 12); standardBall[11] = new Ball(660, 230, 2, 12); standardBall[12] = new Ball(660, 270, 2, 12); standardBall[13] = new Ball(680, 290, 2, 12); standardBall[14] = new Ball(680, 210, 2, 12); standardBall[15] = new Ball(680, 250, 2, 12); standardTable = new Table(0, 0, 800, 400, 25, 15); // init OpenGL GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(0, 900, 0, 500, 1, -1); GL11.glMatrixMode(GL11.GL_MODELVIEW); while (!Display.isCloseRequested()) { int delta = getDelta(); world.step(timeStep, velocityIterations, positionIterations); Vec2 position = body.getPosition(); float angle = body.getAngle(); // System.out.printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle); // Clear the screen and depth buffer GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); standardTable.draw(); for (int i = 0; i < NUMBER_OF_BALLS; i++) { standardBall[i].draw(); standardBall[i].update(delta); } standardBall[0].setX((position.x * 20) + offset_x); standardBall[0].setY((position.y * 20) + offset_y); update(delta); Display.update(); Display.sync(60); } Display.destroy(); }
@Override public void create() { if (firstTime) { setCamera(0.0f, 10.0f, 20.0f); firstTime = false; // this.settings.drawJoints = false; } Body ground = null; { PolygonDef sd = new PolygonDef(); sd.setAsBox(50.0f, 0.4f); BodyDef bd = new BodyDef(); bd.position.set(0.0f, 0.0f); ground = m_world.createBody(bd); ground.createShape(sd); sd.setAsBox(0.4f, 50.0f, new Vec2(-10.0f, 0.0f), 0.0f); ground.createShape(sd); sd.setAsBox(0.4f, 50.0f, new Vec2(10.0f, 0.0f), 0.0f); ground.createShape(sd); } ConstantVolumeJointDef cvjd = new ConstantVolumeJointDef(); float cx = 0.0f; float cy = 10.0f; float rx = 4.0f; float ry = 4.0f; int nBodies = 100; float bodyRadius = 0.4f; ArrayList<Body> bodies = new ArrayList<Body>(); for (int i = 0; i < nBodies; ++i) { float angle = PApplet.map(i, 0, nBodies, 0, 2 * 3.1415f); BodyDef bd = new BodyDef(); // bd.isBullet = true; bd.fixedRotation = true; float x = cx + (1.0f + .5f * (float) Math.cos(4 * (angle + .25f * 3.1415f))) * rx * (float) Math.sin(angle); float y = cy + (1.0f + .5f * (float) Math.cos(4 * (angle + .25f * 3.1415f))) * ry * (float) Math.cos(angle); bd.position.set(new Vec2(x, y)); Body body = m_world.createBody(bd); CircleDef cd = new CircleDef(); cd.radius = bodyRadius; cd.density = 1.0f; // cd.filter.groupIndex = -2; body.createShape(cd); cvjd.addBody(body); body.setMassFromShapes(); bodies.add(body); } cvjd.frequencyHz = 10.0f; cvjd.dampingRatio = 10.0f; ((ConstantVolumeJoint) m_world.createJoint(cvjd)).inflate(1.5f); cvjd = new ConstantVolumeJointDef(); for (int i = 0; i < nBodies / 4; ++i) { cvjd.addBody(bodies.get(i)); } cvjd.frequencyHz = 10.0f; cvjd.dampingRatio = 10.0f; ((ConstantVolumeJoint) m_world.createJoint(cvjd)).inflate(1.5f); cvjd = new ConstantVolumeJointDef(); for (int i = nBodies / 4; i < nBodies / 2; ++i) { cvjd.addBody(bodies.get(i)); } cvjd.frequencyHz = 10.0f; cvjd.dampingRatio = 10.0f; ((ConstantVolumeJoint) m_world.createJoint(cvjd)).inflate(1.5f); cvjd = new ConstantVolumeJointDef(); for (int i = nBodies / 2; i < 3 * nBodies / 4; ++i) { cvjd.addBody(bodies.get(i)); } cvjd.frequencyHz = 10.0f; cvjd.dampingRatio = 10.0f; ((ConstantVolumeJoint) m_world.createJoint(cvjd)).inflate(1.5f); cvjd = new ConstantVolumeJointDef(); for (int i = 3 * nBodies / 4; i < nBodies; ++i) { cvjd.addBody(bodies.get(i)); } cvjd.frequencyHz = 10.0f; cvjd.dampingRatio = 10.0f; ((ConstantVolumeJoint) m_world.createJoint(cvjd)).inflate(1.5f); // cvjd = new ConstantVolumeJointDef(); // for (int i=0+nBodies/8; i<nBodies/4+nBodies/8; ++i) { // cvjd.addBody(bodies.get(i)); // } // cvjd.frequencyHz = 10.0f; // cvjd.dampingRatio = 10.0f; // m_world.createJoint(cvjd); // // cvjd = new ConstantVolumeJointDef(); // for (int i=nBodies/4+nBodies/8; i<nBodies/2+nBodies/8; ++i) { // cvjd.addBody(bodies.get(i)); // } // cvjd.frequencyHz = 10.0f; // cvjd.dampingRatio = 10.0f; // m_world.createJoint(cvjd); // // cvjd = new ConstantVolumeJointDef(); // for (int i=nBodies/2+nBodies/8; i<3*nBodies/4+nBodies/8; ++i) { // cvjd.addBody(bodies.get(i)); // } // cvjd.frequencyHz = 10.0f; // cvjd.dampingRatio = 10.0f; // m_world.createJoint(cvjd); // cvjd = new ConstantVolumeJointDef(); // for (int i=3*nBodies/4+nBodies/8; i<nBodies+nBodies/8; ++i) { // int index = i; // if (index >= bodies.size()) index -= bodies.size(); // cvjd.addBody(bodies.get(index)); // } // // cvjd.frequencyHz = 10.0f; // cvjd.dampingRatio = 10.0f; // m_world.createJoint(cvjd); // cvjd = new ConstantVolumeJointDef(); // cvjd.addBody(bodies.get(0)); // cvjd.addBody(bodies.get(nBodies/4)); // cvjd.addBody(bodies.get(nBodies/2)); // cvjd.addBody(bodies.get(3*nBodies/4)); // cvjd.frequencyHz = 10.0f; // m_world.createJoint(cvjd); // // cvjd = new ConstantVolumeJointDef(); // cvjd.addBody(bodies.get(0 + nBodies/8)); // cvjd.addBody(bodies.get(nBodies/4 + nBodies/8)); // cvjd.addBody(bodies.get(nBodies/2 + nBodies/8)); // cvjd.addBody(bodies.get(3*nBodies/4 + nBodies/8)); // cvjd.frequencyHz = 1.0f; // m_world.createJoint(cvjd); BodyDef bd2 = new BodyDef(); PolygonDef psd = new PolygonDef(); psd.setAsBox(3.0f, 1.5f, new Vec2(cx, cy + 15.0f), 0.0f); psd.density = 1.0f; bd2.position = new Vec2(cx, cy + 15.0f); Body fallingBox = m_world.createBody(bd2); fallingBox.createShape(psd); fallingBox.setMassFromShapes(); }
@Override protected void bodyDefB4CreationCallback(BodyDef def) { def.isBullet = true; super.bodyDefB4CreationCallback(def); }
@Override protected void bodyDefB4CreationCallback(BodyDef def) { super.bodyDefB4CreationCallback(def); def.fixedRotation = true; def.linearDamping = 0.5f; }