Game Development Community

Matrix Problem

by J Brown · in Torque Game Engine · 10/24/2001 (8:16 pm) · 11 replies

I'm hoping someone from the GG staff will reply to this, or someone who is well-qualified to know for sure:

How can I create, from script, a new matrix for an object, from 3 axes and a position?

I have my X,Y,Z axis vectors and my position vec... so I just need to stuff this into a matrix. Question is, HOW!?

#1
10/25/2001 (9:56 am)
Matrices in the scripting language are strings comprised of a position and an axis-angle:

"posX posY posZ axisX axisY axisZ angle"

So to have a rotation around the z axis:

"0 0 0 0 0 1 90" = 90 degree rotation around z axis

"0 10 0 0 1 0 45" = Translation along y axis + 45 degree rotation around Y axis.

(I think the rotation of the axis-angle is in degrees' but it may be radians, I always forget)
#2
10/26/2001 (12:39 am)
Right, ok..

Actually I was trying to create a 4x4 matrix, out of three desired axes and a position. Guess I should have been more specific. Basically I just wanted to create a 4x4 matrix and feed it directly into the object's matrix. Maybe I'm going about this the wrong way?

I guess maybe I can make it work using pos and axis-angle. That might take some more work though.

Oh, that reminds me:

when I do a getTransform, is the rotation part an axis-angle, or a quat? I thought perhaps it was quat, but from what you're saying its axis-angle?
#3
10/26/2001 (7:44 am)
Tim,

After looking at your example above, I think I can use that instead. I 'borrowed' some code from T2 that finds which angle to use, and points the player to the center of the map. But this only takes care of rotating around 1 axis.

What if I need to rotate around 2? Would it be much more difficult?

Here is the code I 'borrowed':

if(VectorDist(%target, %src) == 0)
return " 0 0 1 0 ";
%vec = VectorNormalize(VectorSub(%target, %src));
%angle = mAcos(getWord(%vec, 1));

if(%src < %target)
return(" 0 0 1 " @ %angle);
else
return(" 0 0 1 " @ -%angle);

This would need to be modified to adjust the pitch as well...
#4
10/26/2001 (9:13 am)
At the moment you can't control the player's view directly from the scriting language. View/head control runs strictly off input moves. Setting a transform rotation just tries to rotate the whole boldy, which is not what you want (won't work anyway). If this is for an AI object, you could add a script interface to set it, or generate moves using an AIConnection.
#5
10/27/2001 (5:33 am)
Tim,

Hate to bug you again, but...

I took your advice and modified the way I'm doing this. I borrowed the getMoveList function from AIPlayer, and the setAimLocation function. Put those in AIConnection and did some dancing to get it working.

Again, same thing. I call setAimLocation, and it works for yaw, but not pitch. Any clue? I'm not sure if the math in getMoveList is the problem, or something else. It definitely works for yaw, though.

Thanks in advance. I plan to use this to do a tutorial or two for the site here, bots and turrets, once its working.
#6
10/27/2001 (9:27 am)
So you are generating pitch moves right? not setting the transform?
#7
10/27/2001 (11:55 am)
Correct, using the getMoveList function that was located in AIPlayer. I moved it to AIConnection, commented out the stuff that didn't apply, and changed a couple things to make it work... for yaw, at least, like I said.

I can send you the code if you want, but like I said its basically the code that was in AIPlayer for 1.1.0.

I'm sure this has to be something simple that I'm just missing. At least, I hope so :)

Thanks for your help. I know I'm a pain.
#8
10/27/2001 (5:20 pm)
Well... I wrote AIConnect, but not AIPlayer. Pat wrote that and as far as I know, he's never had it all working. So that may not be a good model to go off of :)

You might try just forcing the pitch component of the move to some value and make sure that he does look up or down, sort of a sanity check :) If you can get him to tilt his head, it shouldn't be hard to debug it from there.
#9
10/28/2001 (1:41 am)
Thanks a lot, man.

I performed the sanity check, he does move his head and weapon to a hardcoded pitch value. So, it's off to the resources section for me, looking for some formulas to use :)

If I find one that works I'll post it here for others.

EDIT:

I finally got it ;-} mostly, anyway.

I ended up subtracting the bot's location from the desired aim location, and calling MathUtils::getAnglesFromVector to produce a pitch and yaw. It works well, except for a minor bug where the bot aims over my shoulder (only sometimes). I'm no mathemetician, but I think maybe PI needs to be factored into this somehow (?)
#10
10/28/2001 (9:29 am)
Ok, nevermind.

Got this figured out. I'll post it here later.
#11
11/18/2001 (1:54 am)
Yeah, here it is... looks like the formula was wrong, or just unfinished...

Here's the formula I ended up using to calculate pitch:

asin((elev2 - elev1) / dist)

where elev2 is the elevation of the target object (z)
and elev1 is the elevation of the bot
and dist is the distance between their positions.

Works like a champ.

Hope this helps someone.