Week 5 – Game Engine (Final space racing game)
In the fifth week, and the final week of the game tutorial engine we have started speaking about the basic collision detection. The tutorial helped me to develop a very basic and unsophisticated collision detection. The tutorial guided me how to create a simple class structure, entitled Collider, with a class that was extended by a CubeCollider. The tutorial was meant to show me that every item inside the GameObject class has a collider and inside the
GameEngine, every time that the updateGame is called, the game would check if the collider of the spaceship, for example, collides with the collider of an asteroid. In the tutorial, a box collier type was introduced in order to make us understand better how the collision is supposed to work. After I manage to finish the tutorial, I have tried to think of a way to improve the collision detection, so I researched and I decided that I want to create a sphere collider for the asteroids because I wanted to make the game as realistic as I could that resulting in my decision of choosing to go with a sphere collider for the asteroids which are present in my game. Another factor which influenced my decision of developing a sphere collider was triggered by some events which happened while I was working on the game engine tutorial. After finishing the box collider and testing it I have observed that objects using to interact with each other even if the three-dimensional objects were not too close to each other, this event representing an important factor in my choice for establishing a sphere collider. The first which I made in order code the sphere collider was to analyze in a serious manner the cube collider which I have created with the help of the game tutorial engine. Following that, I have created a new class, entitled SphereCollider, which inherited from the header named Collider, action which is a necessity in order to make the sphere collider interact with the box collider applied on the space vehicle. After I have created the class, the next step was to declare some variables and function, inside the header file of the sphere collider class, like a float value which represented the radius of the sphere collider, created a function entitled Radius() which sets the collider its radius, an empty, or void, function which draws the sphere and other float functions which draws the collider. I have also declared the Boolean function which verifies with objects collide one with each other. In the CPP file of them, I have drawn the sphere collider with the help of the functions and variables declared in the header file. After my sphere collider was done and it was working, at this moment is the spaceship would give collided with the asteroid, the asteroid would have been destroyed, event triggered by the delete function which I have created with the help the game engine tutorial. At this moment the collision detection and the collision interaction were working but not as I wanted to. In my pursuit of making this game as realistic as I could, I wanted to interact differently with the asteroids rather than deleting them after my spaceship interacted with them. With this being said, I thought about an idea of how I can make the spaceship interact differently with the asteroids and the first thing which came to my mind was a form of reflection, I wanted for my spaceship to get knocked back when it collides with an asteroid.
Graph representing the simple reflection:
After I came up with the idea of how I want my vehicle to interact I thought about how can I make this theory working, so I came up with the idea of playing with the acceleration of the spaceship (I will talk about the idea of acceleration and deceleration after I finish with the collision part of the game). In order to make my ship to move, I have declared a float value named acceleration, and I have stated in the through an if statement that if the acceleration decreases than the spaceship will move distance backwards depending on how fast the ship hit the asteroid. If the if statement returns the acceleration negative that means the ship collided with the sphere and the ship will get knocked back, if not the ship will continue its route. In this way, I have managed to create sort of a realistic way of representing the reflection rule inside the space racer game. After I have managed to finish the collision I have observed that Newton’s third law can be applicable in this case because the law says that when a body exerts a force on a second body, in my case the asteroid, the second simultaneously exerts a force equal in magnitude and in an opposite direction of the first body, the first body being represented by the spaceship.
Graph representing Newton's 3rd Law:
Another factor which I have added to the game in order to make it more realistic was the acceleration and deceleration of the ship, I have done that for the same purpose as in the case of collision, just to give the game a more realistic view. The way in which I did the acceleration and deceleration was fairly simple, I have initialized some float values like acceleration (the one which I have already spoken about a few rows ago), the force, the mass and the speed with which the ships are moving currently. After I initialized everything, I have modified the GLUT function which I have done in the fourth week of the game tutorial engine. So, where the glut function was, for the GLUT_KEY_DOWN function I have subtracted the acceleration, the acceleration is equal with force divided by the mass of the objects multiplied with time divided by 1000. In order to move backwards my acceleration was equal with itself minus the formula which I have written few lines ago. To move forward, I just added both.
The formula of Newton's Second Law represents an important index in classical mechanical physics:
Following the completion of the acceleration and the deceleration, the next step was to create a simple track for the spaceship, track which was formed out of asteroids because in this way I can showcase how the collision is working in the space racer game. Creating the track was fairly easy, I have created two squares a smaller one inside a bigger one just to give a small feeling of a circuit. The track was created with the help of one for loop with the range from 0 to 100 and inside the for loop, I have added four if statements each one of them for one them creating one side of the squares. Inside the if statement I have added two asteroid constructors, one for the bigger or outer square and one for the smaller or the on positioned inside the big square.
As a conclusion for this post, I want to talk about what went well, what not so good and what could have been better. Taking into consideration that this was the first time when I have created a game entirely written in C++ code and reflecting upon my work on the past five weeks I think I have managed to do a decent project. Of course, as in every project, I could have done better, my physics mechanics could have been better, my collision with the asteroids can be done in a better way because the way in which I have done the collision is not entirely realistic, it has some realistic features as I have written before but is not entirely realistic. On the other side, I believe that I have done a decent of implementing all the physics theorems and concepts which we have discussed during the labs, I have managed to create a different type of collider as well which is working properly, my acceleration and deceleration is working fine, but there is a space left there for improvement as well. In terms of what went wrong, I did encounter problems at almost every physics aspect which I have tried to implement because of my small lack of knowledge regarding OpenGL and some C++ aspects, but I have managed to overcome the problems and finish the space racing game. Overall, I believe I did a good job taking in consideration that this was the first time for me when I did a game in this manner.
In the following video, the physics of the space racer game can be seen as well as the track which I have created:
Reference:
- https://www.google.co.uk/search?dcr=0&biw=1536&bih=759&tbm=isch&sa=1&ei=_SWQWq3mH8HawQLu2bj4BQ&q=newton%27s+3rd+law+graphs&oq=newton%27s+3rd+law+graphs&gs_l=psy-ab.3...32042.35041.0.35239.9.8.1.0.0.0.98.615.8.8.0....0...1c.1.64.psy-ab..0.2.176...0i19k1.0.nY-X0PBuIJM#imgrc=0S9iQZEUBHonAM:
- https://en.wikipedia.org/wiki/Newton%27s_laws_of_motio
- https://www.google.co.uk/search?dcr=0&biw=1536&bih=759&tbm=isch&sa=1&ei=kyaQWq-eGoXdwALV1IHwCg&q=reflection+direction&oq=reflection+direction&gs_l=psy-ab.3...5980.7792.0.8130.9.9.0.0.0.0.97.760.9.9.0....0...1c.1.64.psy-ab..0.6.535...0i19k1j0i8i30k1.0.HsIq4MyCNoI#imgrc=ihkuUgeupO8GCM:
- https://www.google.co.uk/search?hl=ro&dcr=0&tbm=isch&sa=1&ei=1iiQWtOXHoz6wALU6KDADg&q=newton%27s+second+law&oq=newtons+se&gs_l=psy-ab.3.0.0i10i19k1l2j0i19k1j0i10i19k1l3j0i19k1j0i10i19k1j0i19k1l2.104258.107482.0.108485.10.10.0.0.0.0.203.1126.7j2j1.10.0....0...1c.1.64.psy-ab..0.10.1124...0j0i67k1j0i30k1j0i10i30k1.0.CIMPDqyXg3A#imgrc=CFa5v7JT1THXiM:


