Libgdx Box2D将图像设置为正文

Met*_*lel 7 box2d libgdx

我将图像设置为动态/静态体时出现问题.

我发现了一个关于它的flash代码

public void setImage()
{
    sprite = new B2Sprite();
    var bitmap:Bitmap = new Image();
    sprite.addChild();
    bitmap.x -= bitmap.width / 2;
    bitmap.y -= bitmap.height / 2;
    body.SetUserData(sprite);
    sprite.body = body;
}
Run Code Online (Sandbox Code Playgroud)

但是将它转换为java :( plz可以帮助我或者为java上的box2D提供教程链接.

Ric*_*rdo 9

你有一个非常有用的关于box2D和libgdx关于逻辑和渲染的教学概念的链接.这里

然后你可以像这样分离渲染部分的逻辑部分:

逻辑部分:

private void createBottleBody() {
    // 0. Create a loader for the file saved from the editor.
    BodyEditorLoader loader = new BodyEditorLoader(sourceFile);

    // 1. Create a BodyDef, as usual.
    BodyDef bd = new BodyDef();
    bd.position.set(0, 0);
    bd.type = BodyType.DynamicBody;

    // 2. Create a FixtureDef, as usual.
    FixtureDef fd = new FixtureDef();
    fd.density = 1;
    fd.friction = 0.5f;
    fd.restitution = 0.3f;

    // 3. Create a Body, as usual.
    bottleModel = world.createBody(bd);

    // 4. Create the body fixture automatically by using the loader.
    loader.attachFixture(bottleModel, "test01", fd, BOTTLE_WIDTH);
}
Run Code Online (Sandbox Code Playgroud)

渲染部分:

public void render() {
    Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin);

    bottleSprite.setPosition(bottlePos.x, bottlePos.y);
    bottleSprite.setOrigin(bottleModelOrigin.x, bottleModelOrigin.y);
    bottleSprite.setRotation(bottleModel.getAngle() * MathUtils.radiansToDegrees);

    ...
}
Run Code Online (Sandbox Code Playgroud)

装载机:在链接中,您可以找到装载机.

public void attachFixture(Body body, String name, FixtureDef fd, float scale) {

    //Load the rigidModel by key
    RigidBodyModel rbModel = (RigidBodyModel) this.model.rigidBodies.get(name);

    if (rbModel == null)
        throw new RuntimeException("Name '" + name + "' was not found.");

    //Loading polygons
    Vector2 origin = this.vec.set(rbModel.origin).mul(scale);
    Vector2[] vertexes;
    PolygonModel polygon;

    for (int i = rbModel.polygons.size()-1; 0 <= i; i--) {
        polygon = (PolygonModel) rbModel.polygons.get(i);
        vertexes = polygon.vectorBuffer;

        //Loading vertexes (scaled) from polygon
        for (int ii = vertexes.length-1; 0 <= ii; ii--) {
            vertexes[ii] = new Vector2().set((Vector2) polygon.vertices.get(ii)).mul(scale);
            vertexes[ii].sub(origin);
        }

        //sets vertexs to polygon
        this.polygonShape.set(vertexes);
        fd.shape = this.polygonShape;
        body.createFixture(fd);

    }


    //Loading circles
    CircleModel circle;
    Vector2 center;
    float radius;

    for (int i = rbModel.circles.size()-1; 0 <= i; i--) {
        circle = (CircleModel) rbModel.circles.get(i);
        center = new Vector2().set(circle.center).mul(scale);
        radius = circle.radius * scale;

        this.circleShape.setPosition(center);
        this.circleShape.setRadius(radius);
        fd.shape = this.circleShape;
        body.createFixture(fd);

    }
}
Run Code Online (Sandbox Code Playgroud)

最后......模特

public static class CircleModel {
    public final Vector2 center = new Vector2();
    public float radius;
}

public static class PolygonModel {
    public final List<Vector2> vertices = new ArrayList<Vector2>();
    private Vector2[] vectorBuffer;
}

public static class RigidBodyModel {
    public String name;
    public String imagePath;
    public final Vector2 origin = new Vector2();
    public final List<PolygonModel> polygons = new ArrayList<PolygonModel>();
    public final List<CircleModel> circles = new ArrayList<CircleModel>();
}

public static class Model {
    public final Map<String, RigidBodyModel> rigidBodies = new HashMap<String, RigidBodyModel>();
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!


jel*_*ion 5

由于您的问题被标记为LibGDX,因此您可以方便地使用Sprites.

public void setImage(Sprite sprite){
    body.setUserData(sprite);
}
Run Code Online (Sandbox Code Playgroud)

然后render(),可能会有这样的事情

private SpriteBatch batch = new SpriteBatch();
public void render() {
    batch.begin();
    Iterator<Body> iter = world.getBodies();
    Body body;
    Sprite sprite;
    while (iter.hasNext()) {
        body = iter.next();
        sprite = (Sprite) body.getUserData();
        // I did not take a look at implementation but you get the idea
        sprite.x = body.x;
        sprite.y = body.y;
        sprite.draw(batch);
    }
    batch.end();  
}
Run Code Online (Sandbox Code Playgroud)

使用getImage()可返回具有适当位置,旋转等的sprite的方法在body周围创建一个包装类并不是一个坏主意.

请注意,我还没有测试过我的代码,可能会有错误.