这里的元件暂且为图片,这里分两种情况:
1.知道图片的position,要绘制一个刚体与图片重合
2.知道刚体的position,要绘制一个图片与刚体重合
将单位换算的代码做一下封装
package com.joye3g.box2d.util; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.math.Vector2; public class Transform { /** * @param x_px 图片所在x坐标 * @param y_px 图片所在y坐标 * @param width_px 图片宽度 * @param height_px 图片高度 * @param scale 缩放比例 * @return (x,y)直接设置为body的position可使body与图片重合 */ public static Vector2 ptm(float x_px, float y_px, float width_px, float height_px, float scale){ float screenWidth = Gdx.graphics.getWidth(); float screenHeight = Gdx.graphics.getHeight(); Vector2 vector2 = new Vector2(); vector2.x = -(screenWidth - x_px * 2 - width_px) / scale / 2; vector2.y = -(screenHeight - y_px * 2 - height_px) / scale / 2; return vector2; } /** * @param x_m body所在x坐标 * @param y_m body所在y坐标 * @param wh (x,y)body的宽高 * @param scale 缩放比例 * @return (x,y)直接设置为图片的position可使图片与body重合 */ public static Vector2 mtp(float x_m, float y_m, Vector2 wh, float scale){ float screenWidth = Gdx.graphics.getWidth(); float screenHeight = Gdx.graphics.getHeight(); Vector2 vector2 = new Vector2(); vector2.x = x_m * scale + screenWidth / 2 - wh.x * scale; vector2.y = y_m * scale + screenHeight / 2 - wh.y * scale; return vector2; } }
Fender.png图片,放到Android工程下的assets文件下
package com.joye3g.box2d; 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.graphics.Texture; 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.BodyDef.BodyType; import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; 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.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.joye3g.box2d.util.Transform; public class Box2dDemo implements ApplicationListener { private static final float PXTM = 30; private OrthographicCamera camera; private Box2DDebugRenderer renderer; private World world; private Stage stage; @Override public void create() { float w = Gdx.graphics.getWidth(); float h = Gdx.graphics.getHeight(); float cameraWidth = w / PXTM; float cameraHeight = h / PXTM; camera = new OrthographicCamera(cameraWidth, cameraHeight); renderer = new Box2DDebugRenderer(); stage = new Stage(w, h, false); //知道body的position,绘制图片与body重合 // initBodyForImage(); //知道图片的position,绘制body与图片重合 initImageForBody(); } private void initImageForBody() { Texture texture = new Texture(Gdx.files.internal("Fender.png")); Image image = new Image(texture); //一个点,表示图片的position,坐标为(200,200) Vector2 vector2 = new Vector2(200f, 200f); image.setPosition(vector2.x, vector2.y); stage.addActor(image); world = new World(new Vector2(0f, 0f), true); BodyDef def = new BodyDef(); def.type = BodyType.StaticBody; //将图片的position转换为物理世界中的点并做一些处理,使绘制出来的body的左下角位置与图片的左下角位置重合 Vector2 vector3 = Transform.ptm(vector2.x, vector2.y, image.getWidth(), image.getHeight(), PXTM); def.position.set(vector3.x, vector3.y); Body body = world.createBody(def); PolygonShape shape = new PolygonShape(); //将图形的宽高稍微加大一点,方便看到效果 shape.setAsBox(image.getWidth() / PXTM / 2 + 0.1f, image.getHeight() / PXTM / 2 + 0.1f); FixtureDef def2 = new FixtureDef(); def2.shape = shape; body.createFixture(def2); } private void initBodyForImage() { Texture texture = new Texture(Gdx.files.internal("Fender.png")); Image image = new Image(texture); world = new World(new Vector2(0f, 0f), true); BodyDef def = new BodyDef(); def.type = BodyType.StaticBody; def.position.set(0f, 0f); Body body = world.createBody(def); PolygonShape shape = new PolygonShape(); Vector2 vector3 = new Vector2(image.getWidth() / 2 / PXTM, image.getHeight() / 2 / PXTM); shape.setAsBox(vector3.x + 0.1f, vector3.y + 0.1f); FixtureDef def2 = new FixtureDef(); def2.shape = shape; body.createFixture(def2); Vector2 vector2 = Transform.mtp(body.getPosition().x, body.getPosition().y, vector3, PXTM); image.setPosition(vector2.x, vector2.y); stage.addActor(image); } @Override public void dispose() { } @Override public void render() { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); renderer.render(world, camera.combined); stage.draw(); } @Override public void resize(int width, int height) { } @Override public void pause() { } @Override public void resume() { } }
运行结果: