A Flash Developer Resource Site














Internet Commerce

Partners & Affiliates














Developer Channel

internet.com


Featured Flash FLA
Gallery Downloads 11246 Flash Movies | 9 New Flash Movies Added
What's New | Top 100

Featured FLA

» Author: Gurgen Tagvorian
» Title: Perpetuum Mobile
» Description: It's just a Perpetuum Mobile for you
» More by Gurgen Tagvorian


Random FLAs | Add Flash Movie
Featured Flash Site
Gallery Downloads 6410 Flash Sites | 0 New Flash Links
What's New | Top 100 Flash Site

Featured Site

» Posted in the Flash Kit Links section
» Title: Atlantis Media Ltd
» Comments: Greg Rudman new media creative & website design - Atlantis Media Ltd


Random Links | Add your own Flash Related Links
Flash Tutorials 1192 Tutorials 7 New Tutorials Added!
What's New | Top100

» How to convert FLV video to MP3 audio for Mac OS
» Join flv videos together with just a few clicks
» How to convert flv file to avi for mac os x
» How to Create Christmas Flash Greeting eCard without programming skills
» How to Create Real Estate Flash Photo Gallery Presentation
» How to burn DVD, VCD, SVCD
» How to convert FLV to ASF with FLV to ASF converter for Mac os X
» 5 ways to convert PowerPoint to video
» How to Make Vista Buttons
» Unknown Tag: Title10
Random Tutorial | Add Site

bbm.netBBM.net is designed to save you time and deliver the highest quality royalty-free music for your multimedia projects. Features include: over 450 Music Loop Packages from some of the best composers in the business, our music search engine to speed your selection process, alternate music versions & bonus sounds to use for rollovers or transitions, free technical support and free consulting.

Click here for details »

Senior Web Analytics Engineer
Professional Technical Resources
US-OR-Portland

Justtechjobs.com Post A Job | Post A Resume


Tutorials Home What's New Top Rated Submit myTutes Random!

Search Tutorials


Tutorials Tutorials » Math-Physics

Categories Changing _rotation to point at stuff, like the mouse.
Author: Jim Bumgardner | Website: http://www.krazydad.com/bestiary/ |

 
Page 1
1

Here's a basic script that can make a movieclip point at another object.

MovieClip.prototype.pointAt = function(x,y)
{
  var dx = x - this._x;         // distance to other object on x-axis
  var dy = y - this._y;         // distance on y-axis
  var dist = Math.sqrt(dx*dx + dy*dy); // true distance (using Pythagorean theorem)
  var angle;
  // figure out angle in radians (0- 2*PI)
  if (dy <0)
    angle = Math.PI*2-Math.acos(dx/dist);
  else
    angle = Math.acos(dx/dist);
  // convert to rotation value (angle in degrees or 0-360)
  this._rotation = angle*180/Math.PI;
}

Math Explanation

The function that does all the work here is Math.acos(). The Math.acos() function is the compliment of the Math.cos() function. On another tutorial here, you may have seen how to use Math.cos() and Math.sin() to draw circle shapes using polar coodinates.

Math.cos() takes an angle (expressed in radians, or going from 0 to 2*PI instead of 0 to 360) and converts it to a position on a circle.

Given an angle (expressed in degrees) and a radius, a point on a circle is given by:

x = Math.cos(angle*Math.PI/180)*radius;
y = Math.sin(angle*Math.PI/180)*radius;

The Math.acos() function I'm using does the reverse, it converts the value returned by Math.cos() back into an angle in radians. So by reversing the math of the above equations for using polar coordinates, we can determine the original angle in radians. Then, to convert this angle to a rotation value (which is in degrees) we scale it by the ratio betwen degres and radians: 360 / (2*PI) which is the same as 180 / PI.

How to use the script

Here's a script, which works with the above script to make a movieclip point in the direction of the mouse.

mc.pointAtMouse = function()
{
  this.pointAt(this._parent._xmouse, this._parent._ymouse);
}

Let's say your have a movieclip that you want to point at the mouse, you can do the following to make this happpen:

mc.onEnterFrame = mc.pointAtMouse;

OR you can call this.pointAtMouse() from within a more complex onEnterFrame function.

Finally, this script assumes that your object points to 3:00 (to the right) when it is in it's 'normal' (rotation=0) state. If it doesn't, you'll need to add an offset to the rotation value. Here's a version of the function which accepts an offset. If your object normally points at 12:00 (upwards) then use -90 to correct it.

MovieClip.prototype.pointAt = function(x,y,offset)
{
  var dx = x - this._x;         // distance to other object on x-axis
  var dy = y - this._y;         // distance on y-axis
  var dist = Math.sqrt(dx*dx + dy*dy); // true distance (using Pythagorean theorem)
  var angle;
  // figure out angle in radians (0- 2*PI)
  if (dy <0)
    angle = Math.PI*2-Math.acos(dx/dist);
  else
    angle = Math.acos(dx/dist);
  // convert to rotation value (angle in degrees or 0-360)
  this._rotation = offset + angle*180/Math.PI;
}

Example usage for object which normally points upwards (12:00):

this.pointAt(this._parent._xmouse, this._parent._ymouse, -90);

- jim

1

» Level Intermediate

Added: : 2004-03-15
Rating: 6.65 Votes: 20
Hits: 1841
» Author
Professional C/C++ programmer and flash hobbyist.
» Download
Download the files used in this tutorial.
Download (0 kb)
Get conversion and unzipping tools for PC and Mac here!

» Forums
More help? Search our boards for quick answers!

Please rate this tutorial, 10 is the top rating, you can also click the comments link to read/write a review.
10 9 8 7 6 5 4 3 2 1
Read or Post Comments
 
   
 

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers