0
votes

im having trouble understanding how to use OpenGL rotations to simulate an MMORPG camera.I want my program to rotate the camera on 2 axes(x and y) when i press right click and i move the mouse.The diference of mouse coordinates beetwen when i press right click and where i am right now should give me 2 numbers x and y.Those numbers i will use to rotate the world around my character.

I first check if the mouse right click is pressed,then redo the last rotation and do the current rotation.But when it combines the 2 rotations i get rotations on the z axis.

I checked to see if the rotations work fine when they are just one and they do.

I tried:

a)using 2 separate glRotatef calls for each axis;

if(mouseRight==true)
        {
            glRotatef(-deltaMouse.x*mouseSensitivity,0,1,0);
            glRotatef(-deltaMouse.y*mouseSensitivity,0,0,1);
            mouse=sf::Mouse::getPosition();
            deltaMouse=initialMouse-mouse;
            glRotatef( deltaMouse.x*mouseSensitivity,0,1,0);
            glRotatef( deltaMouse.y*mouseSensitivity,0,0,1);
        }

So my questions are: 1)how do i combine 2 rotations to simulate an MMORPG camera 2)would it be better if i would make my own functions to replace glRotatef and glTranslatef but instead of using matrixes i just add some numbers to the vertex coordinates when i want to draw them?

2
If you rotate your world around the x, and y axis you can (and probably will) have a change in the z-axis. What exactly are you expecting the camera to do? Will it just rotate within a plane or have full rotation/orbit around your character?Timothy Murphy
Lets say that my character is in the point (0,0,0).now imagine a big sphere .my character is inside the sphere.my perspective of the world would be centered in a point the belongs to the sphere.i wanna rotate around my character in a way that the z axis(the z axis as seen in the beginining) is not afected.Robert Puscasu

2 Answers

1
votes

You should also invert the matrix order:

glRotatef(-deltaMouse.y*mouseSensitivity,0,0,1);
glRotatef(-deltaMouse.x*mouseSensitivity,0,1,0);
mouse=sf::Mouse::getPosition();
...

However, this will also introduce roll rotations if you do this multiple times. If you don't want this, store the both angles in variables. Then, when the mouse moves, just update these two variables. Before rendering, calculate the rotation matrix from scratch (load identity, rotation 1, rotation 2).

0
votes

You should perform matrix operations in a drawing functions, just store angles from mouse. To return to previous state you can use push/pop matrix.

mouse=sf::Mouse::getPosition();
if(mouseRight==true)
{
  deltaMouse = initialMouse-mouse;
  camera_angle.x += deltaMouse.x * mouseSensitivity;
  camera_angle.y += deltaMouse.y * mouseSensitivity;
}
initialMouse = mouse;

Then every frame draw the world.

glPushMatrix();
glTranslatef(0, 0, -camera_orbit_distance);
glRotatef(-camera_angle.x,0,1,0);
glRotatef(-camera_angle.y,0,0,1);
glTranslatef(-camera_center.x, -camera_center.y, -camera_center.z);

// ... draw something with other glTranslatef and glRotatefs if needed for specific things.
// for example:
DrawWorldModel();
for (auto & enemy : enemies)
{
  glPushMatrix();
  glTranslatef(enemy.x, enemy.y, enemy.z);
  glRotatef(enemy.angle,0,0,1);
  DrawEnemyModel();
  glPopMatrix();
}

glPopMatrix();