Game Development Community

dev|Pro Game Development Curriculum

Converting Vectors to 2D Angles

by Paul Ash · 11/12/2007 (2:01 pm) · 5 comments

Only a small addition to the mPoint.h file is needed to gain this method, this should not damage any pre-made code in any way as it's a fully self contained change but its always safe to back up the files you plan to edit!
F
mPoint.H

Line 300 Add
F32 Angle2d() const;


Line 1239 Add
inline32 Point3F::Angle2d() const
{
	//yay AdmiralCompholio!
	F32 Theta = mAtan(y/x, -1);
	if(x < 0)
		Theta = Theta - M_PI;

	return((Theta * 180.0f) / M_PI_F);
}

#1
11/12/2007 (3:57 pm)
Shouldn't you be using the Torque Math function mAtan(y, x) ?
#2
11/13/2007 (7:59 am)
Yeah you are right, i forgot to change it back when i posted this, thanks(tho either would work, one is platform dependent)
#3
11/13/2007 (11:35 am)
oh cool!
you might also enjoy something like

mMathFn.h
inline F32 mAngleBetween(const VectorF& v1, const VectorF& v2)
{
   F32 lenv1sq = v1.lenSquared();
   F32 lenv2sq = v2.lenSquared();
   
   // check for degeneracy
   if (lenv1sq * lenv2sq <= 0.0000001f)   // ~0.0003^2
      return 0.0f;
   
   F32 dot  = mDot(v1, v2);
   dot     *= (1.0f / (mSqrt(lenv1sq * lenv2sq)));
   
   F32 rad  = mAcos(dot);
   
   return rad;
}

mathTypes.cc
ConsoleFunction(VectorAngleBetween, F32, 3, 3, "(Vector3F a, Vector3F b) Calculate the angle between a and b.")
{
   VectorF v1(0,0,0),v2(0,0,0);
   dSscanf(argv[1],"%f %f %f",&v1.x,&v1.y,&v1.z);
   dSscanf(argv[2],"%f %f %f",&v2.x,&v2.y,&v2.z);
   return mAngleBetween(v1, v2);
}
#4
11/13/2007 (11:41 am)
ah, thanks for the math code! i actually programmed something like that the other day to tell what objects are infront of my player in a view cone, this is alittle cleaner tho
#5
12/16/2007 (10:53 pm)
If I were to convert a Point3F coordinate created by the point of a projectile's impact, how would these coordinates relate to the actually object? Would it be a simple 2D XY coordinate in respect to the object's height/Width? Or would it refer more to the texture on the object's 2D coordinates?

I case you are wondering, I am trying to see if this would be useful in detecting the coordinates on an object's texture from a projectile collision.