use of com.badlogic.gdx.physics.box2d.EdgeShape in project libgdx by libgdx.
the class VerticalStack method createWorld.
@Override
protected void createWorld(World world) {
{
BodyDef bd = new BodyDef();
Body ground = world.createBody(bd);
EdgeShape shape = new EdgeShape();
shape.set(new Vector2(-40, 0), new Vector2(40, 0));
ground.createFixture(shape, 0.0f);
shape.set(new Vector2(20, 0), new Vector2(20, 20));
ground.createFixture(shape, 0);
shape.dispose();
}
float[] xs = { 0, -10, -5, 5, 10 };
for (int j = 0; j < e_columnCount; j++) {
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 < e_rowCount; i++) {
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;
int n = j * e_rowCount + i;
m_indices[n] = n;
float x = 0;
bd.position.set(xs[j] + x, 0.752f + 1.54f * i);
Body body = world.createBody(bd);
body.setUserData(n);
m_bodies[n] = body;
body.createFixture(fd);
}
shape.dispose();
}
m_bullet = null;
}
use of com.badlogic.gdx.physics.box2d.EdgeShape in project libgdx by libgdx.
the class ConveyorBelt method createWorld.
@Override
protected void createWorld(World world) {
world.setContactListener(this);
// Ground
{
BodyDef bodyDef = new BodyDef();
groundBody = world.createBody(bodyDef);
EdgeShape shape = new EdgeShape();
shape.set(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));
groundBody.createFixture(shape, 0.0f);
}
// Platform
{
BodyDef bd = new BodyDef();
bd.position.set(-5.0f, 5.0f);
Body body = world.createBody(bd);
PolygonShape shape = new PolygonShape();
shape.setAsBox(10.0f, 0.5f);
FixtureDef fd = new FixtureDef();
fd.shape = shape;
fd.friction = 0.8f;
m_platform = body.createFixture(fd);
}
// Boxes
for (int i = 0; i < 5; ++i) {
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;
bd.position.set(-10.0f + 2.0f * i, 7.0f);
Body body = world.createBody(bd);
PolygonShape shape = new PolygonShape();
shape.setAsBox(0.5f, 0.5f);
body.createFixture(shape, 20.0f);
}
}
use of com.badlogic.gdx.physics.box2d.EdgeShape in project RubeLoader by tescott.
the class FixtureSerializer method read.
@SuppressWarnings("rawtypes")
@Override
public Fixture read(Json json, JsonValue jsonData, Class type) {
if (body == null)
return null;
json.setIgnoreUnknownFields(true);
FixtureDef defaults = RubeDefaults.Fixture.definition;
FixtureDef def = new FixtureDef();
json.readFields(def, jsonData);
def.friction = json.readValue("friction", float.class, defaults.friction, jsonData);
def.density = json.readValue("density", float.class, defaults.density, jsonData);
def.restitution = json.readValue("restitution", float.class, defaults.restitution, jsonData);
def.isSensor = json.readValue("sensor", boolean.class, defaults.isSensor, jsonData);
def.filter.maskBits = json.readValue("filter-maskBits", short.class, defaults.filter.maskBits, jsonData);
def.filter.categoryBits = json.readValue("filter-categoryBits", short.class, defaults.filter.categoryBits, jsonData);
def.filter.groupIndex = json.readValue("filter-groupIndex", short.class, defaults.filter.groupIndex, jsonData);
CircleShape circle = json.readValue("circle", CircleShape.class, jsonData);
if (circle != null) {
def.shape = circle;
} else {
EdgeShape edge = json.readValue("edge", EdgeShape.class, jsonData);
if (edge != null) {
def.shape = edge;
} else {
chainShapeSerializer.setReadLoop(false);
ChainShape chain = json.readValue("chain", ChainShape.class, jsonData);
if (chain != null) {
def.shape = chain;
} else {
chainShapeSerializer.setReadLoop(true);
chain = json.readValue("loop", ChainShape.class, jsonData);
if (chain != null) {
def.shape = chain;
} else {
PolygonShape polygon = json.readValue("polygon", PolygonShape.class, jsonData);
if (polygon != null) {
def.shape = polygon;
} else {
edge = json.readValue("polygon", EdgeShape.class, jsonData);
if (edge != null) {
def.shape = edge;
}
}
}
}
}
}
Fixture fixture = body.createFixture(def);
def.shape.dispose();
scene.parseCustomProperties(json, fixture, jsonData);
String name = json.readValue("name", String.class, jsonData);
if (name != null) {
scene.putNamed(name, fixture);
}
return fixture;
}
use of com.badlogic.gdx.physics.box2d.EdgeShape in project libgdx by libgdx.
the class ApplyForce method createWorld.
@Override
protected void createWorld(World world) {
world.setGravity(new Vector2(0, 0));
float k_restitution = 0.4f;
Body ground;
{
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();
}
}
use of com.badlogic.gdx.physics.box2d.EdgeShape in project libgdx by libgdx.
the class Bridge method createWorld.
@Override
protected void createWorld(World world) {
Body ground;
{
BodyDef bd = new BodyDef();
ground = world.createBody(bd);
EdgeShape shape = new EdgeShape();
shape.set(new Vector2(-40, 0), new Vector2(40.0f, 0));
ground.createFixture(shape, 0);
shape.dispose();
}
{
PolygonShape shape = new PolygonShape();
shape.setAsBox(0.5f, 0.125f);
FixtureDef fd = new FixtureDef();
fd.shape = shape;
fd.density = 20.0f;
fd.friction = 0.2f;
RevoluteJointDef jd = new RevoluteJointDef();
Body prevBody = ground;
for (int i = 0; i < e_count; i++) {
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;
bd.position.set(-14.5f + 1.0f * i, 5.0f);
Body body = world.createBody(bd);
body.createFixture(fd);
Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 5.0f);
jd.initialize(prevBody, body, anchor);
world.createJoint(jd);
prevBody = body;
}
Vector2 anchor = new Vector2(-15.0f + 1.0f * e_count, 5.0f);
jd.initialize(prevBody, ground, anchor);
world.createJoint(jd);
shape.dispose();
}
for (int i = 0; i < 2; i++) {
Vector2[] vertices = new Vector2[3];
vertices[0] = new Vector2(-0.5f, 0);
vertices[1] = new Vector2(0.5f, 0);
vertices[2] = new Vector2(0, 1.5f);
PolygonShape shape = new PolygonShape();
shape.set(vertices);
FixtureDef fd = new FixtureDef();
fd.shape = shape;
fd.density = 1.0f;
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;
bd.position.set(-8.0f + 8.0f * i, 12.0f);
Body body = world.createBody(bd);
body.createFixture(fd);
shape.dispose();
}
for (int i = 0; i < 3; i++) {
CircleShape shape = new CircleShape();
shape.setRadius(0.5f);
FixtureDef fd = new FixtureDef();
fd.shape = shape;
fd.density = 1.0f;
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;
bd.position.set(-6.0f + 6.0f * i, 10.0f);
Body body = world.createBody(bd);
body.createFixture(fd);
shape.dispose();
}
}
Aggregations