[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(); } }
相关文章推荐
- [LIBGDX学习]LibGDX代码详解(十八)Box2D body type
- [LIBGDX学习]LibGDX代码详解(九)Box2D revolute joint
- [LIBGDX学习]LibGDX代码详解(四)Box2D
- [LIBGDX学习]LibGDX代码详解(十四)Box2D apply force
- [LIBGDX学习]LibGDX代码详解(五)Box2D
- [LIBGDX学习]LibGDX代码详解(二十二)Box2D 弹性
- [LIBGDX学习]LibGDX代码详解(七)Box2D
- [LIBGDX学习]LibGDX代码详解(十六)Box2D prismatic joint
- [LIBGDX学习]LibGDX代码详解(十一)Box2D bridge
- [LIBGDX学习]LibGDX代码详解(二十三)Box2D 传送带
- [LIBGDX学习]LibGDX代码详解(十三)Box2D wield joint
- [LIBGDX学习]LibGDX代码详解(十九)Box2D 金字塔
- [LIBGDX学习]LibGDX代码详解(二十一)Box2D 子弹穿透
- [LIBGDX学习]LibGDX代码详解(三)Box2D character control
- [LIBGDX学习]LibGDX代码详解(十五)Box2D
- [LIBGDX学习]LibGDX代码详解(十七) Box2D character collision
- [LIBGDX学习]LibGDX代码详解(二十八)Bullet
- [LIBGDX学习]LibGDX代码详解(三十一)Bullet 基本形状
- LIBGDX学习]LibGDX代码详解(三十九)Bullet Ragdoll
- [LIBGDX学习]LibGDX代码详解(三十五)Bullet