• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
syskrank

Camera rotation jumps and unwanted roll

5 posts in this topic

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.

0

Share this post


Link to post
Share on other sites
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)
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

 
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.
0

Share this post


Link to post
Share on other sites

 

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0