Reputation: 67
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?
Upvotes: 0
Views: 904
Reputation: 91
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();
Upvotes: 0
Reputation: 32587
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).
Upvotes: 1