http://pimentoso.blogspot.com/2013/01/meter-and-pixel-units-in-box2d-game.html
需翻墙
——————————————————————————————————————————————————————————————————
Game World - our container for the box2d world and other game elements. It also contains the game stage.
public class GameWorld { // here we set up the actual viewport size of the game in meters. public static float UNIT_WIDTH = GameScreen.SCREEN_WIDTH/160; // 6.4 meters width public static float UNIT_HEIGHT = GameScreen.SCREEN_HEIGHT/160; // 3.75 meters height public static final Vector2 GRAVITY = new Vector2(0, -9.8f); public final Stage stage; // stage containing game actors (not GUI, but actual game elements) public World box2dWorld; // box2d world public Bob bob; // our playing character public GameWorld() { box2dWorld = new World(GRAVITY, true); stage = new Stage(); // create the game stage stage.setViewport(UNIT_WIDTH, UNIT_HEIGHT, false); // set the game stage viewport to the meters size createWorld(); } private void createWorld() { // create box2d bodies and the respective actors here. bob = new Bob(this); stage.addActor(bob); // add more game elements here } public void update(float delta) { // perform game logic here box2dWorld.step(delta, 3, 3); // update box2d world stage.act(delta); // update game stage } }Bob - our character. It extends Image which is a subclass of Actor, and contains a reference to the box2d body (in this case a circle). Its texture is resized to the game meter units, but it will be rendered using the game stage camera, so even if the texture is scaled to 0.8x0.8, it will be quite big since the viewport is 6x3 meters.
public class Bob extends Image { public static final float RADIUS = 0.4f; // bob is a ball with 0.8m diameter public final Body body; // bob's box2d body public Bob(GameWorld world) { // bob is an Image, so we load the graphics from the assetmanager Texture tex = Assets.manager.get("characters.png", Texture.class); this.setDrawable(new TextureRegionDrawable(new TextureRegion(tex, 0, 256, 128, 128))); // generate bob's box2d body CircleShape circle = new CircleShape(); circle.setRadius(RADIUS); BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DynamicBody; bodyDef.position.x = 2f; bodyDef.position.y = 2f; bodyDef.linearDamping = 0.1f; bodyDef.angularDamping = 0.5f; this.body = world.box2dWorld.createBody(bodyDef); this.body.setUserData(ElementType.BOB); Fixture fix = body.createFixture(circle, 50); fix.setDensity(1); fix.setFriction(1f); fix.setRestitution(0.8f); fix.setFilterData(filter); circle.dispose(); // generate bob's actor this.setPosition(body.getPosition().x-RADIUS, body.getPosition().y-RADIUS); // set the actor position at the box2d body position this.setSize(RADIUS*2, RADIUS*2); // scale actor to body's size this.setScaling(Scaling.stretch); // stretch the texture this.setAlign(Align.center); } @Override public void act(float delta) { // here we override Actor's act() method to make the actor follow the box2d body super.act(delta); setOrigin(RADIUS, RADIUS); setRotation(MathUtils.radiansToDegrees * body.getAngle()); setPosition(body.getPosition().x-RADIUS, body.getPosition().y-RADIUS); } }Game Renderer - a custom renderer used to draw the game world. It just positions the camera each frame and draws the game stage. Very simple.
public class GameRenderer { GameWorld world; OrthographicCamera camera; Box2DDebugRenderer renderer; public GameRenderer(GameWorld world) { this.world = world; this.renderer = new Box2DDebugRenderer(); // we obtain a reference to the game stage camera. The camera is scaled to box2d meter units this.camera = (OrthographicCamera) world.stage.getCamera(); // center the camera on bob (optional) camera.position.x = world.bob.body.getPosition().x; camera.position.y = world.bob.body.getPosition().y; } public void render() { // have the camera follow bob camera.position.x = world.bob.body.getPosition().x; camera.position.y = world.bob.body.getPosition().y; Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // box2d debug renderering (optional) camera.update(); renderer.render(world.box2dWorld, camera.combined); // game stage rendering world.stage.draw(); } }
转载于:https://www.cnblogs.com/cuizhf/p/3611445.html
相关资源:amballah-libgdx:2d libgdx游戏-源码