Posted By

MichaelJWilliams on 04/24/09


Tagged

flash rotation as3 trigonometry Degrees angle radians


Versions (?)

Who likes this?

9 people have marked this snippet as a favorite

ozke
moselicious
Pingpongbal
edsonpavoni
LeeProbert
TrentSkunk
davidino1978
albertomori
scottwatkins


Angles in Flash: Rotation, Degrees and Radians


 / Published in: ActionScript 3
 

URL: http://gamedev.michaeljameswilliams.com/2009/04/25/angles-in-flash/

Two classes for easy conversion between the different units Flash uses for angles. See my blog for details.

  1. package com.michaeljameswilliams.gamedev.angles
  2. {
  3.  
  4. /**
  5. * Full details: http://gamedev.michaeljameswilliams.com/2009/04/24/angles-in-flash/
  6. * Hungarian notation idea: http://www.joelonsoftware.com/articles/Wrong.html
  7. * Optimisation credit: http://www.nbilyk.com/optimizing-actionscript-3
  8. *
  9. * Usage: import com.michaeljameswilliams.gamedev.angles.Angle
  10. * var degAngle:Number = Angle.degFromRad( radAngle );
  11. *
  12. * @author MichaelJWilliams
  13. */
  14. public class Angle
  15. {
  16. private const localPi:Number = Math.PI;
  17. private const localTwoPi:Number = Math.PI * 2;
  18. private const oneOver180:Number = 1 / 180;
  19. private const oneOverPi:Number = 1 / localPi;
  20.  
  21. public function Angle()
  22. {
  23. trace( "The com.michaeljameswilliams.gamedev.angles.Angle class does not need to be instantiated" );
  24. }
  25.  
  26. /**
  27. * @param p_degInput Angle, in degrees
  28. * @return Angle, in degrees, in range 0 to 360
  29. */
  30. public static function degFromDeg( p_degInput:Number ):Number
  31. {
  32. var degOutput:Number = p_degInput;
  33. while ( degOutput >= 360 )
  34. {
  35. degOutput -= 360;
  36. }
  37. while ( degOutput < 0 )
  38. {
  39. degOutput += 360;
  40. }
  41. return degOutput;
  42. }
  43.  
  44. /**
  45. * @param p_rotInput Angle, in degrees
  46. * @return Angle, in degrees, in range -180 to + 180
  47. */
  48. public static function rotFromRot( p_rotInput:Number ):Number
  49. {
  50. var rotOutput:Number = p_rotInput;
  51. while ( rotOutput > 180 )
  52. {
  53. rotOutput -= 360;
  54. }
  55. while ( rotOutput < -180 )
  56. {
  57. rotOutput += 360;
  58. }
  59. return rotOutput;
  60. }
  61.  
  62. /**
  63. * @param p_radInput Angle, in radians
  64. * @return Angle, in radians, in range 0 to ( 2 * pi )
  65. */
  66. public static function radFromRad( p_radInput:Number ):Number
  67. {
  68. var radOutput:Number = p_radInput;
  69. while ( radOutput >= localTwoPi )
  70. {
  71. radOutput -= localTwoPi;
  72. }
  73. while ( radOutput < -localTwoPi )
  74. {
  75. radOutput += localTwoPi;
  76. }
  77. return radOutput;
  78. }
  79.  
  80. /**
  81. * @param p_degInput Angle, in degrees
  82. * @return Angle, in degrees, in range -180 to +180
  83. */
  84. public static function rotFromDeg( p_degInput:Number ):Number
  85. {
  86. var rotOutput:Number = p_degInput;
  87. while ( rotOutput > 180 )
  88. {
  89. rotOutput -= 360;
  90. }
  91. while ( rotOutput < -180 )
  92. {
  93. rotOutput += 360;
  94. }
  95. return rotOutput;
  96. }
  97.  
  98. /**
  99. * @param p_rotInput Angle, in degrees
  100. * @return Angle, in degrees, in range 0 to 360
  101. */
  102. public static function degFromRot( p_rotInput:Number ):Number
  103. {
  104. var degOutput:Number = p_rotInput;
  105. while ( degOutput >= 360 )
  106. {
  107. degOutput -= 360;
  108. }
  109. while ( degOutput < 0 )
  110. {
  111. degOutput += 360;
  112. }
  113. return degOutput;
  114. }
  115.  
  116. /**
  117. * @param p_radInput Angle, in radians
  118. * @return Angle, in degrees, in range 0 to 360
  119. */
  120. public static function degFromRad( p_radInput:Number ):Number
  121. {
  122. var degOutput:Number = 180 * oneOverPi * radFromRad( p_radInput );
  123. return degOutput;
  124. }
  125.  
  126. /**
  127. * @param p_degInput Angle, in degrees
  128. * @return Angle, in radians, in range 0 to ( 2 * pi )
  129. */
  130. public static function radFromDeg( p_degInput:Number ):Number
  131. {
  132. var radOutput:Number = localPi * oneOver180 * degFromDeg( p_degInput );
  133. return radOutput;
  134. }
  135.  
  136. /**
  137. * @param p_radInput Angle, in radians
  138. * @return Angle, in degrees, in range -180 to +180
  139. */
  140. public static function rotFromRad( p_radInput:Number ):Number
  141. {
  142. return rotFromDeg( degFromRad( p_radInput ) );
  143. }
  144.  
  145. /**
  146. * @param p_rotInput Angle, in degrees
  147. * @return Angle, in radians, in range 0 to ( 2 * pi )
  148. */
  149. public static function radFromRot( p_rotInput:Number ):Number
  150. {
  151. return radFromDeg( degFromRot( p_rotInput ) );
  152. }
  153.  
  154. }
  155.  
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164. package com.michaeljameswilliams.gamedev.angles
  165. {
  166.  
  167. /**
  168. * Full details: http://gamedev.michaeljameswilliams.com/2009/04/24/angles-in-flash/
  169. * Hungarian notation idea: http://www.joelonsoftware.com/articles/Wrong.html
  170. * Optimisation credit: http://www.nbilyk.com/optimizing-actionscript-3
  171. *
  172. * Use this instead of the Angle functions when you need extra speed
  173. * and are going to use the functions multiple times (e.g. over a loop)
  174. *
  175. * Usage: import com.michaeljameswilliams.gamedev.angles.Angle
  176. * var angleUtil:AngleUtil = new AngleUtil();
  177. * var degAngle:Number = angleUtil.degFromRad( radAngle );
  178. *
  179. * @author MichaelJWilliams
  180. */
  181. public class AngleUtil
  182. {
  183. private const localPi:Number = Math.PI;
  184. private const localTwoPi:Number = Math.PI * 2;
  185. private const oneOver180:Number = 1 / 180;
  186. private const oneOverPi:Number = 1 / localPi;
  187.  
  188. public function AngleUtil()
  189. {
  190.  
  191. }
  192.  
  193. /**
  194. * @param p_degInput Angle, in degrees
  195. * @return Angle, in degrees, in range 0 to 360
  196. */
  197. public function degFromDeg( p_degInput:Number ):Number
  198. {
  199. var degOutput:Number = p_degInput;
  200. while ( degOutput >= 360 )
  201. {
  202. degOutput -= 360;
  203. }
  204. while ( degOutput < 0 )
  205. {
  206. degOutput += 360;
  207. }
  208. return degOutput;
  209. }
  210.  
  211. /**
  212. * @param p_rotInput Angle, in degrees
  213. * @return Angle, in degrees, in range -180 to + 180
  214. */
  215. public function rotFromRot( p_rotInput:Number ):Number
  216. {
  217. var rotOutput:Number = p_rotInput;
  218. while ( rotOutput > 180 )
  219. {
  220. rotOutput -= 360;
  221. }
  222. while ( rotOutput < -180 )
  223. {
  224. rotOutput += 360;
  225. }
  226. return rotOutput;
  227. }
  228.  
  229. /**
  230. * @param p_radInput Angle, in radians
  231. * @return Angle, in radians, in range 0 to ( 2 * pi )
  232. */
  233. public function radFromRad( p_radInput:Number ):Number
  234. {
  235. var radOutput:Number = p_radInput;
  236. while ( radOutput >= localTwoPi )
  237. {
  238. radOutput -= localTwoPi;
  239. }
  240. while ( radOutput < -localTwoPi )
  241. {
  242. radOutput += localTwoPi;
  243. }
  244. return radOutput;
  245. }
  246.  
  247. /**
  248. * @param p_degInput Angle, in degrees
  249. * @return Angle, in degrees, in range -180 to +180
  250. */
  251. public function rotFromDeg( p_degInput:Number ):Number
  252. {
  253. var rotOutput:Number = p_degInput;
  254. while ( rotOutput > 180 )
  255. {
  256. rotOutput -= 360;
  257. }
  258. while ( rotOutput < -180 )
  259. {
  260. rotOutput += 360;
  261. }
  262. return rotOutput;
  263. }
  264.  
  265. /**
  266. * @param p_rotInput Angle, in degrees
  267. * @return Angle, in degrees, in range 0 to 360
  268. */
  269. public function degFromRot( p_rotInput:Number ):Number
  270. {
  271. var degOutput:Number = p_rotInput;
  272. while ( degOutput >= 360 )
  273. {
  274. degOutput -= 360;
  275. }
  276. while ( degOutput < 0 )
  277. {
  278. degOutput += 360;
  279. }
  280. return degOutput;
  281. }
  282.  
  283. /**
  284. * @param p_radInput Angle, in radians
  285. * @return Angle, in degrees, in range 0 to 360
  286. */
  287. public function degFromRad( p_radInput:Number ):Number
  288. {
  289. var degOutput:Number = 180 * oneOverPi * radFromRad( p_radInput );
  290. return degOutput;
  291. }
  292.  
  293. /**
  294. * @param p_degInput Angle, in degrees
  295. * @return Angle, in radians, in range 0 to ( 2 * pi )
  296. */
  297. public function radFromDeg( p_degInput:Number ):Number
  298. {
  299. var radOutput:Number = localPi * oneOver180 * degFromDeg( p_degInput );
  300. return radOutput;
  301. }
  302.  
  303. /**
  304. * @param p_radInput Angle, in radians
  305. * @return Angle, in degrees, in range -180 to +180
  306. */
  307. public function rotFromRad( p_radInput:Number ):Number
  308. {
  309. return rotFromDeg( degFromRad( p_radInput ) );
  310. }
  311.  
  312. /**
  313. * @param p_rotInput Angle, in degrees
  314. * @return Angle, in radians, in range 0 to ( 2 * pi )
  315. */
  316. public function radFromRot( p_rotInput:Number ):Number
  317. {
  318. return radFromDeg( degFromRot( p_rotInput ) );
  319. }
  320.  
  321. }
  322.  
  323. }

Report this snippet  

You need to login to post a comment.