<p>A common mistake that causes this behavior is to first apply the translation and then the rotation when rendering the scene. I think that's what's happening. If you apply the rotation first and then the translation it should behave as expected. Give that a try</p>

CC BY-SA 3.0
2016-04-10T07:33:33.293Z
Did you get to try this? Did it work?
Leon
CC BY-SA 3.0
2016-04-10T16:05:34.780Z
I just did it now. I originally had the pos defined in the 4x4 view matrix, but i took it out and did it at the end, and it worked! Thankyou!
iBaconButty
CC BY-SA 3.0
2016-04-08T19:21:25.237Z
2016-04-08T19:21:25.237Z
Leon
<p>Currently for a university assignment I have been given a 3D rendering engine, capable of rendering a 3D world. One task is to create a first person camera for it (I know with OpenGL you don't have a camera, you have to effect the modelview). </p>

<p>Anyway, I have managed to get a first person camera working, however rotation is becoming a bit of a problem. I have spent a good few hours trying to fix the problem, but can't seem to find a solution. </p>

<p>Rotation works perfectly, until you move forward about -10 into the z axis. When you do, the camera rotation seemingly rotates around a set point, so it orbits the actual model, rather than just changing the direction the camera is looking at. </p>

<p>So if you move out, then rotate, the camera will move around the object, not just move the object out of sight. </p>

<p>Not sure what code you'll need to see to help, but here's some: </p>

<p><strong>Camera Rotation:</strong></p>

<pre><code>void Camera::RotateCamera(float yaw, float pitch, float roll)
{
//left and right rotation
Quaternion a;
Quaternion b;
Quaternion c;
Quaternion up;
Quaternion right;
Quaternion temp;
float s;
float vx;
float vy;
float vz;

if (yaw != 0)
{
    s = cos(yaw / 2);
    c.SetQuaternion(m_direction, s);
    vx = m_upVector[0] * sin(yaw / 2);
    vy = m_upVector[1] * sin(yaw / 2);
    vz = m_upVector[2] * sin(yaw / 2);
    a.SetQuaternion(vx, vy, vz, s);
    b.SetQuaternion(vx, -vy, -vz, s);

    up = a*c*b;

    m_direction.SetVector(up[0], up[1], up[2]);
}

//up and down rotation
if (pitch != 0 &amp;&amp; curDown &lt; maxDown &amp;&amp; curDown &gt; -maxDown)
{
    curDown += pitch;
    s = cos(pitch / 2);

    a.SetQuaternion(m_direction, s);
    vx = m_rightVector[0] * sin(pitch / 2);
    vy = m_rightVector[1] * sin(pitch / 2);
    vz = m_rightVector[2] * sin(pitch / 2);
    b.SetQuaternion(vx, vy, vz, s);
    c.SetQuaternion(-vx, -vy, -vz, s);

    right;
    temp;
    right = b * a * c;
    m_direction.SetVector(right[0], right[1], right[2]);
}
m_direction.Normalise();
m_rightVector = m_direction.CrossProduct(m_upVector);
m_upVector = m_rightVector.CrossProduct(m_direction);

m_rightVector.Normalise();
m_upVector.Normalise();
UpdateViewMatrix();
}
</code></pre>

<p>The quaternion class is correct (so I'm assuming as it was already created for us). </p>

<p><strong>Camera rotation code</strong></p>

<pre><code>if (x - ox &gt; 0)
{
    cam_pos-&gt;RotateCamera(0.05, 0, 0);
}
else if (x - ox &lt; 0)
{
    cam_pos-&gt;RotateCamera(-0.05, 0, 0);
}

ox = x;
oy = y;
</code></pre>

<p><strong>Applying the camera</strong></p>

<pre><code>modelview = (*cam_pos-&gt;GetViewMatrix()) * modelview;
modelview = modelview*translation;

float aspect_ratio = (float)m_width / (float)m_height;
projection.SetPerspective(60.0f, aspect_ratio, 1.0f, 1000.0f);

glUniformMatrix4fv( m_uniform_projection, 1, GL_FALSE, projection.ToPtr() );
glBindSampler(0, m_texDefaultSampler);
</code></pre>

<p>Where <code>*cam_pos</code> is the actual camera. </p>

<p>The code works, just rotation goes off.</p>

<p>This is what currently happens on rotation: 
<a href="https://i.sstatic.net/5ptng.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/5ptng.jpg" alt="weird rotation effect"></a></p>

<opengl><rendering><c++><mathematics>
OpenGL C++ Camera Rotation Problem
iBaconButty
# OpenGL C++ Camera Rotation Problem

(Asked by: username_0 on 2016-03-23)

Currently for a university assignment I have been given a 3D rendering engine, capable of rendering a 3D world. One task is to create a first person camera for it (I know with OpenGL you don't have a camera, you have to effect the modelview).

Anyway, I have managed to get a first person camera working, however rotation is becoming a bit of a problem. I have spent a good few hours trying to fix the problem, but can't seem to find a solution.

Rotation works perfectly, until you move forward about -10 into the z axis. When you do, the camera rotation seemingly rotates around a set point, so it orbits the actual model, rather than just changing the direction the camera is looking at.

So if you move out, then rotate, the camera will move around the object, not just move the object out of sight.

Not sure what code you'll need to see to help, but here's some:

**Camera Rotation:**

```
void Camera::RotateCamera(float yaw, float pitch, float roll)
{
//left and right rotation
Quaternion a;
Quaternion b;
Quaternion c;
Quaternion up;
Quaternion right;
Quaternion temp;
float s;
float vx;
float vy;
float vz;

if (yaw != 0)
{
    s = cos(yaw / 2);
    c.SetQuaternion(m_direction, s);
    vx = m_upVector[0] * sin(yaw / 2);
    vy = m_upVector[1] * sin(yaw / 2);
    vz = m_upVector[2] * sin(yaw / 2);
    a.SetQuaternion(vx, vy, vz, s);
    b.SetQuaternion(vx, -vy, -vz, s);

    up = a*c*b;

    m_direction.SetVector(up[0], up[1], up[2]);
}

//up and down rotation
if (pitch != 0 && curDown < maxDown && curDown > -maxDown)
{
    curDown += pitch;
    s = cos(pitch / 2);

    a.SetQuaternion(m_direction, s);
    vx = m_rightVector[0] * sin(pitch / 2);
    vy = m_rightVector[1] * sin(pitch / 2);
    vz = m_rightVector[2] * sin(pitch / 2);
    b.SetQuaternion(vx, vy, vz, s);
    c.SetQuaternion(-vx, -vy, -vz, s);

    right;
    temp;
    right = b * a * c;
    m_direction.SetVector(right[0], right[1], right[2]);
}
m_direction.Normalise();
m_rightVector = m_direction.CrossProduct(m_upVector);
m_upVector = m_rightVector.CrossProduct(m_direction);

m_rightVector.Normalise();
m_upVector.Normalise();
UpdateViewMatrix();
}

```

The quaternion class is correct (so I'm assuming as it was already created for us).

**Camera rotation code**

```
if (x - ox > 0)
{
    cam_pos->RotateCamera(0.05, 0, 0);
}
else if (x - ox < 0)
{
    cam_pos->RotateCamera(-0.05, 0, 0);
}

ox = x;
oy = y;

```

**Applying the camera**

```
modelview = (*cam_pos->GetViewMatrix()) * modelview;
modelview = modelview*translation;

float aspect_ratio = (float)m_width / (float)m_height;
projection.SetPerspective(60.0f, aspect_ratio, 1.0f, 1000.0f);

glUniformMatrix4fv( m_uniform_projection, 1, GL_FALSE, projection.ToPtr() );
glBindSampler(0, m_texDefaultSampler);

```

Where `*cam_pos` is the actual camera.

The code works, just rotation goes off.

This is what currently happens on rotation:
[![weird rotation effect](https://i.sstatic.net/5ptng.jpg)](https://i.sstatic.net/5ptng.jpg)

**Tags:** opengl, rendering, c++, mathematics


---

## 1 answers

### Answer by username_1 on 2016-04-08. Score: 1 | Accepted answer

A common mistake that causes this behavior is to first apply the translation and then the rotation when rendering the scene. I think that's what's happening. If you apply the rotation first and then the translation it should behave as expected. Give that a try

> **Comments:**
> **username_1**: Did you get to try this? Did it work?  
> **username_0**: I just did it now. I originally had the pos defined in the 4x4 view matrix, but i took it out and did it at the end, and it worked! Thankyou!  

---

