Welcome to Phrogram Sign in | Join | Help
in Search


Usage of ArcCos() and ArcSin()

Last post 04-26-2008, 12:50 PM by The Dragon Rider. 3 replies.
Sort Posts: Previous Next
  •  04-24-2008, 7:43 PM 6559

    Usage of ArcCos() and ArcSin()

    I've come to a point in my game where I need to calculate an angle depending on the X,Y values of the mouse relative to a given point. The first thing I did was try to reverse engineer the CalculateVector() function found in V2SpaceShooter.kpl. Didn't work. =/ Next I tried using Math.ArcCos for the X value and Math.ArcSin for the Y value. I get a few intelligible numbers when the mouse is close to ( 1-2 pixels away from ) the screen edge. Anywhere else and I get a cryptic "NaN" value. Huh? What does "NaN" mean? And why do ArcCos and ArcSin return it as a number?

     Mostly though, how do I use ArcCos or ArcSin to calculate an angle of a line ( or Vector ) that stretches from point A to point B, assuming that I will get 0 if B.x = A.x and B.y < a.Y (In other words, a line that goes from 0,0 to 0,10 will give me an angle of 180, and a line from 0,10 to 0,0 gives the angle of 0)?
     

    PS. If that was hard to understand, I can try to think up a few more examples.

  •  04-25-2008, 1:44 PM 6568 in reply to 6559

    Re: Usage of ArcCos() and ArcSin()

    In a hurry;   try ArcTan( y / x )  or ArcTan( x / y ). May be you have to add 180 or 270.

    Think I have more time for that on sunday.

    Michael

  •  04-25-2008, 4:01 PM 6574 in reply to 6559

    Re: Usage of ArcCos() and ArcSin()

    The easy parts first:

    NaN = "Not a Number"

    ArcCos means "the angle (in radians) whose cosine is". And since cosines of angles can only have values from -1 to +1, if you try to do

    ArcCos(2.5) = NaN (since it is out of range)
     

  •  04-26-2008, 12:50 PM 6579 in reply to 6568

    Re: Usage of ArcCos() and ArcSin()

    MichaelH:

    try ArcTan( y / x )  or ArcTan( x / y ). May be you have to add 180 or 270.

    Thanks! Using ArcTan instead of ArcCos or ArcSin, I was able to create a function that will calculate the angle of a vector.

     

        Function AngleFromVector( X As Decimal, Y As Decimal ) As Decimal
           
            Var Result As Decimal
           
            // First, check what angle measurement unit we are currently using, and remember it
            Var MathAngleUnit As Int =  math.AngleMeasureUnit
           
            // Now, set the angle measurement to degrees
            Math.AngleMeasureUnit = Degrees
           
            // Calculate the angle
            If Y > 0 Then
                Result = (ArcTan( X*-1 / Y ))
            Else If Y < 1 Then
                Result = (ArcTan( X*-1 / Y ) - 180)
            End If
           
            // Lastly, restore the math angle measurement to the previous value
            Math.AngleMeasureUnit = MathAngleUnit
           
            Return result
        End Function

     

    Again, Thanks! 

     

View as RSS news feed in XML