Posted By: MrCode
Need some math help - 09/18/08 04:49
Ok, I've been working on a completely custom utility function for facing one entity to another (for auto-targeting, facing the camera to an object, etc.). This function uses none of the pre-defined 3DGS functions (maybe save for atan()), and I've pretty much got the math figured out, but with one snag. If the pan angle hits 0, the angles reverse themselves. Here's the function's code:
I have another one of these of the same name (overloaded) that directly faces an entity to another by manipulating it's pan and tilt members, but I'm using this one because it's for a bone angle (using it for turning a gun turret towards an aiming reticule).
I've tried many ideas, like using an if condition to switch the angle to it's proper value when it hits 0 (or in this case, 270, since I've subtracted 90 from the initial calculated angle), and using abs() and using an if to reverse, or make negative, the value it returns when it hits the proverbial 0. All of these ideas have done me no good. Maybe it has something to do with converting from radians? Maybe because of the nature of the atan() function? I don't know. All I know right now is that I'm stumped.
Code:
void FaceObject(ENTITY* ent1,ENTITY* ent2,ANGLE* angle) { float distx,disty,distz,distxy; //local variables. distz= ent2.z - ent1.z; //these are the distance calculations. disty= ent2.y - ent1.y; //as you can see,these find distx= ent2.x - ent1.x; //the distance between each of the x,y,and z components. distxy= sqrt(pow(distx,2) + pow(disty,2)); //finding general distance to object /*I prefer not to use vec_dist() here because *I have already found the z distance above, *and I need to use the components separately *(see below).*/ angle.pan= (atan(disty/distx) * (180 / pi)) - 90; //finding pan angle (and subtracting 90 degs) angle.tilt= (atan(distz/distxy) * (180 / pi)); //and tilt angle }
I have another one of these of the same name (overloaded) that directly faces an entity to another by manipulating it's pan and tilt members, but I'm using this one because it's for a bone angle (using it for turning a gun turret towards an aiming reticule).
I've tried many ideas, like using an if condition to switch the angle to it's proper value when it hits 0 (or in this case, 270, since I've subtracted 90 from the initial calculated angle), and using abs() and using an if to reverse, or make negative, the value it returns when it hits the proverbial 0. All of these ideas have done me no good. Maybe it has something to do with converting from radians? Maybe because of the nature of the atan() function? I don't know. All I know right now is that I'm stumped.