vgo
26-05-2007, 04:19 PM
Still working on my game engine and I have yet another stupid math problem. :)
Here's what I want to do: I have a space station which has several turrets and I want these turrets to rotate to face the player. How can I do this?
Here's some code that I've tried to use, but it doesn't work properly, I have no idea where this code came from, it's probably cut & paste with some trial and error from several different sources... :roll:
function MatrixLookAt(const v1: TVector3f; const v2: TVector3f; const Up: TVector3f): TGLMatrix4f;
var
xAxis, yAxis, zAxis: TVector3f;
begin
zAxis := VectorSubtract(v1, v2);
VectorNormalize(zAxis);
xAxis := VectorCross(Up, zAxis);
VectorNormalize(xAxis);
yAxis := VectorCross(zAxis, xAxis);
VectorNormalize(yAxis);
VectorNormalize(xAxis);
Result := _GLMatrix4f;
Result[0][0] := xAxis.x;
Result[1][0] := xAxis.y;
Result[2][0] := xAxis.z;
Result[3][0] := -VectorDot(xAxis, v1);
Result[0][1] := yAxis.x;
Result[1][1] := yAxis.y;
Result[2][1] := yAxis.z;
Result[3][1] := -VectorDot(yAxis, v1);
Result[0][2] := zAxis.x;
Result[1][2] := zAxis.y;
Result[2][2] := zAxis.z;
Result[3][2] := -VectorDot(zAxis, v1);
end;
So, I put the location of the turret to the first parameter and the location of the player's ship to the second parameter, but what should I put on the third parameter? I've tried 0,1,0 and it seem to work on some cases (sort of), but usually I just get -NAN values in my matrices...
Should I check for example if the two vectors are 0,0,0 and 0,2,0, in this case the Up vector obviously can't be 0,1,0...
Is there a way to easily calculate the correct Up vector? Or is my cut & paste LookAt routine just completely wrong? :(
Here's what I want to do: I have a space station which has several turrets and I want these turrets to rotate to face the player. How can I do this?
Here's some code that I've tried to use, but it doesn't work properly, I have no idea where this code came from, it's probably cut & paste with some trial and error from several different sources... :roll:
function MatrixLookAt(const v1: TVector3f; const v2: TVector3f; const Up: TVector3f): TGLMatrix4f;
var
xAxis, yAxis, zAxis: TVector3f;
begin
zAxis := VectorSubtract(v1, v2);
VectorNormalize(zAxis);
xAxis := VectorCross(Up, zAxis);
VectorNormalize(xAxis);
yAxis := VectorCross(zAxis, xAxis);
VectorNormalize(yAxis);
VectorNormalize(xAxis);
Result := _GLMatrix4f;
Result[0][0] := xAxis.x;
Result[1][0] := xAxis.y;
Result[2][0] := xAxis.z;
Result[3][0] := -VectorDot(xAxis, v1);
Result[0][1] := yAxis.x;
Result[1][1] := yAxis.y;
Result[2][1] := yAxis.z;
Result[3][1] := -VectorDot(yAxis, v1);
Result[0][2] := zAxis.x;
Result[1][2] := zAxis.y;
Result[2][2] := zAxis.z;
Result[3][2] := -VectorDot(zAxis, v1);
end;
So, I put the location of the turret to the first parameter and the location of the player's ship to the second parameter, but what should I put on the third parameter? I've tried 0,1,0 and it seem to work on some cases (sort of), but usually I just get -NAN values in my matrices...
Should I check for example if the two vectors are 0,0,0 and 0,2,0, in this case the Up vector obviously can't be 0,1,0...
Is there a way to easily calculate the correct Up vector? Or is my cut & paste LookAt routine just completely wrong? :(