Wheels Keep on Spinning ‘Round

Yesterday, I was working on a Flash animation for a client. Part of the animation involved an automobile driving over the client’s product as a display of structural integrity. That meant spinning wheels. That meant a little fun with geometry and ActionScript.

I threw together some ActionScript 2 to rotate the wheels based on the movement of the vehicle. Not sure if it’s perfect, but it seemed to work okay.

The ActionScript to calculate the rotation of each wheel depends on two formulae:

Pythagorean Theorem

-and-

Circumference

Thanks, Pythagoras and Pi. Thorasandpi.

Here is the resulting ActionScript (attached to the wheel movie clip):


onEnterFrame = function() {

    // _parent is the vehicle clip. It's what's moving.
    // Figure out its x and y coordinates relative to last time.
    dx = _parent._x - this.lastx;
    dy = _parent._y - this.lasty;

    // The sum of the square of the two sides (horiz. and vert. change)
    sum = Math.pow(dx, 2) + Math.pow(dy, 2)

    // The hypotenuse
    dist = Math.sqrt(Math.abs(sum));

    // The direction is determined by whether dx is positive or negative
    dist = dx < 0 ? -1 * dist : dist;

    _rotation = _rotation + ( (360 / (_width * Math.PI) ) * dist );

    // Store the vehicle's x and y coordinates for next time.
    this.lastx = _parent._x;
    this.lasty = _parent._y;

}

And here is an example:

This Flash animation requires Javascript and Flash 9.

I’m sure this is basic stuff for most ActionScript people, but I don’t work with it that often. Sometimes I wish I did. I’ve always loved geometry, and I know there are all kinds of fun puzzles like this when you combine animation with math. Ah well… just not enough hours in the day.


comment feed And the ensuing discussion…

  1. 1

    Jul 3rd, 2009 at 4:58 am kamran

    brother ur work is courgeos i like that.i want to learn the actione scripting can u teach me plz.

Comments are closed.



Additional Resources


Tumblelog

Tumblr

Tumblr

Delicious

Delicious

Instructional

Recent Instructional Articles

Slicehost (and Linode) LAMP Cheatsheet

17.11
16

A quick list of steps I use to set up a LAMP server on Slicehost or Linode.

Terminal Tip: Prevent Creation of Mac Dot Files

07.08
2

An environment variable can prevent creation of ._filename files.

Terminal Tip: Delete Those Mac Dot Files

08.06
4

Use the find command to delete all of the ._* and .DS_Store files.

Editorial

Recent Editorial Articles

More Usable Mac: Finder Toolbar

05.12
3

I find it useful to keep a few extra items in my Finder toolbar.

No Multiple-Class Support in IE6

18.11
9

IE6 doesn’t respond to multiple class selectors.

New Skin for the Old Blogish

07.10
8

This blogish is finally back online after an extended period of http silence.

Downloadable

Recent Downloadable Articles

Gitup!

01.02
0

Gitup + Transmit = Really Simple Publishing

Leopard-Style iTerm Icon, Take 2

18.12
8

The newer, bluer version of the iTerm icon.

Leopard-Style iTerm Icon

05.12
6

An updated, Leopard-style icon for iTerm.

About This Site

About the Author

That’s me in the photo above. My current profession is web development. Therefore, it is the subject of this site.

Postpostmodern

Postpostmodern is the name of this site and my alias on most of the web. There's nothing really special about the name Postpostmodern. I studied art in college during the years after postmodernism, and nobody knew how else to classify the state of things other than silly words like postpostmodernism.

Sorta Blogish

I'd call this a blog, but I don't feel it fits the 'log' format. My goal is to publish articles on web-related topics that interest me, and while some articles may be time-sensitive, I would prefer that the organizational focus be on the categories and tags rather than chronology.

More Me

More about me can be found on the about page. Or, look me up in the usual places: