Hey there. I'm trying to calculate two angles and some velocity components but I'm ending up with some NaN's. I'll post the code then elaborate.

Code:

//this gets the velocity components from the reflection formula newV=V-2N(V.N)
_xVelocity = d[0] / t;
_yVelocity = d[1] / t;
_zVelocity = d[2] / t;
float vel = sqrt(pow(_xVelocity,2)+pow(_yVelocity,2)+pow(_zVelocity,2));
if(vel==0){cout<<"Warning, vel div by 0\n";vel=0.01;}
_azimuthalAngle = acos(_zVelocity / vel) - 90.0;
if((sin(90.0 + _azimuthalAngle))==0){cout<<"Warning, azim 1 div by 0\n";_azimuthalAngle+=0.01;}
_orbitalAngle = asin(((_yVelocity / (sin(90.0 + _azimuthalAngle))) / vel));
_xVelocity = sin(90.0 + _azimuthalAngle) * cos(_orbitalAngle) * _velocity;
_yVelocity = sin(90.0 + _azimuthalAngle) * sin(_orbitalAngle) * _velocity;
_zVelocity = cos(90.0 + _azimuthalAngle) * _velocity;

so, whats happening here is I'm getting some of those initial velocities showing up as zero or the sin of 90 + azimuthal is coming up zero. I've tried tweaking the numbers here, but that doesn't seem to be doing the trick.

So, I'm getting a vector that points to a new location, but it has a magnitude different from my velocity. I'm using this vector as a velocity vector, calculating a fake magnitude for it, so I can get the orbital and azimuthal angles, and then using my real velocity's magnitude, calculate the actual velocity.

So, is there a more elegant way to do this? I've spent a good bit of time googling and wracking my brain, but I missing something.

Thanks!