Game Development Community

Facing angle

by Eyaly · in Torque Game Builder · 07/20/2008 (2:55 pm) · 15 replies

Hi

i wonder if there is a function which will give me the angle between Player and Target.


let's say Player is facing to 45 deg ( %Player.getrotation() will result 45 )

now Target located at -5,10

the question is if there's a function that will return the angle
that the player should turn so he can "Face" the Target?
please note i dont need the rotateTo function
i really need the facing angle.
also the following function wont work, it give me the angle between Player and Target but it doesn't
take care of Facing Angle.
%angBetween = t2dAngleToPoint( %player.position , %target.position);

thanks

#1
07/20/2008 (3:00 pm)
EDIT: Oops, I didn't realize this was TGB...
#2
07/20/2008 (3:46 pm)
To get the angle between two points you can use:

function getAngle(%pointA, %pointB)
{
    %normal = t2dVectorNormalise(t2dVectorSub(%pointB, %pointA));
    
    return mRadToDeg(mAtan(%normal.Y, %normal.X));
}

You may need to add 90 degrees to it, but I think that the above works okay. I haven't tested it, however.
#3
07/20/2008 (4:08 pm)
Hi

this is not what im looking for since your function(getAngle) and t2dAngleToPoint function is equivalent.

ill try to describe the problem again:
lets assume that i have Player and this Player is "Looking/Facing" at some direction.
this direction is the looking/facing Angle.lets say 60 degree
i can know it by invoke the getRotation function.
for example : %Player.getRotation() will give me 60 degree ( in our example ofc )
now: we have Target located at some position lets say target located at -10,20.
so we have:
1. Player looking at 60 degree ( player can be located at any x,y point )
2.Target located at -10,20
now i need to know how much degree the Player should turn to the Target

thanks
#4
07/20/2008 (4:44 pm)
I don't understand why you cannot just set the rotation of the player to the value that you get from t2dAngleToPoint.
#5
07/20/2008 (8:21 pm)
I can probably write out how you can calculate it, it involves some vector dot products. But Why do you need to know that angle? What will that be used for?

Also, are you aware of t2dSceneObject::RotateTo( %angle, %speed ).
#6
07/20/2008 (11:42 pm)
Hi
what im trying to do is to decide if to take rotation action or not according
to the facing angle relative to the target.
lets assume our player can "see" maximum at +-60 degree ( total degree 120 )
in our example if the player look at 45 degree and the angle between player and target is -90
the total angle is 135.
now we know that the player cant look behing 120 degree so he wont Rotate to the target.

thanks
#7
07/21/2008 (6:39 am)
Hi
in addition to the former post:
is there a function which will give me this angle?

thanks again
#8
07/21/2008 (9:56 am)
// origin@ The position of the agent (t2dVector)
// fvec@ The forward vector of the agent (t2dVector)
// point@ The point to test (t2dVector)
// maxAngle@ The agent's max view angle in degrees (F32)

function pointInSight( %origin, %fvec, %point, %maxAngle )
{
   %toPoint = t2dVectorNormalise( t2dVectorSub( %point, %origin ) );
   %dot = t2dVectorDot( %toPoint, %fvec );
   %angle = mRadToDeg( mAcos( %dot ) );

   return %angle < %maxAngle;  
}

To use this function you need to convert your Agent's rotation into a forward vector...

function rotationToVector( %rotation )
{
   // "0 -1" is the vector straight up in TGB eg. it corresponds to a rotation of zero, 
   // so rotate it by your objects rotation and you get your objects forward vector.

   %fvec = rotateVector( "0 -1", %rotation );
   return %fvec;
}

function rotateVector( %vector, %angle )
{
   %angle = mDegToRad(%angle);

   %x = getWord(%vector,0);
   %y = getWord(%vector,1);

   %nx = mCos(%angle) * %x - mSin(%angle) * %y;
   %ny = mSin(%angle) * %x + mCos(%angle) * %y;

   return %nx SPC %ny;
}

Example usage...

%origin = Player.getPosition();
%fvec = rotationToVector( Player.getRotation() );
%point = Enemy.getPosition();
%maxAngle = 45;

%canSee = pointInSight( %origin, %fvec, %point, %maxAngle );
#9
07/21/2008 (10:46 am)
Am I being blonde or do you really want to be returning %nx and %ny in the rotateVector function instead?
#10
07/21/2008 (10:53 am)
Good call, edited.
#11
07/21/2008 (11:03 am)
This is exactly what i needed, and it work fine !!!
Thanks James.
#12
07/21/2008 (12:30 pm)
Mmm there is a little bug in the code
the following parameter will generate this bug:

my player is at 0,0 axis and hes looking ahed at angle 0.
my target location is at 0,15 ( right behind the target )
after run this code the result of the angle inside the pointInSight function is 0.
when it suppose to be 180.

any suggestion?

thanks
#13
07/21/2008 (12:58 pm)
Ok I had a couple typos, updated my code in my post above.


1. Calculation of %ny needs the sign flipped, should look like..

%ny = mSin(%angle) * %x + mCos(%angle) * %y;

2. t2dVectorNormalize is not a function, its t2dVectorNormalise.
#14
07/21/2008 (1:04 pm)
Work fine.
thanks again James !!!
#15
01/23/2009 (11:49 pm)
James, your code saved me some time :) Thanks!