Results 1 to 6 of 6

Thread: How can I take reverse of tan() ? (Extracting angles from velocity)...

  1. #1
    Senior Member Regular Contributor
    Join Date
    Jul 2003
    Location
    London
    Posts
    163

    How can I take reverse of tan() ? (Extracting angles from velocity)...

    Hi, I ahve run into a minor problem programming a simple asteroid game in open gl.
    I have an Object class (in C++) that holds vectors for acceleration, velocity, position and a double for rotation.
    What I would like to do is have the object rotate to its velocity.
    For instance, if velocity is v(1,1) the ship would be rotated 45 degrees (the game is 2d so rotation happens on the z axis).
    The rotation and translation is defined as follows:
    glTranslated(position.x, position.y, 0.0);
    glRotated(rotation, 0.0, 0.0, 1.0);

    There's also a limit on the velocity and when player presses a button, the acceleration increases, and then is decreased a bit each frame (causing acceleration and drag to the ship movement).

    The problem I ran into is extracting the rotation value...

    If I set theta = the angle between x axis and velocity vector, I could extract it as follows (correct me if I'm wrong):

    tan theta = opposite/adjacent // these are lenths of the x, y components of the vector.

    The problem is that I can't find a cotangent function is c or c++ that would take the invesre of tangent to get me the theta...

    I guess I could also get the angles by using angle cosines, but if I have cos theta = x/ |velocity| but then I have to have a cosecant (spelling?) csc? function... which I also can't find it.... reverses for sin,cos,tan are in my calculator... aren't there any in C++ ?

    Thank you for the help....

  2. #2
    Member Contributor
    Join Date
    Feb 2005
    Posts
    83

    Re: How can I take reverse of tan() ? (Extracting angles from velocity)...

    Look up atan(), acos, and asin. For c++, they come from the math.h standard library. All functions return angles in radians I think.

  3. #3
    Guest

    Re: How can I take reverse of tan() ? (Extracting angles from velocity)...

    Another good one is

    radians = atan2(y, x);

    This works for most any combination of y and x, including the cases when one or both are zero and one or more is infinity. (But not in the case where one is not-a-number.)

    atan2(y,x) is therefore better than atan(y/x) because
    * atan2 knows what quadrant you are in, while atan assumes that you are always in the right side of the origin.
    * atan2 doesn't have divide-by-zero problems when y != 0 and x == 0
    * atan2 doens't have Not-a-Number problems when both x and y == 0, or both x and y == infinity.

    0 radians is pointing along +x axis. +radians is a movement in the CounterClockwise(CCW) direction. To convert this to normal compass directions (+y, CW, never negative), use the formula degrees = (int)(360.5 + 90 - 180 * radians / M_PI ) % 360;


    Special note to graphics persons:
    If you are trying to rotate a directional pointer in the direction of momement based on delta-x and delta-y, then you should handing the 0,0 case specially, because even though atan2 returns an answer, it might not be the answer you want.
    Example:
    Target is 5.37 meters at bearing 230 degrees
    Target is 2.52 meters at bearing 225 degrees
    Target is 0.03 meters at bearing 043 degrees
    Target is 0.00 meters at bearing 0 degrees.
    (The last lines makes little sense as the bearing can abruptly changes as two points get close together if they are not staying on the same mathematically perfect line. It is better in some applications to use branching logic.
    epsilon = 0.30;
    if ( distance <= epsilon ) {
    #display round dot
    printf("Target is here!\n");
    } else {
    #rotate radians
    #display arrow
    printf("Target is %lf meters at bearing %03d degrees\n", distance, degrees);
    }

    -Richard Penner

  4. #4
    Senior Member Regular Contributor
    Join Date
    Jul 2003
    Location
    London
    Posts
    163

    Re: How can I take reverse of tan() ? (Extracting angles from velocity)...

    Thank you Richard,
    Luke

  5. #5
    Guest

    Re: How can I take reverse of tan() ? (Extracting angles from velocity)...

    Originally posted by BigShooter:
    Thank you Richard,
    Luke

  6. #6
    Guest

    Re: How can I take reverse of tan() ? (Extracting angles from velocity)...

    ...

Similar Threads

  1. Velocity and rotation help
    By SacAche in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-19-2014, 07:41 AM
  2. Change velocity at a particular point
    By Abdul in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-17-2012, 08:23 PM
  3. Jittering while Interpolating ( using velocity )
    By OGL_PGR in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 08-23-2005, 04:11 AM
  4. gravity effecting y velocity
    By Josh Kasten in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-07-2003, 09:56 PM
  5. STB Velocity
    By w00t in forum OpenGL: User Software
    Replies: 0
    Last Post: 01-08-2002, 07:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean