angle function

Posted By: dicethrower

angle function - 09/23/07 09:57

ok you might think this is something covered before, but i have a problem with my angle function....here is what it is

Code:
 
function getAngle(ENTITY* pObject1, ENTITY* pObject2){
return atan((pObject1.y - pObject2.y) / (pObject1.z - pObject2.z));
}
action roll_me ()
{
while(1){
var angle = getAngle(g_cCharacters[0].eChar, g_cCharacters[1].eChar);
me.roll = angle;
wait(1);
}
}
int main(){
g_cCharacters[0].eChar = ent_create("block.mdl", vector(-144,-112,160) , cursor_me);// mouse
g_cCharacters[1].eChar = ent_create("block.mdl", vector(-144,-112,160) , roll_me);// player/gun
}



NOTE: its in 3d so don't get confused to see Y and Z instead of X and Y. I know the cursor_me action is missing it's just a complex calculation to put the object in 3d on the exact spot where my mouse is in 2d on my screen.

Ok my problem is I get a E1513 error. If I do that calculations in the roll_me action itself it works just fine only with this simple function it does not work....any thoughts anyone?
Posted By: Fenriswolf

Re: angle function - 09/23/07 13:07

Hi,

Quote:

(pObject1.y - pObject2.y) / (pObject1.z - pObject2.z)



According to your ent_create functions in your main, both entities have the same height. In this case the calculation above would lead to a division by zero.

Also make sure, that g_cCharacters[1].eChar already exists, when getAngle is executed.
Posted By: i_program_games

Re: angle function - 09/23/07 18:04

Here is some code but make sure your arguments are set properly. Here are some examples:

/*
//Example 1: Two VECTORS or two arrays of a length less than 3
vec myAngle = GetAngle(fromAngle,toAngle);

//Example 2: a VECTOR or array with a length < 3 and an entity
vec myAngle = GetAngle(fromAngle,my.x); //notice that my is entered as my.x

//Example 3: an entity and a VECTOR or array with a length < 3
vec myAngle = GetAngle(my.x,toAngle); //notice that my is entered as my.x

//Example 3: two entities
vec myAngle = GetAngle(my.x,you.x); //notice that the my and you entities are entered as my.x and you.x respectivly
*/
///////////////////////////////////
//CODE
///////////////////////////////////

//Custom struct for easier reading
typedef struct
{
var x;
var y;
}VEC_XY;

//function prototypes
function atan2(var nY,var nX);
function GetAngle(var* vecFrom, var* vecTo);

//function definations
function GetAngle(var* vecFrom, var* vecTo)
{
VEC_XY vecTranslatedVector;
vecTranslatedVector.x = vecTo[0]-vecFrom[0];
vecTranslatedVector.y = vecTo[1]-vecFrom[1];
return(atan2(vecTranslatedVector.y,vecTranslatedVector.x));
}


function atan2(var nY,var nX)
{
var nPI = 180;

if(nX > 0)//first or forth quadrent
{
return(atan(nY/nX));
}

if(nX<0)//second or third quadrent
{
if(nY>=0)//second quadrent
{
return(atan(nY/nX)+nPI);
}
else //y<0 //third quadrant
{
return(abs(atan(nY/nX))+nPI);
}

}
else //x=0;
{
if(nY>0)
{
return(nPI/2);
}

if(nY<0)
{
return(-(nPI/2));
}
else //y=0
{
//undefined
}
}

}
© 2023 lite-C Forums