4

I've been using OpenGL with SFML 1.6 for some time now, and it has been a blast! With one exception: I can't seem to implement a camera class correctly. You see, I am trying to create a C++ class called "Camera". Here are my functions:

Camera::Strafe(float fSpeed)

checks whether the WASD keys are pressed, and if so, move the camera at "fSpeed" in their respective directions.

Camera::MouseMove(int currentX, int currentY)

should provide a first-person mouse look, taking in the current mouse coordinates and rotating the camera accordingly. My Strafe() implementation works fine, but I can't seem to get MouseMove() right.

I already know from reading other resources on OpenGL mouse look implementations that I must center the mouse after every frame, and I have that part down. But that's about it. I can't seem to get how to actually rotate the camera on the spot from the mouse coordinates. Probably need to use some trig, I bet.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Eyal Kalderon
  • 247
  • 1
  • 4
  • 10

1 Answers1

0

I've done something similar to this (it was a 3rd person camera). If I remember what I did correctly, I took the change in mouse position and used that to calculate two angles (I did that with some trig, I believe). One angle gave me horizontal rotation, the other gave me vertical rotation. Pitch, Yaw and Roll specifically, although I can't remember which refers to which direction. There is also one you have to do before the other, or else things will rotate funny. I'm pretty sure it was pitch first, then yaw or roll.

Hopefully it should be obvious what the change in mouse position did. It allowed mouse senesitivity. If I moved the mouse fast, I would have a larger change, and so I would rotate "faster."

EDIT: Ok, I looked at my code and it's a very simple calculation.

This was done with C#, so bear with me for syntax:

_angles.X += MathHelper.ToDegrees(changeInX / 100);
_angles.Y += MathHelper.ToDegrees(changeInY / 100);

my angles were stored in a 2 dimensional vector (since I only rotated on two axes). You'll see I took my changeInX and changeInY values and simply divided them by 100 to get some arbitrary radian value, then converted that number to degrees. Adjust the 100 for sensitivity. Keep in mind, no solid-math was done here to figure this out. I just did some trial-and-error until I got something that worked well.

MGZero
  • 5,812
  • 5
  • 29
  • 46
  • Okay, that clears some stuff up. But should the pitch, yaw, and roll be saved as floats or vectors? Is there an advantage to either one? Thanks! – Eyal Kalderon Jul 13 '11 at 18:14
  • 1
    The values for pitch, yaw and roll will be the actual angles you're moving by (storing in degrees or radians is up to you). Each individual one would be a float, but there's no reason you can't store all 3 of them into a 3 dimensional vector just for ease of access. – MGZero Jul 13 '11 at 18:17
  • Pitch is rotation around X-axis, Yaw is rotation around Y-axis, and Roll is rotation around Z-axis. But how do you get the angles from the mouse input? – Eyal Kalderon Jul 13 '11 at 19:06
  • Off the top of my head, I can't remember. Give me about 2 hours or so to get home from work and to dig up my code and I'll get back to you. – MGZero Jul 13 '11 at 20:17
  • I still can't seem to get it to work. It's not your fault; it's just that I can't figure out how to rotate evrything correctly. How are you rotating the camera? Are you using "glRotatef()" or "gluLookAt()"? Also, there doesn't seem to be an equivalent to MathHelper in C++ (note that this is ANSI/ISO C++, not MS Visual C++). Do you know of another, more standard way of getting the degrees? – Eyal Kalderon Jul 13 '11 at 23:39
  • You'll probably want to use gluLookAt(). Rotate the player, then point the camera in its direction. As for converting radians to degrees, it's some simple math: multiply by (pi/180). – MGZero Jul 14 '11 at 00:23