Game Development Community

Getting player direction (in degrees)

by Simon Goodchild · in Torque Developer Network · 09/13/2007 (11:10 am) · 5 replies

I've hunted around, looking at code snippets, but I'm a self confessed noob at TGE (v1.5) trying out things, so I admit defeat and am asking for help!! (My last post was brilliantly responded to - great forum!)

I want to be able to put on the screen something like "Your direction is 180 degrees" or "Your direction is 210 degrees". I'm assuming for the sake of argument, straight up the map is north, and North is zero degrees.

I'm not looking for a compass at the moment, as the degrees/direction is more important than a rotating compass control. (Eventually I want both, but one thing at a time!)

Having done the intro logo collection game, putting text on the GUI is okay, but how would I get the current angle the player is facing in degrees? You can forget about up and down, just horizontally will do!

Thank you!!!

Simon

#1
09/13/2007 (11:32 am)
Try this:

function getAngleofVector(%vec)
{
	%vector = VectorNormalize(%vec);
	%vecx = getWord(%vector,0);
	%vecy = getWord(%vector,1);
	if(%vecx >= 0 && %vecy >= 0)
		%quad = 1;
	else
		if(%vecx >= 0 && %vecy < 0)
	  	%quad = 2;
	  else
	  	if(%vecx < 0 && %vecy < 0)
	    	%quad = 3;
	    else 
	    	%quad = 4;
	%angle = mATan(%vecy/%vecx, -1);
	%degangle = mRadToDeg(%angle);
	switch(%quad)
	{
		case 1:
	  	%angle = %degangle-90;
	  case 2:
	  	%angle = %degangle+270;
	  case 3:
	  	%angle = %degangle+90;
	  case 4:
	  	%angle = %degangle+450;
	}
  if (%angle < 0)  %angle = %angle + 360;
  return %angle;
}

echo(getAngleofVector(%player.getForwardVector());
#2
09/13/2007 (1:11 pm)
Thanks Peter!!!

To save me getting in wrong several times, and so I can test it out (and then work on it!), where would you recommend putting this for starters?
#3
09/13/2007 (1:22 pm)
There are several ways you could go about it. The most simple answer would be to create a looping schedule to update a gui like so:

function Player::onAdd(%this)
{
     %this.schedule(100, "updateHeading");
}

function Player::updateHeading(%this)
{
     %heading = getAngleofVector(%this.getForwardVector());
     myGui.setText("Facing angle:" @ %heading);
     %this.schedule(100, "updateHeading");
}

This code may require some modifications though, as I don't believe stock torque calls onAdd() for ghosted players... but it should work fine single player.
#4
09/13/2007 (2:25 pm)
No, that's really helpful - I'm still learning and this is the first time trying to do something "real" !

I can understand how the function calculates the heading, and also how the schedule updates regularly calling the updateHeading fucntion.

My last problem (and I really have tried rather than just ask every time!!!) is how to call such functions.

I tried putting

heading.setText("TESTING"); <<< My GUI text control is called "heading"

inside

function PlayerBody::onAdd(%this,%obj) -- in server/player.cs --

just to see if I could change the text on the GUI! And thought if that worked, I could put in the schedule command instead. (presumably the other functions would also be put in here, or would I put them in a separate .cs file and include them with exec)?

Sorry to be so basic - hopefully this will be the only time of asking such newbie questions!!!

Appreciate your help alot, thank you.
#5
09/14/2007 (8:52 am)
I felt this was is a bit of a "Getting Started" issue now, so carried it on here (in Getting Started!)