Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Libgdx box2d主体与sprite重叠_Java_Libgdx_Collision Detection_Box2d - Fatal编程技术网

Java Libgdx box2d主体与sprite重叠

Java Libgdx box2d主体与sprite重叠,java,libgdx,collision-detection,box2d,Java,Libgdx,Collision Detection,Box2d,我一直在困惑我的头脑与代码,我已经取得了巨大的进步,从没有做像素到米,现在有一个身体和精灵几乎在线,但我已经到了另一个点,我似乎无法找出我哪里出了错 正如你可以从图像中看到的,身体与实际的精灵不同,我有一种预感,它可能是起源,因为它的区别非常微小,但我似乎无法找出如何使它一致 我的创建方法 public class Physics1 extends ApplicationAdapter implements InputProcessor { SpriteBatch batch; Spri

我一直在困惑我的头脑与代码,我已经取得了巨大的进步,从没有做像素到米,现在有一个身体和精灵几乎在线,但我已经到了另一个点,我似乎无法找出我哪里出了错

正如你可以从图像中看到的,身体与实际的精灵不同,我有一种预感,它可能是起源,因为它的区别非常微小,但我似乎无法找出如何使它一致

我的创建方法

    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);
}

假警报,在physics editor中,你有机会改变你正在绘制的身体的原点,只需点击一下,我就能将原点移动到中心,你就可以看到它了