A Physics Simulation Engine for Modeling Autonomous Motion through Reverse Engineering of SodaConstructor

AICCSA 2009: International Conference on Computer and Applications A Physics Simulation Engine for Modeling Autonomous Motion through Reverse Enginee...
Author: Rachel Ross
1 downloads 5 Views 406KB Size
AICCSA 2009: International Conference on Computer and Applications

A Physics Simulation Engine for Modeling Autonomous Motion through Reverse Engineering of SodaConstructor Joseph Yang and Gongzhu Hu Department of Computer Science Central Michigan University Mount Pleasant, MI 48859, USA [email protected] Abstract Simulation of physics plays an important role in many fields such as engineering, science, and education. There are many simulation tools for various types of applications, including those for physics. In this paper, we describe a physics simulation engine we developed based on the concepts and models in SodaConstructor, a construction tool kit for learning physics. The models demonstrate several physics phenomena and are explained with experiments to illustrate the functionality of the simulation engine. Keywords classic physics, simulation, software reverse engineering, SodaConstructor.

1

Introduction

Physics plays an important part in our every day lives. It allows us to understand the laws of motion, matter, space and time. To understand the complex physical world for better design and creation of solutions to real world problems, particularly engineering problems, the ability to simulate the physical behavior is critical. Simulation allows us to virtually go through the design and modeling stages before put in real implementation. It makes it much easier to understand the behavior of the systems and to correct errors or improve the functionalities without much risk. Various industries have used simulation to assist improve the design and production process of their products, such as film, computer games, automobile, space exploration, and many more. The study of simulation has been around for a long time, and many simulation tools have been developed for different kinds of application domains, including simulation for physics. One of these tools is SodaConstructor [12] that is for interactive creations using masses and springs (Mass and Spring will be explained later in section 3). It is also a physics learning tool used

978-1-4244-3806-8/09/$25.00 © 2009 IEEE

by many students around the world to model classical physics. It allows users to create autonomous models whose autonomous motions are displayed to visually demonstrate the underlying physics concepts. To build an engine for simulation of the classical physics, rather than create everything from scratch, we took a reverse engineering approach by digesting the inner working of SodaConstructor and created a system to model it. The engine was built by applying the physics concepts to object-oriented programming. The program loads multiple models from SodaConstructor and creates a few more by analyzing its methods and observing the interface when running SodaConstructor. The system was implemented in XNA [4], Microsoft’s new graphics framework. The system was written in C# and XML. By mimicking the functionality of SodaConstructor, the main concepts of point particles can be demonstrated. We tested our system with several types of physical motions with different parameters for masses and strings to illustrate the effectiveness of the system.

2

Related Work

Simulation has been around for long time that help understand, design, and create various types of applications. Physics is one of the application domains simulation appears very useful for. There are some work in the literature on simulation in physics in the last two decades. The theoretical foundation and general methodologies for physics simulation can be found in [2, 7, 8]. Baraff proposed dynamic simulation approach for rigid bodies [1] and Egan developed techniques for real time rigid body simulation [5] in their doctoral studies. Ellis and Conger discussed the physics modeling for computer games development [6] and Stahler provided the math and physics behind computer games [10] that are simulation of physical motions. Evaluation of various real time physics

287

AICCSA 2009: International Conference on Computer and Applications

simulation methods can be found in [3]. SodaConstructor [12], a physics simulation kit that allow the user to create 2-D physical motions, has been attracted a lot of attention in recent years, mostly for educational purpose. People have used this tool kit to apply to different application domains. One such application is SodaConductor [9] that is an application of SodaConstructor to music. The control data generated by the dynamic physics simulation of the SodaConstructor is transmitted using the OSC (Open Sound Control) to a local network where the data can be interpreted by custom built audio patches. SodaConstructor has also been used for education. Svarovsky and Shaffer recently conducted a study using SodaConstructor in teaching middle-school students to solve engineering design problems [11]. Their study shows that “the process of simulation-based modeling, rapid iterations of the design-build-test (DBT) cycle progressively linked students interest in the design activities and understanding of the concept of center of mass.”

3

Basic Physics for Linear Motion

The premise of SodaConstructor revolves around a simple modeling of masses and springs. To understand the model, we review some basic physics concepts (often referred to as classical physics) in this section to provide means to model objects realistically. Newton’s laws hold the key to the concepts of kinematics. These laws are the building blocks for understanding the properties that govern the motion of a point particle that can be thought of as the most basic unit of any object.

3.1

Point Mass

