44 minutes ago, the incredible smoker said:
Really JoeJ ?, great, now let me find out how it works.
To understand you only need to think of it geometrically i grant you, and this applies to almost anything related to transformation matrices.
It's NOT necessary to know about linear algebra at all.
I'll comment the code:
vec2 diff = goal - head; // direction from head to goal. (imagine the goal is left from the head)
vec2 dir = diff / diff.Length(); // make the vector unit length, that's what i've meant with Unit()
vec2 orientationXaxis = dir; // now the xAxis points along the direction from head to goal (imagine as a red line pointing to the left)
vec2 orientationYaxis = vec2(-dir.y, dir.x); // by swapping and flipping a sign, we get a perpendicular direction. (imagine green line upwards)
// those two now vectors now define orientation as an orthogonal frame
// notice there is no rotation involved, we just construct the final orientation in place
// we could see this as a 2x2 orientation matrix if we want. But D3D wants a 4x4 matrix, so lets convert to this:
D3DXMATRIX matrix;
// first 3 rows contain the orthonormal orientation axis:
matrix (0,0) = orientationXaxis.x; matrix (0,1) = orientationXaxis.y; matrix (0,2) = 0; matrix (0,3) = 0; // set x axis
matrix (1,0) = orientationYaxis.x; matrix (1,1) = orientationYaxis.y; matrix (1,2) = 0; matrix (1,3) = 0; // set y axis
matrix (2,0) = 0; matrix (2,1) = 0; matrix (2,2) = 1; matrix (2,3) = 0; // z axis remains just z axis, because we don't change it for the 2D case
// final row is position:
matrix (3,0) = head.x; matrix (3,1) = head.y; matrix (3,2) = 0; matrix (3,2) = 1;
// (!) i'm afraid you need to swap all row/column indices, if you stick to D3Ds (unpractical) default convention (Syntax may be also wrong, i have no experience with D3D)
So again, try to imagine how this works and ask if necessary.
Next we could work out a routine to rotate one direction to another by a given factor using linear approximisation still avoiding trig.
Can't edit post, so code formatted again:
vec2 diff = goal - head; // direction from head to goal. (imagine the goal is left from the head)
vec2 dir = diff / diff.Length(); // make the vector unit length, that's what i've meant with Unit() :)
vec2 orientationXaxis = dir; // now the xAxis points along the direction from head to goal (imagine as a red line pointing to the left)
vec2 orientationYaxis = vec2(-dir.y, dir.x); // by swapping and flipping a sign, we get a perpendicular direction. (imagine green line upwards)
// those two now vectors now define orientation as an orthogonal frame
// notice there is no rotation involved, we just construct the final orientation in place
// we could see this as a 2x2 orientation matrix if we want. But D3D wants a 4x4 matrix, so lets convert to this:
D3DXMATRIX matrix;
// first 3 rows contain the orthonormal orientation axis:
matrix (0,0) = orientationXaxis.x; matrix (0,1) = orientationXaxis.y; matrix (0,2) = 0; matrix (0,3) = 0; // set x axis
matrix (1,0) = orientationYaxis.x; matrix (1,1) = orientationYaxis.y; matrix (1,2) = 0; matrix (1,3) = 0; // set y axis
matrix (2,0) = 0; matrix (2,1) = 0; matrix (2,2) = 1; matrix (2,3) = 0; // z axis remains just z axis, because we don't change it for the 2D case
// final row is position:
matrix (3,0) = head.x; matrix (3,1) = head.y; matrix (3,2) = 0; matrix (3,2) = 1;
// (!) i'm afraid you need to swap all row/column indices, if you stick to D3Ds (unpractical) default convention (Syntax may be also wrong, i have no experience with D3D)
11 minutes ago, the incredible smoker said:
By the way what are trig functions ?
cos sin atan acos... (trigonometry)