【问题标题】:libgdx - I can't see my object renderinglibgdx - 我看不到我的对象渲染
【发布时间】:2014-03-24 20:24:29
【问题描述】:

我是 libgdx 的新手。我想在屏幕上渲染一个球,但我只能看到一个空的黑屏。

主要

public class Main implements ApplicationListener
{
   @Override
   public void render()
   {
    if (!paused)
    {
        worldController.update(Gdx.graphics.getDeltaTime());
    }

    Gdx.gl.glClearColor(0, 0, 0, 0x255/255f);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    worldRenderer.render();
   }
}

世界渲染器

public class WorldRenderer implements Disposable
{
   public void render()
   {
      batch.setProjectionMatrix(camera.combined);
      batch.begin();

    for (Ball b : getWorldController().balls)
        b.render(batch);

    batch.end();
   }
}

public class Ball extends AbstractGameObject
{
    private TextureRegion textureRegion;

    public Ball()
    {
        Texture texture = new Texture(Gdx.files.internal("images/ball.png"));

        dimension.x = 1;
        dimension.y = 1;

        // Center image on game object
        origin.set(dimension.x / 2, dimension.y / 2);
        // Bounding box for collision detection
        bounds.set(0, 0, dimension.x, dimension.y);
        // Set physics values
        terminalVelocity.set(3f, 4f);
        friction.set(12f, 0f);
        acceleration.set(0f, -25f);

        position.x = 5;
        position.y = 5;

        textureRegion = new TextureRegion(texture, 0, 0, dimension.x, dimension.y);
    }

    @Override
    public void render(SpriteBatch batch)
    {
        batch.draw( textureRegion,
                    position.x,
                    position.y,
                    origin.x,
                    origin.y,
                    dimension.x,
                    dimension.y,
                    scale.x,
                    scale.y,
                    rotation,
                    false);
    }

}

抽象游戏对象

public abstract class AbstractGameObject
{
    public Vector2 position;
    public Vector2 dimension;
    public Vector2 origin;
    public Vector2 scale;
    public float rotation;

    public Vector2 velocity;
    public Vector2 terminalVelocity;
    public Vector2 friction;
    public Vector2 acceleration;
    public Rectangle bounds;

    public AbstractGameObject()
    {
        this.position = new Vector2();
        this.dimension = new Vector2();
        this.origin = new Vector2();
        this.scale = new Vector2();

        this.velocity = new Vector2();
        this.terminalVelocity = new Vector2();
        this.friction = new Vector2();
        this.acceleration = new Vector2();
        this.bounds = new Rectangle();
    }

    public void updateMotionX(float deltaTime)
    {
        if (velocity.x != 0)
        {
            // Apply friction
            if (velocity.x > 0)
            {
                velocity.x = Math.max(velocity.x - friction.x * deltaTime, 0);
            }
            else
            {
                velocity.x = Math.min(velocity.x + friction.x * deltaTime, 0);
            }
        }

        // Apply acceleration
        velocity.x += acceleration.x * deltaTime;

        // Make sure that acceleration does not exceed the terminal velocity
        velocity.x = MathUtils.clamp(velocity.x, -terminalVelocity.x, terminalVelocity.x);
    }

    public void updateMotionY(float deltaTime)
    {
        if (velocity.y != 0)
        {
            if (velocity.y > 0)
            {
                velocity.y = Math.max(velocity.y - friction.y * deltaTime, 0);
            }
            else
            {
                velocity.y  = Math.min(velocity.y + friction.y * deltaTime, 0);
            }
        }

        // Apply acceleration
        velocity.y += acceleration.y * deltaTime;

        // Make sure that acceleration does not exceed the terminal velocity
        velocity.y = MathUtils.clamp(velocity.y, -terminalVelocity.y, terminalVelocity.y);
    }

    public void update(float deltaTime)
    {
        updateMotionX(deltaTime);
        updateMotionY(deltaTime);

        position.x += velocity.x * deltaTime;
        position.y += velocity.y * deltaTime;
    }

    public abstract void render (SpriteBatch batch);
}

我错过了什么?我做错了什么?

【问题讨论】:

  • 对于初学者,您缺少一些代码。你的相机代码在哪里?
  • 您绝对需要向我们展示您的相机。否则我们无能为力。一点点:如果您使用new OrthographicCamera(80, 45) 创建一个摄像头,它将创建一个新摄像头,其 0,0 点位于屏幕中间,宽度为 80 个单位,高度为 45 个单位。所以你的Balls 半径将是 1/80 屏幕宽度。在 1600*900 的屏幕上(有漂亮的数字),它的半径为 20 像素。

标签: android libgdx game-engine desktop game-physics


【解决方案1】:

您需要设置您的相机,通常在屏幕调整大小的覆盖范围内:

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

    this.camera = new OrthographicCamera(width, height);
    this.camera.position.set(width / 2f, height / 2f, 0);
    this.camera.update();
}

这会将 1 个像素设置为 1 个单位,因此您可能需要增加球的尺寸。屏幕的左下角将为 0,0。我还建议您将球放在屏幕中间。

【讨论】:

    猜你喜欢
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    相关资源
    最近更新 更多