switch to orthogonal homogenous 4x4 transform matrices
- euler angles suck for complex memoizing movement.
- M is your 4x4 camera transform matrix
rotation around global world X axis by ang:
double c=cos(ang),s=sin(ang);
double Q[16]=
{
1, 0, 0, 0,
0, c,-s, 0,
0, s, c, 0,
0, 0, 0, 1
};
M=M*Q;
rotation around local camera X axis by ang:
double c=cos(ang),s=sin(ang);
double Q[16]=
{
1, 0, 0, 0,
0, c,-s, 0,
0, s, c, 0,
0, 0, 0, 1
};
M=((M^-1)*Q)^-1;
there are libraries for transform matrices like glm but i prefer my own
- I know many will disagree but mine is here longer than any of the other public one
- and i am used to it
- the idea is to remember both direct and inverse matrix of M at all times
- have a flag which one is updated and which not
- and before any operation check if the used matrix is relevant
- if not then compute it from the other one
- also add count of operation and after reach a treshold
- do orthogonality or orthonormality check/corrections
here you can see the differences between homogenous (4x4) and normal (3x3+1x3) 3D transform matrices:
Now the driving of camera
- I will refer to my image inside that link above of 4x4 transform matrix
- I usually use Z axis as a movement/viewing direction
- so at start reset your matrix to unit or set it to its position and orientation as you need
- when you want to rotate use local rotations
- when you want to move just add to position x0,y0,z0 the ofset you want to move (in global coordinates)
so for move forward if d units do this:
x0+=d*Zx;
y0+=d*Zy;
z0+=d*Zz;
for side movement use X vector
- for up/down use Y vector
for rotations different then around X axis just use different Q matrix
rotation around Y-axis
c, 0, s, 0,
0, 1, 0, 0,
-s, 0, c, 0,
0, 0, 0, 1;
rotation around Z-axis
c,-s, 0, 0,
s, c, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;