Jump to content

  • Log In with Google      Sign In   
  • Create Account

Camera rotation jumps and unwanted roll


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 syskrank   Members   -  Reputation: 188

Like
0Likes
Like

Posted 23 April 2014 - 08:17 AM

Hello everyone.

Yes, that's another camera rotation question smile.png Maybe I'm overlooking something, but anyways.

 

Problem #1:

 

When I rotate my camera around global Y, everything is OK untill it reaches the point of -90 or +90 degrees from initial rotation. Then suddenly it jumps right to the 180 degrees opposite!

 

Problem #2:

 

The unwanted rolling. I am rotating my camera around global Y for yaw and around camera's local X for pitch.

Yet, I am getting the roll. I've tried to use scene graph here to eliminate roll by attaching camera to a series of scene nodes: one for yaw, one for pitch; but there was no good.

________________

 

My code regarding rotations::

    Quaternion::Quaternion( const Vector3f& axis, const float degrees ) {
        float radian = RADIANS( degrees ) * 0.5f;
        float sinHalf = std::sin( radian );
        float cosHalf = std::cos( radian );
        Vector3f a = axis.GetNorm();

        this->x = a.x * sinHalf;
        this->y = a.y * sinHalf;
        this->z = a.z * sinHalf;
        this->w = cosHalf;
    }

    void Transform::RotateAxisAngle( const Vector3f& axis, const float degrees ) {
        Quaternion q( axis, degrees );
        rotation = q * rotation;
        rotation.Normalize();
    }

    Matrix4f Quaternion::GetRotationMatrix() const {
        Matrix4f R;
        R.InitIdentity();
        R.m[0] = 1 - 2 * y * y - 2 * z * z;
        R.m[1] = 2 * x * y - 2* z* w;
        R.m[2] = 2 * x * z + 2 * y * w;
        R.m[4] = 2 * x * y + 2 * z * w;
        R.m[5] = 1 - 2 * x * x - 2 * z * z;
        R.m[6] = 2 * y * z - 2 * x * w;
        R.m[8] = 2 * x * z - 2 * y * w;
        R.m[9] = 2 * y * z + 2 * x * w;
        R.m[10] = 1 - 2 * x * x - 2 * y * y;
        return R;
    }

The above works fine, however, the following code might been screwing up something.

The code regarding camera matrices:

// this is called every frame, t is the reference to the parent's transformations;
// V is the View matrix
    void Camera::Update( const core::Transform& t ) {
        core::Matrix4f R;
        R.InitCamera( t.GetForward(), t.GetUp() );
        core::Matrix4f T;
        T.InitTranslation( t.position * -1.0f );
        V = R * T;
    }

    core::Matrix4f Camera::GetVP() {
        return P * V;
    }

// this is projection matrix:
    void Matrix4f::InitPerspective( const float fov, const float aspect, const float near, const float far ) {
        float tanHalfFov = std::tan( RADIANS( fov/2.0f ) );
        float range = near - far;

        InitIdentity();

        m[ 0 ] = 1.0f / ( tanHalfFov * aspect );
        m[ 5 ] = 1.0f / tanHalfFov;
        m[ 10 ] = ( -near - far ) / range;
        m[ 11 ] = 2.0f * far * near / range;
        m[ 14 ] = 1.0f;
        m[ 15 ] = 0.0f;
    }

// this is camera matrix:
    void Matrix4f::InitCamera( const Vector3f& forward, const Vector3f& up ) {
        Vector3f f = forward.GetNorm(); // actual normalized forward
        Vector3f r = up.GetNorm();
        r = r.Cross( f ).GetNorm(); // actual right vector
        Vector3f u = f.Cross( r ).GetNorm(); // recalculated up

        InitIdentity();

        m[ 0 ] = r.x;
        m[ 1 ] = r.y;
        m[ 2 ] = r.z;

        m[ 4 ] = u.x;
        m[ 5 ] = u.y;
        m[ 6 ] = u.z;

        m[ 8 ] = f.x;
        m[ 9 ] = f.y;
        m[ 10 ] = f.z;
    }

So, I suppose that the problem is coming from the View-Projection or Projection separately.

Could anyone help me to determine what the error exactly is ?

How do I eliminate unwanted rolling properly?

 

Thanks in advance.

 

_____

P.S.

 

1) Object rotations are working just fine. Only the camera rotations are screwed up.

 

2) My 4x4 matrices elements arranged like this ( they are one-dimensional arrays of float ):

 

|  0  1  2  3 |      
|  4  5  6  7 |      
|  8  9 10 11 |         
| 12 13 14 15 |
 

My quaternions' elements are placed like the following:

 

x, y, z, w

 

3) Transform aggregates within itself Vector3 for position and scaling and Quaternion for rotation.

So, all my GameObjects have the Transform component.

 

4) Camera is implemented as a component and can be attached to any GameObject to inherit it's transformations.

Camera utilizes Matrix4f class.



Sponsor:

#2 JohnnyCode   Members   -  Reputation: 219

Like
0Likes
Like

Posted 23 April 2014 - 04:59 PM

void Matrix4f::InitCamera( const Vector3f& forward, const Vector3f& up ) {
Vector3f f = forward.GetNorm(); // actual normalized forward
Vector3f r = up.GetNorm();
r = r.Cross( f ).GetNorm(); // actual right vector
Vector3f u = f.Cross( r ).GetNorm(); // recalculated up



this is gimbal lock. Look at matrix is constructed from lookto direction, up direction, and their cross product, and third cross product of the ones. It is 3 normalized vectors resulting in orthonormal matrix that rotates everything, with possible translation added.

If lookto vector in space "flows through" up vector (getting close enough) their crossproduct is negative, but the same direction pattern on the plane they form. Normalizing this result will still point to the new (negated) direction. Your right vector inverts

In your code I cannot to track the const Vector3f& up. Many camera implementations does not need such gimbal case to handle, up vector stays 0,1,0 constantly throuh program (the one as input, not the one in orthonormal matrix). If you need to eliminate gimbal lock, you have few options. For example to have up vector not constant but kept as a vector in the plane that cuts verticaly through the camera and always with the same moment towards lookat vector (not necesarilly angle, only moment)

#3 syskrank   Members   -  Reputation: 188

Like
0Likes
Like

Posted 24 April 2014 - 02:16 AM

Thanks for the reply, JohnnyCode.

 

Well, maybe it's due to my lack of experience, but I think that it has nothing to do with the actual gimbal lock problem.

 

As it's stated here, the gimbal lock is an inability to rotate around certain axes or the presence of the undesired rotation around some other axis, not the needed one ( which fits to my second problem with unwanted roll );

 

So, yes, the rolling I get may be caused somehow by so called gimbal lock, but:

 

I am forming my View matrix from the two vectors: object's up and object's forward, calculating the 'right' direction vector inside the Matrix4f::InitCamera() method. These two are acquired from the object's current rotation, which is represented as quaternion ( yeah, that's not a panacea, I know :) ).

 

So, to get the current 'forward' direction, I multiply the const Vector3f globalZAxis by the current object's rotation.

I'm getting the local 'up' the same way - by multiplying subsystem-wise const Vector3f globalYAxis by the current object's rotation.

    Vector3f Transform::GetForward() const {
        Quaternion q = rotation * globalZAxis * rotation.GetConjugate();
        Vector3f f( q.x, q.y, q.z );
        return f.GetNorm();
    }

    Vector3f Transform::GetUp() const {
        Quaternion q = rotation * globalYAxis * rotation.GetConjugate();
        Vector3f u( q.x, q.y, q.z );
        return u.GetNorm();
    }

    Vector3f Transform::GetRight() const {
        Vector3f f = GetForward();
        Vector3f u = GetUp();
        Vector3f r = u.Cross( f );
        return r.GetNorm();
    }

I've tried to use this const 'up' direction, as you suggested, but it's no good - the same rotation jumps occurred.

The solution you suggested with the (x,y+y*y,z) vector is not working.

 

Now will look towards the 'right' direction flipping, maybe I should use my Transfrom::GetRight directily. Anyways, I should check all the math for the camera again.



#4 syskrank   Members   -  Reputation: 188

Like
0Likes
Like

Posted 24 April 2014 - 04:08 AM

Well, I've managed to fix these sudden rotation warps by constructing camera rotation matrix directly from the quaternion and then transposing the result ( as it's stated in the Matrix and Quaternion FAQ ).

 

    void Camera::Update( const core::Transform& t ) {
        // FIXME: moving directions are ruined at some point
        core::Matrix4f R = t.rotation.GetRotationMatrix().GetTransposed();
        core::Matrix4f T;
        T.InitTranslation( t.position * -1.0f );
        V = R * T;
    }

I think this is the right way of doing it, since I've got only one Transform instance per object ( with single position, rotation and scaling ).

 

Still, the minor rolling persists and the new problem has arrived:

 

even if the camera uses the same 'forward' direction as it's parent object ( it uses his 'forward', actually ), translating parent object is breaking the directions at some point - 'forward' appears to be rotated. Gonna to find out why.



#5 JohnnyCode   Members   -  Reputation: 219

Like
0Likes
Like

Posted 24 April 2014 - 06:43 AM

 
I am forming my View matrix from the two vectors: object's up and object's forward, calculating the 'right' direction vector inside the Matrix4f::InitCamera() method. These two are acquired from the object's current rotation, which is represented as quaternion ( yeah, that's not a panacea, I know :) ).
 
So, to get the current 'forward' direction, I multiply the const Vector3f globalZAxis by the current object's rotation.
I'm getting the local 'up' the same way - by multiplying subsystem-wise const Vector3f globalYAxis
   
 


if the quaternion axis is x axis, then rotatin z and y should do perfect job I believe?
the suggestion I adviced does not achieve up vector correct moment relative to lookat vector at all. Sorry for that.

#6 JohnnyCode   Members   -  Reputation: 219

Like
0Likes
Like

Posted 24 April 2014 - 08:20 PM

 

if the quaternion axis is x axis, then rotatin z and y should do perfect job I believe?

Again, axises of world space towards a quaternion of world space will always result in reflow- in crossing oposite moment. In case of x axis as well.

 

Though if you  can achive this :

 

to have up vector not constant but kept as a vector in the plane that cuts verticaly through the camera and always with the same moment towards lookat vector (not necesarilly angle, only moment)

you would be all set. But I do not know how to derive up vector from lookat vector in this manner.

 

In the manner of being slightly rotated "above" of lookat vector in plane of [lookat,Yaxis]. Not considering its length






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS