libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感。
libgdx中,Box2d程序的大概过程:
1. 创建物理世界world,并设置重力加速度。
2. 创建正交相机,并设置其宽高。Box2d中使用物理世界中米作为单位,而不是图像中的像素,通常设一个比值,这里为了方便,直接设置为10。
3. 创建世界中的动态物体(一般是方块、圆环或其他形状物体)和静态物体(主要指地面、墙壁等)。
4. 在渲染函数中添加world时间布,并利用DebugRenderer将添加的物体绘制出来即可。
具体代码实例:
package com.fxb.newtest; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; public class Lib018_Box2d extends ApplicationAdapter{ World world;
OrthographicCamera camera;
Box2DDebugRenderer debugRenderer;
ShapeRenderer rend; @Override
public void create() {
// TODO Auto-generated method stub
super.create(); world = new World( new Vector2( 0, -10f ), true );
debugRenderer =new Box2DDebugRenderer(); camera = new OrthographicCamera();
camera.setToOrtho( false, Gdx.graphics.getWidth()/10, Gdx.graphics.getHeight()/10 ); ////create the body of box
BodyDef boxBodyDef = new BodyDef();
boxBodyDef.type = BodyType.DynamicBody;
//boxBodyDef.position.x = -24 + (float)(Math.random() * 48);
//boxBodyDef.position.y = 10 + (float)(Math.random() * 100);
boxBodyDef.position.set( 40, 50 );
Body boxBody = world.createBody(boxBodyDef); PolygonShape boxPoly = new PolygonShape();
boxPoly.setAsBox(2, 1);
boxBody.createFixture(boxPoly, 1);
boxPoly.dispose(); /////create the body of circle
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set( 60, 100 );
Body bodyCircle = world.createBody( bodyDef ); CircleShape circle = new CircleShape();
circle.setRadius( 2 );
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = 1f;
fixtureDef.friction = 0.4f;
//fixtureDef.restitution = 0.6f; bodyCircle.createFixture( fixtureDef );
circle.dispose(); /////create the body of ground, static body, can't move
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
groundBodyDef.angle = (float)Math.PI*15/180;
groundBodyDef.position.set( 0, 0 );
Body groundBody = world.createBody( groundBodyDef ); PolygonShape groundBox = new PolygonShape();
groundBox.setAsBox( camera.viewportWidth+5, 0.5f );
groundBody.createFixture( groundBox, 0.0f );
groundBox.dispose(); } @Override
public void render() {
// TODO Auto-generated method stub
super.render();
//Gdx.gl.glClearColor( 1, 1, 1, 0.2f );
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); world.step( Gdx.graphics.getDeltaTime(), 6, 2 );
camera.update(); debugRenderer.render( world, camera.combined ); } @Override
public void dispose() {
// TODO Auto-generated method stub
debugRenderer.dispose();
world.dispose();
super.dispose();
} }
运行结果:
程序中,添加了方块、圆环和地面三个物体,前两个为动态,地面为静态,让地面倾斜15度。
方块和圆环从高空下落到斜坡时,就向下滑动,与我们平常生活常识相吻合。