Skip to main content

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password
Register
  • Home
  • Browse
    • 2D Art
    • 3D Art
    • Documents
    • Music
    • Sound Effects
    • Textures
    • Featured Tutorials
  • Submit Art
  • Collect
    • My Collections
    • Art Collections
  • Forums
  • FAQ
Programming

Determining angle between targets

Garbeld
Monday, December 19, 2011 - 10:37

I want my program to determine in which 16th of a circle an object is from another.

Determining which octant it is based on whether the x or y difference is larger, and which ones are negative/positive, is trivial. I'm hoping that there's a similar way to do so for 16ths.

Basically, I'm a minimalist and perfectionist, I'd prefer to avoid using a technically imperfect slope lookup table or trigonometric functions, though I'm afraid that's impossible. Still, hoping someone here can prove me wrong.

  • Add new comment
MoikMellah
Monday, December 19, 2011 - 14:18
MoikMellah's picture

I think you can either use an approximated lookup table OR use the trig functions, but you won't be able to avoid both.  Best I could come up with is to have an approximated lookup table of tangent values for 22.5, 45, and 67.5 degrees (.4142, 1, and 2.4142, respectively), and compare abs(y/x) to each (warning, awful pseudocode):

//You've already figured out the quad with negative/positive x and y

tans = array(0 = .4142, 1 = 1, 2 = 2.4142)

sector = 3

for tc = 0 to 2 do

  if (abs(y/x) < tans[tc]) then sector = tc

done

return sector

Obviously some glaring flaws in it (doesn't account for (x == 0), for starters) but it might be a starting point.

Hope it helps!

-Moik

  • reply
Garbeld
Monday, December 19, 2011 - 14:22

Oh, it's no problem if I'm not unwilling to do so. I had already calculated the relative x/y factors at these angles before realizing I only needed the slopes of the octant lines for handling movement; convert these to slopes and find whether the line between my two objects is above or below the line passing through its octant. It's just an issue of my having an irrational dislike for this method.

edit: Ach, neither [i][/i] nor <i></i> applied italics? o_O

  • reply
Anonymous (not verified)
88.153.142.176
Monday, December 19, 2011 - 15:16

Have a look at the atan2(y,x) function. It's the inverse tangens function:

http://en.wikipedia.org/wiki/Atan2

Should deliver the angle in radians (pi range) from which you can calculate a value between 0 to 15.

  • reply
Team_SD
Sunday, December 25, 2011 - 10:09
Team_SD's picture

hey i found an approach for 8 directions which works great. this is coded in a script language that uses a state machine, but the logic is all here:

if "($xdistance > 0) and ($ydistance > 0) and ($absx > 0.414*$absy) and ($absx < 2.414*$absy)" "down_right"
        if "($xdistance < 0) and ($ydistance > 0) and ($absx > 0.414*$absy) and ($absx < 2.414*$absy)" "down_left"
       
        if "($xdistance > 0) and ($ydistance < 0) and ($absx > 0.414*$absy) and ($absx < 2.414*$absy)" "up_right"
        if "($xdistance < 0) and ($ydistance < 0) and ($absx > 0.414*$absy) and ($absx < 2.414*$absy)" "up_left"
       
        if "($xdistance > 0) and ($absx > $absy)" "right"
        if "($xdistance < 0) and ($absx > $absy)" "left"
       
        if "($ydistance > $absx) and ($absx < 0.5*$absy)" "down"
        if "($ydistance < $absx) and ($absx < 0.5*$absy)" "up"

 

$absx and $absy are variables for the absolute values of x and y distance, respectively.

X is negative to the left, positive to the right, Y is negative up, positive down.

when the IF condition is met, the script jumps to the state specified in the last word, in this case, the directions.

thank you all (and a couple other team mates who wanted this badly :P )

  • reply

Add new comment

The content of this field is kept private and will not be shown publicly.
Right Column

More information about text formats

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <br> <img> <del> <strike> <h1> <h2> <h3> <h4> <h5>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Attachments
You may attach one or more images to your post.
Files must be less than 200 MB.
Allowed file types: png gif jpg jpeg.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Fill in the blank.