Flash CS5 Built-in Physics Discovered

Does anyone remember the “Physics Panel” and the new Physics API that was supposed to be part of Flash CS5? Adobe mentioned it at their Adobe Max 2009 session. Doesn’t ring a bell? Just in case you forgot here’s a reminder: When Flash CS5 came out in April we were excited to check out the new features, especially the built-in physics. When the first trial versions were released a couple of weeks later, we rushed to the downloads page. Thirty minutes later Flash CS5 was installed and our hopes were sky high as we stared at the loading screen. To our surprise there was no Physics Panel. “Maybe because its a trial version” we thought. “At least there’s still the API, that’s the most important part” we said. Once again we were disappointed when a Physics API for Actionscript 3 was no where to be found. It seemed like Adobe completely abandoned one of the most awaited features in Flash CS5. This was made even worse as Apple had announced their banishment of the iPhone Packager (A ban which has been lifted since then), leaving the only new feature to be…code snippets. We went back to CS4, leaving to rest of the CS5 trial period to rest in peace. Back in June (according to AIM logs on June 1st) a friend of mine over at slanko.com and I were discussing this crazy decision Adobe had made and decided to open up Flash CS5 once again. A few hours and a sunrise later we had discovered the undocumented API with the help of intelliSenseand nemo440. “Nemo 440 can read SWF files compiled with Flex 2/Flex 3/Flex 4 and translates ActionScript 3 byte code to more understandable text dump.”

This is what the Physics library (located at /Applications/Adobe Flash CS5/Common/Configuration/ActionScript 3.0/libs/PffLib.swc) looks like opened in Nemo440.

With this tool you can see how easy it is to find out which classes are in the library, how many functions each class has, and how may parameters there are for each function along with their data types. We made “aswome.swf” (yes we see the typo) to test out the new API (Requires Flash Player 10 or higher).

The source code is below. WARNING: There are some expletives in the source that express our frustration during this sleepless night of discovery but don’t let that fool you, we still love Adobe.

<pre lang=”actionscript”>
//You always need a PhysicsManger.
var physm:PhysicsManager = new PhysicsManager(stage);

//You need a world to place all of your PhysObjs in.
var fuckAdobe:World = physm.createWorld();

/**
*PhysObj is simply an object that can be manipulated by the physics engine
*You can assign any DisplayObject to a PhysObj, but if its shape is
*anything more than a simple box, there will be some weird results since it
*uses a bounding box for hit testing.
*/
var po:PhysObj = fuckAdobe.addPhysObj(test, new Point(1,1), 0, false);
var po2:PhysObj = fuckAdobe.addPhysObj(test2, new Point(1,1), 0, false);
var po3:PhysObj = fuckAdobe.addPhysObj(test3, new Point(1,1), 0, false);
var po4:PhysObj = fuckAdobe.addPhysObj(test4, new Point(1,1), 0, false);
var po5:PhysObj = fuckAdobe.addPhysObj(test5, new Point(1,1), 0, false);

//setNonMoving does exactly what you think it does
po2.setNonMoving(true);
po3.setNonMoving(true);
po4.setNonMoving(true);

//enableCollisions does exaclty what you think it does
fuckAdobe.enableCollisions(true);
/**
*Adds gravity to the world. The first parameter is the horizontal force.
*The second parameter is the vertical force.
*/
fuckAdobe.setGravity(new Point(0,200));

startButton.buttonMode = true;
startButton.addEventListener(MouseEvent.MOUSE_DOWN, start);

function start(e:MouseEvent)
{
/**
*you have to run updateAllObjects and step on an interval in order
*to run and see the results of the physics calculations
*/
this.addEventListener(Event.ENTER_FRAME, go);
}

function go(e:Event)
{
fuckAdobe.updateAllObjects();
fuckAdobe.step();
}
</pre>

Here’s a little more advanced test dubbed “aswome2″

Instructions: Use your mouse to change the horizontal forces. Source Files: aswome2.zip Nemo440 is an Adobe Air application developed by Vadim Melnik. If you want to learn more about the Physics library in flash CS5, head over to the downloads section of Melnik’s site and open up “Applications/Adobe Flash CS5/Common/Configuration/ActionScript 3.0/libs/PffLib.swc”. Of course windows users will find the flash CS5 folder in “Program Files”. Nemo440: Download Link We hope you mess around with the library and show us what you come up with flash cs5 physics!

If you enjoyed this article, please consider sharing it!
Icon Icon Icon

Related Posts