android游戏引擎libgdx使用教程9:libgdx中box2d的用法
java代码
- package&com.cnblogs.htynkn.listener;&&
- &&
- import&com.badlogic.gdx.applicationlistener;&&&
- import&com.badlogic.gdx.gdx;&&&
- import&com.badlogic.gdx.graphics.gl10;&&&
- import&com.badlogic.gdx.graphics.orthographiccamera;&&&
- 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.world;&&&
- import&com.badlogic.gdx.physics.box2d.bodydef.bodytype;&&
- &&
- public&class&demogame&implements&applicationlistener&{&&
- &&
- protected&orthographiccamera&camera;&&&
- protected&box2ddebugrenderer&renderer;&//&测试用绘制器&&&
- private&world&world;&&
- &&
- @override&&&
- public&void&create()&{&&&
- camera&=&new&orthographiccamera(48,&32);&&&
- camera.position.set(0,&15,&0);&&&
- renderer&=&new&box2ddebugrenderer();&&&
- &&
- world&=&new&world(new&vector2(0,&-9.8f),&true);&//&一般标准重力场&&&
- bodydef&bd&=&new&bodydef();&//声明物体定义&&&
- bd.position.set(2f,&2f);&&&
- bd.type=bodytype.dynamicbody;&&&
- body&b&=&world.createbody(bd);&//通过world创建一个物体&&&
- circleshape&c&=&new&circleshape();&//创建一个形状(圆)&&&
- c.setradius(1f);&//设置半径&&&
- b.createfixture(c,&1f);&//将形状和密度赋给物体&&&
- }&&
- &&
- @override&&&
- public&void&dispose()&{&&&
- &&
- renderer.dispose();&&&
- world.dispose();&&
- &&
- renderer&=&null;&&&
- world&=&null;&&&
- }&&
- &&
- @override&&&
- public&void&pause()&{&&&
- //&todo&auto-generated&method&stub&&
- &&
- }&&
- &&
- @override&&&
- public&void&render()&{&&&
- world.step(gdx.app.getgraphics().getdeltatime(),&3,&3);&&&
- gl10&gl&=&gdx.app.getgraphics().getgl10();&&&
- gl.glclear(gl10.gl_color_buffer_bit);&&&
- camera.update();&&&
- camera.apply(gl);&&&
- renderer.render(world,&camera.combined);&&&
- }&&
- &&
- @override&&&
- public&void&resize(int&width,&int&height)&{&&&
- //&todo&auto-generated&method&stub&&
- &&
- }&&
- &&
- @override&&&
- public&void&resume()&{&&&
- //&todo&auto-generated&method&stub&&
- &&
- }&&
- &&
- }&&