inline short int GetHeadingFromVector(float dx,float dz) { float h; //printf("(%.2f,%.2f)\n",dx,dz); if(dz!=0){ float d=dx/dz; if(d > 1){ h=(PI/2) - d/(d*d + 0.28f); } else if (d < -1){ h=-(PI/2) - d/(d*d + 0.28f); }else{ h=d/(1 + 0.28f * d*d); } if (dz<0) { if (dx>0) h+=PI; else h-=PI; } } else { if(dx>0) h=PI/2; else h=-PI/2; } h*=SHORTINT_MAXVALUE/PI; // Prevents h from going beyond SHORTINT_MAXVALUE. // If h goes beyond SHORTINT_MAXVALUE, the following // conversion to a short int crashes. // if (h > SHORTINT_MAXVALUE) h=SHORTINT_MAXVALUE; // return (short int) h; int ih = (int) h; ih %= SHORTINT_MAXVALUE; return (short int)ih; /* float wantedHeading; if(dz!=0) wantedHeading=atanf(dx/dz); else wantedHeading=PI; if(dz<0) wantedHeading+=PI; wantedHeading*=32768/PI; return wantedHeading;*/ } struct shortint2{ short int x,y; }; //vec should be normalized inline shortint2 GetHAndPFromVector(const float3& vec) { shortint2 ret; // Prevents ret.y from going beyond SHORTINT_MAXVALUE. // If h goes beyond SHORTINT_MAXVALUE, the following // conversion to a short int crashes. //this change destroys the whole meaning with using short ints.... int iy = (int) (asin(vec.y)*(SHORTINT_MAXVALUE/PI)); iy %= SHORTINT_MAXVALUE; ret.y=(short int) iy; ret.x=GetHeadingFromVector(vec.x, vec.z); return ret; }