您的位置:首页 > 编程语言

[LIBGDX学习]LibGDX代码详解(十八)Box2D

2018-02-24 13:46 141 查看
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

public class SimpleTest extends Box2DTest {
@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;// 这里没有设定position应该就是原点位置。
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);

// next we create the 50 box bodies using the PolygonShape we just
// defined. This process is similar to the one we used for the ground
// body. Note that we reuse the polygon for each body fixture.
for (int i = 0; i < 50; i++) {// ori20
// 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);
}

// 自己创造围栏
BodyDef barrierDef = new BodyDef();
barrierDef.type = BodyType.StaticBody;
barrierDef.position.x = -25;
Body barrierBody;
for (int i = 2; i < 22; i += 2) {
barrierDef.position.y = i;
barrierBody = world.createBody(barrierDef);
barrierBody.createFixture(boxPoly, 10);
}

barrierDef.position.x = 25;
for (int i = 2; i < 22; i += 2) {
barrierDef.position.y = i;
barrierBody = world.createBody(barrierDef);
barrierBody.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 < 50; i++) {// ori10
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: