Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Cos inverse help

  1. Dec 26, 2004 #1

    eax

    User Avatar

    Hi,

    I'm programming a game and need a character to turn facing another character. I got far but the problem is the language(Turing) Iam provided with doesn't support a cos/sin inverse function but has sin/cos.

    Anyone know the formula? I have cos(x) how can I get x? Searching on google is not help all I get is what cos (x) = to relative to sin/tan.

    Thanks in advance!
     
  2. jcsd
  3. Dec 26, 2004 #2
    Hi
    I am not sure about your question but I will try my best to anwser it...
    Cosine(x)= adj/hyp.
    cos(x)^-1= Ratio of the adjecent arm to the ratio of the hypotines.

    Or in terms of the unit circle...
    Cos(x) is equal to x/r
    so...
    Cos(x)^-1= Ratio of x/r. THis solves for the angle of x.

    Ok and finally...
    cot(x)=cos(x)/sin(x)
    the inverse of Cos(x) is sec(x) ie. sec(x)=r/x
    the inverse of Sin(x) is csc(x) ie. csc(x)=r/y
    I hope that helped....
    The ratio of sin(x)/
     
  4. Dec 26, 2004 #3

    eax

    User Avatar

    This is what I have for mouse to character interaction, instead of character(enemy) to character(player)

    Code (Text):

    set x/y to equal mouse position x/y
    x := mousex
    y := mousey
    drawx/yoff are < 0 but we want to add them to x/y thus
    subtracting adds them to x/y
    x -= drawxoff
    y -= drawyoff


    bound.x/y are the coordinates for the character(player)
    subtracting x/y by bound.x/y gives the x/y position reveloving
    bound.x/y so (0,0) = (bound.x, bound.y)
    y -= bound.y
    x -= bound.x
    get distance
    dist := sqrt (x * x + y * y)
    if dist < 1 then
        return
    end if

    I added 90 for a reason but it doesn't matter
    pangle += 90
    this next function just makes sure pangle is >= 0 and <= 360
    rapanglei (pangle)

    cos_t is a table of cos values because calculating cos everytime takes
    lots of CPU time

    calculating dotproduct (I think thats what it is called)
    (and I like to reuse variables to save memory :))

    We are treating these as vectors. bound.r = radius of character(player)
    multiplying by cos(angle) gives us the "vector" its facing, then calculating
    dot product
    x := (x * cos_t (pangle) * bound.r + y * sin_t (pangle) * bound.r) / (bound.r * dist)
    puting pangle back to original value
    pangle -= 90

    and now x = cos(angle I want)
    angle I want = cos inverse(x)
     
    There is no cos/sin inverse function in this language. How can I get "angle I want"?
    I hope my question is more clear now

    Thanks in advance!
     
  5. Dec 26, 2004 #4
    Maybe you could make an arccos() look-up table? I don't know of any mathematical way to transfer cos(x) to x without arccos(). A look-up table would probably be faster anyways. You'd just need to make a function that finds the closest related value.

    By the way, I've never seen the language Turing, but it kind of looks like Pascal...
     
  6. Dec 26, 2004 #5

    eax

    User Avatar

    Im not even sure if http://www.holtsoft.com/turing/support/#currentversion [Broken] is a language. Its more of an interpreter, I don't know why my school starts of with Turing then C++ is next year(or cemester :)). It would be better to start with C/ASM (in 16bit(DOS) mode)

    Someone has to know. My math teacher said that before there were calculators people new the formula's for cos/sin and arccos/sin. If everyone forgot then everyone just copy's and pastes code into calculators and every where else :D.
     
    Last edited by a moderator: May 1, 2017
  7. Dec 27, 2004 #6
    I'm pretty sure there's a Taylor series for all the trigonometric functions, but I can't seem to find the one for arccos(). Try looking up on Google the Taylor series for inverse cosine, and see if you can find it.

    Learning an interpreted language before a compiled one? That's quite odd.
     
  8. Dec 27, 2004 #7
    You could use the derivative of cos^-1 x and work out it's Taylor series, then integrate term by term.
     
  9. Dec 28, 2004 #8

    VietDao29

    User Avatar
    Homework Helper

    Hi,
    I am not an anvanced programmer, but anyway, I suggest this:
    Since you know cos(x) then you know it's positive/negative/0.

    If cos(x) = 0 then
    x = 90
    EndIf

    If cos(x) < 0 then
    For s = 91 to 180
    ...
    (You can find the difference between cos(x) and cos(s). When the cos(s) - cos(x) < 0, stop the loop and you will get the s).
    Next s
    EndIf

    If cos(x) > 0 then
    For s = 1 to 90
    ...
    (You can find the difference between cos(x) and cos(s). When the cos(s) - cos(x) > 0, stop the loop and you will get the s).
    Next s
    EndIf

    And you will get cos(s) approximetely equal to cos(x)... But it's not the best way, though,...
    Hope this help :-)
    Bye bye,
    Viet Dao,
     
  10. Dec 28, 2004 #9

    eax

    User Avatar

    Thank you :)
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook