Game Development Community

~:[SOLVED]:~ Angle to an object funny business...

by Alpha-Kand · in Torque Game Builder · 05/29/2012 (10:24 am) · 3 replies

[EDIT]
{
To anyone in the future who finds this forum here is some a function for finding the angle between to vectors or objects-
/*
	This returns the angle between two positions.
	PARAMS: %firstPos = A vector or a reference to an object to take a position from, %secondPos = A vector or a reference to an object to take a position from
*/
function angleFromPositions(%firstPos, %secondPos)
{
	%x1 = getWord(%firstPos,0);
	%y1 = getWord(%firstPos,1);
	
	//If we were given reference to an object it wouldn't have two numbers.
	if(%y1 $= "")
	{
		%x1 = %firstPos.getPositionX();
		%y1 = %firstPos.getPositionY();
	}
	
	%x2 = getWord(%secondPos,0);
	%y2 = getWord(%secondPos,1);
	
	//If we were given reference to an object it wouldn't have two numbers.
	if(%y2 $= "")
	{
		%x2 = %secondPos.getPositionX();
		%y2 = %secondPos.getPositionY();
	}
	
	%xLen = %x1 - %x2;
	%yLen = %y1 - %y2;
	
	%angle = mRadToDeg(mAtan(%yLen,%xLen));

	//This fixes the angle.
	%angle += 180;		
			
	return %angle;
}
}

All I need is to find out what the angle is between two objects or the angle between two vectors(in math speech).

Yes. I have looked at google and yes I have looked through the forums looking for an answer and I found it. But something else keeps messing up.

I looked at this thread http://www.garagegames.com/community/forums/viewthread/12632. And copied the code but the problem is if two objects have the EXACT y factor and one object moves towards the other on the x axis the 'angle' changes. Turns out by looking at math learning sites I figured it out before looking at this thread.*Feels good about accomplishment*


blah blah::onUpdate(%this)
{
%this.moveTo(MainCat.getPosition(),%this.cruiseSpeed);
//Or
%this.setLinearVelocityX(-%this.cruiseSpeed);//Which currently is 1

%this.pos = get2DVectorAngle(MainCat.getPosition(), %this.getPosition());
//Or
%this.pos = getVectorAngle(MainCat.getPosition(), %this.getPosition());

echo(%this.pos);//I hate what this sucker spits out, so fustrating...
}

//Code I copied from the other thread

function getVectorAngle(%vec1, %vec2)  
{  
  %vec1n = VectorNormalize(%vec1);  
  %vec2n = VectorNormalize(%vec2);  
  
  %vdot = VectorDot(mAbs(%vec1n),mAbs(%vec2n));  
  %angle = mACos(%vdot);  
  
  // convert to degrees and return  
  %degangle = mRadToDeg(%angle);  
  return %degangle;  
} 

function get2DVectorAngle(%vec1, %vec2)  
{  
  %vec1n     = VectorNormalize(%vec1);  
  %vec2n     = VectorNormalize(%vec2);  
  %vec3Perp  = getWord(%vec2, 1) SPC (-1 * getWord(%vec2, 0));     // x gets y, y gets -x.  
  
  %vdot      = VectorDot(%vec1n, %vec2n);  
  %angle     = mACos(%vdot);  
    
  %pdot      = VectorDot(%vec1n, %vec2Perp);  
  if (%pdot < 0)  
  {  
     %angle *= -1;  
  }  
  
  // convert to degrees and return  
  %degangle = mRadToDeg(%angle);  
  return %degangle;  
}

That's pretty much it. See anything obvious that I missed?

About the author

I have always loved video games and computer games so decided to try making my own. Spent a little more than a year on a program simply named Game Maker which really got me interested. Finally decided to get "professional" by getting Torque 2D.


#1
05/29/2012 (11:01 am)
Quote:but the problem is if two objects have the EXACT y factor and one object moves towards the other on the x axis the 'angle' changes.
To anyone interested, the problem mentioned here is called the Gimbal Lock. You can read about it here:

Gimbal lock
#2
05/29/2012 (11:49 am)
Oh boy. I guess that makes sense... I'll look around and see if I can find a solution but any help is appreciated.

Hopefully figuring it out in 2d should help when I eventually move to 3d game making.
#3
05/29/2012 (8:55 pm)
Nailed it! I figured it out (mostly). In stead of using yucky gimbal logics and vector math I went and found out the formula for finding the angle of the hypotenuse of a triangle made from the two objects x offset and y offset (or the difference between the x's and the y's). Can't show you any code because it isn't working perfectly yet but I will solve it yet!