use of com.badlogic.gdx.physics.box2d.PolygonShape in project libgdx by libgdx.
the class Box2DTest method createPhysicsWorld.
private void createPhysicsWorld() {
// we instantiate a new World with a proper gravity vector
// and tell it to sleep when possible.
world = new World(new Vector2(0, -10), true);
float[] vertices = { -0.07421887f, -0.16276085f, -0.12109375f, -0.22786504f, -0.157552f, -0.7122401f, 0.04296875f, -0.7122401f, 0.110677004f, -0.6419276f, 0.13151026f, -0.49869835f, 0.08984375f, -0.3190109f };
PolygonShape shape = new PolygonShape();
shape.set(vertices);
// next we create a static ground platform. This platform
// is not moveable and will not react to any influences from
// outside. It will however influence other bodies. First we
// create a PolygonShape that holds the form of the platform.
// it will be 100 meters wide and 2 meters high, centered
// around the origin
PolygonShape groundPoly = new PolygonShape();
groundPoly.setAsBox(50, 1);
// next we create the body for the ground platform. It's
// simply a static body.
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
groundBody = world.createBody(groundBodyDef);
// finally we add a fixture to the body using the polygon
// defined above. Note that we have to dispose PolygonShapes
// and CircleShapes once they are no longer used. This is the
// only time you have to care explicitly for memory management.
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = groundPoly;
fixtureDef.filter.groupIndex = 0;
groundBody.createFixture(fixtureDef);
groundPoly.dispose();
// We also create a simple ChainShape we put above our
// ground polygon for extra funkyness.
ChainShape chainShape = new ChainShape();
chainShape.createLoop(new Vector2[] { new Vector2(-10, 10), new Vector2(-10, 5), new Vector2(10, 5), new Vector2(10, 11) });
BodyDef chainBodyDef = new BodyDef();
chainBodyDef.type = BodyType.StaticBody;
Body chainBody = world.createBody(chainBodyDef);
chainBody.createFixture(chainShape, 0);
chainShape.dispose();
createBoxes();
// You can savely ignore the rest of this method :)
world.setContactListener(new ContactListener() {
@Override
public void beginContact(Contact contact) {
// System.out.println("begin contact");
}
@Override
public void endContact(Contact contact) {
// System.out.println("end contact");
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
// Manifold.ManifoldType type = oldManifold.getType();
// Vector2 localPoint = oldManifold.getLocalPoint();
// Vector2 localNormal = oldManifold.getLocalNormal();
// int pointCount = oldManifold.getPointCount();
// ManifoldPoint[] points = oldManifold.getPoints();
// System.out.println("pre solve, " + type +
// ", point: " + localPoint +
// ", local normal: " + localNormal +
// ", #points: " + pointCount +
// ", [" + points[0] + ", " + points[1] + "]");
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
// float[] ni = impulse.getNormalImpulses();
// float[] ti = impulse.getTangentImpulses();
// System.out.println("post solve, normal impulses: " + ni[0] + ", " + ni[1] + ", tangent impulses: " + ti[0] + ", " + ti[1]);
}
});
}
use of com.badlogic.gdx.physics.box2d.PolygonShape 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.PolygonShape 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.PolygonShape 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.PolygonShape in project commons-gdx by gemserk.
the class Box2dUtils method translateFixtures.
/**
* Internally translates all the Body's fixtures the specified translation, without moving the Body's center. For now, it generates garbage so should be used only once.
*
* @param body
* The body to work on.
* @param tx
* The translation in x coordinate.
* @param ty
* The translation in y coordinate.
*/
public static void translateFixtures(ArrayList<Fixture> fixtures, float tx, float ty) {
// TODO: should be in a Box2dUtils
for (int i = 0; i < fixtures.size(); i++) {
Fixture fixture = fixtures.get(i);
Shape shape = fixture.getShape();
if (shape.getType() == Type.Polygon)
ShapeUtils.translatePolygonShape((PolygonShape) shape, tx, ty);
else if (shape.getType() == Type.Circle)
ShapeUtils.translateCircleShape((CircleShape) shape, tx, ty);
}
}
Aggregations