PDA

View Full Version : Rotation by y axis

andrei.alex
07-08-2015, 04:40 AM
The problem that I have is that my cube is not rotationg by the Y axis bu it rotates by the X or Z.

Matrix4 Math::createTransformationMatrix(const Vector3 &position, Vector3 &rotation, const float &scale)
{
Matrix4 matrix(1.0f);
Matrix4::translate(position, matrix, matrix);
Matrix4::rotate(toRadians(rotation.x), Vector3(1, 0, 0), matrix, matrix);
Matrix4::rotate(toRadians(rotation.y), Vector3(0, 1, 0), matrix, matrix);
Matrix4::rotate(toRadians(rotation.z), Vector3(0, 0, 1), matrix, matrix);
Matrix4::scale(Vector3(scale), matrix, matrix);

return matrix
}

The rotation function is:

Matrix4 Matrix4::rotate(float angle, const Vector3 &axis, const Matrix4 &src, Matrix4 &dest)
{
float c = cos(angle);
float s = sin(angle);
float omc = 1.0f - c;

float x = axis.x;
float y = axis.y;
float z = axis.z;

float xy = x * y;
float yz = y * z;
float xz = x * z;
float xs = x * s;
float ys = y * s;
float zs = z * s;

float f00 = x * x * omc + c;
float f01 = xy * omc + zs;
float f02 = xz * omc - ys;

float f10 = xy * omc - zs;
float f11 = y * y * omc + c;
float f12 = yz * omc + xs;

float f20 = xz * omc + ys;
float f21 = yz * omc - xs;
float f22 = z * z * omc + c;

float t00 = src.elements[0 + 0 * 4] * f00 + src.elements[0 + 1 * 4] * f01 + src.elements[0 + 2 * 4] * f02;
float t01 = src.elements[1 + 0 * 4] * f00 + src.elements[1 + 1 * 4] * f01 + src.elements[1 + 2 * 4] * f02;
float t02 = src.elements[2 + 0 * 4] * f00 + src.elements[2 + 1 * 4] * f01 + src.elements[2 + 2 * 4] * f02;

float t10 = src.elements[0 + 0 * 4] * f10 + src.elements[0 + 1 * 4] * f11 + src.elements[0 + 2 * 4] * f12;
float t11 = src.elements[1 + 0 * 4] * f10 + src.elements[1 + 1 * 4] * f11 + src.elements[1 + 2 * 4] * f12;
float t12 = src.elements[2 + 0 * 4] * f10 + src.elements[2 + 1 * 4] * f11 + src.elements[2 + 2 * 4] * f12;

dest.elements[0 + 2 * 4] = src.elements[0 + 0 * 4] * f20 + src.elements[0 + 1 * 4] * f21 + src.elements[0 + 2 * 4] * f22;
dest.elements[1 + 2 * 4] = src.elements[1 + 0 * 4] * f20 + src.elements[1 + 1 * 4] * f21 + src.elements[1 + 2 * 4] * f22;
dest.elements[2 + 2 * 4] = src.elements[2 + 0 * 4] * f20 + src.elements[2 + 1 * 4] * f21 + src.elements[2 + 2 * 4] * f22;

dest.elements[0 + 0 * 4] = t00;
dest.elements[1 + 0 * 4] = t01;
dest.elements[2 + 0 * 4] = t02;

dest.elements[0 + 1 * 4] = t10;
dest.elements[1 + 1 * 4] = t11;
dest.elements[2 + 1 * 4] = t12;

return dest;
}

This is the matrix I get after rotating by Y and for me it looks good.

0.587789 0 -0.809014 0
0 1 0 0
0.805926 0 0.592017 0
0 0 0 1

Am I doing something wrong? I checked multiple times and it looks correct. Also everything is set correct in the vertex shader.

__bob__
07-08-2015, 05:21 AM
For openGL GLM is your freind!!!!

andrei.alex
07-14-2015, 11:36 AM
I finally used GLM and everything is working now. Thanks.