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
    • Concept Art
    • Textures
    • Music
    • Sound Effects
    • Documents
    • Featured Tutorials
  • Submit Art
  • Collect
    • My Collections
    • Art Collections
  • Forums
  • FAQ
  • Leaderboards
    • All Time
      • Total Points
      • Comments
      • Favorites (All)
      • Favorites (2D)
      • Favorites (3D)
      • Favorites (Concept Art)
      • Favorites (Music)
      • Favorites (Sound)
      • Favorites (Textures)
    • Weekly
      • Total Points
      • Comments
      • Favorites (All)
      • Favorites (2D)
      • Favorites (3D)
      • Favorites (Concept Art)
      • Favorites (Music)
      • Favorites (Sound)
      • Favorites (Textures)
  • ❤ Donate
Programming

Determining angle between targets

Garbeld
Monday, December 19, 2011 - 07: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.

  • Log in or register to post comments
MoikMellah
joined 14 years 2 months ago
Monday, December 19, 2011 - 11: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

My project: Bits & Bots

  • Log in or register to post comments
Garbeld
joined 14 years 10 months ago
Monday, December 19, 2011 - 11: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

  • Log in or register to post comments
Anonymous (not verified)
joined 0 sec ago
88.153.142.176
Monday, December 19, 2011 - 12: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.

  • Log in or register to post comments
Fracture
joined 13 years 11 months ago
Sunday, December 25, 2011 - 07:09

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 )

"I was dancing with two kunai and a giant shuriken jumped out of the cake. Fun stuff!"

http://oxidmedia.deviantart.com/

  • Log in or register to post comments