use of com.badlogic.gdx.physics.box2d.PolygonShape in project libgdx by libgdx.
the class Box2DCharacterControllerTest method createPlayer.
private Body createPlayer() {
BodyDef def = new BodyDef();
def.type = BodyType.DynamicBody;
Body box = world.createBody(def);
PolygonShape poly = new PolygonShape();
poly.setAsBox(0.45f, 1.4f);
playerPhysicsFixture = box.createFixture(poly, 1);
poly.dispose();
CircleShape circle = new CircleShape();
circle.setRadius(0.45f);
circle.setPosition(new Vector2(0, -1.4f));
playerSensorFixture = box.createFixture(circle, 0);
circle.dispose();
box.setBullet(true);
return box;
}
use of com.badlogic.gdx.physics.box2d.PolygonShape in project libgdx by libgdx.
the class Box2DTest method createBoxes.
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);
// 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();
}
use of com.badlogic.gdx.physics.box2d.PolygonShape in project libgdx-inGameConsole by StrongJoshua.
the class Box2DTest method create.
@Override
public void create() {
float w = Gdx.graphics.getWidth();
w *= 2;
float h = Gdx.graphics.getHeight();
h *= 2;
ratio = h / w;
Gdx.app.getGraphics().setWindowedMode((int) w, (int) h);
mX = (float) WIDTH / w;
mY = (float) HEIGHT / h;
Box2D.init();
world = new World(new Vector2(0, -9.81f), true);
batch = new SpriteBatch();
sprites = new Sprite[250];
bodies = new Body[sprites.length];
float k, j;
for (int i = 0; i < sprites.length; i++) {
if (i < 50) {
k = 0;
j = 1;
} else if (i < 100) {
k = 50 * sprites[i - 1].getWidth() + sprites[i - 1].getWidth() / 2;
j = 2;
} else if (i < 150) {
k = 100 * sprites[i - 1].getWidth() + sprites[i - 1].getWidth() / 2;
j = 3;
} else if (i < 200) {
k = 150 * sprites[i - 1].getWidth() + sprites[i - 1].getWidth() / 2;
j = 4;
} else {
k = 200 * sprites[i - 1].getWidth() + sprites[i - 1].getWidth() / 2;
j = 5;
}
sprites[i] = new Sprite(new Texture(Gdx.files.classpath("tests/badlogic.jpg")));
sprites[i].setSize(2, 2);
sprites[i].setOriginCenter();
BodyDef bdef = new BodyDef();
bdef.type = BodyDef.BodyType.DynamicBody;
bdef.position.set(i * sprites[i].getWidth() + sprites[i].getWidth() / 2 - k, 15 * j);
bodies[i] = world.createBody(bdef);
PolygonShape poly = new PolygonShape();
poly.setAsBox(sprites[i].getWidth() / 2, sprites[i].getHeight() / 2);
FixtureDef fdef = new FixtureDef();
fdef.shape = poly;
fdef.restitution = .2f;
fdef.density = 1f;
bodies[i].createFixture(fdef);
poly.dispose();
}
BodyDef bdef = new BodyDef();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(WIDTH / 2, -5);
Body b = world.createBody(bdef);
PolygonShape poly = new PolygonShape();
poly.setAsBox(50, 5);
b.createFixture(poly, 0);
bdef = new BodyDef();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(-5, HEIGHT / 2);
b = world.createBody(bdef);
poly = new PolygonShape();
poly.setAsBox(5, 50);
b.createFixture(poly, 0);
bdef = new BodyDef();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(WIDTH + 5, (HEIGHT * ratio) / 2);
b = world.createBody(bdef);
poly = new PolygonShape();
poly.setAsBox(5, 50);
b.createFixture(poly, 0);
bdef = new BodyDef();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(WIDTH / 2, HEIGHT * ratio + 5);
b = world.createBody(bdef);
poly = new PolygonShape();
poly.setAsBox(50, 5);
b.createFixture(poly, 0);
poly.dispose();
c = new OrthographicCamera(WIDTH, HEIGHT * ratio);
c.position.set(c.viewportWidth / 2, c.viewportHeight / 2, 0);
c.update();
batch.setProjectionMatrix(c.combined);
debugRenderer = new Box2DDebugRenderer();
console = new GUIConsole(false);
cExec = new MyCommandExecutor();
console.setCommandExecutor(cExec);
// set to 'Z' to demonstrate that it works with binds other than the
// default
console.setDisplayKeyID(Input.Keys.Z);
console.setVisible(true);
// test multiple resets with nested multiplexers
InputMultiplexer im1 = new InputMultiplexer();
im1.addProcessor(new Stage());
im1.addProcessor(new Stage());
InputMultiplexer im2 = new InputMultiplexer();
im2.addProcessor(new Stage());
im2.addProcessor(new Stage());
im1.addProcessor(im2);
Gdx.input.setInputProcessor(im1);
console.setMaxEntries(16);
console.resetInputProcessing();
// console already present, logged to consoles
console.resetInputProcessing();
console.setSizePercent(100, 33);
console.setPositionPercent(0, 67);
}
use of com.badlogic.gdx.physics.box2d.PolygonShape in project RubeLoader by tescott.
the class RubeLoaderTest method create.
@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
Gdx.input.setInputProcessor(this);
mB2Controllers = new Array<B2Controller>();
mCamPos = new Vector3();
mCurrentPos = new Vector3();
camera = new OrthographicCamera(100, 100 * h / w);
camera.position.set(50, 50, 0);
camera.zoom = 1.8f;
camera.update();
loader = new RubeSceneLoader();
scene = loader.loadScene(Gdx.files.internal("data/palmcontrollers.json"));
debugRender = new Box2DDebugRenderer();
batch = new SpriteBatch();
polygonBatch = new PolygonSpriteBatch();
textureMap = new HashMap<String, Texture>();
textureRegionMap = new HashMap<Texture, TextureRegion>();
createSpatialsFromRubeImages(scene);
createPolySpatialsFromRubeFixtures(scene);
mWorld = scene.getWorld();
// configure simulation settings
mVelocityIter = scene.velocityIterations;
mPositionIter = scene.positionIterations;
if (scene.stepsPerSecond != 0) {
mSecondsPerStep = 1f / scene.stepsPerSecond;
}
mWorld.setContactListener(this);
//
// example of custom property handling
//
Array<Body> bodies = scene.getBodies();
if ((bodies != null) && (bodies.size > 0)) {
for (int i = 0; i < bodies.size; i++) {
Body body = bodies.get(i);
String gameInfo = (String) scene.getCustom(body, "GameInfo", null);
if (gameInfo != null) {
System.out.println("GameInfo custom property: " + gameInfo);
}
}
}
// Instantiate any controllers that are in the scene
Array<Fixture> fixtures = scene.getFixtures();
if ((fixtures != null) && (fixtures.size > 0)) {
for (int i = 0; i < fixtures.size; i++) {
Fixture fixture = fixtures.get(i);
int controllerType = (Integer) scene.getCustom(fixture, "ControllerType", 0);
switch(controllerType) {
case B2Controller.BUOYANCY_CONTROLLER:
// only allow polygon buoyancy controllers for now..
if (fixture.getShape().getType() == Shape.Type.Polygon) {
float bodyHeight = fixture.getBody().getPosition().y;
// B2BuoyancyController b2c = new B2BuoyancyController();
// need to calculate the fluid surface height for the buoyancy controller
PolygonShape shape = (PolygonShape) fixture.getShape();
shape.getVertex(0, mTmp);
// initialize the height, transforming to 'world'
float maxHeight = mTmp.y + bodyHeight;
// find the maxHeight
for (int j = 1; j < shape.getVertexCount(); j++) {
shape.getVertex(j, mTmp);
// transform to world coordinates
maxHeight = Math.max(maxHeight, mTmp.y + bodyHeight);
}
B2BuoyancyController b2c = new B2BuoyancyController(// assume up
B2BuoyancyController.DEFAULT_SURFACE_NORMAL, (Vector2) scene.getCustom(fixture, "ControllerVelocity", B2BuoyancyController.DEFAULT_FLUID_VELOCITY), mWorld.getGravity(), maxHeight, fixture.getDensity(), (Float) scene.getCustom(fixture, "LinearDrag", B2BuoyancyController.DEFAULT_LINEAR_DRAG), (Float) scene.getCustom(fixture, "AngularDrag", B2BuoyancyController.DEFAULT_ANGULAR_DRAG));
// reference back to the controller from the fixture (see
fixture.setUserData(b2c);
// beginContact/endContact)
// add it to the list so it can be stepped later
mB2Controllers.add(b2c);
}
break;
case B2Controller.GRAVITY_CONTROLLER:
{
B2GravityController b2c = new B2GravityController();
b2c = new B2GravityController((Vector2) scene.getCustom(fixture, "ControllerVelocity", B2GravityController.DEFAULT_GRAVITY));
fixture.setUserData(b2c);
mB2Controllers.add(b2c);
}
break;
}
}
}
scene.printStats();
// no longer need any scene references
scene.clear();
}
Aggregations