I remember the first time I played Jurassic Park: Trespasser. It was one of the first games to implement physics. I was in awe as I watched a create fall down the side of a mountain, realistically flipping and bouncing along the ground. Until that point most games didn’t implement any sort of physics. Times have changed though. Games like Half Life 2 make use of physics extensively as an integral part of the game play. Developers too (well, at least the C++ developers anyway) are also spoilt for choice when it comes to physics middleware engines with several free, high quality options to choose from.
Flash developers haven’t been left out in the cold though. A physics engine called WOW gives Flash/Flex developers a taste of real time, 3D physics. I say a taste because at this point WOW is in its very early stages. It has several limitations, like only detecting collisions between spheres, or between a sphere and a plane. Still, it does offer a promising glimpse of the future of real time Flash physics.
WOW only does the physics calculations, and requires a 3D engine like Papervision 3D to actually render the result to the screen. However creating and syncing both engines does not involve a lot of work. Here I’ll show you a very simple example of how to integrate the WOW and Papervision 3D engines. The Papervision 3D code presented here is based off the article Papervision 3D Programming Tutorial - Loading and Displaying a 3D Model, so I recommend you take a look at that article first.
Let’s start with the changes to the EngineManager.
Actionscript source code for EngineManager.as
Initialising and updating the WOW engine takes only an additional 3 lines of code. We instantiate a new WOWEngine object, add a massless force (to mimic gravity), and then call wow.step during the render loop.
The ApplicationManager has a few more changes.
Actionscript source code for ApplicationManager.as
In the startupApplicationManager function we create a new PhysicsMeshObject to represent the ground. We then call createBalls to create 10 sphere objects which will fall to the ground. I have structured the createBalls function to position the new balls so that one will always fall on top of another. This means that we can see how the WOW engine handles objects colliding. Finally we have the drop function where we set the fixed property of each the balls to false, meaning that they should be simulated in the physics system.
Actionscript source code for PhysicsMeshObject.as
The PhysicsMeshObject extends MeshObject to include a WOW primitive, which is the actual object that is simulated in the physics system. Remember that WOW and Papervision 3D are two separate engines. The Papervision 3D engine renders a mesh, while the WOW engine simulates the interactions between its own invisible primitives. The PhysicsMeshObject essentially syncs up the Papervision 3D mesh to the position of the WOW primitive inside the enterFrame function. One thing to note is that the y axis for Papervision 3D and the WOW engine are opposite, so we need to negate the y axis position of a WOW primitive before assigning it to a Papervision 3D model.
Check out the end result at here, and download the source from here.