Game Development Community

Rotating the player problem

by Playware · in Torque Game Engine Advanced · 02/01/2009 (5:49 pm) · 3 replies

Hi,

I calculated the angle of player and the target's transform and rotate the player to face to the target using the following piece of code.

//player x,y,z
%px = firstWord($player.getTransform());
%py = getWord($player.getTransform(), 1);
%pz = getWord($player.getTransform(), 2);

//target x,y,z
%tx = firstWord(%targetPos);
%ty - getWord(%targetPos, 1);
%tz = getWord(%targetPos, 2);

%eyeVec = VectorNormalize(%px SPC %py SPC %pz);
%targetVec = VectorSub(%tx SPC %ty SPC %tz, %px SPC %py SPC %pz);
%dot = VectorDot(VectorNormalize(%targetVec), %eyeVec);
%rad = mAcos(%dot);
%deg = mRadToDeg(%rad);

//rotate the player to face to the target
$player.setTransform(%px SPC %py SPC %pz SPC "0 0 1" SPC %deg);

But it rotates the player to some false direction.
I don't know is it because of the way I calculated the angle is incorrect or is it the fault of rotating the player.

Your help will be really appreciated.
Thanks in advance.

#1
02/02/2009 (2:24 am)
You do not need the whole transform to rotate the player.
Thus your transform should have a little lag on rotate,you are out of sync in Torque Script transform.
You can use setAimObject() function.

Or better find setForwardvector resource.
#2
02/02/2009 (10:37 pm)
Thanks for your reply.
I tried setForwardVector resource.
But it also doesn't make my player to face exactly to the target object.
I'm not sure how can I use setAimObject on player object as I found it's usage only in AIPlayer.

Is there any other better way to rotate and face the player exactly to the target object?
Thanks in advance.
#3
02/03/2009 (1:51 am)
This thread solved my problem. Thank you anyone.
http://www.garagegames.com/community/forums/viewthread/28227