【问题标题】:sprite's setPosition method does not worksprite 的 setPosition 方法不起作用
【发布时间】:2017-05-01 12:46:05
【问题描述】:

所以我一直在尝试将精灵移动到我的身体在世界上移动的任何地方。所以我使用了精灵的 setPosition 方法,但事实证明它没有帮助。我的图像只是位于 (0,0) 位置,我不明白为什么。

代码如下:

package com.example.shapetest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

class ShapeTest implements ApplicationListener {

    SpriteBatch batch;
    World world;
    OrthographicCamera camera;
    Box2DDebugRenderer debugRenderer;
    Texture squareTx;
    Sprite square;
    Body body;

    @Override
    public void create() {
        batch = new SpriteBatch();
        world = new World(new Vector2(0,-10),false);
        squareTx = new Texture(Gdx.files.internal("sq.png"));
        square = new Sprite(squareTx);

        debugRenderer = new Box2DDebugRenderer();
        camera = new OrthographicCamera();
        camera.setToOrtho(false);

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(new Vector2((Gdx.graphics.getWidth() / 2) * 0.01f, Gdx.graphics.getHeight() * 0.01f));
        body = world.createBody(bd);
        BodyDef bd2 = new BodyDef();
        bd2.type = BodyType.StaticBody;
        bd2.position.set(new Vector2(Gdx.graphics.getWidth()/2 * 0.01f, 100 * 0.01f));
        Body body2 = world.createBody(bd2);

        square.setSize(100,100);

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f,0.5f);
        PolygonShape shape2 = new PolygonShape();
        shape2.setAsBox(1.6f,0.3f);

        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.restitution = 0.2f;
        fd.shape = shape;
        fd.friction = 0.4f;

        body.createFixture(fd);
        body2.createFixture(shape2, 0f);

        shape.dispose();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        Vector2 pos = body.getPosition();
        square.setPosition(pos.x ,pos.y);

        camera.update();
                batch.setProjectionMatrix(camera.combined);
        Matrix4 cameraCopy = camera.combined.cpy();
        debugRenderer.render(world, cameraCopy.scl(100f));
        world.step(1/60f,10,10);
        batch.begin();
        square.draw(batch);
        batch.end();
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        batch.dispose();
    }
}

结果如下:

(精灵应该跟随粉红色框的位置)

【问题讨论】:

  • 你忘记了batch.setProjectionMatrix(camera.combined);。并且您的相机需要在resize() 中调整大小。并且为 box2d 世界和游戏场景使用不同的相机比例是令人费解的。阅读 LibGDX 视口。
  • @Tenfour04 编辑了代码,添加了batch.setProjectionMatrix(camera.combined);,结果还是一样,无法将精灵移动到那个粉色框位置
  • ...您仍然没有调整相机大小或使用视口,或为世界和游戏使用相同的相机...
  • 如果您指的是黄色框。在我看来它很可能在 (Gdx.graphics.getWidth() / 2) * 0.01f, Gdx.graphics.getHeight() * 0.01f),巧合的是你给身体的位置。
  • 看看这个,可能对你有帮助stackoverflow.com/a/43410465/3445320

标签: libgdx game-engine


【解决方案1】:

body.getPosition() 返回较小的值,因此要进行绘图,您需要将该值转换为像素值。

根据您的说法,精灵大小为 100 像素,它表示动态物理体,即多边形形状,在物理中具有大小 0.5f * 2 = 1; 单位,即您假设 1 单位/米的物理等于 100 像素。

Vector2 pos = body.getPosition();   // value return in physics unit
square.setPosition(pos.x*100 ,pos.y*100); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多