ghosttie [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=17598 angle math <p>I'm trying to animate a bullet going from one place to another. First I get the angle from the source to the destination by doing</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> local angle = atan2(startx - endx, starty - endy) </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>then I move the bullet from the source to the destination by using that angle and incrementing the distance from the source</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> bulletx = startx + dist * cos(angle) bullety = starty + dist * sin(angle) </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>but the angle is all messed up - the bullet goes in completely the wrong direction.</p> <p>As far as I can tell the units of atan2, sin and cos are the same so I don't think converting between radians and degrees is the problem. I googled getting the angle between two points and getting a position from an angle but every example I found in other languages seemed consistent with what I'm doing...</p> <p>Can anyone point out what I'm doing wrong?</p> https://www.lexaloffle.com/bbs/?tid=28720 https://www.lexaloffle.com/bbs/?tid=28720 Mon, 23 Jan 2017 22:21:51 UTC