Flash and JavaScript 3D with Sandy-HX - Physics

Page content

JavaScript has not been used much to create multimedia application: it’s only now, through the new Canvas, Video and Audio tags that are part of the upcoming HTML 5 standard, that JavaScript has had the ability to directly manipulate multimedia objects. This means that there are not a large number of 3rd party libraries that multimedia JavaScript developers can build their work off. But by using Haxe, and some of the libraries that support it, to create JavaScript applications, you get access to some very cool libraries. One of these is the Wow physics engine.

Physics engines have become quite popular with the latest generate of video games, and C/C++ developers have a huge range of physics engines to choose from. JavaScript developers have a few 2D physics engines to play with, but very few (if any) 3D physics engines. By using the Haxe Wow library in conjunction with Sandy-HX we can quite easily bring 3D physics to JavaScript.


The EngineManager is modified to create a new WOWEngine object, and to call its step function during the render loop to update the physics simulation. [code]


The Wow objects that are used to calculate the physics are separate from the Sandy-HX objects that display a 3D model on the screen. The PhysicsObject class is used to synchronize these two objects, so the physics calculations done by the Wow engine are reflected in the 3D scene through the positioning of the Sandy-HX objects.

The new function takes a WParticle (the base class for all physical objects) and adds it to the WOWEngine created in the EngineManager. It then constructs the underlying MeshObject class with the supplied Shape3D. [code]

The enterFrame function has been overridden to synchronize the position of the Wow physics object with the position of the Sandy-HX 3D model. You will note that the y axis is reversed in the Wow physics engine, and so the negative is supplied to the Sandy-HX model. [code]


The PlanePhysicsObject extends the PhysicsObject class to create a fixed horizontal plane that will serve as the ground for our demo. The fixed property on the WOWPlane object is set to true, which means that it will not move as a result of gravity or any collision.


Like the PlanePhysicsObject, the SpherePhysicsObject also extends the PhysicsObject class. The SpherePhysicsObject creates a sphere with a random position in a defined area. This sphere is not fixed, and so will fall with gravity and collide with other objects. After 10 seconds the SpherePhysicsObjects all reset themselves to a new random position.


The ApplicationManager creates one PlanePhysicsObject, to represent the ground, and a number of SpherePhysicsObjects to fall down and bounce around.

The Wow physics engine is a very simple, but effective, way to create a physical 3D simulation. While the JavaScript version does push the limits of what is possible with the current JavaScript engines (even Chromes mighty V8 engine tends to slow down with more than a few balls colliding), you can still create simple, realistic 3D physics simulations.

Check out the online demo here, and download the source code here.

Read more in the Flash and JavaScript 3D with Sandy-HX series