libgdx - 我看不到我的对象渲染

时间:2014-03-24 20:24:30

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

我是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();
   }
}

WorldRenderer

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

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

1 个答案:

答案 0 :(得分:0)

您需要设置相机,通常是在屏幕调整大小的重写中:

@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。我还建议将球放在屏幕中间。