Thursday, November 28, 2013

OOP vs DDP



OOP vs DDP
            Let’s start off with introducing our Fighters, in the red corner we have OOP or object oriented programing. OOP represents code as objects that have data fields (defining attributes) and associated procedures know as methods. The code of OOP is done through classes that contain the defining features of an object. OOP will usually have inheritance to increase its usability.  For example, we can have a class that defines a Ferrari which inherits from the class car. The class car or object car is a base class that defines the car and the Ferrari class or object expands on the functionality.

 http://www.ntu.edu.sg/home/ehchua/programming/java/images/OOP_Objects.png
       
           In the blue corner we have DDP or data driven programing. DDP code groups the data and the processing required rather than defining a sequence of steps to be taken. The code of DDP is done through classes as well but instead of defining an object, it defines a data process. This means that if we were to take the same example of a Ferrari, the classes would define processes. The Ferrari would be an Entity that has many components. The components would be stuff like rendering, transform, etc. DDP is usually implemented in a component based engine like unity3D.

            There are benefits to both methods as well as their fair share of draw backs. OOP is more user friendly and more readable for a programmer. It is also easier to debug because code is set up again in a readable way. In contrast, DDP is the opposite; it can be hard to read and debug. DDP is faster and more cache coherent then OOP because the data and its processes are streamlined. Streamlining data means that the CPU doesn’t have to stop and look for data because all the data it needs is tightly packed in its cache. 

The game I have been working on is programed using OOP because we were really only formally introduced to DDP mid-way through the semester. I have worked with DDP, however, because I have made many games in unity3D which is a data driven engine.

Tuesday, November 26, 2013

Multi-Threading



Multi-Threading
Multi-Threading might sound like some sort of super complicated tech term. No fear, at the ending for this blog you should know the basics of it. First off, a thread in the land of computers is the smallest sequence of programmed instructions that can be managed independently by an operating system scheduler. We will talk about the system scheduler later on. Most of the programs I have coded only run on one thread but my laptop has 4 threads. Those other 3 threads are basically just sitting on the couch not doing anything for my program. As a result my program is running below optimal performance because those other cores could speed it up. To fully utilize my computers performance I can multi-thread my program so that it uses all four threads. Doing so is actually hard and we will talk about that later on.

An operating Scheduler is implemented by an operation system. The Scheduler schedules the time a thread has to process and the order that the threads will be processed. There are plenty of implementations of a scheduler which include but are not limited to: first in first out which acts like a queue where the first thread registered is the first one processed, and Round-robin, where the scheduler assigns a fixed time unite per process, and cycles through them.

The difference between code structure between single and Multi-Thread can be very large. One problem is that Multi-Threaded code needs to be syncretised unlike single threaded. One Advantage of Multi-Threading is that while a CPU is idling it can be used to run another thread. When coding for a multi-threaded program, the code needs to be as independent as possible so that they can be run on a thread. For example physics can be run on one thread and rendering on another. This example will need to sync up the physics and rendering when drawing to the screen because the physics needs to give data to the rendering logic. To sync the two threads one thread has to be locked so that it stops processing data. If the thread is not locked the data that is being read by the rendering might at the same instance be written to by the physics. The outcome cannot be well defined but regardless the values read will be garbage. For every Lock on a thread there needs to be an unlock so that the thread can continue being used.

Multi-Threading gives a developer the ability to use an idling thread and make it process another thread of their program. In addition if a thread gets a lot of cache misses other threads can take advantage of the unused resources.  If several threads process the same data they can begin to share their cache. Overall, Multi-Threading increases performance and fully utilizes the computing power that a computer has to offer.

Monday, November 18, 2013

Navigation Mesh Revisited



Navigation Mesh Revisited 

I would like to revisit navigation meshes as I feel that my last blog was a little slim in information. Moving on, Navigation Meshes are used as a data structure that defines the traversable areas for AI path finding. Navigation Meshes differ from how waypoints represent the traversable areas because instead of a series of connected points, a navigation mesh represents it as a connected graph of convex polygons. Unlike waypoints, the navigation mesh does not have to sample a waypoint directly because it represents a more complete version of the level. This can additional information makes Navigations meshes a better choice, for instance, they can help entities turn corners more fluently. 

Figure 1 shows the difference between the path calculated by a Navigation mesh, and that of a more traditional method. We can notice that the path generated by the navigation mesh is much representative of the path that should be taken.
Figure 1

Now that we know about navigation meshes what is the process of actually making or generating one? First off, the easiest way is to generate them with third party applications like Recast, which is a great tool. There are plenty of algorithms for creating a navigation mesh but one algorithm does it in a very cool way. The level geometry is subjected to intense ray casting which produces a grid like mesh on the floor of the game world. The ray cast can also be done by a voxel grid with both of them having multiple parameters, such as subdivision count. This grid is then reduced or simplified into a small number of convex polygons. Figure 2 shows the ray casted grid on the floor of the game world. Note the subdivisions in the grid to increasing the accuracy of the final navigation mesh.

Figure 2

The algorithm for utilizing Navigation Meshes is virtually the same as that of traditional waypoints; They both use A* at their core. A* finds one of the shortest paths from the start to the end goal by evaluating the nodes connections and distance them. This process is easy for Navigation Meshes because the polygons in the data structure are convex, so we can guarantee that within a polygon (node) any point is a valid. This means that any connected polygons create a valid path, and A* becomes easy to apply to it. Furthermore, because Navigation Meshes are usually less complex then waypoints, path finding is more efficient because there are fewer nodes to process.

In conclusion, I would recommend Navigation Meshes over waypoints as they are easy to make, produce better results and are quicker to process.

Saturday, November 16, 2013

Navigation Meshes



Nav Mesh
            Video games are generally funnier when entities have the ability to move around. I’m talking about entities that are NPCs or characters that the player does not have direct control of their movement. Getting back to my point games need some way of giving entities the ability to move around to achieve some goal. An example of a goal would be telling a friendly entity to go to a specific location. To achieve this some games are implementing a pathing technique that uses “Navigation Meshes” as the entities treasure map of where it’s allowed to traverse.

            Navigation Meshes are a 3d mesh that maps out the walkable areas within a game world. Generally the centers of the triangles in the navigation mesh act as wav points or the edges of the triangles. The act of finding the path to move along usually is done with A* which is a path finding algorithm. Together the Navigation mesh holds the valid paths or what triangles connect and A* finds the path to the goal then it’s up to the entity to walk the path given. Below is a little example of what a navigation mesh looks like.



Wednesday, November 13, 2013

What Is Scripting



What Is Scripting
            Many Games are now using scripting languages to do high level game code. Using scripts to do high level game code or game code for that matter is an excellent way to increase productivity because scripts do not need to be compiled. This means that the coder does not need to wait for the game to compile before testing his latest changes and compilation can take a long to do depending on the size of the project.

Now what is scripting in games? Scripting is when a text file that contains information which is interpreted and then influences the game in some way. Scripts can influence the game to create entities, give properties, and define functions, classes and much more. One of the best examples of a game engine that relies heavily on scripts would be Unity. In Unity every object can have a script attached to it to influence its properties and behaviours. Below is a script that I made for one of my unity games for another class, I would like to show it as an example.

using UnityEngine;
using System.Collections;
public class Death : MonoBehaviour
{//When The Player Hits this it will send the player to its last checkpoint saved
                public Transform Player;
                void Start () {/*nothing*/}
               
                // Update is called once per frame
                void Update () {/*nothing*/}
               
                void OnTriggerEnter(Collider other)
                {
                                if (other.tag == "Player")
                                {// if player hit this send him back to the last checkpoint
                                                Player.GetComponent<ThirdPersonController>().GotoChechpoint();
                                }
                }
               
}

What we can notice from this simple script (all it does is moves the players position to the last saved Checkpoint) is that the scripts have functions that are exposed from the game engine, GetComponent being one of them. Also that functions define in another class defined in another script can be called within this script. These interactions between scripts are easy to do and act similar to how code would normally be done but with the advantage of being quick to make and no compilation time.

I would highly recommend having some sort of scripting in any game project. Below is a link to the game that uses this script, it’s called Hover Tank which is extremely difficult and a link to the Unity home page.

Hover Tank:

Unity Home Page: