• 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
CirdanValen

Moving camera in direction of rotation

6 posts in this topic

I'm trying to get my camera to move in the direction of it's rotation. My code is mostly working, but I have one problem and I'm not sure how to fix it. I'm trying to make the camera move in any direction on the horizontal plane and up and down on the vertical plane. No rolling. My problem is, when the camera is facing straight up (or close to it) the camera continue to moves on the horizontal plane instead vertically. I don't know how to make it so as the pitch get's closer to 90/-90, the movement on the horizontal plane becomes less and less. Here is my code so far:

void ITransformable::moveInRotation(float distance) {
   
    float x = 0.f;
    float z = 0.f;
    float y = 0.f;

    
    if(mRotation.x < DEGREES_TO_RADIANS(45.f)) {
        y = std::sin(mRotation.x) * distance;
    } else {
        y = std::cos(mRotation.x) * distance;
    }

    if(mRotation.y >= DEGREES_TO_RADIANS(90.f)) {
        mRotation.y -= DEGREES_TO_RADIANS(90.f);
    }

    if(mRotation.y < DEGREES_TO_RADIANS(45.f)) {
        x = std::sin(mRotation.y) * distance;

        // Find the adjacent side, which is our distance on the x-axis
        z = std::cos(mRotation.y) * distance;
    } else {
        z = std::sin(mRotation.y) * distance;
        x = std::cos(mRotation.y) * distance;
    }

    translate(x, -y, z);
}
Edited by CirdanValen
0

Share this post


Link to post
Share on other sites

The code snippet in the OP does some things that are ... unexpected. It isn't clear to me what you exactly want to achieve, so I may be wrong in hinting at the following:

 

1. Switching between sin(rx) and cos(rx) at 45° shows a discontinuity in the first derivative, suddenly changing from +0.707 to -0.707

 

2. The length of the motion vector isn't equal to distance in general:

( sin2(y) + cos2(y) + sin2(x) ) * distance = sin2(x) * distance

( sin2(y) + cos2(y) + cos2(x) ) * distance = cos2(x) * distance

 

 

The "usual" way would be to use distance as radius and heading and pitching as the angles in a spherical co-ordinate system, and hence using a conversion like this one to yield in cartesian co-ordinates. You perhaps have to permute the co-ordinates to match your needs.

 

 

However, it is also questionable why to use angles at all. One usually has an orientation matrix at hand, and picking one of its column or row vectors, resp., gives the so-called forward direction. Scaling this vector with the motion distance then yields in the desired motion vector.

0

Share this post


Link to post
Share on other sites

I would expect this sort of code to look a little like this:

 

x = sinf(fHeading) * cosf(fPitch);

y = sinf(fPitch);

z = cosf(fHeading) * cosf(fPitch);

 

There should be no need for all the if statements.

0

Share this post


Link to post
Share on other sites
The reason why I added them is because after those angles, the movement would not go in the correct direction. For example: if my camera was rotated to the left (-yaw) and I moved the camera forward and backward it would work fine. However if I rotated the camera to the right(+yaw) would not work properly, so I figured that the formula had to change depending on which quadrant I was working in. I did notice in my time searching for an answer that none of the examples people have posted do not have conditions and work like what Columbo posted. Are those conditions then just hiding a problem somewhere else in my math?
 
I'm not at the computer with my code on it at the moment, so I can't post my matrix code...but it's setup very similarly to this guide: http://openglbook.com/the-book/chapter-4-entering-the-third-dimension/


EDIT:

So I commented out all of the conditions so I was only left with
y = std::sin(mRotation.x) * distance;
x = std::sin(mRotation.y) * distance;
z = std::cos(mRotation.y) * distance;

and the movement does work fine on the horizontal plane. However, if I face directly up or directly down (as in, looking down the y axis) the camera moves on the horizontal plane (x and z) when it should be restricted to moving vertically.


EDIT #2:

Okay, instead of using the translation method I had before, I did this:
 
    mTransformationMatrix = Matrix4f::getIdentityMatrix();
    mTransformationMatrix.rotate(mRotation); 
    mTransformationMatrix.translateMatrix(0.f, 0.f, mPosition.z += 1.f*distance);

And it sorta works how I want it to. I'm rotating the camera around a center point. Doesn't feel like I'm flying around.

EDIT #3: I have no idea what I'm doing tongue.png
void ITransformable::translate(const Vector3f &vec) {
    mPosition += vec;

    mTransformationMatrix = Matrix4f::getIdentityMatrix();
    
    mTransformationMatrix.scaleMatrix(mScale);
    mTransformationMatrix.rotate(mRotation); 
    mTransformationMatrix.translateMatrix(mPosition);
}

Matrix functions:
        void rotate(float x, float y, float z) {
            Matrix4f xRotation = getIdentityMatrix();
            Matrix4f yRotation = getIdentityMatrix();
            Matrix4f zRotation = getIdentityMatrix();

            float cos = std::cos(x);
            float sin = std::sin(x);
            xRotation[5] = cos;
            xRotation[6] = -sin;
            xRotation[9] = sin;
            xRotation[10] = cos;

            cos = std::cos(y);
            sin = std::sin(y);
            yRotation[0] = cos;
            yRotation[2] = sin;
            yRotation[8] = -sin;
            yRotation[10] = cos;

            cos = std::cos(z);
            sin = std::sin(z);
            zRotation[0] = cos;
            zRotation[1] = -sin;
            zRotation[4] = sin;
            zRotation[5] = cos;

            Matrix4f r = xRotation * yRotation * zRotation;
            const float *result = ((*this)*r).getValues();
            memcpy(mValues, result, sizeof(float)*16);
        }
        void translateMatrix(float x, float y, float z) {
            Matrix4f translation = getIdentityMatrix();

            translation[12] = x;
            translation[13] = y;
            translation[14] = z;
            /*translation[3] = x;
            translation[7] = y;
            translation[11] = z;*/

            const float *result = ((*this)*translation).getValues();
            memcpy(mValues, result, sizeof(float)*16);
        }
Edited by CirdanValen
0

Share this post


Link to post
Share on other sites

(Stroke though done by me, because that was probably what you actually meant:)

[quote name='CirdanValen' timestamp='1357383510' post='5017701']
... I did notice in my time searching for an answer that none of the examples people have posted do not have conditions and work like what Columbo posted. Are those conditions then just hiding a problem somewhere else in my math?
[/quote]

The code posted by C0lumbo is a spherical to cartesian conversion as suggested by me, too. It is a method that works well for a full sphere. Under the assumption that we understood your desires correctly, then actually it should work for you, too. Things like assigning the correct dimensions often plays a role, but the shown method itself is okay.

 

You have given a link to a side "OpenGLBook.com". Therein matrices for rotations are presented. If you multiply the matrices for Ry and Rx (but use different angles then, of course) and look the 3rd column vector of the resulting matrix, then you'll see a structure that looks like the one of the spherical to cartesian conversion (with the exception of the distance, because the said column vector only gives the direction). That demonstrates that the formula works for the entire range of angles, otherwise the rotation formulas had to be wrong, too.

0

Share this post


Link to post
Share on other sites

[quote name='CirdanValen' timestamp='1357366548' post='5017664']
My problem is, when the camera is facing straight up (or close to it) the camera continue to moves on the horizontal plane instead vertically. I don't know how to make it so as the pitch get's closer to 90/-90, the movement on the horizontal plane becomes less and less.
[/quote]

 

 

Problem:

http://en.wikipedia.org/wiki/Gimbal_lock

 

 

Solution(s):

http://www.geometrictools.com/Documentation/RotationIssues.pdf

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