Every point particle has a position, mass, and a force associated with it. The position vector represents the (x, y, z) components of the point particle’s location, the mass is a scalar value and the force is a vector that is applied to the position to allow the point particle to move. At any given instant, there are actually many forces acting upon a body of mass. These forces can be categorized into two types: impulse force (which is a small, instantaneous force) and constant force (which is always applied to an object, such as gravity). According to the Newton’s law of motion, an object moves as a constant speed unless acted upon by a force. The object’s speed may increase or decrease depending on the directions of the forces acted on it. One such force, for example, is friction force that depends on the friction coefficient cf ric and the normal force n that is caused by the gravity and perpendicular to the surface

the object sits on ff ric = cf ric n

(1)

When two objects collide, the amount of momentum remains the same before and after the collision, expressed in the conservation of momentum equation (2). m1 v10 + m2 v20 = m1 v1 + m2 v2 (2) where m is the mass. v and v 0 are the velocity of an object before and after the collision. When two objects collide, their energy and velocity may change. An elastic collision is the case with no lose of energy. On the other extreme, an inelastic collision is the case with total lose of energy. Collisions seldom fall as completely elastic or inelastic and actually fall somewhere in-between. Formula (3) models the velocity before and after a collision. e = (v20 − v10 ) / (v2 − v1 )

(3)

where e is the coefficient of restitution measuring how elastic or inelastic a collision is. The value of e is between 0.0 and 1.0, with a value of 0.0 meaning a very inelastic collision. Combining equations (2) and (3), the final velocity can be solved as

3.2

v10 =

(e + 1)m2 v2 + v1 (m1 − e m2 ) m1 + m2

(4)

v20 =

(e + 1)m1 v1 + v2 (m1 − e m2 ) m1 + m2

(5)

Spring

A spring is a device that returns back to itself when pulled. When a spring is stretched it doesn’t just revert back but it bounces back and forth. As a spring bounces in and out, that is an example of harmonic motion. Examples of harmonic motion occur in many objects. A good example is a pendulum. When a pendulum begins swinging, the mass at the end of the string swings back and forth. Gravity pulls the mass back toward the center where the mass began swinging. The restoring force F is given by a simple equation (6). F = −mg sin(θ)

(6)

where m is mass, g is gravity, and θ is the angle of displacement from the vertical position. The relationship between the force on a mass attached to a spring and its position x obeys the the Hooke’s Law as expressed in equation (7). F = −kx

288

(7)

AICCSA 2009: International Conference on Computer and Applications

where k is a spring constant. The value of k close to 1 indicates a strong spring and close to 0 indicates a weak spring. Another factor to consider is the dampening forces (such as air resistance) that also effect springs. With consideration of dampening forces, the Hooke’s Law is extended to equation (8). F = −kx − bv

It is seen in this example that a XML file starts with a number of global variables which hold constants such as gravity and friction. The XML files also hold the variables for masses, springs and muscles. Masses are point particles present within a model, the forces holding masses together are represented by springs, and muscles are part of the XML schema and apart from additional variables, same as spring nodes. Once the XML files were parsed, it is a matter of loading the data into the mass and spring objects. The mass and spring are simple containers of variables that apply force to a particle in a given direction. The main module of the simulation engine we have implemented is a loader (hence we call it Soda Loader ) that loads some of the models from SodaConstructor. At its core, the Soda Loader holds masses. It applies a single force to each point. The force is a combination of gravity, friction, and the added energy of the muscle. For some of the models loaded from SodaConstructor, we adjusted the parameters (such as the friction coefficient constant) and iterated multiple times through trialand-error to overcome the problems exhibited in the original SodaConstructor model. One of such models is the Walker that simulate objects “walking” on a floor, as shown in Figure 1. The Walker in the original model only moves for a brief period of time and then stand still. One possible cause was an incorrect use of the friction constant.

(8)

where b is the dampening constant and v is the velocity of the dampening force. Given these basic physics principles we can accurately create a moving SodaConstructor model.

4

Simulation Engine

The physics simulation engine we built is an attempt to recreate a SodaConstructor-like system. It is a reverse engineered from a closed-source application and loads some of the physics models from SodaConstructor as a guide for our implementation.

4.1

XML Specification of Models

The first task is an examination of the XML files produced by SodaConstructor that stores the physics models. A sample XML file looks like this: - - - true - - 0.05 - 0.025600000000000046 - 1 - 430 - - - - -0.09142269997498978 ......

Figure 1. Simulation of a walker. We only loaded five models from SodaConstructor simply because they are sufficient to illustrate the basic construct of the simulation engine.

