Euler angles to Cartesian Coordinates for use with gluLookAt
Posted
by
notrodash
on Game Development
See other posts from Game Development
or by notrodash
Published on 2012-07-09T18:46:16Z
Indexed on
2012/07/09
21:24 UTC
Read the original article
Hit count: 313
I have searched all of the internet but just couldn't find the answer. I am using LibGDX and this is part of my code that loops over and over:
public void render()
{
GL11 gl = Gdx.gl11;
float centerX = (float)Math.cos(yaw) * (float)Math.cos(pitch);
float centerY = (float)Math.sin(yaw) * (float)Math.cos(pitch);
float centerZ = (float)Math.sin(pitch);
System.out.println(centerX+" "+centerY+" "+centerZ+" ~ "+GDXRacing.camera.position.x+" "+GDXRacing.camera.position.y+" "+GDXRacing.camera.position.z);
Gdx.glu.gluLookAt(gl, GDXRacing.camera.position.x, GDXRacing.camera.position.y, GDXRacing.camera.position.z, centerX, centerY, centerZ, 0, 1, 0);
if(Gdx.input.isKeyPressed(Keys.A))
{
yaw--;
}
if(Gdx.input.isKeyPressed(Keys.D))
{
yaw++;
}
}
I might just be bad at the math, but I dont get it. Does someone have a good explanation and an idea about how to deal with this? I am trying to make a first person camera. By the way, the camera is translated by +10 on the Z axis. Currently when I run the application, this is what I get:
Watch video in browser | Download video (for those who cant download the video, everything shakes in a clockwise/anticlockwise action, depending on if I increase or decrease the Yaw value)
-Thank you.
[edit]
and with this code:
public void render()
{
GL11 gl = Gdx.gl11;
float centerX = (float)(MathUtils.cosDeg(yaw)*4);
float centerY = 0;
float centerZ = (float)(MathUtils.sinDeg(yaw)*4);
System.out.println(centerX+" "+centerY+" "+centerZ+" ~ "+GDXRacing.camera.position.x+" "+GDXRacing.camera.position.y+" "+GDXRacing.camera.position.z);
Gdx.glu.gluLookAt(gl, GDXRacing.camera.position.x, GDXRacing.camera.position.y, GDXRacing.camera.position.z, centerX, centerY, centerZ, 0, 1, 0);
if(Gdx.input.isKeyPressed(Keys.A))
{
yaw--;
}
if(Gdx.input.isKeyPressed(Keys.D))
{
yaw++;
}
}
it slowly swings from the left to the right. This approach worked for turning left and right for 2d games though. What am I doing wrong?
© Game Development or respective owner