# MGED CMD simulate

BRL-CAD recently integrated a new command in its mged tool, the simulate command. To simulate a cube falling to the ground plane one should follow those steps in order to run it in the mged tool.

BRL-CAD is a CSG (Constructive Solid Geometry) program. The representation is done in a system of axis XYZ. The XY axis is the ground axis, so you should make sure later in the tutorial that your ground plane (referenced as "gp") is being parallel with this axis.

First open the mged tool, if you have BRL-CAD installed on your Linux system it should be easy, just type mged in a terminal and a mged terminal should open.

 ```brlcad@brlcad: mged ```

In the mged terminal select the File menu, and then the New... (file dialog button). A window will appear and a name for your new database will be required. For the purpose of this tutorial we will name the new database, sim1. Introduce the chosen name (sim1) in the "File name:" field and hit save. Now you have opened a new database. Next we want to add some geometry to the database. Something that we will have to simulate on. In the mged command window we will now type the following series of commands :

 ```mged> in Enter name of solid: cube Enter solid type: rpp Enter XMIN, XMAX, YMIN, YMAX, ZMIN, ZMAX: -1 1 -1 1 -1 1 ```

The "in" command creates new geometry, it allows the user to type in the arguments to create a shape with the name provided by the second argument, in the example above is the name provided when "Enter name of solid:" appears on the screen. The arguments of the in command can be provided one by one, as in the example above or in one single line, like in the example below:

 ```mged> in cube rpp -1 1 -1 1 -1 1 ```

We have so far a box, now we want to add a ground plane to our simulation. Now is the moment to remember that the XY axis is the ground axis, so out plane should be parallel to it, that's why we first introduce the lengths of the the ground plane and then we introduce the "thickness", in our case 2mu. The Z axis is up, so the -1mu and +1mu on it gives the "thickness" of our ground plane. The ground plane must be name "gp", that's the way the program knows that you are defining a surface that will play the role of the ground. After creating regions, the ground region will be named "gp.r" so that the simulation will know that it is actually the plane on which you want your objects to fall onto. (*mu = measuring units, by default it is mm)

 ```mged> in gp rpp -15 15 -15 15 -1 1 ```

You should see now 2 boxes on your screen, one perpendicular on the other one, with a little overlap between them. Using the sed command we will select the box and translate it 100 units on the Y axis.

 ```mged> sed box mged> tra 0 0 100 mged> accept ```

To be able to run the simulate command our geometry shapes should be grouped in regions to do this we will use the "r" command and create 2 regions for the box and the ground shapes.

 ```mged> r cube.r u cube mged> r gp.r u gp ```

Now we are ready to run the simulate command:

 ```mged> simulate 100 ```

After the simulation runs its course, we will use the "who" command to find out what objects are being displayed.

 ```mged> who ```

You should get the following answer:

 ```box ground sim.c ```

Now, we don't really want to display the box and the ground shapes, we already have them in the sim.c group so we will unload all the geometry(using the "Z" command) and will load only the simulate geometry(using the "draw sim.c" command).

 ```mged> Z mged> draw sim.c ```

Now to actually see the simulation we will raytrace it, using the "rt" command.

 ```mged> rt ```

To see the initial position of the cube just type :

 ```mged> draw cube.r ```

Now in order to create an animation from your simulation, you will have to run the script below. It simulates the cube falling on the ground every step for 300 steps and then it renders an image with the specified view (parameters between the 2 EOF lines). The -M option for the rt command specifies that you are going to provide a view script for a file, that's what you are doing between the 2 EOF lines. The -s800 option makes it possible for you to get an 800x800 pixels image. You can try and modify it, to get different size images. The pix-png command outputs a png file from the pix file that was previously rendered.

 ```#!/bin/sh # simulate.sh ######################################################################## for i in `seq 1 300` ; do echo "Simulating..." mged sim.g simulate \$i echo "Raytracing..." rt -M -s800 -o image\$i.pix \ \$*\ '/home/brlcad/brlcad/sim.g'\ 'sim.c' \ 2>> sim.log\ < image\$i.png rm -f image\$i.pix echo "image"\$i".png done" done ```

If you want something simpler, without specifying exactly the view script, you can run the next script, where only the elevation (in degrees is being specified). Also the dimensions of the output image are specified by the -w(width) and -n(height).

 ```#!/bin/sh # simulate.sh ######################################################################## for i in `seq 1 300` ; do echo "Simulating..." mged sim.g simulate \$i echo "Raytracing..." rt -e 35 -w 640 -n 480 -o image\$i.png sim.g sim.c done ```