Previous topic TOC

First simulation

Next topic

Simulation In order to simulate a scene, we have to define what material has to be used for each object. Furthermore, we have to set up the simulator.
Material In order to calculate the mechanical properties for each object, each object should have assigned a Material. A material object holds parameter information about density, friction and collision. Example:
// define a box
var b = Box(10,10,10);		              
// assign material to the box
b.material = Material(1e-6, 1, .5, .4); 

The material has a density of 1e-6 mass-units per volume-units, Other parameters are explained later. When we have more objects, it is easier to use the factory object:

// assign a default material 
factory.material = Material(1e-6, 1, .5, .4); 
// define a box, it will use the default material 
var a = Box(10,10,10);	
// define a box, it will use the default material also	            
var b = Box(10,10,10);		            

When an object is created, it will assign all its properties to values that are set in the factory object. In other words, the factory objects controls the default settings of objects. When an object has assigned a material, ThreeDimSim will calculate its mechanical properties: volume, mass, and the inertia tensor.

simulator object

The simulator is accessible by the predefined 'simulator' object. This object keeps parameter information and supports the Run method that will initiate the actual simulation. Example:

// create a material
var m = Material(1e-6, 1, .5, .4);
// define a box   
var b = Box(10,10,10);	
// assign material to the box	   
b.material = m;	  
// simulate for 1 time-unit (seconds )                 
while (simulator.time < 1)	   
{
	WaitFrame();		   // synchronize screen
	simulator.run(.01);	   // simulate .01 time-unit
};

Notice the simulation is in essence the same as the animation loop described in the previous chapter. The difference is that objects are not moved by script, but by simulator.

gravity property In this example, the box will slowly move downwards. This is due to the default (uniform) gravity field applied on the scene. The gravity is defined by the scene.gravity acceleration vector and set to {0,-9.81, 0}. Note this setting assumes a distance-unit of meters, and a time-unit of seconds. You can change this setting by assigning a different vector to the scene.gravity property.
Collision Two objects may collide, and this is simulated as well. Example:
// create a material for default use
factory.material = Material(1e-6, 1, .5, .4); 
// define two boxes  
var up   = Box({10,10,10}, {8,12,0});         
var low  = Box(10,10,10);		
// the lower one will not move	        
low.static = true;			      
// simulate for 3 seconds 
while (simulator.time < 3)	   
{
	WaitFrame();		   // synchronize screen
	simulator.run(.01);	   // simulate .1 seconds 
};
The lower object immobilized by setting its property static to true. Many times we will use a big static box that will serve as a floor for the scene. The collision is modeled by a spring/damper system for each collision. When objects intersect, the precise intersection is calculated. The collision location, velocity, volume and collision plane are derived and used to calculate the reacting collision forces and friction forces. The material provides parameters for the amount of reacting collision force (spring), the amount of dampening and the static and dynamic Colomb friction parameters. When two objects collide which have different materials, you have to define the friction  parameters for the material combination by using the setFriction method of a Material.