box2d米不起作用 - libgdx

时间:2014-09-27 08:39:42

标签: position libgdx box2d

似乎每当我定义身体定义时,我需要放置所谓的“米”的大值,以便将身体定位在我想要的位置。它似乎需要我的像素值(!)。

另一件事是当我在box2d中定义东西时,点(0,0)位于屏幕的左下角。

这是我的一个身体的代码:

package com.david.objects;

import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.david.gameworld.GameRenderer;
import com.david.helpers.AssetsLoader;

public class CatapultArm extends ObjectInWorld{

    public CatapultArm(World world) {
        super(world);
        initiate();
    }

    @Override
    protected void initiate() {
        //The Body
        bodyDef.type = BodyType.DynamicBody;
        bodyDef.position.set(154, GameRenderer.FLOOR_HEIGHT);

        // The shape
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(11, 91);

        // The fixture
        fixtureDef.shape = shape;
        fixtureDef.restitution = .80f;
        fixtureDef.friction = .75f;
        sprite = AssetsLoader.armSprite;
        body = world.createBody(bodyDef);
        body.setUserData(sprite);
        fixture = body.createFixture(fixtureDef);
    }
}

ObjectInWorld.java

package com.david.objects;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;

public abstract class ObjectInWorld {

    protected BodyDef bodyDef;
    protected Body body;
    protected World world;
    protected FixtureDef fixtureDef;
    protected Fixture fixture;
    protected Sprite sprite;
    protected ObjectInWorld(World world) {
        this.world = world;
        bodyDef = new BodyDef();
        fixtureDef = new FixtureDef();
        sprite = new Sprite();
    }

    protected void initiate() {

    }
    public float getX() {
        return body.getPosition().x;
    }
    public float getY() {
        return body.getPosition().y;
    }
    public Body getBody() {
        return body;
    }
}

你能告诉我什么是错的,在box2d中定义事物和职位的正确方法是什么?

0 个答案:

没有答案