Chapter 3
The Translating Cube
3.1 Introduction
The purpose of this chapter is to introduce MATLAB® users that are interested in
animating their physical system in a virtual reality environment to the virtual reality
tool, V-Realm Builder (VRML). This chapter will walk the new user through a
complete exercise on how to animate a physical problem governed by an equation(s)
of motion in VRML environment using M-script commands.
In Sect. 3.2, a simple dynamics problem of a cube that is subjected to an external
force will be presented. Newton’s second law will be used to derive the equation of
motion.
In Sect. 3.3, step-by-step instructions will guide the reader through drawing the
cube in VRML and changing its name, color, and size. A simple background will
also be added to the scene.
The x-, y-, and z-axes convention of VRML will be briefly explained in Sect. 3.4.
In addition, a simple familiarization experiment will be described to enable the
reader to better understand the coordinate system of VRML.
In Sect. 3.5, an M-script file that numerically computes the solution of the dif-
ferential equation (derived from Newton’s second law) will be developed.
To animate the virtual scene, an M-script file that uses the numerical solution
from Sect. 3.5 will be described in Sect. 3.6. Furthermore, a procedure to save the
animation as a movie file will be included.
The chapter concludes by an application problem of two colliding masses in
Sect. 3.7.
The electronic version of all the M-script files and VRML models in addition to
the recorded movies for the cube and the collision problem can be downloaded from
Springer’s web site [Link]
Matlab® is a registered trademark of The Mathworks, Inc.
N. Khaled, Virtual Reality and Animation for MATLAB® and Simulink® Users: 7
Visualization of Dynamic Models and Control Simulations,
DOI 10.1007/978-1-4471-2330-9_3, © Springer-Verlag London Limited 2012
8 3 The Translating Cube
3.2 Cube Problem
Given a cube of mass m = 1(kg) and of dimensions 0.5 ´ 0.5 ´ 0.5(m 3 ). The cube
is subjected to the following external force: Fz = -1 (N).
Using Newton’s second law, the following second-order differential equation for
the z-displacement (check Sect. 3.4 for coordinate’s convention) of the cube can be
developed:
Fz
mz(t ) = Fz Þ
z (t ) = (3.1)
m
The purpose of this exercise is to animate the solution of this equation for zero
initial conditions.
3.3 Creating the Virtual Scene for the Cube
To create the virtual scene for the cube, follow the following steps:
1. Open the V-Realm Builder and click on File > New to create a new virtual
world.
2. Click on the Insert Background button (Fig. 3.1) to create a background for
the scene.
3. Click on the Insert Box (Fig. 3.2) to create the cube. In the main view window,
you should see a cube with a green and blue background behind it. Notice that
on the left side of the main view window, some text and yellow icons have
appeared, all under the title Transform (Fig. 3.3).
4. Click once on the title Transform to select it. Click another time on Transform
and change the text to a meaningful name such as Cube (Fig. 3.4). Do not
double-click on Transform; you should wait at least 1 s after clicking the first
time. The purpose behind changing the name from Transform to Cube is to
allow MATLAB® to uniquely identify the object Cube. In other words, suppose
you wanted to have two cubes instead of one, then both of the cubes will have
the name Transform, and MATLAB® will not be able to differentiate between
both objects. In this case, you better name one cube as Cube1 and the other as
Cube2.
5. Click on the Color Mode button (Fig. 3.5).
6. Double-click on the Diffusive Color gray area (Fig. 3.6).
7. Click on the blue color or any other color you prefer (Fig. 3.7). Press OK when
done.
8. Click on any face of the cube, and the whole object will turn blue (Fig. 3.8).
Press Close when done.
9. By default, the size of the cube is 2 ´ 2 ´ 2 , and its center is positioned at the
origin (0,0,0) . To change its size, click the + sign to the left of Box and then
double-click on size (Fig. 3.9). Tick the x-axis box (Fig. 3.10) and change
3.3 Creating the Virtual Scene for the Cube 9
Fig. 3.1 Insert Background button is shown inside the rectangle
Fig. 3.2 Insert Box button is shown inside the inside the rectangle
10 3 The Translating Cube
Fig. 3.3 The text and the yellow icons appear under the title Transform
Fig. 3.4 Change the name of the cube from Transform to Cube
3.3 Creating the Virtual Scene for the Cube 11
Fig. 3.5 Color Mode button is shown inside the rectangle
Fig. 3.6 Diffusive Color gray area inside the rectangle
12 3 The Translating Cube
Fig. 3.7 Click on the blue area to choose the blue color
Fig. 3.8 Click on any face of the cube
3.3 Creating the Virtual Scene for the Cube 13
Fig. 3.9 The size property is shown inside the rectangle (under Box)
Fig. 3.10 Tick the x-axis box by clicking on the box in the rectangle
14 3 The Translating Cube
the value to 0.5 . Similarly, tick the y- and z-axes boxes, and change their
respective values to 0.5 . Press OK when done. Notice the change in the size
of the cube.
10. Click on File > Save as, and save it in MATLAB® working directory. Name the
file Cube_Virtual. The extension of your virtual reality file will be .wrl.
3.4 Cartesian Coordinates for the VRML
The plane of the screen is of two dimensions. In VRML, the x-axis is the horizontal
line of the screen and points to the right. The y-axis is vertical and points upward.
As for the z-axis, it is perpendicular to the plane of the screen, and it is pointing
toward the user (Fig. 3.11).
To experiment with the convention of the Cartesian coordinates of VRML, dou-
ble-click the translation property of Cube (Fig. 3.12). Tick the x-axis box and then
use the slider bar to vary the x-value. Notice that the positive values of x are moving
the box in the right direction. Tick the x-axis box to unselect it and then tick the
Fig. 3.11 Cartesian coordinates for VRML
3.5 M-script File for Solving the Differential Equation 15
Fig. 3.12 The translation property is shown inside the rectangle
y-axis box. Use the slider bar to vary the y-value. Notice that the positive values of
y are moving the box upward. Tick the y-axis box to unselect it, and then tick the
z-axis box. Use the slider bar to vary the z-value. Notice that that box is moving
closer for positive values of z.
3.5 M-script File for Solving the Differential Equation
To numerically integrate for the solution of the differential equation given in Eq. 3.1,
use the ode45 function in MATLAB®. For more information about this function,
type in the MATLAB® command window: help ode45.
The following M-script file, cube_solver.m, will numerically integrate the
differential equation given in Eq. 3.1. Some comments have been added to
briefly explain the M-script file. The M-script can be found in Chap. 3 folder of
the extra material of the book. The folder can be downloaded by following
[Link] and then searching for the book by entering the ISBN
or the title.
The above M-script file, cube_solver.m, calls myfunc.m which computes the
derivative vector [z(t )
z (t )] . The following is the script for myfunc.m which can
also be found in the extra material of Chap. 3:
3.6 M-script File for Animating the Virtual Scene
In what follows, the virtual scene, Cube_Virtual.wrl, will be animated based on the
solution given by cube_solver.m (the reader should run cube_solver.m before
running cube_animate.m), and the animation will be recorded into a movie file,
Cube_Movie.avi. The only parameter that will be changed in the virtual world is the
translation property of the cube.
A first look at cube_animate.m script might seem overwhelming and full of
commands; thereby the reader is advised to start writing his/her own code by
modifying an existing one instead of starting from scratch every time.
3.6 M-script File for Animating the Virtual Scene 17
The script that comes before the for-loop consists of two parts. The first one (refer
to Part 1 of cube_animate.m) deals with opening and viewing the virtual world. The
second (refer to Part 2 of cube_animate.m) deals with setting the recording of the ani-
mation to a *.avi file. In this example, the movie will be saved as Cube_Movie.avi.
As was mentioned before in this section, the only parameter that is being varied
in this virtual world is the translation property of the cube. Going back to Fig. 3.12,
the object that was named Cube has a translation property. The created virtual
world is named “world” in Part 1 of the code:
world = vrworld(‘Cube_Virtual’);
To change the translation property, assign a new value for the translation by
writing [Link] = [0 0 z(Index,1)];
After you run cube_animate.m, a video file Cube_Movie.avi should be cre-
ated in the same directory as the MATLAB® file. Try playing the movie to see a
playback of the animation. To increase or decrease the duration of the movie,
change the number of samples used for the numerical integration of the differen-
tial Eq. 3.1.
,[t0 tf]
18 3 The Translating Cube
pause(5);
Fig. 3.13 Dimensions for the boxes
3.7 Application Problem
Given two boxes of mass mB1 = 1 (kg) and mB2 = 2 (kg) and their dimensions are
2 ´ 2 ´ 2 (m 3 ) and 4 ´ 2 ´ 2 (m 3 ), respectively (Fig. 3.13). The initial velocity of box
1 (the smaller box) in the x-direction is 1(m / s), while box 2 is initially at rest. The
initial position of box 1 is at ( -3,1,0), while box 2 is initially at (3,1,0). Solve the
following two parts (the solution is included in the electronic material for Chap. 3):
Reference 19
Part 1
Assuming no friction with the ground, and using a coefficient of restitution of e = 0.9
(Hibbler 1986):
1. Reconstruct Fig. 3.13 in VRML assuming that the bottom faces of the boxes are
located at y = 0 and that the two boxes are aligned horizontally. Add an additional
box component to represent the ground where the top face is at y = 0.
2. Develop the equations of motion of boxes 1 and 2 before and after the collision
(Hibbler 1986).
3. Develop the M-script file to compute the numerical solution of the equations of
motion and animate the VRML model. Assume that the total time of simulation
is 10 s and that the step size is 0.01 s.
4. Plot the x-positions and x-velocities of boxes 1 and 2 as function of time. Plot the
combined energy (potential and kinetic) of the system as function of time.
Part 2
Assume that the friction with the ground is opposing the motion and takes the form
- μ ´ (normal force). μ is the friction coefficient (Hibbler 1986) and is equal to
0.005 in this part, and normal force is equal to m ´ g, where g is the gravitational
acceleration and is equal to 9.81 m/s2.
1. Develop the equations of motion of boxes 1 and 2 before and after the collision.
2. Develop the M-script file to compute the numerical solution of the equations of
motion and animate the VRML model. Assume that the total time of simulation
is 10 s and that the step size is 0.01 s.
3. Plot the x-positions and x-velocities of boxes 1 and 2 as function of time. Plot the
combined energy (potential and kinetic) of the system as function of time.
4. Compare the energy from Parts 1 and 2. What do you notice?
The solution of the problem can be downloaded from Springer’s web site http://
[Link]/. The files can be found in folder /Chapter 3/Application
Problem.
Reference
Book
Hibbler RC (1986) Engineering mechanics: dynamics, 4th edn. McMillan, New York