Rendering FOAM
I’m sorry that this took so long- especially those of you who’ve written me and patiently waited for my help with rendering… I simply don’t have as much time as I would like. Anyway- this will offer a solution for rendering your FOAM physics simulation with:
- Drawing API (SimpleFoamRenderer)
- BitmapData (BitmapFoamRenderer)
- DisplayObjects (DisplayObjectFoamRenderer)

Here’s a very silly demo- Mouse down anywhere to switch from the DisplayObject renderer to the Bitmap renderer.
demo
source
Note that these new renderers are meant as example only- I do think in most cases the developer will need a more specific setup than I could hope to generically offer. So consider this my implementation and at best a nudge in the right direction.
Hopefully both of these will be fairly self-explanatory, but I’ll briefly go over the DisplayObject renderer and what my thinking was when addressing this issue.
I wanted a means to render a DisplayObject (base class of Sprite, MovieClip etc.) that’s the visual representation of a RigidBody being simulated. I knew that I’d like to be able to pass either the DisplayObject’s Class or the instance itself. I also knew that I’d need an easy way to adjust the position of the DisplayObject- so first, I wrote a datatype to hold these properties- DisplayObjectData.
You’ll see that it has a setter for defining its displayObject property. This is what facilitates passing either a DisplayObject Class or a DisplayObject instance. The other properties:
- offsetX - the amount to offset the DisplayObject from its origin in the x direction
- offsetY - the amount to offset the DisplayObject from its origin in the y direction
- autoCenter - Boolean indicating whether to center the contents about the origin- Because of the way a RigidBody rotates about its center of mass, I figured this would be typical.
- hasBeenDisplayed - a helper Boolean for determining whether to perform the indicated transformations
In Rocket.as, you can see the way I’m embedding my visual assets:
[Embed(source="../../../bin/images/rocket/lunarPod.png")] private var rocketSpriteClass:Class; [Embed(source="../../../bin/images/rocket/asteroid.png")] private var asteroidSpriteClass:Class;
If this is unfamiliar with you, simply note that it’s analogous to the classes you’re instantiating as MovieClips in Flash CS3. As you’ll see in a moment, it’s not necessary to pass the Class, we can pass the DisplayObject instance as well.
Here’s how I’m passing the class to FOAM to tie it to to a RigidBody. Note that the instance of DisplayObjectData simply defines Foam.addElement’s renderData argument.
foam.addElement( rocket, true, true, new DisplayObjectData( rocketSpriteClass, 0, -5 ) );
Compare this with how I’m creating the asteroid (I wanted to scale the asteroid, or else I’d have created it in the same way as the rocket- it worked out well for the purpose of demonstration):
var asteroidSprite:DisplayObject = new asteroidSpriteClass(); asteroidSprite.scaleX = asteroidSprite.scaleY = size / 64; var asteroid:Circle = new Circle( sx, sy, size, size * 4, svx, svy, 0.5, 0.25, 0, -0.1 + Math.random() * 0.2 ); foam.addElement( asteroid, true, true, new DisplayObjectData( asteroidSprite ) );
There’s one last important thing to note in the Rocket example class. Because I don’t want to use the default FOAM renderer, I have to tell it which renderer to use:
foam.setRenderer( new DisplayObjectFoamRenderer() );
Now let’s move on to DisplayObjectFoamRenderer. Note that I’ve chosen to extend SimpleFoamRenderer so that I don’t have to rewrite drawing routines for objects I don’t have graphics for (such as the mouse dragger bungee). You’ll see my use of the DisplayObjectData properties in the addRenderable method.
So- ultimately all of this boils down to 3 lines of code which line up our DisplayObject with our RigidBody:
displayObject.x = ISimulatable( renderable.element ).x; displayObject.y = ISimulatable( renderable.element ).y; if( renderable.element is IBody ) displayObject.rotation = IBody( renderable.element ).q * 180 / Math.PI;
It places the DisplayObject at the x and y coordinates of the RigidBody’s center of mass. Because rotation is given in radians, we have to convert to an angle which is what DisplayObject.rotation expects.
And that’s pretty much it; for the most part a FOAM renderer is bookkeeping. It might suit you far better to just maintain reference to your DisplayObject and update it every frame just as I am in the above 3 lines of code… simply set render to false in Foam.addElement.
I can’t get the example to work.
In the document class i type: example.rocket.Rocket
When i publish it throws an error:
ArgumentError: DisplayObjectData.displayObject must be a DisplayObject or DisplayObject Class
at example.rocket.view::……….
Hi James,
I don’t have Flash CS3, so I’m not able to test that. My guess based on the Error is that the Sprite Classes from the example aren’t being transcoded properly. I’m fairly certain this doesn’t throw an error at compile-time in Flash. Double check that you have the embed tag actually points to the images its trying to transcode on your harddrive.
Great engine!
You should really think about setting up a forum for discussion.
Also, it would be nice if you created a general tutorial just on basic stuff. Like attaching two objects with a spring, and coloring an object, etc.
Great work.
hey, how would i check if the two objects are colliding, im tyring to make a physics based pong game just to learn the engine.
So i want to check if my ball hits one of the paddles so it can swap its direction.
Hi Jason,
Ideally you’d just let the engine handle all of the movement, ie: not worrying about when it hits the paddle. It’s just an alpha right now, and one big missing piece is event dispatching for events like collision. This will be addressed in the future however. If you’re comfortable augmenting the engine’s code, it is possible to accomplish by yourself- shoot me an email if you need any help in this respect.
This is looking amazing man!
Hey,
Are you still working on this? This stuff is really cool. I have looked at Ape and BOX2D and they are really hard to set up and understand the syntax. You use easy-to-understand syntax and its really easy to implement with limited amount of code needed on the front end.
Cheers.
Hey,
I just finished my final project for school and I used FOAM. Thought you might like to check it out.
http://www.pablosgarden.com
Thanks a lot for building a great physics engine and having great documentation.
Cheers,
Pablo
Hi Drew,
I’m having the same problem as James Brown is having, I have moved around with the location of the images and the changed the path’s accordingly but all seem to fail.
any clue?
To Kasper and James,
As far as I can tell, the way that Drew has embedded the images in the Rocket.as class doesn’t work in Flash, but does in Flex.
To solve it, either load the image a different way, or import the image into Flash and call it that way.
Great demo & nice tutorial. Will have to think about using this engine in a project. thx
Is there a way to improve the smoothing of bitmap display objects? I’ve modified the BitmapFoamRenderer class by setting the _bitmap variable to ’smoothing = true’ but it doesn’t seem to have done much at all.
Thanks for the code in the first place though, top stuff!
Nevermind, sorted! I just changed the bitmap in the library to allow smoothing - thought the BitmapFoamRenderer class was bitmapDataing the displayObjects - duh.
Hi. I’m trying to compile some examples, and to construct others, but it’s happening some errors.
I’m using Flash CS4, and the Vector class from AS3 is conflicting with the one from FOAM. I have changed the name of this class, and put Vecctor (with double ‘c’). The conflict is over, but another errors is happening…
I would like to know if I need configure the Flash CS4 to use the FOAM’s API. If true, how can I do this?
Regards,
Isaque
Hello.
I think I discovered the mistake. I’m using Flash CS4. When I use Flash CS3, that’s OK.
Now, I want to know if there is a solution to I use the FOAM with CS4.
Salutations,
Isaque
Hey, this is really good stuff, I need to look a little deeper but you might have just saved me writing my own lib, I’m not so keen on the structure of other libraries like box2d (although it’s great) and was about to embark on a rewrite and I had been looking at RK4 etc, this code just looks like it will fit my code style and workflow better. I’ll keep you checked on my progress as I look more at this stuff…thanks!