3
\$\begingroup\$

I am very confused about camera matrices and I don't know where to start my studying. Maybe I have to study OpenGL, but I don't know C++ ...

Ok so I have a camera, Box2d world and a Box2d body.

private World world;
private Box2DDebugRenderer b2dr;
private OrthographicCamera cam;
private Body player;

@Override
public void create () {
    //Create the world without gravity
    world = new World(new Vector2(0, 0), false);
    b2dr = new Box2DDebugRenderer();
    //I create my camera
    cam = new OrthographicCamera();

    //Create the player
    BodyDef bdef = new BodyDef();
    bdef.type = BodyDef.BodyType.DynamicBody;

    //Sets the body position to the center of the screen
    bdef.position.set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);

    player = world.createBody(bdef);
    CircleShape shape = new CircleShape();
    shape.setRadius(2f);
    player.createFixture(shape, 1f);
}

@Override
public void render () {
    world.step(1f/60f, 6, 2);

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    b2dr.render(world, cam.combined);
}

I am not seeing the player on the center of the screen, just a black void.

But if I add cam.setToOrtho(false); I see the player! Why is that?

setToOrtho sets the camera to Orthographic projection WHY?

The camera is Orthographic what oder projection can it have?? Isn't the orthographic projection obvious?

\$\endgroup\$
4
  • \$\begingroup\$ According to the documentation, setToOrtho is not about setting it ortho or not ortho, it's about where does the y axis points: up or down. \$\endgroup\$
    – Vaillancourt
    Aug 8, 2016 at 17:19
  • \$\begingroup\$ Both setToOrtho(false) and setToOrtho(true) work, so that shouldn't be the case... \$\endgroup\$
    – Sharpirate
    Aug 8, 2016 at 17:33
  • \$\begingroup\$ Well maybe the important part is "Sets this camera to an orthographic projection using a viewport fitting the screen resolution, centered at (Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2)". Not sure, though, I'm not an expert in libGDX. \$\endgroup\$
    – Vaillancourt
    Aug 8, 2016 at 17:37
  • \$\begingroup\$ My question is more general... I want to know why I have to set an Orthographic camera to orthographic projection, isn't this by default? I mean what oder projection can an orthographic camera have... \$\endgroup\$
    – Sharpirate
    Aug 8, 2016 at 17:42

1 Answer 1

2
\$\begingroup\$

setToOrtho does not set the projection type of the camera. An OrthographicCamera will always have an orthographic frustrum.

Looking at the source, it seems to me that setToOrtho is a convenience method to set the viewport size to the screen size and center the camera on the viewport. setToOrtho(false) is showing your character is because your character is within this default viewport that setToOrtho creates.

The reason why you can't see your character without this call, is you never give your camera a viewport size and you never update the camera. Try

cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

And then every time you change any parameters of your camera, you must call cam.update(); before any draw calls. I usually end up changing the camera so often that I call this every frame.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You are right, thanks man! setToOrtho() has very confusing documentation for me... \$\endgroup\$
    – Sharpirate
    Aug 8, 2016 at 18:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .