Friday, 23 February 2018


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:




Friday, 16 February 2018

Week 4 – C++ Debugging


In the fourth week, we have received a poorly written C++ program, a hangman game, which we had to debug. When I have tried to compile it, the errors window became full. The code was poorly written, there was no incidence, a couple of typo errors as well as logical errors. The first error which I have observed is the following one:



The issue here is that the "q", which is the identifier, is not defined as a string so the command "getline(cin, q)" cannot operate. The second pair of errors are coming from the following lines:






This error occurs because in the while statement shouldn't be any constant, like 0 because the value can differ, the value being influenced by the input from the user. So in order to fix this problem, we just have to change the number 0 to number 1, in this manner, the mistakes can be counted until the user either gets the answer or he lost all of his chances, the number of chances being 6. Other errors which were present in the code were rudimentary ones like missing a semi-column, one which can be seen in of the previous pictures. Another error is located at the call for cout where one of this type symbol "<" is missing as it can be seen in the following picture:



In the following pictures, it can be seen the corrected and fully functional code:









                             Week 4 – Collision 


In the fourth week, besides the debugging problem we have received some exercises regarding the collision detection. 



In the first exercise we were given a link and I had to read through an commentary about the detection of the collision in a 2D environment and research for the names of the fundamental theorems that are "underlined" in the tutorial given by the exercise. The basic definition of the collision is that when two rigid bodies are intersecting with each other. On the website, on which I did the research were defined three big phases:

1. Broad Phase - which is considered to be responsible for finding pairs of objects where there might be a chance of collision and totally eliminating the pairs of rigid bodies which will not collide.

2. Narrow Phase - this particular phase comes after the Broad Phase and actions on the pairs which have a chance of colliding and it figures out the collision point between the points.

3. Continuous Collision Detection - attempts to find the time when two objects collided. Basically, it is looking for the time of the impact.





For the second exercise I had to cover the circle collision and then answer the queries.
In order for two circle to intersect the distance between their centers has to be either equal or less to the sum of the radius of the first circle with the radius of the second. For the spheres is basically the same thing.

For the second sub question I found out the distance between the centers of the circles and just compared them to the sum of the radius. The circles collided because the distance was smaller than the sum of the radius. For the final sub question, I had to find the discriminant to check if the circle collides with the wall. I determined the discriminant to be bigger than 0 which results in an intersection.






In the third exercise I had to show what happens if a ball collides with a wall at an angle of 0 degrees.
The answer of the question can be seen in the following image:








Wednesday, 14 February 2018


Week 4 – Game Engine


In the fourth so-called chapter of the game engine tutorial, the main idea was to make the spaceship move. In order to make the intergalactic vehicle move, we had to use some special functions which GLUT have that can interact and do something if the user interacts with it. In order to make it interact with any specific key which is pressed, we had to implement a map data structured which permits to store different keys or pairs of values to make the pressed key give a certain response when they are being interacted with. Another concept which was "underlined" was the delta time, which is being determined by a function. GLUT includes a function which showcases the amount of time passed in the game in a certain unit of time, the unit is milliseconds. Thinking from a physics perspective and from a realist point of view, the ship needs to rotate as well this meaning that the space vehicle needs rotation angles, without rotation the controlling of the ship can become really difficult. The controls of the ship are basic, in order to move forward or backwards the up, respectively the down, arrow key needs to be pressed. To move left or right, the left and right arrow key need to be operated. To fly upwards or downwards the page up, respectively the page down needs to be used. After successfully following the tutorial I managed to get a moving ship that rotates and moves.

In the following images can be seen the spatial vehicle in different positions, with different angles: 





Saturday, 10 February 2018


Week 3 – Game Engine

In the third part of the space game tutorial engine, or in the third week, we dig more into the inheritance idea. One of the first things which I have done, all my actions being instructed by the tutorial, was to create a new class entitled GameObject because all the following classes which will be created afterwards will inherit the equivalent functions which are written in the GameObject class. Basically, the asteroid and the spaceship class will inherit their functions, such as drawScene(), from the GameObject because at this point in time the asteroids and the spaceship have similar functions. The newly created class will consist of every function that is shared between the objects we have created and some of which will be created during the tutorial, it will contain functions and variables which will be needed by both of them classes, the asteroid and the spaceship. This demonstrates even better than the previous week, where I have talked about the importance of inheritance, how important this concept can be in programming, coming back to the idea of reusing the code. The output of the game is the same because I have not changed the position or the colour of the asteroids, I just followed the tutorial and I have created the class which will become the basis of the game, at least for now.

Images which show that the GameObject became the parent class and now the Asteroid and the SpaceShip class inherit from it:






Friday, 9 February 2018


Week 2 – Game Engine

In the second week of the Game Engine Tutorial, we learnt how to properly code the asteroids in our game, objects which we will need in the later stage of our game. In the tutorial is presented how to properly develop them with the help of classes, a more advantageous way rather than putting all the code in one CPP file, or one cluster of code into a single file. The tutorial walked me through the exact process of creating the asteroids in order to have the best result. If the tutorial is followed perfectly step by step it should output 2 spheres, which are representing the asteroids and the cone which was implemented in the last week, representing our ship. Another aspect which we have covered in this weeks tutorial was the inheritance part which comes when we are working with classes. Inheritance can represent such an important aspect of programming because in this way we can, basically, reuse parts of code without having to rewrite the code every time when we need it, more specifically inheritance is a really important aspect in the code usage area. Following the creation of the asteroid class, I have tried to colour them differently by creating a colour property which can be seen in the following images:






The output after the colour structure got introduced, two different spheres with two different colours:


Week 2 – Movement

In the second week, we got introduced to the movement and Newton's laws theorems, laws which can represent a substantial importance regarding this module. For the  Movement part, we have received a set of exercises to solve in order to understand better the concepts behind the laws of movement. In the first exercise, a formula which calculates the average speed of a vehicle on a straight road for a mile at a speed of 50 mph then the vehicle turns around and rides back at a speed of 70 mph is needed. The average of the speed is the distance which has been covered over the time, thus representing the only mathematical computation which had to be done. Following this subquestion, I had to find the average velocity which resulted to be 0 because the car turns around so the distance covered had to be subtracted. Als, it does not matter with what speed the car has manoeuvred the velocity will still be 0.



In the second exercise, one of the requirements was to check which of the two weights struck the ground first, one being dropped straight down and one being thrown horizontally. The answer is that both of them have touched the ground at the same time because the force of the gravity affects both of the weights, in the same way, the mode in which they got dropped not representing an important factor. The time required for them to hit the ground was taken from the height formula, as it can be seen in the solution of the exercise. The distance can be seen in the second picture and it was found by performing the multiplication between the velocity with the time.




For the last exercise, which was again about speed, a special formula was needed, which can be seen in the following picture, where v1 represented the ultimate velocity and v2 the initial one. For this exercise to give a correct result the conversion from km/h to m/s had to be done. The answer is a negative one because the vehicle is decelerating (or slowing down).



Week 2 – Newton's Law

The second paper sheet with physics queries was about Newton's laws of motion, the three physics laws which created the foundation for classical mechanics physics.  They characterize the relationship between a body and the forces which are acting upon it, and its motion in response to those forces. To be more specific, the first law defines the force qualitatively, the second law showcases a quantitative measure of the force and the third and final law underlines the idea that a single isolated force does not exist. After I have studied the laws of Newton I began working on the exercises, starting with the first one. In order to solve the first query, I have applied the first and the second law of Newton. In order to resolve the second exercise, the second law of Newton needs to be applied, F = m*a because the acceleration can be found from this formula.



In the third exercise, I had to draw the forces by Tarzan hanging from a vine. The forces which I have drawn represent the friction created between Tarzan and the vine, the weight and the tension created in the point where the character hold the vein.





Week 2 – 3D Coordinate Systems

For this specific query, the main idea was to mainly focus ideas of multiplication and/or commutation of the matrices formulas so we received an exercise where we had to check if two matrices perform the commutation or not. Before starting to resolve the problem, I have studied the concepts of 3D Coordinate systems. After I got familiarized with the concepts, I started solving the exercises. The first step was to write the formulas which I needed in order to solve the problem. After that, I introduced some values, gave θ a value of 90 degrees and made the x, y and z values equal with 2 to simplify the calculations. Following the declaration of the variables and finding the angle values, I have started multiplying the two newly formed matrices. The result was a negative one because (as it can be seen in the following pictures), the calculations have shown that the two matrices did not commute with the values which I have declared.










Refference: https://en.wikipedia.org/wiki/Newton%27s_laws_of_motion
Week 2 – Presentation

One of the tasks of the second week was to present the physical part of a game we were supposed to choose and to present the purpose and physics of the game, the good parts as well as the bad parts. The team I was part of chose to talk about the game I Am Bread, a simulation of Bossa Studios (the same gaming company that developed games like Worlds Adrift and Surgeon Simulator 2013) launched on April 9, 2015. Also, the game is available on Microsoft Windows, OS X, iOS, PlayStation 4, Xbox One and Android. The reason behind choosing this game was that this game can be considered a "physics" game because its whole gameplay is based on physical concepts such as collision detection, soft body dynamics, or deformable physics. My contribution to my team's presentation was a significant one because I had to investigate the physics on which the game is based, research the purpose of the game and follow various game views or videos to better understand the game, which led to a better presentation of the team, of which I was part of. In the following images, it can be seen some of the slides which I have worked on for the presentation: