这里的元件暂且为图片,这里分两种情况:
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坐标
* @p