Comments
Gabor Liptak wrote: Please share an invite at gl2002 [@] freemail.hu Thank you
Cloud Expo on Google News

SYS-CON.TV

2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Click For 2008 West
Event Webcasts
Snow Problem
The Physics of Havok

So here's the deal: you want to create a snowboarding game. You have three weeks to do it, it can't exceed 1MB, and it must work online on at least 80% of kids' PCs. So let's list the options open to us. That's going to be one short list: boot up your copy of Director MX 2004 and get started! Before we get our boots on and set off for a great half-pipe session on our favorite board, we're going to need some assets. Here at Catalyst, we use Lightwave 3D for 3D asset creation. For most games that you create using Shockwave 3D, you will benefit by controlling at least some of the interactions using Havok. This remarkable Xtra is a rigid body physics simulation. If that sounds like a complicated thing and your eyes are beginning to cloud over, then wake up, because this is one to try.

Trust me on this; you need to know this stuff. It's tricky at first, but the results are nothing short of amazing and before long you will be creating physics-based games with the best of them. In the next few pages we will start with an introduction to physics simulations followed by an overview of Havok in particular. Finally, we will look at applying this to an actual snowboarding game that is available online. There's lots to learn, but once you have mastered the basics, you will find that Havok looks after things in your game that, even after weeks of complex coding, would otherwise never have looked right.

What Is Rigid Body Physics?
A rigid body physics simulation takes the meshes in your scene, then gives them weight and friction, so if one hits another, it will react in a way that is a close mirror to what happens in real life, except no deformations of the object will occur. The bodies will always hold their shape, but they may go tumbling off into the distance.

For most 3D worlds, the actual size of the world is not important. However, when you start playing with physics, size is very important. Units in Havok are, by default, expected to be in meters and kilograms. The usual value for the force of gravity is 10ms2 (meters per second squared), which means that if the object starts from being stationary, after one second it will be travelling at 10ms. If you have an object that is about a meter square and give it a weight of around 10 kilograms and then drop it from a height of 3 meters with a force of gravity of 10ms2, then the way it tumbles will be just how you anticipated; it will hit the floor in less than a second and bounce or tumble depending on settings that you apply to the object to initialize it. If, however, the object is 100 units square and is dropped from 300 units high with a similar gravitational force, then you may think that the speed is wrong; in actual fact, you are dropping a large warehouse from 300 meters in the sky, not a small crate (see Figure 1). The distance travelled is given by the formula

d = vt + 1/2 at2

Because a rigid body physics simulation has to work at an alarming rate, it approximates a huge number of the calculations to speed up the processing. Nevertheless, you should never give a physics simulation more work to do than it needs. Suppose you are using a physics simulation to move a car that is made from 1200 polygons. The car will look beautiful, but the same motion can be achieved by using a very simple substitute for the car. Look at Figure 2, where we show a car and the proxy object that can be used by the physics simulation to move the car. The proxy contains less than 100 polygons, whereas the car contains several thousand. The technique is to make the proxy invisible, to let Havok move the proxy, and then to copy the transform matrix of the proxy to the actual visible car model. A rigid body simulation at some stage in the calculations gets down to examining each vertex and triangle in the object. If we can get these down, the performance will soar.

A common problem with a physics simulation is the time interval used in allowing impossible things to happen. In Figure 3, we see a ball going straight through a wall when it should have bounced off. Havok knows nothing about your world in the way you imagine it. It does know about time. If the number of frames updated a second is 25 and Havok calculates the positioning of the ball just prior to hitting the wall at 1.0 seconds, then just 0.04 seconds later it calculates that the ball is happily on the opposite side of the wall from where it appeared just a short interval before. As far as Havok is concerned, that's okay. It doesn't "realize" that the ball passed through the wall, because in the time intervals where it does do calculations, the ball is found to be in an acceptable position. There is a way out of this problem that does not require a computer capable of displaying 200 frames a second. Instead, we tell Havok to calculate sub-steps. If we tell it to calculate three sub-steps, then the very different behavior illustrated to the right of Figure 3 will occur. Although only two screen updates occur, three additional intermediary calculations ensure that Havok is kept abreast of the situation. It is possible to adjust the number of sub-steps to suit your application. You should aim to use the minimum number of sub-steps that avoids physics errors. Each additional sub-step takes computational time; therefore, by minimizing the number you will get the optimum frame rate performance.

Creating a Scene that Uses Havok
The initialize method is the most important when you use Havok. It starts the simulation and the parameters passed greatly affect the appearance of the simulation. There are two ways to define physical information for the Havok Xtra: The first method for creating physical simulation information is through a modeling tool. The modeling tool you use must support exporting .hke files. You can import this .hke file as a movie cast member using the File > Import menu options. .hke files already contain world scaling information and tolerance (as specified within the 3D modeler), so you don't have to supply this value when initializing. 3DS supports .hke files but Lightwave does not.

