Archive for January, 2009

Code Snippet of the Week: Rotating about an arbitrary point

Sunday, January 11th, 2009

 

Pool in Flash

Pool in Flash

I am going to try to start releasing useful code snippets each week.  While I can’t guarrantee I can release a snippet every week,  I will try to write some interesting snippet and release it as often as I can.  The code snippets will range from game play to tool development.  So with that aside, lets begin!

Recently I have been working on a pool game.  I needed to be able to rotate the stick about the cue ball.  At first glance, to the untrained mathematician, this may seem like a complex task.  However it isn’t that bad… really!

 

Take a look at the following image,

As you can see,  when dealing with rotations,  the cosine determines where your point should be on the X axis while the sine determines where your point should be on the Y axis.  When you plug in a value into both the cosine and sine functions, you will get a point that is located on the circle’s edge or rather a rotation about the origin. If our cue ball is centered at the origin, and our stick is the diagnol line extruding from the origin,  all we need to do is increase/decrease the angle of the stick in order to rotate it around the cue ball or origin.  The following code shows how to do that in ActionScript 3.0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// We will want to rotate the stick based
// on the mouse's position.  So add an event listener
// for the mouse move event.
stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouse);
 
function handleMouse(evt:MouseEvent): void {
// Get the difference between the coordinates of
// the mouse and cue ball.
var dx:Number = evt.stageX - ball1.x;
var dy:Number = evt.stageY - ball1.y;
 
// Given the difference in xy coordinate positions,
// the atan2 trig function will return the angle between
// our stick and cue ball.
var fAlpha:Number = Math.atan2(dy, dx);
 
// Physically move the stick based on the Alpha angle
// Starting from the cue ball's origin,  rotate about the
// cue ball by angle "fAlpha" and then move 50 pixels
// away.
stick.x = ball1.x + (Math.cos(fAlpha) * 50);
stick.y = ball1.y + (Math.sin(fAlpha) * 50);
 
// Convert the angle from radians to degrees
// and change the stick's rotational value.
stick.rotation = (180 * fAlpha) / Math.PI;
}


Well that is it for now. Stay tuned for more useful snippets. If you have any particular requests, please feel free to email them to me.

Is this code snippet useful to you?

View Results

Loading ... Loading ...

Why I love Flash and ActionScript 3.0

Friday, January 9th, 2009
Adobe Flash
Adobe Flash

I remember playing around with flash about 8 years ago.  I didn’t care much about the platform at the time become most people were running on dial up modems.  They would need to download the plugin which was a little bloated.  Not to mention,  ActionScript 1.0 and 2.0 were very buggy and unorganized.  I quickly told myself that I would never use flash again and I became very skeptic toward any product macromedia released.

 

Over time,  I would get tired of writing demos or game prototypes in C++ and Java because of the shear time it took to get the wheels turning.  When working at Motorola, I saw the human computer interaction people using it because of its ability to prototype designs quickly.  I decided to give flash another chance.

I decided to port a mini game I wrote for the gameboy advance to flash so that the game could be made available for a wider audience.  It was very easy to port however I was dissappointed to find that ActionScript 2.0 was buggy and a little troubling at times.  Again I decided to leave flash behind.  Laster after ActionScript 3.0 was released with the Flash 9 player,  I had heard rumors that the structure of ActionScript had been rewritten from the ground up.  ActionScript 3.0 had finally become a real object oriented language.  Not only that,  the ActionScript 3.0 virtual machine had been greatly optimized in respect to the ActionScript 1.0/2.0 virtual machines.

Over the past year,  I have become more and more eager to use flash for my projects.  Currently at Kaneva,  I am kind of a flash evangelist.  I am able to prototype ideas quickly and efficiently with ActionScript 3.0.  Flash allows a developer to dive straight in designing your game vs reinventing the engineering wheel.  Flash is also an extremely powerful platform.  Its use of vector graphics, bitmap filters, web cam support, ease of intialization, media loading/streaming capabilities, xml parsering, and much more allows a developer to focus more on the look and feel of the game.  I am now usually able to prototype a game idea in a few hours as opposed to weeks.  While testing flash for our game engine,  we found that the ActionScript 3.0 virtual machine uses about 70% less cpu than the ActionScript 1.0/2.0 virtual machine.  So a developer is actually rewarded for taking advantage of the plethora of new features available in ActionScript 3.0.  

Some developers are still a little hesitant to make the switch from ActionScript 2.0 to 3.0 for fear of the new language and syntax.  Granted,  ActionScript 3.0 does force the developer to take a few more steps code wise to do somethings that where very quick in ActionScript 2.0.   Take the example of a button.  Imagine you want to print a message when the user clicks a button. The following shows how you would do it in ActionScript 2.0 vs ActionScript 3.0.

1
2
3
4
// ActionScript 2.0
on(release) {
trace("You clicked me!");
}
1
2
3
4
5
6
// ActionScript 3.0
btnClickMe.addEventListener(MouseEvent.CLICK, handleClick);
 
function handleClick(event:MouseEvent):void {
trace("You clicked me!");
}

Now at first glance,  ActionScript 3.0 may look a little more convoluted.  Remember that ActionScript 3.0 is object oriented though and you can turn on and off the event listeners at your own discretion.  The price of writing a couple more lines of code is nominal when compared to the benefits that ActionScript 3.0 provides.  Plus,  I just gave you the source code… so copy and paste  it :wink:

For those of you who are interested in using ActionScript 3.0 but don’t want to pay the $700 price tag for Flash,  then use FlashDevelop!  Adobe has released the Flash SDK ActionScript 3.0 compiler free of charge.  So all you need to do is download the free FLEX SDK (Comes with the ActionScript compiler) and then get yourself the free FlashDevelop IDE.  

If you would like to read a good book to give you a good foundation of ActionScript 3.0 and the Flash platform,  I would highly recommend the following two books.

 

Learning ActionScript 3.0: A Beginners Guide

Learning ActionScript 3.0: A Beginner's Guide

   

 

Object-Oriented ActionScript 3.0

Object-Oriented ActionScript 3.0

Which version of ActionScript do you prefer?

View Results

Loading ... Loading ...