4.2

Algorithms and Code Synopsis

In this section, we give some details of the system we developed to illustrate the process of the models. The system was written using the XNA framework. All XNA modules follows the XNA framework’s logic flow shown in the code fragment below. The GraphicssDeviceManager and the ContentManager provide access to the input and output devices necessary to begin coding a 2-D/3-D application, such

289

AICCSA 2009: International Conference on Computer and Applications GraphicsDeviceManager g r a p h i c s ; ContentManager c o n t e n t ; public Game1 ( ) {} protected o v e r r i d e void I n i t i a l i z e ( ) {} protected o v e r r i d e void LoadGraphicsContent ( b o o l l o a d A l l C o n t e n t ) {} protected o v e r r i d e void UnloadGraphicsContent ( b o o l u n l o a d A l l C o n t e n t ) {} protected o v e r r i d e void Update ( GameTime gameTime ) {} protected o v e r r i d e void Draw ( GameTime gameTime ) {}

as access to video card and graphics devices, create and load models, effects, shades, etc. The Game1 constructor and the Initialize method initialize the variables and other values for internal house keeping to allow a component to communicate with other components. The LoadGrachisConent and UnloadGraphicsContent methods run every time graphics need to be loaded and unloaded. Lastly, for every frame, the Update and Draw methods are called through out the entire lifespan of the program.

The Soda Loader loads the XML files into an internal data structure (a list, for example). These XML files defines the models of some physical motions of masses connected by springs, as discussed before. To load five models, the code looks like this: public TriangleLineList(Game game) : base(game) { .... fileList = new ArrayList(); fileList.Add("daintywalker.xml"); fileList.Add("mmaarrkkuuss.xml"); fileList.Add("carefulslug.xml"); fileList.Add("monster_truck.xml"); fileList.Add("astronomy_tk.xml"); }

Every XNA program follows a logic flow path as depicted in Figure 2.

Game Constructor Other functions ...

Componentk -

Constructor Initialize

- XNA framework 

 LoadGraphicsContent  Update

 

Draw



Dispose



device lose or reset

• • • user exits

UnloadGraphicsContent  ? game exits Figure 2. Logic flow of XNA program.

After extracting model information from the XML files, the system creates point particles (masses), springs, and muscles. Then it applies constant and external forces to these objects according to the physics equations discussed before. The process of applying forces to the objects is outlined in Algorithm 1. In the algorithm, m.x and m.y are the x- and yposition of the mas m; m.f is the force applied to the mass m and m.fx and m.fy are the x- and y-directional forces of m.f . A spring object s ties together two mass objects as its two vertexes s.v1 and s.v2 . By combining the masses into a spring, a very realistic muscle movement can be modeled. Once the set of springs are loaded, the masses associated with each spring in the set as well as the constant forces are retrieved. The components of the masses are then added to the forces created by the reaction of the spring equations to each mass. To do this, the oscillation algorithm of a spring is utilized. This is done by adding the resultant force of a springs constant (s.k) with the distance offset of the two masses. Since a position for each mass object is kept as well as the distance between springs, a force that acts on a spring is always produced. After the forces are found, they are added/subtracted from the force of each mass. This results in the oscillation of each mass. Once this force is applied to the mass, the simple mass equations are applied to each object calculating the new position of each component.

290

AICCSA 2009: International Conference on Computer and Applications

Algorithm 1: Apply forces to strings and muscles Input: S: set of springs, M : set of muscles Output: Masses associated with S and M are changed (forces applied) 1 begin 2 foreach spring s ∈ S do 3 m1 ← s.v1 // get masses from string s 4 m2 ← s.v2 5 k ← s.k // string constant p 6 d ← (m1 .x − m2 .x)2 + (m1 .y − m2 .y)2 7 d ← d − s.d; // adjust with string 8 θ ← arctan((m1 .x − m2 .x) / (m1 .y − m2 .y)) 9 m1 .fx ← m1 .fx − k ∗ d ∗ sin(θ) 10 m1 .fy ← m1 .fy − k ∗ d ∗ cos(θ) 11 m2 .fx ← m2 .fx + k ∗ d ∗ sin(θ) 12 m2 .fy ← m2 .fy + k ∗ d ∗ cos(θ) 13 end 14 foreach muscle m ∈ M do 15 perform similar operations as for springs 16 ······ 17 Let as be the static amplitude factor 18 Let am be m’s amplitude 19 Let aw be wave amplitude 20 Let v be variable wave phase 21 m.d ← m.d+as ∗am ∗aw ∗cos(v)+2π∗d.phase 22 end 23 v ← v + 2π ∗ waveP hase 24 end