For the snowboarding game, we had to use the second method to create physical information where we use the models within a 3D scene to define the physics information. In this case, you must create a blank Havok cast member using the Insert > Media Element > Havok Physics Scene menu option. It is very important that you establish the scale of the physics scene from the start; as we discussed earlier, scale controls how the simulation matches real world experience. Internally, the Havok physics simulation employs the metric system (i.e., the default unit is meters). A W3D cast member may have been created in any number of world units (meters, inches, feet, user, generic). The Havok Xtra interface can work with the same units as this W3D cast member. However, in order to perform the proper simulation, Havok Xtra must know the correspondence between the display (3D scene) units and the simulation units.

You must provide a world-scaling factor when initializing the physical simulation. For example, if you designed a scene using inches, then you would supply a scaling value of 0.0254 (1 inch = 0.0254 meter). Be aware that any values in the scene (like gravity, rest length of springs, etc.) are interpreted as scene units rather than internal physics units. That means that a real-world gravity value of 9.81 meters/second2 would have to be set as 386.22 inches/sec2 if working in inches.

You must also provide a collision tolerance parameter. This tolerance is used to determine when objects are touching (i.e., if they are closer than the tolerance). In general, higher collision tolerance values yield more stable simulations. However, setting too high a value could lead to noticeable gaps between stacked objects. So it is recommended that you set the collision tolerance to the highest value at which it does not visually affect the scene. Figure 4 shows how tolerance affects the positioning of objects that stack.

If a scene consists of many objects in a room (crates, tables, chairs, etc.), a tolerance of around 0.1m should be fine. However, if the objects in the scene are dice on a table, a smaller tolerance - say 0.01m or less - is preferable. If the objects are cars or buildings, a higher tolerance applies. If no value is supplied, then the default tolerance of 0.1 is used. As a general rule of thumb, the tolerance value should be set to around 10% of the scaling factor used in the simulation. Collision tolerance is a value measured in scene units. That means that the scaling factor affects its actual value. havok.initialize() must be the first Havok function called or other Havok functions will have no effect.

