use of com.badlogic.gdx.physics.box2d.BodyDef in project libgdx by libgdx.
the class KinematicBodyTest method create.
public void create() {
cam = new OrthographicCamera(48, 32);
cam.position.set(0, 15, 0);
renderer = new Box2DDebugRenderer();
world = new World(new Vector2(0, -10), true);
Body body = world.createBody(new BodyDef());
CircleShape shape = new CircleShape();
shape.setRadius(1f);
MassData mass = new MassData();
mass.mass = 1f;
body.setMassData(mass);
body.setFixedRotation(true);
body.setType(BodyType.KinematicBody);
body.createFixture(shape, 1);
body.setBullet(true);
body.setTransform(new Vector2(0, 0), body.getAngle());
body.setLinearVelocity(new Vector2(50f, 0));
}
use of com.badlogic.gdx.physics.box2d.BodyDef in project RubeLoader by tescott.
the class BodySerializer method read.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Body read(Json json, JsonValue jsonData, Class type) {
if (world == null)
return null;
BodyDef defaults = RubeDefaults.Body.definition;
int bodyType = json.readValue("type", int.class, defaults.type.getValue(), jsonData);
if (bodyType == BodyType.DynamicBody.getValue())
def.type = BodyType.DynamicBody;
else if (bodyType == BodyType.KinematicBody.getValue())
def.type = BodyType.KinematicBody;
else
def.type = BodyType.StaticBody;
def.position.set(json.readValue("position", Vector2.class, defaults.position, jsonData));
def.linearVelocity.set(json.readValue("linearVelocity", Vector2.class, defaults.linearVelocity, jsonData));
def.angle = json.readValue("angle", float.class, defaults.angle, jsonData);
def.angularVelocity = json.readValue("angularVelocity", float.class, defaults.angularVelocity, jsonData);
def.linearDamping = json.readValue("linearDamping", float.class, defaults.linearDamping, jsonData);
def.angularDamping = json.readValue("angularDamping", float.class, defaults.angularDamping, jsonData);
def.gravityScale = json.readValue("gravityScale", float.class, defaults.gravityScale, jsonData);
def.allowSleep = json.readValue("allowSleep", boolean.class, defaults.allowSleep, jsonData);
def.awake = json.readValue("awake", boolean.class, defaults.awake, jsonData);
def.fixedRotation = json.readValue("fixedRotation", boolean.class, defaults.fixedRotation, jsonData);
def.bullet = json.readValue("bullet", boolean.class, defaults.bullet, jsonData);
def.active = json.readValue("active", boolean.class, defaults.active, jsonData);
Body body = world.createBody(def);
if (def.type == BodyType.DynamicBody) {
Vector2 center = json.readValue("massData-center", Vector2.class, jsonData);
float mass = json.readValue("massData-mass", float.class, 0.0f, jsonData);
float I = json.readValue("massData-I", float.class, 0.0f, jsonData);
if (center != null) {
MassData massData = new MassData();
massData.center.set(center);
massData.mass = mass;
massData.I = I;
if (massData.mass != 0.0f || massData.I != 0.0f || massData.center.x != 0.0f || massData.center.y != 0.0f)
body.setMassData(massData);
}
}
scene.parseCustomProperties(json, body, jsonData);
String name = json.readValue("name", String.class, jsonData);
if (name != null) {
scene.putNamed(name, body);
}
fixtureSerializer.setBody(body);
scene.addFixtures(json.readValue("fixture", Array.class, Fixture.class, jsonData));
return body;
}
use of com.badlogic.gdx.physics.box2d.BodyDef in project commons-gdx by gemserk.
the class BodyBuilder method reset.
private void reset(boolean disposeShapes) {
if (fixtureDefs != null && disposeShapes) {
for (int i = 0; i < fixtureDefs.size(); i++) {
FixtureDef fixtureDef = fixtureDefs.get(i);
fixtureDef.shape.dispose();
}
}
bodyDef = new BodyDef();
fixtureDefs.clear();
fixtureUserDatas.clear();
angle = 0f;
userData = null;
position.set(0f, 0f);
massSet = false;
}
use of com.badlogic.gdx.physics.box2d.BodyDef 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.BodyDef in project libgdx by libgdx.
the class SimpleTest method createWorld.
@Override
protected void createWorld(World world) {
// 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 memomry managment.
groundBody.createFixture(groundPoly, 10);
groundPoly.dispose();
// 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);
// 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);
// add the boxPoly shape as a fixture
boxBody.createFixture(boxPoly, 10);
}
// we are done, all that's left is disposing the boxPoly
boxPoly.dispose();
// next we add a few more circles
CircleShape circleShape = new CircleShape();
circleShape.setRadius(1);
for (int i = 0; i < 10; i++) {
BodyDef circleBodyDef = new BodyDef();
circleBodyDef.type = BodyType.DynamicBody;
circleBodyDef.position.x = -24 + (float) (Math.random() * 48);
circleBodyDef.position.y = 10 + (float) (Math.random() * 100);
Body circleBody = world.createBody(circleBodyDef);
// add the boxPoly shape as a fixture
circleBody.createFixture(circleShape, 10);
}
circleShape.dispose();
}
Aggregations