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

 
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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值