A report about creating a solar system Jørgen Elden Lindgren 27.02.2016 Table of contents A report about creating a solar system ................................................................................................... 1 Abstract ................................................................................................................................................... 2 1. Introduction ......................................................................................................................................... 2 2. Geometric modeling library (GMlib) ................................................................................................... 2 2.1 Main structure and modules ......................................................................................................... 2 2.1.1 Core ........................................................................................................................................ 3 2.1.2 OpenGL ................................................................................................................................... 3 2.1.3 Parametrics............................................................................................................................. 3 2.1.4 Scene ...................................................................................................................................... 3 2.1.5 Stereolithography ................................................................................................................... 3 2.1.6 Trianglesystem ....................................................................................................................... 4 2.1.7 OpenCL ................................................................................................................................... 4 2.1.8 Wavelet .................................................................................................................................. 4 2.2 Programming-style and Code convention ..................................................................................... 4 3. Qt Framework...................................................................................................................................... 5 4. The Project........................................................................................................................................... 5 4.1 Gameplay....................................................................................................................................... 5 4.2 Structure of the resulting application ........................................................................................... 5 4.2.1 Programming-style and Code convention .............................................................................. 5 4.3 Class description ............................................................................................................................ 6 4.3.1 The Spaceship Class ................................................................................................................ 6 4.3.2 The Sphere Class ..................................................................................................................... 6 Page 1 of 11 4.3.3 The Collision Object Class ....................................................................................................... 6 4.3.4 The Controller Class ................................................................................................................ 7 4.3.4 The Gmlibwrapper Class ......................................................................................................... 7 4.3.5 The GLscenerenderer Class .................................................................................................... 7 4.3.6 The GUIapplication Class ........................................................................................................ 7 4.3.7 The Window Class .................................................................................................................. 7 4.4 Collision detection system description ......................................................................................... 7 4.4.1 findCollision ............................................................................................................................ 8 4.4.2 collisionDetection ................................................................................................................... 9 4.4.3 setVelocity .............................................................................................................................. 9 4.5 ODE description ............................................................................................................................. 9 4.6 UI ................................................................................................................................................. 10 6. Result ................................................................................................................................................. 10 7. Concluding remarks ........................................................................................................................... 11 8. References ......................................................................................................................................... 11 Abstract This report is about a simulation of multiple objects in space by simulating the laws of physics in creating a solar system. For the simulation C++, GMlib, Qt and OpenGL, indirectly, is used. A template application has been provided for use in programming the simulation. 1. Introduction This project is about the creation of a 3D computer game (simulator) that is loosely based on our solar system. A program template has been made by UiT, previously known as HiN, for use in the project. The programming language used is C++. There are four library used, the Standard Template Library (STL), which consists of containers, functions, algorithms and more, and the Geometric Modeling Library(GMlib), which is an in-house developed library at Narvik University College, the Open Graphics Library (OpenGL) and the QT library. 2. Geometric modeling library (GMlib) GMlib is an open source programming library developed in the 1995 by the staff at HiN, now part of UiT, and later the library has started being incorporated with works from students that has taken the computer science master course. This library is written in C++ and consist mainly of a template library with several modules that is described in 2.1 Main structure/ modules. GMlib is still being developed and new additions are released periodically. GMlib is dependent on the Glew library without it GMlib won’t work. 2.1 Main structure and modules The geometric modeling library is made up of eight modules, Core, OpenGL, Parametrics, Scene, Stereolithography, Trianglesystem, OpenCL and Wavelet. Two of the eight modules, OpenCL and Wavelet, are disabled in the current version of GMlib in use. Page 2 of 11 2.1.1 Core The core module, is a template library for affine spaces; points, vectors, matrices, frames, simplices etc. defined by the template parameters <typename T, int n> with type T, (float/double/…) and interger n, dimension (2D/3D/…) as parameters in general.[1] The program uses, from the module, ‘Point’, ‘Vector’ and ‘HqMatrix’. ‘Point’ is used to get a sphere’s position in the ‘findCollision’ function, for instance. ‘Vector’ is used to set up a private velocity variable in the sphere class. And HqMatrix is used to rotate every asteroids starting point around the axis while the asteroids position in the x-axis and velocity in the y-axis is still intact. 2.1.2 OpenGL The OpenGL module, contains the graphic interface including interfaces to GPU and shaders for graphic purposes.[1] The program uses the GMlib OpenGL module indirectly. 2.1.3 Parametrics The Parametrics module, has a template baseclass Parametric with the inherited PCurve, PSurf and PTriangle handling parametric curves, and surfaces as Bezier, Hermite, B-Splines, NURBS, Exporational B-Splines, etc.[1] The program uses, from the module, ‘PSphere’, ‘PTorus’, ‘PSurfTexVisualizer’ and ‘PSurf’. The sphere class inherits from the ‘PSphere’ class. The spaceship class uses the ‘Ptorus’ class to draw the spaceship’s body. ‘PsurfTexVisualizer’ is used to apply texture to the planets. ‘PSurf’ is used to create a container that store the parts of the spaceship. 2.1.4 Scene The Scene module, is a module consisting of an hierarchical object-tree with a Scene as a root object and SceneObject as nodes. The scene has display, select, tracing, editing and simulation functionality. It also includes special SceneObject as different camera types, different light types etc.[1] The program uses, from the module, ‘SceneObject’ and without it nothing would really work. For a class to draw something on the scene it must inherit from the ‘SceneObject’. The ‘SceneObject’ contains the function ‘local simulate’ which is used to simulate, for example, an object’s movement direction, movement speed, rotation, collision, etc. 2.1.5 Stereolithography The Stereolithography module, handling stl-objects for stereo-lithography, object scanning, 3Dprinting etc.[1] The program does not use anything from this module. Page 3 of 11 2.1.6 Trianglesystem The Trianglesystem module, is a set of template classes TriangleFacets, Triangle, Edge and Vertex useful for terrain modeling, and other type of ‘’2D-base’’ (without overhang) triangle surfaces. A Delaunay triangulation also including constrains is implemented.[1] The program does not use anything from this module. 2.1.7 OpenCL The OpenCL module, contains the interfaces to GPU to make general computation (not for graphic purposes).[1] This module is not enabled in GMlib for use. 2.1.8 Wavelet The Wavelet module, is a module for developing wavelets. It includes several predefined types and is specialized for image compression and computations.[1] This module is not enabled in GMlib for use. 2.2 Programming-style and Code convention Gmlib is template based. most of all the classes in GMlib have a header file and a source file. Each class starts with an upper case letter, ‘Scene’ for instance, and when a class has two or more words in its name then the first word and the words after the first one starts with an upper case letter, ‘SelectorGridVisualizer’ for instance. In the header files the functions in the class are declared and in the source files they are defined. Every function that has one word starts with a lower case letter and every function that has two or more words starts with the first word having a lower case and the words after the first one starts with an upper case. Private and protected variables are declared with an underscore, and every variable that has one word starts with a lower case and every variable that has two or more words starts with the first word being in a lower case and the second word being in an upper case. When including other classes or libraries in a class, Classes are included as the first, GMlib is included as the second, and last is STL included as the third. The code in GMlib is somewhat commented. Page 4 of 11 3. Qt Framework Qt Creator is a cross-platform application development tool for developing C++ application. From Qt are ‘QImage’, ‘QImageReader’, ‘QOpenGLTexture’, ‘QDirIterator’, ‘QString’, ‘QStringList’, ‘QDir’, ‘QFile’, ‘QMouseEvent’, ‘QHoverEvent’, ‘QKeyEvent’ and ‘QWheelEvent’ used in the program. ‘QImage’, ‘QImageReader’, ‘QOpenGLTexture’, ‘QDirIterator’, ‘QString’, ‘QStringList’, ‘QDir’ and ‘QFile’ are used to add textures to planets. ‘QString’ is the function gets the path to the textures. ‘QStringList’ is used to filter through ‘QString’ path and find only .jpg. Then ‘QString’ is added to ‘QImage’. ‘QFile’ checks if the texture exists. ‘QImage’ then convertes the image to a Format_RGB888. ‘QMouseEvent’ is used to handle mouse events in the program. ‘QHoverEvent’ is used to handle the mouse icon when it is in the scene. ‘QKeyEvent’ is used to handle key button events. And ‘QWheelEvent’ is used to zoom in and out, in the scene. 4. The Project 4.1 Gameplay There are some gameplay elements in the game. Flying around the solar system in a spaceship or observing the solar system as it moves. There is a description in the UI section about the controls in the simulator. 4.2 Structure of the resulting application 4.2.1 Programming-style and Code convention In this project the ‘Jørgen style’ is used for writing the program. Each class made in the project has a header and a source file and each class name with one word starts with an upper case letter, ‘Sphere’ for instance, and when a class has two words in its name then the first word and the second word starts with an upper case letter, ‘CollisionObject’ for instance. In the header files the functions in the class are declared and in the source files they are defined. Every function name that has one word starts with a lower case letter and every function name that has two words starts with the first word having a lower case and the second word having an upper case. Only private variables are declared with an underscore, and every variable that has one word starts with a lower case and every variable that has two words starts with the first word being in a lower case and the second word being in an upper case. When including other classes or libraries in a class, Classes are included as the first, GMlib is included as the second, STL is included as the third and last is QT included as the fourth. The code is somewhat commented in certain parts of the program. ‘Jørgen style’ shares some similarities with the programming style used in GMlib. Page 5 of 11 4.3 Class description There are four classes that have been created in this project, which is; the controller class, the collision object class, the sphere class and the spaceship class. And from the template application, four classes are used; the guiapplication Class, the window class, the glscenerenderer class, and the gmlibwrapper class. The sphere, the collision object and the controller object are template classes, where the variable T is a double because float is a not very precis data type for this type of simulator because of the small numbers the simulator handles. 4.3.1 The Spaceship Class The spaceship class was created to give the player the ability to move around in the solar system. The spaceship class is not a template class. The spaceship class set up variables and functions that controls the ship’s speed, yaw, pitch and roll. The functions gets input from key, mouse and hover events that comes from the gmplibwrapper class. 4.3.2 The Sphere Class The sphere class was created for use in creating a sun, planets, a moon and asteroids for the solar system. The sphere class inherits from GMlib PSphere class for use in making spherical objects. The sphere class is a template class. The sphere class have variables and functions that sets a planet's size, mass, density, velocity, force, step, acceleration and the derivatives of the acceleration. The sphere class has several functions that are used for updating existing variables by either adding the new value to the previous value or overriding it, and one function used for calculating a sphere's step movement when in orbit around another sphere, more about the movement calculation in ‘ODE description’. 4.3.3 The Collision Object Class The collision object class was created for use to calculate the movement after a sphere collides with another sphere. It has several functions that are used for updating existing variables by either adding new values to the previous ones or overriding the existing ones. More about this in the ‘collision detection system description’. Page 6 of 11 4.3.4 The Controller Class The controller class was created to use and control the other classes. The controller class inherits from GMlib SceneObject class to draw objects and all its children the scene. The controller creates a sphere setting the values that are used for the size, mass, density, velocity, position and adds a texture to sphere, and then pushes it onto an array. The controller then calculates the force of gravity When a sphere have been added to the array the controller class then computes the Newton’s gravitational law so that the gravitational force between the two sphere is found. The formula for Newton’s gravitational is as followed: 𝑚1 ∗ 𝑚2 𝐹𝑔𝑟𝑎𝑣 = 𝐺 ∗ 𝑟2 𝐹𝑔𝑟𝑎𝑣 is the gravitational force, 𝐺 is the universal gravitational constant, 𝑚1 is the mass of the first planet, 𝑚2 is the mass of the second planet, 𝑟 is the distance between the two planets. When 𝐹𝑔𝑟𝑎𝑣 is found then the force (𝐹) is found by multiplying the gravitational force with a directional force, which is a simple unit vector, and multiplied with a universal scaling factor. The universal scaling factor is used to scale down the solar system. The controller can scale the time up or down so to make the planets in the solar system move around seem like they move faster. The controller class also detect and calculates the collision between the spheres, more about this will be explained in the ‘collision detection system description’. 4.3.4 The Gmlibwrapper Class The gmlibwrapper class creates the camera, render and creates the scene that draws the planets, asteroid and the spaceship. As well, as set up key and mouse events for use to increase and decrease the time it takes for the planets to rotate around the sun and for moving the spaceship around. 4.3.5 The GLscenerenderer Class The glscenerenderer class is from the template application. A new function has been added to focus on the hover event. 4.3.6 The GUIapplication Class The guiapplication class is from the template application. In this class the hover event from the window class gets connect to the event handler in the gmlibwrapper. 4.3.7 The Window Class The window class is from the template application. A new event function has been added to register when the mouse icon hovers over the window. 4.4 Collision detection system description To start there are three functions responsible for handling collision in the project. The collisionDetection function, the findCollision function and the setVelocity function. Page 7 of 11 4.4.1 findCollision This function finds the positions p0 and p1 of two spheres, as shown figure 1, that are about to collide and take the information from both planets and set it up in the quadratic equation 𝑥 = −𝑏−√𝑏2 −4𝑎𝑐 2𝑎 where 𝑎 = 𝑑𝑠 ∗ 𝑑𝑠, 𝑏 = 2 ∗ (𝑑𝑠 ∗ 𝑑𝑝) and 𝑐 = (𝑑𝑝 ∗ 𝑑𝑝) − (𝑟 ∗ 𝑟). 𝑑𝑝 = 𝑝𝑜𝑠1 − 𝑝𝑜𝑠2 is the distance between the planets, 𝑑𝑠 = 𝑠𝑡𝑒𝑝1 − 𝑠𝑡𝑒𝑝2 is the step between the planets, and 𝑟 = 𝑟1 + r2 – is the sum of the radius of the planets Figure 1: Two spheres before and after colliding. Originally the quadratic equation has two solutions, one is positive the other is negative. However in this case to get the desired result only the negative solution from the quadratic equation is required to make sure that when two planets collide that they don’t appear behind each other, as shown in figure 2. Figure 2: A moving sphere (black) collides with a stationary sphere (red) and pass through each other. To make sure that the quadratic equation works properly there are two if checks. First if check tests that 𝑏 2 − 4𝑎𝑐 > 0. And the second if check test that the result of the equation is between x > 0 and x ≤ 1 to give the current collision. Page 8 of 11 4.4.2 collisionDetection This function goes through a for loop that stores the initial collisions between the spheres, and when enough collision have been stored the function goes through a while loop that handles the collisions and sorts them so that the collision does not happen again, and then starts all over again. 4.4.3 setVelocity This function handles what happens after when a collision has occurred between two spheres. By using the following formula [2] to determine the new velocity. 𝑣1′ = 𝑣1 − 2∗𝑚2 𝑚1 +𝑚2 ∗ (𝑣1 −𝑣2 )∗(𝑥1 −𝑥2 ) ∗ (𝑥1 (𝑥1 −𝑥2 )2 − 𝑥2 ) , 𝑣2′ = 𝑣2 − 2∗𝑚1 𝑚1 +𝑚2 ∗ (𝑣2 −𝑣1 )∗(𝑥2 −𝑥1 ) ∗ (𝑥2 −𝑥1 )2 (𝑥2 − 𝑥1 ) 𝑣1′ is the new velocity for the first sphere and 𝑣2′ is the new velocity for the second sphere, 𝑣1 and 𝑣2 are the old velocities for the spheres, 𝑚1 and 𝑚2 are the masses for the spheres, and 𝑥1 and 𝑥2 are the spheres positions. 4.5 ODE description The computeStep function in the sphere class is responsible to calculate a spheres movement by use of the Taylor expansion equation. The computeStep function has two parts to it, the first part is for calculating the step vector, which means calculate the distance to translate the object in a calculated direction, and the second part is to calculate the velocity based on time and acceleration. The equation of the first part: 1 1 1 1 𝑑𝑠 = 𝑑𝑡 ∗ 𝑣 + ∗ 𝑑𝑡 2 ∗ 𝑎 + ∗ 𝑑𝑡 3 ∗ 𝑎′ + ∗ 𝑑𝑡 4 ∗ 𝑎′′ + ∗ 𝑑𝑡 5 ∗ 𝑎′′′ 2 6 24 120 𝑑𝑠 is the step vector, 𝑑𝑡 is the time in second passed since last call given from the system[1] , 𝑣 is the velocity, 𝑎 is the acceleration found by using newton’s second law 𝐹 = 𝑚𝑎 where 𝑎 = And 𝑎′ is the first derivative of 𝑎, where 𝑎′ = 𝑎0 −𝑎0−1 . 𝑑𝑡 𝐹 . 𝑀 𝑎0 is the previous 𝑎 in the previous 𝑑𝑡 and 𝑎0−1 is the current 𝑎 in the current 𝑑𝑡. The equation of the second part: 1 1 1 𝑣 = 𝑣 ′ + 𝑑𝑡 ∗ 𝑎 + ∗ 𝑑𝑡 2 ∗ 𝑎′ + ∗ 𝑑𝑡 3 ∗ 𝑎′′ + ∗ 𝑑𝑡 4 ∗ 𝑎′′′ 2 6 24 Where 𝑣 ′ is the previous 𝑣. With these equations the sphere is able to move around in space. Page 9 of 11 4.6 UI The user interface for the simulator: Button/mouse R Mouse wheel Right mouse button click Right mouse button hold Alt + right mouse button click Ctrl + left/right mouse buttons Shift + left/right mouse buttons Right and left mouse button hold + and - Description Pauses and resume the simulation. Zooms in and out selects an object or Moves the camera along the axis. If an object selected then camera rotates around object. Selects or deselects one or more objects Rotates the selected object Translates the selected object around in the camera’s angle. Moves the camera around in camera’s angle. Increases and decrease time it takes a planet to move around the sun Turns the spaceship left Turns the spaceship right Increases and decrease the spaceships speed Tilts the spaceship up and down Arrow key left Arrow key right Arrow key up and arrow key down Mouse movement up and mouse movement down Mouse movement left and mouse movement Rolls the spaceship to the left and to the right right + need to be pressed before the planets start to move. 6. Result The programming was conducted on a computer with the following specifications: CPU: Intel Core i5 3230M 2.6 GHz OS: Windows 7 64bit RAM: 12 GB DDR3 Memory. The simulator was developed in Qt Creator and written in C++. As it stands now it is just a simple simulator. The Solar system have one sun, nine planets, one moon around the earth, and a 100 asteroids, which is forming the Kepler belt, and the simulation is running smoothly. And flying around works aswell. Page 10 of 11 7. Concluding remarks ‘Flying’ the spaceship around in space with key arrows is okay enough except that the arrow key right is pressed the spaceship turns to the left and vice versa, making it seem that some variables may end up being inverted somewhere. After two planets hit each other they are usually going to combine, instead they move away from each other and this is because it is not set up for handling combining the planets. When ‘Flying’ the spaceship, the camera is inserted on the ship, which results in some weird camera movement. When the mouse icon is move outside the window the spaceship still keeps moving in the mouse icon last position, and when the mouse icon enter again then the spaceship find the mouse icon again. And when the mouse icon is in the window the spaceship move continually towards the mouse icon even when it should not. Future development for the simulator: - Fix the arrow key problems. - Implement a function that handles the destruction and combination of spheres. - Fix the camera problem. - Implement a function that grabs the mouse and keep it inside the window. - Fix the function that moves the spaceship continually towards the mouse. - Generally improve the functions in the simulator for better runtime and optimization. - Add more to the user interface. - Add a HUD - Add a better spaceship - Add a way to scale the solar system 8. References [1] Arne Lakså. Blending technics for Curve and Surface constructions. 2012. [2] Arne Lakså, Børre Bang. Simulating rolling and colliding balls on freeform surfaces with friction. Tapir Akademisk Forlag, October 2005. Page 11 of 11
© Copyright 2026 Paperzz