model. Gravity is applied to the x direction keeping the model rested to the floor. These applications of forces are constrained by the condition that the simulation is with the boundary walls of the display window. When a model hits the wall (constraint) the bounce of the model sends the model back the other direction.

5

Experiments

We have run several experiments using our physics engine to show the use of the tool to illustrate some of the physics concepts. Below are a few of the examples we had experimented. Figure 3 shows a “walker” similar to the one directly run on the original SodaConstructor, but it has overcome the weakness of the original model as mentioned before.

Figure 3. Simulation example: walker. Figure 4 captures a snapshot of a moving vehicle.

The main focus of the mass class is to simulate the forces that would affect any point particle. Its behavior is outlined in Algorithm 2. Algorithm 2: Update of a mass object Input: M : a mass object 1 begin 2 Let p, v, f, m be the position, velocity, force, and mass of M , respectively. 3 foreach (direction d = x, y) do 4 vd ← vd + (gravityd + fd )/m 5 pd ← pd + vd 6 fd ← 0 7 vd ← vd ∗ f riction 8 end 9 Check boundary conditions, apply bounce factor if needed. 10 end A constant friction is applied to dampen the momentum of the moving particle. A bounce constant is also applied to help add a spring-like motion to the

Figure 4. Simulation example: moving vehicle. A “slug” examples is shown in Figure 5 that the creature moves using its bottom like a worm. These figures are simply snapshots of the videos in which the mass-spring objects move. It is seen from

291

AICCSA 2009: International Conference on Computer and Applications

on Computer graphics and interactive techniques in Australia and Southeast Asia, pages 281–288, Perth, Australia, 2007. ACM. [4] Chad Carter. Microsoft XNA Unleashed : Graphics and Game Programming for Xbox 360 and Windows. Sams, 2007. [5] Kevin Egan. Techniques for Real-Time Rigid Body Simulation. PhD thesis, Dept. of Computer Science, Brown University, 2003.

Figure 5. Simulation example: slug.

[6] J. Robert Ellis and David Conger. Physics Modeling for Game Programmers. Course Technology, 2004.

these examples that the system we built as a result of reserve engineering of SodaConstructor is able to provide the anticipated simulation functionalities.

6

[7] Harvey Gould, Jan Tobochnik, and Wolfgang Christian. Introduction to Computer Simulation Methods: Application to Physical Systems. Addison-Wesley, 3rd edition, 2006.

Conclusion

In this paper we described a physics simulation engine, which models a pre-existing program, the SodaConstructor. The goal was implementing classical physics. Implementing some key concepts of classical physics using objects in our system, we took advantage of the new technology XNA that allowed us to program graphic application relatively easily. The system utilized the core .NET functions to parse XML as well as 2-D physics equations. It is a very basic physics engine for springs and masses. using the 2-D functions is a good start towards a fully functioning physics engine which can model very realistic movement of muscles. SodaConstructor itself is a close-source software written in Java. Our system (XNA program) was both an experiment in the new Microsoft framework as well as a test to see if SodaConstructor could be remodeled and ported to the PC. With minimum changes to the code, it would be easy to port this application to the XBOX or any device which supports XNA. Future work will include building and testing more physics models, and to extend it to 3-D.

[8] Dieter W. Heermann. Computer Simulation Methods in Theoretical Physics. Springer-Verlag, 1986. [9] David Muth and Ed Burton. Sodaconductor. In Proceedings of the 2003 Conference on New Interfaces for Musical Expression (NIME-03), pages 222–224, Montreal, Canada, 2003. [10] Wendy Stahler. Math and Physics for Game Programmers. New Riders, 2004. [11] Gina Navoa Svarovsky and David Williamson Shaffer. Sodaconstructing knowledge through exploratoids. Journal of Research in Science Teaching, 44(1):133–153, 2007. [12] Sodaplay. SodaConstructor. http://sodaplay. com/creators/soda/items/constructor.

References [1] David Baraff. Dynamic Simulation of NonPenetrating Rigid Bodies. PhD thesis, Dept. of Computer Science, Cornell University, 1992. [2] B. Wayne Bequette. Process dynamics: Modeling, analysis, and simulation. Prentice Hall, 1998. [3] Adrian Boeing and Thomas Br¨ aunl. Evaluation of real-time physics simulation systems. In Proceedings of the 5th international conference

292

Suggest Documents