List of usage examples for com.badlogic.gdx.physics.box2d World setGravity
public void setGravity(Vector2 gravity)
From source file:com.badlogic.gdx.tests.box2d.ApplyForce.java
License:Apache License
@Override protected void createWorld(World world) { world.setGravity(new Vector2(0, 0)); float k_restitution = 0.4f; Body ground;/*from www . j ava2s . c o m*/ { BodyDef bd = new BodyDef(); bd.position.set(0, 20); ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); FixtureDef sd = new FixtureDef(); sd.shape = shape; sd.density = 0; sd.restitution = k_restitution; shape.set(new Vector2(-20, -20), new Vector2(-20, 20)); ground.createFixture(sd); shape.set(new Vector2(20, -20), new Vector2(20, 20)); ground.createFixture(sd); shape.set(new Vector2(-20, 20), new Vector2(20, 20)); ground.createFixture(sd); shape.set(new Vector2(-20, -20), new Vector2(20, -20)); ground.createFixture(sd); shape.dispose(); } { Transform xf1 = new Transform(new Vector2(), 0.3524f * (float) Math.PI); xf1.setPosition(xf1.mul(new Vector2(1, 0))); Vector2[] vertices = new Vector2[3]; vertices[0] = xf1.mul(new Vector2(-1, 0)); vertices[1] = xf1.mul(new Vector2(1, 0)); vertices[2] = xf1.mul(new Vector2(0, 0.5f)); PolygonShape poly1 = new PolygonShape(); poly1.set(vertices); FixtureDef sd1 = new FixtureDef(); sd1.shape = poly1; sd1.density = 4.0f; Transform xf2 = new Transform(new Vector2(), -0.3524f * (float) Math.PI); xf2.setPosition(xf2.mul(new Vector2(-1, 0))); vertices[0] = xf2.mul(new Vector2(-1, 0)); vertices[1] = xf2.mul(new Vector2(1, 0)); vertices[2] = xf2.mul(new Vector2(0, 0.5f)); PolygonShape poly2 = new PolygonShape(); poly2.set(vertices); FixtureDef sd2 = new FixtureDef(); sd2.shape = poly2; sd2.density = 2.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.angularDamping = 5.0f; bd.linearDamping = 0.1f; bd.position.set(0, 2); bd.angle = (float) Math.PI; bd.allowSleep = false; m_body = world.createBody(bd); m_body.createFixture(sd1); m_body.createFixture(sd2); poly1.dispose(); poly2.dispose(); } { PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; fd.friction = 0.3f; for (int i = 0; i < 10; i++) { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(0, 5 + 1.54f * i); Body body = world.createBody(bd); body.createFixture(fd); float gravity = 10.0f; float I = body.getInertia(); float mass = body.getMass(); float radius = (float) Math.sqrt(2 * I / mass); FrictionJointDef jd = new FrictionJointDef(); jd.localAnchorA.set(0, 0); jd.localAnchorB.set(0, 0); jd.bodyA = ground; jd.bodyB = body; jd.collideConnected = true; jd.maxForce = mass * gravity; jd.maxTorque = mass * radius * gravity; world.createJoint(jd); } shape.dispose(); } }
From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DMapObjectParser.java
License:Apache License
/** creates the given {@link Map Map's} {@link MapObjects} in the given {@link World} * @param world the {@link World} to create the {@link MapObjects} of the given {@link Map} in * @param map the {@link Map} which {@link MapObjects} to create in the given {@link World} * @return the given {@link World} with the parsed {@link MapObjects} of the given {@link Map} created in it */ public World load(World world, Map map) { MapProperties oldMapProperties = mapProperties; mapProperties = map.getProperties(); world.setGravity(vec2.set(getProperty(mapProperties, aliases.gravityX, world.getGravity().x), getProperty(mapProperties, aliases.gravityY, world.getGravity().y))); world.setAutoClearForces(getProperty(mapProperties, aliases.autoClearForces, world.getAutoClearForces())); if (!ignoreMapUnitScale) unitScale = getProperty(mapProperties, aliases.unitScale, unitScale); tileWidth = getProperty(mapProperties, aliases.tileWidth, tileWidth); tileHeight = getProperty(mapProperties, aliases.tileHeight, tileHeight); listener.init(this); @SuppressWarnings("unchecked") Array<MapLayer> layers = Pools.obtain(Array.class); layers.clear();//from w w w.j a v a 2 s.c o m listener.load(map, layers); for (MapLayer mapLayer : layers) load(world, mapLayer); layers.clear(); Pools.free(layers); mapProperties = oldMapProperties; return world; }
From source file:net.dermetfan.utils.libgdx.box2d.Box2DMapObjectParser.java
License:Apache License
/** creates the given {@link Map Map's} {@link MapObjects} in the given {@link World} * @param world the {@link World} to create the {@link MapObjects} of the given {@link Map} in * @param map the {@link Map} which {@link MapObjects} to create in the given {@link World} * @return the given {@link World} with the parsed {@link MapObjects} of the given {@link Map} created in it */ public World load(World world, Map map) { MapProperties oldMapProperties = mapProperties; mapProperties = map.getProperties(); world.setGravity(vec2.set(getProperty(mapProperties, aliases.gravityX, world.getGravity().x), getProperty(mapProperties, aliases.gravityY, world.getGravity().y))); world.setAutoClearForces(getProperty(mapProperties, aliases.autoClearForces, world.getAutoClearForces())); if (!ignoreMapUnitScale) unitScale = getProperty(mapProperties, aliases.unitScale, unitScale); tileWidth = getProperty(mapProperties, aliases.tileWidth, tileWidth); tileHeight = getProperty(mapProperties, aliases.tileHeight, tileHeight); for (MapLayer mapLayer : map.getLayers()) load(world, mapLayer);//ww w .j a v a 2s. co m mapProperties = oldMapProperties; return world; }
From source file:zzz.box2dtest.ApplyForce.java
License:Apache License
@Override protected void createWorld(World world) { world.setGravity(new Vector2(0, 0)); Gdx.input.setInputProcessor(this); float k_restitution = 0.4f; Body ground;/* w w w . j ava2 s . co m*/ { BodyDef bd = new BodyDef(); bd.position.set(0, 20); ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); FixtureDef sd = new FixtureDef(); sd.shape = shape; sd.density = 0; sd.restitution = k_restitution; shape.set(new Vector2(-20, -20), new Vector2(-20, 20)); ground.createFixture(sd); shape.set(new Vector2(20, -20), new Vector2(20, 20)); ground.createFixture(sd); shape.set(new Vector2(-20, 20), new Vector2(20, 20)); ground.createFixture(sd); shape.set(new Vector2(-20, -20), new Vector2(20, -20)); ground.createFixture(sd); shape.dispose(); } { Transform xf1 = new Transform(new Vector2(), 0.3524f * (float) Math.PI); xf1.setPosition(xf1.mul(new Vector2(1, 0))); Vector2[] vertices = new Vector2[3]; vertices[0] = xf1.mul(new Vector2(-1, 0)); vertices[1] = xf1.mul(new Vector2(1, 0)); vertices[2] = xf1.mul(new Vector2(0, 0.5f)); PolygonShape poly1 = new PolygonShape(); poly1.set(vertices); FixtureDef sd1 = new FixtureDef(); sd1.shape = poly1; sd1.density = 4.0f; Transform xf2 = new Transform(new Vector2(), -0.3524f * (float) Math.PI); xf2.setPosition(xf2.mul(new Vector2(-1, 0))); vertices[0] = xf2.mul(new Vector2(-1, 0)); vertices[1] = xf2.mul(new Vector2(1, 0)); vertices[2] = xf2.mul(new Vector2(0, 0.5f)); PolygonShape poly2 = new PolygonShape(); poly2.set(vertices); FixtureDef sd2 = new FixtureDef(); sd2.shape = poly2; sd2.density = 2.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.angularDamping = 5.0f; bd.linearDamping = 0.1f; bd.position.set(0, 2); bd.angle = (float) Math.PI; bd.allowSleep = false; m_body = world.createBody(bd); m_body.createFixture(sd1); m_body.createFixture(sd2); poly1.dispose(); poly2.dispose(); } { PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; fd.friction = 0.3f; for (int i = 0; i < 10; i++) { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(0, 5 + 1.54f * i); Body body = world.createBody(bd); body.createFixture(fd); float gravity = 10.0f; float I = body.getInertia(); float mass = body.getMass(); float radius = (float) Math.sqrt(2 * I / mass); FrictionJointDef jd = new FrictionJointDef(); jd.localAnchorA.set(0, 0); jd.localAnchorB.set(0, 0); jd.bodyA = ground; jd.bodyB = body; jd.collideConnected = true; jd.maxForce = mass * gravity; jd.maxTorque = mass * radius * gravity; world.createJoint(jd); } shape.dispose(); } }