6 ms·
atan2() is incredibly useful in game development. You can use it to determine the angle between objects, calculate the angle of a 2d vector, and other useful th
by doomlaser 6y ago
atan2() is incredibly useful in game development. You can use it to determine the angle between objects, calculate the angle of a 2d vector, and other useful things.
https://ryankubik.com/blog/atan2/ https://ryankubik.com/blog/atan2/
- SamBam 6y agoYup, I felt incredibly stupid about six months ago finding out about atan2 for the first time, given the number of times I've made stupid errors using atan, and then correcting those errors by manually checking what quadrant I was in.
- ajuc 6y agoSame. My favorite "looks simple but is hard" task is "given object position and angle and target position return +1 if the object should turn clockwise and -1 if the object should turn counterclockwise to face the target". I've got it subtly wrong so many times and using atan2 only solves half the problems. Now I just copypaste it from my previous game :)
- OskarS 6y agoIsn’t that just the sign of the cross product of the direction vector and the vector from the object position to the target (or the orientation of the bivector in geometric algebra)? No need for trig there, I don’t think.
- jacobolus 6y agoThis is a question of whether the target is to the right or left of the line where the object is pointing. As OskarS says, you don’t need trigonometry for this at all. Just take the wedge product of the two vectors, and check the sign. That is, if your direction is the vector (a, b), and the displacement to the target is the vector (c, d), you can compute ad – bc. If this quantity is positive turn left, else turn right.
- cmroanirgo 6y agothere is also sincos(), which is useful when you need both, such as building matrices. https://man7.org/linux/man-pages/man3/sincos.3.html https://man7.org/linux/man-pages/man3/sincos.3.html https://home.deec.uc.pt/~jlobo/tc/opcode_f.html https://home.deec.uc.pt/~jlobo/tc/opcode_f.html
- eigenket 6y agoThats quite cute. I imagine it is (doing something equivalent to) using the complex exponential and taking the real an imaginary parts? exp(iz) = cos(z) + i sin(z), so by working out exp you can get cos and sin for free.
- jacobolus 6y agoIn almost every context (certainly in game development) it is typically most efficient and numerically pleasant to avoid angle measures altogether and work in terms of vectors directly. To represent an angle, use a pair of numbers representing a complex number (optionally normalized to lie on the unit circle). If you want to reduce it to a single number (e.g. you have a large number of them you need to serialize to disk or send over the wire) use the stereographic projection.
- enriquto 6y ago> atan2() is incredibly useful in game development Why game development? It's useful everywhere! I have never found a genuine usage of atan(), it's always atan2() that is natural to use.
- jacobolus 6y agoAs on example, to find longitude and latitude on a sphere from stereographic projection (given as Cartesian coordinates) you need both: [ x, y ] ↦ [ atan2(y, x), π/2 − 2 atan(√(x² + y²)) ] But atan per se comes up all over the place if you start doing much work with angle measures.
- enriquto 6y agoshouldn't that be atan(1/sqrt(x^2+y^2))? In that case, I'd still use atan2 with y=1.
- deleted 6y ago[deleted]
- deleted 6y ago[deleted]
- impendia 6y agoNitpick: this blog post makes an incorrect statement, namely that the domain of the inverse tangent function is -pi/2 to pi/2. The domain of a function is the set of all possible inputs. In particular, the domain of the inverse tangent consists of all real numbers. The author means the range.
- lopmotr 6y agoI wish we used different words for domain and range. Like "input range" and "output range" or something. It's an endless source of confusion, especially since range can legitimately be used for the domain if you phrase it like "the range of values we can use for the input of the inverse tangent function is -infinity to infinity."
- jacobolus 6y agoIf you like, you can call it the image. https://gowers.wordpress.com/2011/10/13/domains-codomains-ranges-images-preimages-inverse-images/ https://gowers.wordpress.com/2011/10/13/domains-codomains-ra...
- publicola1990 6y agoIn many places though it is clearer to to use vector dot product to calculate the angle than use atan2.