1. ## 3D velocity issues

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!

2. I'm not an expert, but I would walk through your code with breakpoints and validate that the numbers that you are doing calculations on are correct.

I can think of two cases which you would be getting NaN:
1. Performing a calculation which results in an overflow: maybe you are using huge numbers
2. Passing an improper value to a math function

I know the DirectX acos function expects a parameter with a value within -1 to 1, so I would verify what functions are expecting and what you are actually passing.

3. Well first of all, the standard math functions all use radians, so this 90 stuff is probably wrong.

Why don't you just calculate the unit vector and multiply is by your magnitude to get your velocity vector? This is basically what you're already doing, in a very roundabout fashion.

Code:
```u[0] = d[0]/sqrt(d[0]*d[0]+d[1]*d[1]+d[2]*d[2]);
u[1] = d[1]/sqrt(d[0]*d[0]+d[1]*d[1]+d[2]*d[2]);
u[2] = d[2]/sqrt(d[0]*d[0]+d[1]*d[1]+d[2]*d[2]);

_xVelocity = u[0]*_velocity;
_yVelocity = u[1]*_velocity;
_zVelocity = u[2]*_velocity;```
So now we have no need to calculate angles. You can even simplify this further is you need to.

4. And I can't quite make what you're doing match the comment. If you really are bouncing something off a wall, you really do just want to do the dot product you've got there.

5. First, correct your code to use radians instead of degrees. If it still doesn't work, post the revised code.

6. Thanks for the suggestions I'll look into that.

I do still need to get the angles out of this as turning parameters use them. This calculation is for collision which is a special case in the simulation.

Most likely I'll use the unit vector and the real magnitude to get the real velocity vector, then get the angles based off of that, after checking to make sure I need to pass radians instead of degrees. I'm using the trig functions in libm.a, not from directx.

Thanks!