Camera Direction Messup

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Camera Direction Messup

OliverY
This post was updated on .
EDIT 1: So I noticed that the camera can only move forward and backward on the z axis. Why not the X?

Hi everyone, so I'm working on a camera class for my 3d game. I think some of my calculations of off. So if I strafe left or right then move forward it doesn't consider that strafed movement, if that makes sense... Also the camera rotation gets messed up.

Here's my code:
switch (direction)
{
        case DONT_MOVE:
                break;
        case FORWARD:
                x -= 0.1 * (float)Math.sin(Math.toRadians(yaw));
                z += 0.1 * (float)Math.cos(Math.toRadians(yaw));
                break;
        case BACKWARD:
                x += 0.1 * (float)Math.sin(Math.toRadians(yaw));
                z -= 0.1 * (float)Math.cos(Math.toRadians(yaw));
                break;
        case STRAFE_L:
                x -= 0.1 * (float)Math.sin(Math.toRadians(yaw - 90));
                z += 0.1 * (float)Math.cos(Math.toRadians(yaw - 90));
                break;
        case STRAFE_R:
                x -= 0.1 * (float)Math.sin(Math.toRadians(yaw + 90));
                z += 0.1 * (float)Math.cos(Math.toRadians(yaw + 90));
                break;
        default:
                break;
}

dx += mouseCenter.x - MouseInfo.getPointerInfo().getLocation().x;
dy += mouseCenter.y - MouseInfo.getPointerInfo().getLocation().y;

pitch = dy * 0.05f;
yaw = dx * 0.05f;

gl.glLoadIdentity();

gl.glTranslatef(x, y, z);
gl.glRotatef(-pitch, 1, 0, 0);
gl.glRotatef(-yaw, 0, 1, 0);

try
{
        new Robot().mouseMove(mouseCenter.x, mouseCenter.y);
} catch (final AWTException e) {
        e.printStackTrace();
}

Anyone have an idea? Is it my calculations?
Reply | Threaded
Open this post in threaded view
|

Re: Camera Direction Messup

gouessej
Administrator
This post was updated on .
Hi

If you're allowed to use some source code in your project, please look at how I implemented the exact same thing in Truly Unusual Experience of Revolution® almost 8 years ago. I spent several months to make it work which was difficult because AWT isn't very good to achieve that (and don't create a Robot instance each time you want to move the mouse cursor).

You have to store the direction and to update it when you turn. You have to use the direction when you move forward or backward.

Take care of the units (degrees, radians). glRotatef takes degrees in input whereas Math.cos and sin take radians in input.

Edit.: Actually, you already take care of the units.

Edit.2: In my code, it looks like this:
x += 0.1 * (float)Math.sin(Math.toRadians(yaw - 90));
z += 0.1 * (float)Math.cos(Math.toRadians(yaw - 90));

Edit.3: You use a break then you can't strafe and move forward at the same time. Rather use several flags and conditions to update the abscissa (x) and the applicate (z).

Edit.4: I use gluLookAt with the up vector at (0,1,0) instead of using glRotate.
Julien Gouesse | Personal blog | Website