Author Topic: Get the exact (x/y/z) location/rotation of a player?  (Read 2829 times)

Maybe I'm just a moron who doesn't know how to script, (like there are any good (*free*) torquescript tutorials out there for people who AREN'T starting from scratch...) but I have no idea how to get the position of a player object.
I want to make a slash command (to preferably be converted for client-side-ed-ness) that gives the x y z location of a player.

In other words, I need someone to fill in the blank provided by the following code's comment:
Code: [Select]
function serverCmdGetMyLocation(%client)
{
//this part causes the code to get the x y z location of the player, and sets it to a varaible, %location
messageClient(%client, '', "\c2You are located at:\c1" SPC %location);
}
That, and could I also get a quick little function that returns the rotation of a player using only a %client argument?

Thanks to whoever helps.

Position:
Code: [Select]
%location = %client.player.getPosition();
Rotation:
Code: [Select]
%rotation = getWords(%client.player.getTransform(), 3, 6);(The rotation is not just x y z, it's a matrix I believe)

Thanks a bundle. :cookie:

Now I just need to figure out how to seperate out the numbers in those things...

You could use getWord();

Code: [Select]
//Example
%var = getWord("1 2 3 4 5", 0);
// %var = 1

getWord( string , word number (starting from 0) );

Actually I figured that out for myself, didn't bother to post, lol.
Thanks for the help anyway.

Transform includes both position and rotation.

function location(%type)
{
 if(%type = 1){%location = ServerConnection.getControlOb ject().position;}else
 {%location = ServerConnection.getControlOb ject().getTransform();}
 commandtoserver('messagesent',"I'm located at: " @ %location);
}

Clientside, put in any file in the Add-Ons/Client folder, then in a server open the console and type location(1); for your position to be sent or location(); for your whole location and rotation to be sent.

Since it's clientside it has to be a message. This will cause you to "Say" (as if you'd typed it) "I'm located at: 0.1 10.23 0 1 0 0 45.67" (or whatever your position is, last four digits won't be there if you type location(1);.

Hmm, although I probably won't use that exact piece of code in what I'm working with, I do thank you for telling me how to pull a "getlocation" that works client-side.

Now it's time to get working on a super-huge collection of if statements to determine the cardinal direction that a player is facing in...

How the heck does rotation work in torque anyway?

How the heck does rotation work in torque anyway?
Well, first off, 1 0 0 0 is perfect north with no vertical rotation...

I don't know what the first 2 numbers in rotation do. Probably vertical rotation, but that doesn't affect player objects.

Number 3 is the multiplier for Number 4.
Number 4 is the degrees that a player is turned, depending on number 3.
if number 3 is positive, the player is rotated #4 degrees to the right, if #3 is negative then it's rotated to the left.

keep in mind that if you want to use REAL degree numbers IN CODE, you have to multiply the value you have by 57.07854238075 or something. It's a REALLY LONG decimal, and you have to be careful. The mission editor multiplies it for you, sadly.

The rotations are in a "matrix" (x y z vectors + angle) format rather than "Euler" rotations (x y z rotations).

The function exists, already defined in the main game scripts but also in constants.cs of v0002:
Code: [Select]
function eulerToMatrix( %euler )
{
%euler = VectorScale(%euler, $pi / 180); //convert euler rotations to radians

%matrix = MatrixCreateFromEuler(%euler); //make a rotation matrix
%xvec = getWord(%matrix, 3); //get the parts of the matrix you need
%yvec = getWord(%matrix, 4);
%zvec = getWord(%matrix, 5);
%ang  = getWord(%matrix, 6); //this is in radians
%ang = %ang * 180 / $pi; //convert back to degrees

%rotationMatrix = %xvec @ " " @ %yvec @ " " @ %zvec @ " " @ %ang; //put it all together

return %rotationMatrix; //send it back
}
The $pi and 180 parts are to do with conversion of radians to degrees, your "really long decimal".

So, to put rotations in as 10 degrees clockwise z axis -
Code: [Select]
%rot = eulertoMaxtrix("0 0 10");
and you get your set of four digits for Torque to use.

mDegToRad works too
like...
Code: [Select]
$mvYaw += mDegToRad( 180 );turns you around

But what does it all mean?

How do the four numbers construct the rotation? Is it like, "OK face this XYZ coordinate assuming you're the origin and then roll #4 degrees" or what?

I just combed the whole internet and it was fail.  :panda:
« Last Edit: August 19, 2007, 04:42:44 AM by JJ10DMAN »

thats the way it was made
the engine knows how to read it that way

If you want more information on this, Torque actually uses quaternion rotation.  Check out the article on Wikipedia.

http://en.wikipedia.org/wiki/Quaternion#Rotation_group