Adobe, MAKE SOME NOISE

Rotating objects toward a point

December 14th, 2009

I've seen a lot of questions about rotation in AS3 lately so I thought I'd post a quick writeup. It's a deceptively simple formula but there are a couple snags that can trip you up if you aren't aware of them.

Get Adobe Flash player

First off - Flash uses a fairly confusing rotational layout. A DisplayObject's rotation is measured in degrees from 0, which is at 3 O'Clock. Degrees extend clockwise to 9 O'Clock (180°) where an odd thing happens. Flash converts any number you pass in to the closest possible value to zero, so 181° gets recorded as -179°. This is still technically correct, but it can wreak havoc on your code if you're not expecting it.

Here's a quick method to get the positive angle back:

  1. spinner.rotation = 181;
  2. trace(spinner.rotation); // -179
  3. var angle:Number = spinner.rotation; // -179
  4. angle = (angle + 360) % 360 // 181

Alright - what people really want to know is how to calculate the angle between an object and the mouse, or another object. The solution is to take the arc-tangent of the offset in y, divided by the offset in x, which, if you remember your trig from highschool, would be the two perpendicular sides of your right triangle. Here's what the code looks like:

  1. package {
  2.  
  3. /*
  4. * Flash 10.0 ? ActionScript 3.0
  5. * www.calypso88.com
  6. */
  7.  
  8. //--------------------------------------
  9. // PACKAGES
  10. //--------------------------------------
  11.  
  12. import flash.display.Sprite;
  13. import flash.display.DisplayObject;
  14. import flash.geom.Point;
  15. import flash.events.Event;
  16.  
  17. public final class Arrow extends Sprite {
  18.  
  19. //--------------------------------------
  20. // CONSTRUCTOR
  21. //--------------------------------------
  22.  
  23. public function Arrow(){
  24. super();
  25. addEventListener(Event.ENTER_FRAME, enterFrame);
  26. }
  27.  
  28. //--------------------------------------
  29. // EVENT HANDLERS
  30. //--------------------------------------
  31.  
  32. private function enterFrame(e:Event):void{
  33. var p1:Point = new Point(this.x, this.y);
  34. var p2:Point = new Point(stage.mouseX, stage.mouseY);
  35. this.rotation = angleBetween(p2, p1);
  36. }
  37.  
  38. //--------------------------------------
  39. // PRIVATE & PROTECTED INSTANCE METHODS
  40. //--------------------------------------
  41.  
  42. private function angleBetween(p1:Point, p2:Point):Number{
  43. var deltaX:Number = p1.x - p2.x;
  44. var deltaY:Number = p1.y - p2.y;
  45. var angle:Number = Math.atan2(deltaY, deltaX); // in radians
  46. return radiansToDegrees(angle);
  47. }
  48.  
  49. private function radiansToDegrees(r:Number):Number{
  50. return(r * (180 / Math.PI));
  51. }
  52.  
  53. private function degreesToRadians(d:Number):Number{
  54. return(d * (Math.PI / 180));
  55. }
  56. }
  57. }

The last surprise is that the arc-tangent comes back in radians (π radians = 180° degrees) so you'll need to convert back to degrees before you set the rotation. Here is the final result applied to a few dozen arrows.

Get Adobe Flash player

3 Responses to “Rotating objects toward a point”

  1. Tweets that mention Calypso88 » Blog Archive » Rotating objects toward a point -- Topsy.com Says:

    [...] This post was mentioned on Twitter by net.art GIANT FUNNEL. net.art GIANT FUNNEL said: ? [from bitless] Calypso88 » Blog Archive » Rotating objects toward a point http://bit.ly/9YEhRk [...]

  2. THEODORE Says:


    CheapTabletsOnline.Com. Canadian Health&Care.Best quality drugs.Special Internet Prices.No prescription online pharmacy. Low price pills. Buy drugs online

    Buy:Lipothin.SleepWell.Advair.Acomplia.Ventolin.Lipitor.Zocor.Prozac.Amoxicillin.Aricept.Lasix.Female Pink Viagra.Female Cialis.Benicar.Buspar.Seroquel.Nymphomax.Cozaar.Zetia.Wellbutrin SR….

  3. PHILIP Says:


    CheapTabletsOnline.com. Canadian Health&Care.No prescription online pharmacy.Best quality drugs.Special Internet Prices. High quality pills. Order pills online

    Buy:100% Pure Okinawan Coral Calcium.Zyban.Accutane.Lumigan.Prednisolone.Actos.Zovirax.Prevacid.Nexium.Mega Hoodia.Human Growth Hormone.Retin-A.Valtrex.Synthroid.Arimidex.Petcam (Metacam) Oral Suspension….

Leave a Reply