libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感。
libgdx中,Box2d程序的大概过程:
1. 创建物理世界world,并设置重力加速度。
2. 创建正交相机,并设置其宽高。Box2d中使用物理世界中米作为单位,而不是图像中的像素,通常设一个比值,这里为了方便,直接设置为10。
3. 创建世界中的动态物体(一般是方块、圆环或其他形状物体)和静态物体(主要指地面、墙壁等)。
4. 在渲染函数中添加world时间布,并利用DebugRenderer将添加的物体绘制出来即可。
具体代码实例:
1 package com.fxb.newtest;
2
3 import com.badlogic.gdx.ApplicationAdapter;
4 import com.badlogic.gdx.Gdx;
5 import com.badlogic.gdx.graphics.GL10;
6 import com.badlogic.gdx.graphics.OrthographicCamera;
7 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
8 import com.badlogic.gdx.math.Vector2;
9 import com.badlogic.gdx.physics.box2d.Body;
10 import com.badlogic.gdx.physics.box2d.BodyDef;
11 import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
12 import com.badlogic.gdx.physics.box2d.CircleShape;
13 import com.badlogic.gdx.physics.box2d.Fixture;
14 import com.badlogic.gdx.physics.box2d.FixtureDef;
15 import com.badlogic.gdx.physics.box2d.PolygonShape;
16 import com.badlogic.gdx.physics.box2d.World;
17 import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
18
19 public class Lib018_Box2d extends ApplicationAdapter{
20
21 World world;
22 OrthographicCamera camera;
23 Box2DDebugRenderer debugRenderer;
24 ShapeRenderer rend;
25
26 @Override
27 public void create() {
28 // TODO Auto-generated method stub
29 super.create();
30
31 world = new World( new Vector2( 0, -10f ), true );
32 debugRenderer =new Box2DDebugRenderer();
33
34 camera = new OrthographicCamera();
35 camera.setToOrtho( false, Gdx.graphics.getWidth()/10, Gdx.graphics.getHeight()/10 );
36
37
38 ////create the body of box
39 BodyDef boxBodyDef = new BodyDef();
40 boxBodyDef.type = BodyType.DynamicBody;
41 //boxBodyDef.position.x = -24 + (float)(Math.random() * 48);
42 //boxBodyDef.position.y = 10 + (float)(Math.random() * 100);
43 boxBodyDef.position.set( 40, 50 );
44 Body boxBody = world.createBody(boxBodyDef);
45
46 PolygonShape boxPoly = new PolygonShape();
47 boxPoly.setAsBox(2, 1);
48 boxBody.createFixture(boxPoly, 1);
49 boxPoly.dispose();
50
51
52 /////create the body of circle
53 BodyDef bodyDef = new BodyDef();
54 bodyDef.type = BodyType.DynamicBody;
55 bodyDef.position.set( 60, 100 );
56 Body bodyCircle = world.createBody( bodyDef );
57
58 CircleShape circle = new CircleShape();
59 circle.setRadius( 2 );
60 FixtureDef fixtureDef = new FixtureDef();
61 fixtureDef.shape = circle;
62 fixtureDef.density = 1f;
63 fixtureDef.friction = 0.4f;
64 //fixtureDef.restitution = 0.6f;
65
66 bodyCircle.createFixture( fixtureDef );
67 circle.dispose();
68
69
70
71 /////create the body of ground, static body, can‘t move
72 BodyDef groundBodyDef = new BodyDef();
73 groundBodyDef.type = BodyType.StaticBody;
74 groundBodyDef.angle = (float)Math.PI*15/180;
75 groundBodyDef.position.set( 0, 0 );
76 Body groundBody = world.createBody( groundBodyDef );
77
78 PolygonShape groundBox = new PolygonShape();
79 groundBox.setAsBox( camera.viewportWidth+5, 0.5f );
80 groundBody.createFixture( groundBox, 0.0f );
81 groundBox.dispose();
82
83 }
84
85 @Override
86 public void render() {
87 // TODO Auto-generated method stub
88 super.render();
89 //Gdx.gl.glClearColor( 1, 1, 1, 0.2f );
90 Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
91
92 world.step( Gdx.graphics.getDeltaTime(), 6, 2 );
93 camera.update();
94
95 debugRenderer.render( world, camera.combined );
96
97 }
98
99 @Override
100 public void dispose() {
101 // TODO Auto-generated method stub
102 debugRenderer.dispose();
103 world.dispose();
104 super.dispose();
105 }
106
107 }
运行结果:
程序中,添加了方块、圆环和地面三个物体,前两个为动态,地面为静态,让地面倾斜15度。
方块和圆环从高空下落到斜坡时,就向下滑动,与我们平常生活常识相吻合。
时间: 2024-11-23 05:59:46