• Create Account

# Moving camera in direction of rotation

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.

6 replies to this topic

### #1CirdanValen  Members   -  Reputation: 272

Like
0Likes
Like

Posted 05 January 2013 - 12:15 AM

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;

y = std::sin(mRotation.x) * distance;
} else {
y = std::cos(mRotation.x) * distance;
}

}

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, 05 January 2013 - 05:01 AM.

### #2haegarr  Crossbones+   -  Reputation: 6998

Like
0Likes
Like

Posted 05 January 2013 - 03:17 AM

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.

### #3C0lumbo  Crossbones+   -  Reputation: 3763

Like
0Likes
Like

Posted 05 January 2013 - 04:23 AM

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

y = sinf(fPitch);

There should be no need for all the if statements.

### #4CirdanValen  Members   -  Reputation: 272

Like
0Likes
Like

Posted 05 January 2013 - 04:58 AM

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
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, 05 January 2013 - 07:37 AM.

### #5haegarr  Crossbones+   -  Reputation: 6998

Like
0Likes
Like

Posted 05 January 2013 - 07:00 AM

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

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

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.

### #6CirdanValen  Members   -  Reputation: 272

Like
0Likes
Like

Posted 05 January 2013 - 04:45 PM

Alrighty, I got it guys thanks.

### #7DT....  Members   -  Reputation: 487

Like
0Likes
Like

Posted 07 January 2013 - 12:36 PM

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.

Problem:

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

Solution(s):

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

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