Defining the Physical Properties of Your Models
The function havok.makeMovableRigidBody(modelName, mass, isConvex) creates a movable rigid body with mass, mass; and name modelName. When you create a rigid body, you need to decide whether your geometry is concave or convex. A model is convex when it has no hollows. Havok will create a convex version of your model if you choose simply by choosing true for isConvex; this is called a convex hull. It is possible to use the convex hull of a model as the physical simulation in many cases. A convex hull does not work when it is important in your simulation for objects to drop into a hollow. In the snowboarding game, the slope has sides; if we used the convex hull then the sledge would sit on an imaginary surface at the top of the sides. Figure 5 shows the effect. Clearly this is not as intended so you would not use the convex hull. It is easier and faster to use convex geometries to resolve collisions, so you should use them wherever possible. When you are creating a rigid body from a model, you must add the meshdeform modifier to the model, i.e., model.addModifier(#meshDeform ). Otherwise, the Havok Xtra cannot access the geometry of the model.

If you have an object in your scene that needs to be part of the physics but you know will never move, then use havok.makeFixedRigidBody(modelName, isConvex). This function creates a fixed rigid body from a model of name modelName and adds it to the simulation. Fixed rigid bodies never move, but are still involved in collision detection. These are mostly used for scenery elements like walls. Fixed bodies do not have mass. Mass is a property that only makes sense for objects which are free to move.

To set up the simplest Havok scene follow these steps. We will use Lingo to provide all the physical simulation information rather than a prepared HKE file.

  1. First we import the 3D world: Click the File menu, select Import, and select the required Shockwave 3D (W3D) file from the dialog window. This adds the 3D cast member to the cast. This may then be dragged onto the stage.
  2. To create the empty Havok simulation: Click the Insert menu and select Media Element>Havok Physics Scene. This adds an empty Havok cast member to the cast.
  3. Now add a Physics (No HKE) behavior to the cast (from the Havok > Setup behavior library): Playing the movie at this stage will show a static world, i.e., we have not associated any rigid bodies with the 3D models in the W3D file.
  4. Now we need to create the Havok simulation information by editing the Physics (No HKE) behavior. The Lingo function shown in Listing 1 creates two rigid bodies that are added into the Havok simulation and associated with two 3D models from the W3D file, "Box01" and "Text01". The first is a fixed convex rigid body, i.e., one that may never move during the simulation. The second is a concave movable object. Listing 1 is from the sample "helloworld.dir" in the support files for this article. Running the sample, you will see the lettering fall to the floor surface and bounce and tumble in a very convincing manner. All this is done with just the lines of code you see in Listing 1. The "Havok Physics (No HKE)" takes care of initializing the physics using the following parameters.


Havok cast member	2
Tolerance		0.1
Time Step		0.03
Sub Steps		3
Scale			1.0

The scale is set to 1.0 because the geometry was created using meters as the scale. The tolerance is set to 0.1 of a meter. Try adjusting the tolerance and scale to see how it affects the scene. You could also try adjusting the gravity vector set in line 5 of Listing 1.

Adding Keyboard Control
In this example, we will build on what we have learned so far. Open 'sledgedemo.dir'. Unfortunately, the geometry for this example was produced at completely the wrong size, but it illustrates how you can set up even a badly prepared 3D world to use Havok by adjusting the values for tolerance, scale, and gravity. In this instance, tolerance is set to 20, because at the scale of this world, that represents a small gap around the sledge. Scale is set to 0.025 and gravity to (0, -10000, 0)! Nevertheless, by using these values you can create a usable simulation.

As in many games we produce, the main work of the demo is in a main loop, which in this case is the enterFrame handler of the 3D sprite behavior '3D Behavior'. Listing 2 shows the script. Starting with line 2, we call a simple keyboard reader. This stores whether or not the arrow keys and the space key are currently pressed. The values are stored in the property variables leftkey, rightkey, upkey, downkey, and spacekey. In this world, there are two rigid bodies, the course and the sledge. Notice that the sledge has two strange spikes sticking up. These help handle situations where the sledge is inverted. Once the demonstration is complete we can make the sledge object invisible and replace it with a visible alternative. We did this for a popular game at www.pingu.net; the crazy sledging game uses Listing 3 to handle most of the behavior of the sledge. The proxy object being used to control the sledges regardless of the choice of character is exactly the one shown in Figure 7.

A rigid body has many additional properties and methods. To find out the full details, consult the Havok documentation either from your CD or from the Web site given earlier. One property is used in line 4 of Listing 3 - the linear Velocity; think of it as speed. Since we are going downhill, the y value should decrease so the difference between the previous y position and the current y position should be positive if we do the calculation shown in line 5. If it is negative, then we are trying to sledge uphill so we can use this to inform the player that they are going in the wrong direction; the code between lines 8 and 18 handles this, in this case simply by forcing the sledge to stop so the user cannot go too far in the wrong direction.

A Practical Example
A Word of Caution

Before you can use Havok, you need to ensure that it is set up correctly. Unfortunately, because of a time limit passing on the inclusion agreement, the Havok Xtra is not, at the time of writing, part of Director MX 2004, so you may have some problems in this regard. Macromedia is trying to reach an agreement with Havok to make the Xtra available to Director MX 2004 users, so by the time you read this, it may be on the CD or available as a download. Or, you can install Shockwave10 from the CD and then navigate to the Macromed folder. On a Windows machine this will be in the folder 'Windows\System32\Macromed'. Inside this folder is a 'Shockwave10' folder that contains an 'Xtras' folder. The file 'Havok.x32' should be in this folder. Once you have located the Xtra, you need to copy it to your Director MX 2004 installation folder. On a Windows machine, this will be in 'Program Files\Macromedia\ Director MX 2004\Configuration\Xtras'. Create a folder called 'Havok' inside this folder and then copy 'Havok.x32' to this folder. You will also need the documentation and the cast libraries which you can get from http://oldsite.havok.com/xtra. Place the CastLibs in a Havok folder inside the 'Configuration/Libs' folder. The documentation can be placed wherever it suits your needs. Once you have added these files, you will need to restart Director MX 2004 before the xtra will be available for use.

Line 21 is where we use Havok to control the motion of the sledge. Instead of simply moving the sledge we give it a nudge using the rigid body method 'applyAngularImpulse'. This uses a vector to apply rotational motion to the rigid body. In the example at line 21, we use the previously stored property variable pTurn to give a rotation in the y axis. The left and right keys are used to control the rotation while the up and down arrows control forward and back motion. Lines 27 and 28 show how this is achieved; again we use a method of the rigid body, applyImpulse, that takes a vector and applies a force defined by the vector. To move the sledge, we want the force for a forward motion to be along its z axis, so we first get the z axis of the sledge, called board in line 27. This is scaled up by a boost value that is increased or decreased in lines 14 to 18.

The behavior of the sledge would be highly erratic if we left it at that, so we must dampen down the effect in a separate handler, dampenPhysics, which we will look at in the next section.

Controlling the Results
Physical simulations are great unless the behavior does not accurately suit your application. In this case, you must try to control the behavior. One thing you cannot do is place a rigid body directly. You must use Havok's methods to achieve the ends you desire. Some tips are shown in Listing 3. Lines 3 to 12 get the height of the ground directly below the sledge. Lines 15 and 16 show how the rotation of the sledge is reduced by applying a damping affect which is the opposite of the sledge's angular velocity. Lines 19 to 28 stop the sledge sliding too much in the x axis. We want the sledge to appear as though the runners hold the snow so it is only happy when going forwards or back. The physics simulation will slide to the left and right just as easily as it will slide forward or back so we need to stop this sliding action. This should only happen when the sledge is on the ground - boardHeight<100 - not in the air. At line 21 we get the linearVelocity of the sledge. This is a vector giving a speed in the x, y and z axes. We use the vector method normalize to get a vector of unit length. Line 24 uses the vector method 'dot' to find how much of the vector is aligned with the sledge's x axis. The result is a single value that we then map to the x axis by multiplying the x axis by this value in line 25. We now have a vector that expresses the direction we need to use for dampening the sideways slide. By multiplying this by the magnitude of the linear velocity and a previously stored scaling factor 'pSlideDamping' that we can use to give more or less sideways slide we can finally, at line 27, apply this to the sledge to minimize the sideways slide.

Another thing we need to check is whether or not we are upside down. In any simulation, this can happen. To do this, we use the dot product of the sledge's y axis with the default y axis (0, 1, 0). If this is less than 0.5, then we are currently upside down. Rather than react immediately to this, we simply update a counter. This lets us adjust how quickly we reset from such a situation. In lines 42 to 48, you can see that we allow a count of 20 before attempting a correction. To do the correction, we take a default transformation matrix, set the position of the matrix to the current sledge position, then move this up a little to ensure we are above the snow. Then we use, in line 46, the 'interpola-tingMoveTo' method of the rigid body. This takes 2 parameters, position and orientation, expressed as an angle axis list. If the rigid body can be moved without interpenetrating other rigid bodies in the scene then the body is moved; if not it remains unmoved.

A Real World Example
The ideas given in the sledging example were extended to controlling a snowboarder in a game we market as 'SnowboarderXS'. In this game, which is available from the support files at 'SnowboarderXS.html' (see Figure 8), you choose one of two characters and courses and then slide down the course, jumping ramps, collecting stars, and performing tricks. The control of the snowboarder uses a proxy object that operates identically to the sledge demo.

The Havok Xtra is a fantastic tool for creating high-quality online games. In this article, we covered the basics of using the control then moved on to some of the more complex ways you can control the simulations. Always remember that the most important starting point for a simulation that uses Havok is to either bring in your geometry scaled to meters, or set up the scaling parameters to suit your geometry. Okay, boots on, bindings fastened, let's get some air!

. . .

The source code for this article is online at www.sys-con.com/mx/sourcec.cfm.

About Nik Lever
Nik Lever runs Catalyst Pictures Ltd (www.catalystpics.co.uk), a Manchester, UK-based company that makes online games for clients including Kellogg's, BBC, and Cartoon Network. Originally trained as an animator, Nik now writes code more
often than he scribbles and is the author of several Flash and Director books (www.niklever.net ). When he's allowed out into the light, he likes to go sailing across the Irish sea.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Brilliant discourse, Nik. Havok is our friend, and Mr. Lever has proven it once again. I hope Macromedia and Havok work out their differences. Articles like this one prove they should try.


Your Feedback
mike kelly wrote: Brilliant discourse, Nik. Havok is our friend, and Mr. Lever has proven it once again. I hope Macromedia and Havok work out their differences. Articles like this one prove they should try.
Latest Cloud Developer Stories
CloudBench Applications, Inc. announced its financial results for the three months and nine months ending September 30, 2009. All amounts are stated in Canadian dollars unless otherwise noted. Revenues from BasicGov, the Company's cloud computing solution for local government, gr...
The new contract is an industry first, with CSC being the first Microsoft partner to lead and win a cloud computing services agreement of this scale. Under terms of the contract, CSC will provide Royal Mail Group's 30,000 employees with access to new IT services using Microsoft's...
Operates in over 170 countries and is one of the world’s leading providers of communications solutions and services. Richard Tarboton talks for MeettheBoss.TV on his role as Head of Energy & Carbon for BT and what they are doing towards reducing carbon emissions.
CA is going to put its Agile Planner software on salesforce.com’s Force.com platform in the first half to accelerate development time and give users visibility over their development initiatives to reduce time-to-market. Customers are supposed to be able to accelerate the deploym...
Despite its uncertain fate Sun soldiers on. Monday it trotted out a cloud-based multiplatform desktop as a service for K-12 and community colleges that can run Windows, the Mac OS, Linux and Solaris applications to nearly any client device, including its own Sun Ray thin clients....
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE

Breaking Cloud Computing News
CloudBench Applications, Inc. announced its financial results for the three months and nine months e...