ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-40, 0), new Vector2(40, 0)); bd.allowSleep = false; Body body = world.createBody(bd); body.createFixture(shape, 5.0f); pjd.enableLimit = true; m_joint = (PrismaticJoint)world.createJoint(pjd);
private void createPhysicsWorld () { world = new World(new Vector2(0, -10), true); groundBody = world.createBody(groundBodyDef); BodyDef chainBodyDef = new BodyDef(); chainBodyDef.type = BodyType.StaticBody; Body chainBody = world.createBody(chainBodyDef); chainBody.createFixture(chainShape, 0); chainShape.dispose(); world.getFixtures(fixtures); world.setContactListener(new ContactListener() { @Override public void beginContact (Contact contact) {
public void render () { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); world.step(Math.min(0.032f, Gdx.graphics.getDeltaTime()), 3, 4); cam.update(); renderer.render(world, cam.combined); }
@Override public boolean touchDown (int x, int y, int pointer, int newParam) { // translate the mouse coordinates to world coordinates testPoint.set(x, y, 0); camera.unproject(testPoint); // ask the world which bodies are within the given // bounding box around the mouse pointer hitBody = null; world.QueryAABB(callback, testPoint.x - 0.1f, testPoint.y - 0.1f, testPoint.x + 0.1f, testPoint.y + 0.1f); // if we hit something we create a new mouse joint // and attach it to the hit body. if (hitBody != null) { MouseJointDef def = new MouseJointDef(); def.bodyA = groundBody; def.bodyB = hitBody; def.collideConnected = true; def.target.set(testPoint.x, testPoint.y); def.maxForce = 1000.0f * hitBody.getMass(); mouseJoint = (MouseJoint)world.createJoint(def); hitBody.setAwake(true); } else { for (Body box : boxes) world.destroyBody(box); boxes.clear(); createBoxes(); } return false; }
private void renderBodies (World world) { renderer.begin(ShapeType.Line); if (drawBodies || drawAABBs) { world.getBodies(bodies); for (Iterator<Body> iter = bodies.iterator(); iter.hasNext();) { Body body = iter.next(); if (body.isActive() || drawInactiveBodies) renderBody(body); } } if (drawJoints) { world.getJoints(joints); for (Iterator<Joint> iter = joints.iterator(); iter.hasNext();) { Joint joint = iter.next(); drawJoint(joint); } } renderer.end(); if (drawContacts) { renderer.begin(ShapeType.Point); for (Contact contact : world.getContactList()) drawContact(contact); renderer.end(); } }
@Override public boolean touchDown (int x, int y, int pointer, int button) { // translate the mouse coordinates to world coordinates camera.unproject(testPoint.set(x, y, 0)); // ask the world which bodies are within the given // bounding box around the mouse pointer hitBody = null; world.QueryAABB(callback, testPoint.x - 0.0001f, testPoint.y - 0.0001f, testPoint.x + 0.0001f, testPoint.y + 0.0001f); if (hitBody == groundBody) hitBody = null; // ignore kinematic bodies, they don't work with the mouse joint if (hitBody != null && hitBody.getType() == BodyType.KinematicBody) return false; // if we hit something we create a new mouse joint // and attach it to the hit body. if (hitBody != null) { MouseJointDef def = new MouseJointDef(); def.bodyA = groundBody; def.bodyB = hitBody; def.collideConnected = true; def.target.set(testPoint.x, testPoint.y); def.maxForce = 1000.0f * hitBody.getMass(); mouseJoint = (MouseJoint)world.createJoint(def); hitBody.setAwake(true); } return false; }
PhysicalWorld.WORLD.getJoints(joints); for (Joint j : joints) { try { PhysicalWorld.WORLD.destroyJoint(j); } catch (Exception ex) { PhysicalWorld.WORLD.getBodies(bodys); for (Body body : bodys) { try { PhysicalWorld.WORLD.destroyBody(body); } catch (Exception ex) { PhysicalWorld.WORLD.dispose(); PhysicalWorld.WORLD = new World(new Vector2(0, -10), false);
private void createBoxes () { // next we create 50 boxes at random locations above the ground // body. First we create a nice polygon representing a box 2 meters // wide and high. PolygonShape boxPoly = new PolygonShape(); boxPoly.setAsBox(1, 1); // next we create the 50 box bodies using the PolygonShape we just // defined. This process is similar to the one we used for the ground // body. Note that we reuse the polygon for each body fixture. for (int i = 0; i < 20; i++) { // Create the BodyDef, set a random position above the // ground and create a new body BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = BodyType.DynamicBody; boxBodyDef.position.x = -24 + (float)(Math.random() * 48); boxBodyDef.position.y = 10 + (float)(Math.random() * 100); Body boxBody = world.createBody(boxBodyDef); boxBody.createFixture(boxPoly, 1); // add the box to our list of boxes boxes.add(boxBody); } // we are done, all that's left is disposing the boxPoly boxPoly.dispose(); }
@Override public void create () { // setup the camera. In Box2D we operate on a // meter scale, pixels won't do it. So we use // an orthographic camera with a viewport of // 48 meters in width and 32 meters in height. // We also position the camera so that it // looks at (0,16) (that's where the middle of the // screen will be located). camera = new OrthographicCamera(48, 32); camera.position.set(0, 15, 0); // create the debug renderer renderer = new Box2DDebugRenderer(); // create the world world = new World(new Vector2(0, -10), true); // we also need an invisible zero size ground body // to which we can connect the mouse joint BodyDef bodyDef = new BodyDef(); groundBody = world.createBody(bodyDef); // call abstract method to populate the world createWorld(world); batch = new SpriteBatch(); font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false); }
@Override public void create () { world = new World(new Vector2(0, -40), true); renderer = new Box2DDebugRenderer(); cam = new OrthographicCamera(28, 20); createWorld(); Gdx.input.setInputProcessor(this); batch = new SpriteBatch(); font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false); }
@Override public void dispose () { world.dispose(); renderer.dispose(); } }
protected Body[] getBodyAB(World world){ Body[] bodies = new Body[2]; Array<Body> bodyArray = new Array<>(world.getBodyCount()); world.getBodies(bodyArray); for(Body body : bodyArray){ if(this.bodyA.equals(body.getUserData())) bodies[0] = body; else if(this.bodyB.equals(body.getUserData())) bodies[1] = body; } return bodies; }
@Override public boolean keyDown (int keyCode) { if (keyCode == Input.Keys.COMMA) { if (m_bullet != null) { world.destroyBody(m_bullet); m_bullet = null; } { CircleShape shape = new CircleShape(); shape.setRadius(0.25f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.restitution = 0.05f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.bullet = true; bd.position.set(-31, 5); m_bullet = world.createBody(bd); m_bullet.createFixture(fd); m_bullet.setLinearVelocity(new Vector2(400, 0)); } } return false; }
world.step(Gdx.graphics.getDeltaTime(), 8, 3); float updateTime = (TimeUtils.nanoTime() - start) / 1000000000.0f; renderer.begin(ShapeType.Point); renderer.setColor(0, 1, 0, 1); for (int i = 0; i < world.getContactCount(); i++) { Contact contact = world.getContactList().get(i);
@Override public void run () { world.destroyBody(toRemove); } });
@Override public void act(float delta) { super.act(delta); Array<Body> bodies = new Array<Body>(world.getBodyCount()); world.getBodies(bodies); for (Body body : bodies) { update(body); } // Fixed timestep accumulator += delta; while (accumulator >= delta) { world.step(TIME_STEP, 6, 2); accumulator -= TIME_STEP; } //TODO: Implement interpolation }
@Override protected void createWorld (World world) { world.setGravity(new Vector2(0, 0)); ground = world.createBody(bd); bd.angle = (float)Math.PI; bd.allowSleep = false; m_body = world.createBody(bd); m_body.createFixture(sd1); m_body.createFixture(sd2); Body body = world.createBody(bd); jd.maxTorque = mass * radius * gravity; world.createJoint(jd);
@Override public boolean touchUp (int x, int y, int pointer, int button) { // if a mouse joint exists we simply destroy it if (mouseJoint != null) { world.destroyJoint(mouseJoint); mouseJoint = null; } return false; }
/** Ray-cast the world for all fixtures in the path of the ray. The ray-cast ignores shapes that contain the starting point. * @param callback a user implemented callback class. * @param point1 the ray starting point * @param point2 the ray ending point */ public void rayCast (RayCastCallback callback, Vector2 point1, Vector2 point2) { rayCast(callback, point1.x, point1.y, point2.x, point2.y); }