ATan2
Blitz3D Forums/Blitz3D Beginners Area/ATan2
| ||
I'm using the atan2 function to work out the angle to get to a waypoint (in 2d)and then angle the sprite in the correct direction. Because atan2 doesn't work in 360 - just 180 I'd thought just add 180 to the result - but that doesn't work. I'm sure it's something very simple, but I can't see the solution atm. I've worked around the problem by rotating the sprite to 180 degree's and loading using loadanimimage as usual. Here's my code... angle# = ATan2(y_dist#,x_dist#) If angle# < 0.0 angle# = angle# + 360.0 |
| ||
The blitz rotation system in 2d is a bit funny. 0 deg is the sprite/image moving right. Also, the angle returned is between -180 and 180 i'm sure. So..angle# = ATan2(y_dist#,x_dist#) If angle# < 0.0 angle# = angle# + 360.0 try angle# = ATan2(y_dist#,x_dist#) angle# = angle# + 180.0 And don't rotate the sprite/image to 180. :o) |
| ||
Yes, I noticed this with Blitz3D when I was doing a 3D compass for games - it doesn't work on 360 degrees - you end up with what "Ross C" said at the start of his post. EDIT: A text version (of my rough compass) looks like this: compass#=EntityYaw(campiv) Print compass If compass>-45 And compass<45 Then Locate 440,10: Print "NORTH" If compass<-45 And compass>-135 Then Locate 440,10: Print "EAST" If compass<-135 Or compass>135 Then Locate 440,10: Print "SOUTH" If compass>45 And compass<135 Then Locate 440,10: Print "WEST" |
| ||
Ross, no I tried that before and my character moves away from the waypoint! |
| ||
ATan2() returns the theta component from standard polar coordinates. In polar coordinates, 0 degrees is facing positive X, i.e. to the right. From 0 to 90 degrees will bring you to positive Y, that would normally be up, but since the Y coordinate is flipped on the screen, that'll point your object downward.. From 0 to -90 will point you into -Y. 90 to 180 is -X, +Y, -90 to -180 is -X, -Y. So: -90 | | -180 | 0 ------O------ 180 | | | 90 It's much easier to use if you start using -179 to 180 all the time instead of 0 to 359. |
| ||
Thanks for the info - In that case I'll stick with my workaround as this seems the simplest way around the problem. |