Game Development Community

Retrieving and using player's direction

by c-level · in Torque Game Engine · 09/05/2003 (7:02 pm) · 3 replies

How can I make my aiPlayers face the same direction as my player?

I wish there was something like:

%bot.orientation = %player.orientation;

I see how to get a bot to face a particular object (with 'setAimObject()'), but what I want seems almost simpler.

Using the aiPlayer resource, I've managed to get 3 bots to follow my player wherever I go, and it's nice that they face me. When I stop, they form a circle (triangle) around my player. But when they form that circle, I'd like them to face the direction my player does.
I tried using the 'player.getEyeVector()', but I don't know what I'm doing.
I undertand 'vectorAdd', but I don't understand any other vector math.

So, how can I get the player's orientation and then apply it to a bot? (I realize I'm using the words 'orientation', 'direction', and 'vector' somewhat interchangeably, and that may be a mistake).

I'm hoping this can be done in the Torque scripting language :)

Thank you

#1
09/05/2003 (8:50 pm)
Add these two functions to your scripts. These are from T2 and work well in getting position and rotation of an object, including the player.

function posFromTransform(%transform)
{
   // the first three words of an object's transform are the object's position
   %position = getWord(%transform, 0) @ " " @ getWord(%transform, 1) @ " " @ getWord(%transform, 2);
   return %position;
}

function rotFromTransform(%transform)
{
   // the last four words of an object's transform are the object's rotation
   %rotation = getWord(%transform, 3) @ " " @ getWord(%transform, 4) @ " " @ getWord(%transform, 5) @ " " @ getWord(%transform, 6);
   return %rotation;
}

Next we need to get the rotation of the player, this can be done like...

%rot = rotFromTransform(%player.getTransform());

Then we need to get the position of the bots once they have stopped.

%pos = posFromTransform(%bot.getTransform());

Now we need to set the new transform of the bot based off its position and the players rotation.

%bot.setTransform(%pos SPC %rot);

Just do this with each bot in your group and they will all look in the same direction you are.
#2
09/06/2003 (10:16 am)
Great answer. Thanks.
#3
09/07/2003 (2:16 pm)
Darn. Your functions work perfectly, but I can't implement them. I've used echo statemenst to find that not only are your functions returning the correct rotation, but when I use getTransform to echo my bots location and rotation, the changes are corroborated. But my guys don't turn!

I've tried crazy tests to see if the position part of 'setTranform' works, and it IS affecting the bots.
I've tried commenting out my use of 'setMoveDestination', and I made sure the correct part of my if statement is called at the right time.
According to my echo statements comparing the players getTranform to the bots, everything is working perfectly.

if (%cl.player.still == false) ///DETERMINED in trailPlayer, below
    {
      // GO to where player was very recently
      %pos =  %cl.player.positionit[%badGuy.ptNum];                     
      %badGuy.setMoveDestination(%pos);
      %badGuy.setAimLocation(%cl.player.getPosition());

    } else {
      // OTHERWISE, go to where he is, but incorporate a hardcoded offset
      %pos = vectorAdd(%cl.player.getPosition(), %badGuy.offVecX @ " " @ %badGuy.offVecY @ " 0");
      %rot = rotFromTransform(%cl.player.getTransform());
      %botpos = posFromTransform(%badGuy.getTransform());
      %badguy.setTransform(%botpos @ " " @ %rot);
      %badGuy.setMoveDestination(%pos);
    }

In summary, what are some reasons why the bots would ignore the rotation aspect of 'setTransform' but not the position end of it?
I've tried different character models.

Thanks