How do you perform global oriented rotation on a matrix

Started by
1 comment, last by Weeve 12 years, 1 month ago
I can't seem to understand how rotation works globally, although I followed a tutorial, and failed at replicating the results, my code from the tutorial is:

// matrix ordering:
// XMatrix(x,y,z) // basically the X vector, and so on
// YMatrix(x,y,z)
// ZMatrix(x,y,z)

void MeshObject::GlobalRotateY(float roty){
float cosy = cos(roty);
float siny = sin(roty);
sf::Vector3<float> XMatrix(cosy,0,-siny);
sf::Vector3<float> ZMatrix(siny,0,cosy);
sf::Vector3<float> XMtransform((XMatrix * _XMatrix.x) + (ZMatrix * _XMatrix.z) + sf::Vector3<float>(0,_XMatrix.y,0));
sf::Vector3<float> ZMtransform((XMatrix * _ZMatrix.x) + (ZMatrix * _ZMatrix.z) + sf::Vector3<float>(0,_ZMatrix.y,0));
_XMatrix = XMtransform;
_ZMatrix = ZMtransform;
PointsToMatrix();
}


This has the odd effect of when the matrix is tilted on the X or Z axis, the code ends up rotating the mesh, but it also skews and squishes it in the process, so I had to have done something wrong.. heres working code for object oriented rotation, that I could easily build myself:

void MeshObject::RotateY(float roty){
for (unsigned int i=0;i<_verts.size();++i){
sf::Vector3<float> transform( _verts.x - _Position.x, _verts.y - _Position.y, _verts.z - _Position.z );
_verts = transform;
}
float cosy = cos(roty);
float siny = sin(roty);
sf::Vector3<float> XMatrix(cosy,0,-siny);
sf::Vector3<float> YMatrix(0,1,0);
sf::Vector3<float> ZMatrix(siny,0,cosy);
XMatrix = sf::Vector3<float> ((_XMatrix * XMatrix.x) + (_YMatrix * XMatrix.y) + (_ZMatrix * XMatrix.z));
YMatrix = sf::Vector3<float> ((_XMatrix * YMatrix.x) + (_YMatrix * YMatrix.y) + (_ZMatrix * YMatrix.z));
ZMatrix = sf::Vector3<float> ((_XMatrix * ZMatrix.x) + (_YMatrix * ZMatrix.y) + (_ZMatrix * ZMatrix.z));
_XMatrix = XMatrix;
_YMatrix = YMatrix;
_ZMatrix = ZMatrix;
PointsToMatrix();
for (unsigned int i=0;i<_verts.size();++i){
sf::Vector3<float> transform( _verts.x + _Position.x, _verts.y + _Position.y, _verts.z + _Position.z );
_verts = transform;
}
}


please help me, I have been having headaches when I try to understand what went wrong, and help will be appreciated ^.^, as it would let me focus on other matters of the program that sorely need attention
Advertisement
The skew problem is typical of when you update some coordinates and then accidentally use the new coordinates to compute the others. This is easily solved by creating separate variables for pre-rotation and post-rotation coordinates.

So instead of
sf::Vector3<float> XMatrix(cosy,0,-siny);
sf::Vector3<float> YMatrix(0,1,0);
sf::Vector3<float> ZMatrix(siny,0,cosy);
XMatrix = sf::Vector3<float> ((_XMatrix * XMatrix.x) + (_YMatrix * XMatrix.y) + (_ZMatrix * XMatrix.z));
YMatrix = sf::Vector3<float> ((_XMatrix * YMatrix.x) + (_YMatrix * YMatrix.y) + (_ZMatrix * YMatrix.z));
ZMatrix = sf::Vector3<float> ((_XMatrix * ZMatrix.x) + (_YMatrix * ZMatrix.y) + (_ZMatrix * ZMatrix.z));
_XMatrix = XMatrix;
_YMatrix = YMatrix;
_ZMatrix = ZMatrix;


try
sf::Vector3<float> XMatrix(cosy,0,-siny);
sf::Vector3<float> YMatrix(0,1,0);
sf::Vector3<float> ZMatrix(siny,0,cosy);
sf::Vector3<float> XMatrix_after_rotation = sf::Vector3<float> ((_XMatrix * XMatrix.x) + (_YMatrix * XMatrix.y) + (_ZMatrix * XMatrix.z));
sf::Vector3<float> YMatrix_after_rotation = sf::Vector3<float> ((_XMatrix * YMatrix.x) + (_YMatrix * YMatrix.y) + (_ZMatrix * YMatrix.z));
sf::Vector3<float> ZMatrix_after_rotation = sf::Vector3<float> ((_XMatrix * ZMatrix.x) + (_YMatrix * ZMatrix.y) + (_ZMatrix * ZMatrix.z));
_XMatrix = XMatrix_after_rotation;
_YMatrix = YMatrix_after_rotation;
_ZMatrix = ZMatrix_after_rotation;
Its still broken, your fix didnt work :( also, the rotation only messes up when its been tilted, I can rotate on the Y axis without skewing, but as soon as I rotate it on a Z or X and then the Y, it skews, and at a 90* rotation, where the object is facing upward, and a object space rotation 180* on the Y axis rotates the car on the Y axis 90*, while changing its Z axis to a length of 0, and reverses the Z axis for the rest of the rotation, bringing the car back to normal, but with a flipped Z axis. I'm thinking deffinitley a math problem

This topic is closed to new replies.

Advertisement