【问题标题】:Libgdx box2d body overlaps with spriteLibgdx box2d 主体与精灵重叠
【发布时间】:2018-07-21 19:40:14
【问题描述】:

我一直对代码感到困惑,我已经取得了巨大的进步,从不做像素到米到现在有一​​个身体和精灵几乎在一条线上,但是我到达了另一个我似乎无法做到的点找出我哪里出错了。

正如您从图像中看到的那样,身体与实际的精灵有所不同,我有一种预感,它可能是起源,因为它的差异非常轻微,但我似乎无法弄清楚如何让它对齐.

我的创建方法

    public class Physics1 extends ApplicationAdapter implements InputProcessor {
SpriteBatch batch;
Sprite sprite;
Texture img;
World world;
Body body;
Box2DDebugRenderer debugRenderer;
Matrix4 debugMatrix;
OrthographicCamera camera;
Vector2 bodyOrigin;


float torque = 0.0f;
boolean drawSprite = true;

final float PIXELS_TO_METERS = 100f;

final float WORLD_WIDTH =100;
final float WORLD_HEIGHT=100;


@Override
public void create() {

    Assets.instance.init(new AssetManager());
    batch = new SpriteBatch();
    bodyOrigin = new Vector2();

    sprite = new Sprite();
    sprite.setRegion(Assets.instance.tyre.tyre);
    sprite.setSize(12,12);
    sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);




    sprite.setPosition(50-sprite.getWidth()/2,25);

    Gdx.app.log("Physics1", "Sprite positions"+ -sprite.getWidth()/2+ " ,"+ -sprite.getHeight()/2);

    world = new World(new Vector2(0, 0f),true);

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    BodyEditorLoader load = new BodyEditorLoader(Gdx.files.internal("levels/pittstop.json"));

    bodyDef.position.set(sprite.getX()/PIXELS_TO_METERS,sprite.getY()/PIXELS_TO_METERS);


    Gdx.app.log("Physics1", "Body positions calculations"+ sprite.getX() +" "+ sprite.getWidth()/2);

    Gdx.app.log("Physics1", "Body positions"+ bodyDef.position);
    body = world.createBody(bodyDef);

    FixtureDef fixtureDef = new FixtureDef();

    fixtureDef.density = 0.1f;


    fixtureDef.friction = 0.0f;
    fixtureDef.restitution = 0.0f;


    float scale =0.2f;

    load.attachFixture(body, "tyre", fixtureDef, scale);
    Gdx.app.log("Physics1"," Orgin of body" +load.getOrigin("tyre", scale).cpy());
    bodyOrigin = load.getOrigin("tyre", scale).cpy();

    body.setUserData("tyre");


    Gdx.input.setInputProcessor(this);

    debugRenderer = new Box2DDebugRenderer();

    camera = new OrthographicCamera(WORLD_WIDTH,WORLD_HEIGHT);
    Gdx.app.log("Physics1", "camera "+ camera.viewportWidth+" "+camera.viewportHeight);

    camera.position.set(WORLD_WIDTH / 2f, WORLD_HEIGHT / 2f, 0);
    Gdx.app.log("Physics1", "camera "+ camera.viewportWidth+" "+camera.viewportHeight);

}

我的渲染方法

    public void render() {
    camera.update();
    world.step(1f/60f, 6, 2);

    body.applyTorque(torque,true);

    sprite.setPosition((body.getPosition().x * PIXELS_TO_METERS), 
            (body.getPosition().y * PIXELS_TO_METERS))
             ;

    sprite.setRotation((float)Math.toDegrees(body.getAngle()));

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(camera.combined);

    debugMatrix = batch.getProjectionMatrix().cpy().scale(PIXELS_TO_METERS, 
                  PIXELS_TO_METERS, 0);

    batch.begin();


    if(drawSprite)
        batch.draw(sprite, sprite.getX(), sprite.getY(),bodyOrigin.x,
                   bodyOrigin.y,
            sprite.getWidth(),sprite.getHeight(),sprite.getScaleX(),sprite.
                            getScaleY(),sprite.getRotation());


    batch.end();

    debugRenderer.render(world, debugMatrix);
}

【问题讨论】:

    标签: java libgdx collision-detection box2d


    【解决方案1】:

    误报,在物理编辑器中你有机会改变你正在绘制的身体的原点,只需点击周围,我就能将原点移动到中心,你就有了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多