Game Development Community

Error in Point3D normalize function

by Scott Richards · in Torque Game Engine · 09/04/2005 (12:02 am) · 4 replies

In math/mMath_C.cc @ line 85:

static void m_point3D_normalize_f_C(F64 *p, F64 val)
{
   F64 factor = val / mSqrtD([b]p[0]*p[0] + p[1]*p[1][/b] );
   p[0] *= factor;   
   p[1] *= factor;   
   p[2] *= factor;   
}

The Z component is missing. It should read:

static void m_point3D_normalize_f_C(F64 *p, F64 val)
{
   F64 factor = val / mSqrtD(p[0]*p[0] + p[1]*p[1] + [b]p[2]*p[2][/b]);
   p[0] *= factor;   
   p[1] *= factor;   
   p[2] *= factor;   
}

Not that anybody uses Point3D::normalize anyway, but still...

About the author

I am an artist, a programmer, and a rogue developer, subbornly chasing a dream, trying to make the game I want to play.


#1
09/04/2005 (12:37 am)
I also personally would prefer that all the normalize functions contain a divide-by-zero safety check. This isn't really a bug, but I'll post it here anyway:

static void m_point2F_normalize_C(F32 *p)
{
   F32 squared = p[0]*p[0] + p[1]*p[1];
   if (squared > 0.0f) {
      F32 factor = 1.0f / mSqrt(squared);
      p[0] *= factor;   
      p[1] *= factor;   
   } else {
      p[0] = 0;
      p[1] = 1;
   }
}   

//--------------------------------------
static void m_point2F_normalize_f_C(F32 *p, F32 val)
{
   F32 squared = p[0]*p[0] + p[1]*p[1];
   if (squared > 0.0f) {
      F32 factor = val / mSqrt(squared);
      p[0] *= factor;   
      p[1] *= factor;   
   } else {
      p[0] = 0;
      p[1] = [b]val[/b];
   }
}   

//--------------------------------------
static void m_point2D_normalize_C(F64 *p)
{
   F64 squared = p[0]*p[0] + p[1]*p[1];
   if (squared > 0.0f) {
      F64 factor = 1.0f / mSqrt(squared);
      p[0] *= factor;   
      p[1] *= factor;   
   } else {
      p[0] = 0;
      p[1] = 1;
   }
}   

//--------------------------------------
static void m_point2D_normalize_f_C(F64 *p, F64 val)
{
   F64 squared = p[0]*p[0] + p[1]*p[1];
   if (squared > 0.0f) {
      F64 factor = val / mSqrt(squared);
      p[0] *= factor;   
      p[1] *= factor;   
   } else {
      p[0] = 0;
      p[1] = [b]val[/b];
   }
}   

//--------------------------------------
static void m_point3F_normalize_C(F32 *p)
{
   F32 squared = p[0]*p[0] + p[1]*p[1] + p[2]*p[2];
   if (squared > 0.0f) {
      F32 factor = 1.0f / mSqrt(squared);
      p[0] *= factor;   
      p[1] *= factor;   
      p[2] *= factor;   
   } else {
      p[0] = 0;
      p[1] = 0;
      p[2] = 1;
   }
}   

//--------------------------------------
static void m_point3F_normalize_f_C(F32 *p, F32 val)
{
   F32 squared = p[0]*p[0] + p[1]*p[1] + p[2]*p[2];
   if (squared > 0.0f) {
      F32 factor = val / mSqrt(squared);
      p[0] *= factor;   
      p[1] *= factor;   
      p[2] *= factor;   
   } else {
      p[0] = 0;
      p[1] = 0;
      p[2] = [b]val[/b];
   }
}   

//--------------------------------------
static void m_point3D_normalize_C(F64 *p)
{
   F64 squared = p[0]*p[0] + p[1]*p[1] + p[2]*p[2];
   if (squared > 0.0f) {
      F64 factor = 1.0f / mSqrtD(squared);
      p[0] *= factor;   
      p[1] *= factor;   
      p[2] *= factor;   
   } else {
      p[0] = 0;
      p[1] = 0;
      p[2] = 1;
   }
}   

//--------------------------------------
static void m_point3D_normalize_f_C(F64 *p, F64 val)
{
   F64 squared = p[0]*p[0] + p[1]*p[1] + p[2]*p[2];
   if (squared > 0.0f) {
      F64 factor = val / mSqrt(squared);
      p[0] *= factor;   
      p[1] *= factor;   
      p[2] *= factor;   
   } else {
      p[0] = 0;
      p[1] = 0;
      p[2] = [b]val[/b];
   }
}
#2
09/04/2005 (2:24 am)
Nice, thanks for the code. Definitely going into 1.4! #335.
#3
09/10/2005 (1:53 am)
Minor improvement to the code in my previous post. See changes in bold. They were "1".
#4
09/11/2005 (6:44 pm)
Alright, thanks!