Thread: 3D velocity issues

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    18

    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. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    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. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    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.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    First, correct your code to use radians instead of degrees. If it still doesn't work, post the revised code.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    18
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ DirectX 9 3D z-buffer issues?
    By jrbarr in forum Game Programming
    Replies: 3
    Last Post: 02-14-2008, 01:23 AM
  2. Velocity vector troubles
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 01-18-2005, 11:40 AM
  3. 3D starfield
    By VirtualAce in forum Game Programming
    Replies: 6
    Last Post: 06-26-2003, 12:40 PM
  4. 3D SDK for C++ programmers
    By chand in forum Game Programming
    Replies: 2
    Last Post: 05-20-2003, 07:38 AM
  5. 3d engines
    By Unregistered in forum Game Programming
    Replies: 7
    Last Post: 12-17-2001, 11:19 AM