Wednesday, 19 February 2014

The Optimal Racing Line


I've recently reached the stage in the TORCS-Adaptive project in which I need to consider implementing some more sophisticated performance measurement than simply monitoring the player/AI driver's speed. I stumbled across a rather dated but still very relevant series produced by Brian Beckman, PhD, named The Physics of Racing. There were many useful articles here, however Part 4 and Part 5 were the most relevant to this project. Part 4 of the series introduces the idea of centripetal force (centre-seeking force), and is able to produce an equation from which we can derive the maximum driving speed for a given arc radius. I won't go into the details here - those interested can see the link at the bottom of this post - but ultimately the equation derives to:

Where v is the maximum possible velocity, a is the sideways acceleration and r is the radius of the curve. Here we can see that the radius is being used almost as a scalar for the velocity - the sideways acceleration multiplied by the radius gives the maximum possible velocity. Therefore we an infer that the optimal corner arc in terms of speed alone is in fact the one with the largest possible radius. The diagram below has been adapted from the diagram shown in The Physics of Racing Part 5:


Here, w is the width of the track and W is the width of the track minus half of the car's width. When considering the optimal path we are considering the trajectory of the car's centre of mass as opposed to the paths of individual wheels. Therefore, the width of the track is slightly lower to prevent the car dragging it's wheels off the sides. The optimal path can be seen here as Line M, the line that passes through points E, Q and X. Two more sub-optimal lines that hug the sides of the track are shown for sake of example, Line o and Line i. Ro and Ri are the inner and outer radii of the curve, and also happen to be respective to the arc radius of the curves in Line o and Line i. If Ri is the radius of the curve of line i and K is the radius of Line M, it can be seen that the speed possible on Line M is far greater.

References:
Beckman, B. (1993) The Physics of Racing [online] Available at: http://phors.locost7.info/contents.htm

Tuesday, 4 February 2014

Pinball!

As part of a recent University project, I implemented a Pinball game using PhysX 3.3.0, GLUT and OpenGL for some basic rendering. I also made use of SOIL for simple image loading and BASS for audio. The game consists of a few different features, including switches, spinners and bumpers, and was designed to accurately model the physics of a pinball machine. It also marks my first ever complete (if small) game written entirely in C++! By complete, I mean an application consisting of rendering, input and audio, all of the components required for a basic game.



I have uploaded the game and it's source code (with a VS2010 solution, as anyone with more recent windows IDEs, i.e. Visual Studio 2013 should be able to open this). It can be downloaded from here.

TORCS-Adaptive Progress

I've been making some progress recently on TORCS-Adaptive, my final year project at the University of Lincoln. Thus far, I've managed to implement procedurally generated tracks into TORCS, that now function both in console racing mode and in graphical mode. The generation of new 3D models when adding new track segments still has some performance issues, however I've manage to set up a pipeline in which performance measurement and adaptive difficulty algorithms can be easily tested and analysed using AI drivers. Currently, I have a very simple AI driver set up. In order to build a completely random track, I use the 'Procedural' race mode. When in the runtime/runtimed folder, it is possible to run TORCS in console mode on Windows as follows:




This will run TORCS in the console, with the race manager contained in procedural.xml. This file can either be modified directly in a text editor, or the race can be set up within TORCS itself and it will be saved. At the end of the race, there is now a feature of the procedural library that builds the entire track, and writes it's data out to a .txt file with data readable by gnuplot. Gnuplot allows the user to then plot this track, with results such as the following:


This image shows a plot of the entire track, with accurate scaling. The values on the X and Y axis are both in metres. 

Tuesday, 26 November 2013

TORCS Adaptive Real-Time Tracks

Since my last post I've managed to get a prototype of the track generation system up and running. It currently generates and loads an entirely new model file for each segment that is added to the track, running through TORCS' existing track generation functions. These functions have all been exported to a static library, and the client has access to these functions to enable what you see in the video below.


As can be seen, this is not the most efficient method for track generation at run-time within TORCS. It does however prove that it is possible without drastic changes to the existing framework. Possible optimizations would be to only generate a single track segment's AC3D file, and somehow merge this with the existing file. However, it is not yet clear where the computationally expensive part of this process is. It may be that removing and re-attaching the node containing the track from the scene graph is expensive, or it may be the track generation itself. This needs to be further explored before deciding on how it could be optimized.

Monday, 18 November 2013

ScareJam!

Artist: Brooke Hayes (Blog, Twitter)

Back in October, I competed alongside an artist in the ScareJam at the University of Lincoln. This was an interesting experience as it's the first time I've done any rapid development, particularly focussed on programming, alone. I decided to use XNA and C#, as I have a lot of experience with this framework and can develop quite rapidly within it. The ScareJam was focussed on creating scary games, and the additional game mechanics were "Loss" and "Important Sounds". We came up with "Lost and Sound", a title with a subtle nod towards the game mechanics! Lost and Sound is a game in which you must go into an abandoned hospital in search of your lost daughter, as she's been kidnapped by the ghost that haunts it! Most of the game mechanics are intended for the player to discover alone, and the game is intended to be somewhat of a mystery. I will let you explore the rest for yourself! To play the game you'll need a wired Xbox 360 gamepad, and the XNA 4.0 redistributables which can be found here.

Download Lost&Sound.

TORCS Adaptive

For my final year project at the University of Lincoln, I am currently modifying TORCS (The Open Source Racing Car Simulator) to generate tracks at runtime. It is then intended that these tracks will adapt to the skill of the player, depending on several performance measures. The project is currently in it's very early stages, but some simple run-time track generation has been implemented alongside some telemetric functionality, though these values are not yet used in any kind of performance measure. The following videos show some of the current features.


This video shows telemetry printing data to the console at runtime about the current car. The car here is one of the 'robots' that come with TORCS, which are compiled AI drivers written in C++.


This video shows a single track segment that has been generated completely at runtime. TORCS stores all of it's 3D models, including tracks, in AC3D files. Here, a segment has been generated at runtime and an AC3D file generated for it all during the runtime of the application. The next step is to find a way to update the existing AC3D file with new segment data each time a new segment is added.

The project's Github page can be found here, and more information can be found on it's wiki.

Wednesday, 10 April 2013

Component-Based Game Engines

I've been doing some reading recently into 'Component Based Game Engines', and wanted to share a few of my findings and thoughts about it, in the hopes it might interest someone. Component Based development has existed for years in the software development sector, so it only seems natural to attempt to apply this same theory to game engines. Before you read on, I'd like to point out that this is in no way going to be a tutorial or contain any implementation of Component Based Architecture, it simply serves to outline the theory behind it. I'd also like to mention that there is no RIGHT way of implementing Component Based Architecture - different approaches can be taken by different engines, depending on personal preferences and the requirements of the engine.

Imagine we're building an engine for a very generic 3D FPS. Usually, the first thing we would do is examine what 'objects' we have in our game, and create classes for them. We would also consider any shared behavior or data that certain objects need to have. For example, we have an 'Enemy' and a 'Player', which are both entities within our physical space. They'll need a position, maybe they each carry a weapon, and they each need health points. We'd then consider some objects further down our inheritance tree, for example different types of enemies would need different behaviors so we would probably be deriving enemies from our enemy class. This is the kind of game architecture that we are all used to. The following class diagram is one we should be comfortable with, it's a simple representation of part of the object hierarchy for a 3D FPS.


Familiar enough, yes? The Player inherits different behavior by overriding the virtual Update function in the ModelEntity, as does the PowerUp. The PowerUp then has two derived classes that add additional behavior on to that. This is a well established paradigm for structuring a game engine, as in our update loop we can treat all classes derived from ModelEntity uniformly in a single data structure, and make use of polymorphism and dynamic-dispatch to handle the different behaviors of each entity.

So, what actually is Component Based Architecture? Component Based architecture is different to our usual object hierarchy in that it focuses on making compositions of 'components' to form a game object, as opposed to the game object getting it's functionality through inheritance. When applying component based architecture to game engines, I find it is easier to refer to these components as 'Behaviours'. Each of these different Behaviours have their own Update() and Draw() functions that pretty much allow them to 'do their thing', and data members that are specific to that job. For example, the component 'ModelRenderable' may contain a Model and it's Draw() function may simply render that model. It may also have a velocity, or direction, or bounding box, the functionality of which is handled by it's Update() function. We can then group these behaviours together in composite classes in order to create what we consider to be a 'Game Object'. A player, for example, may contain a 'ModelRenderable' behaviour, a 'Health' behaviour that keeps track of their health, and any other functionality needed for that specific game. We can then refer to these 'Game Objects' polymorphically, just as before in our object hierarchy approach, in a 'Game World' object, that contains a collection of Game Objects. It's Update() and Draw() functions simply call the Game Object's Update() and Draw(), which in turn can carry out all of it's different behaviours. The following class diagram is a very light-weight outline of how Component Based Architecture could be implemented for games.

So the next question is - why use Component Based Architecture? We're all comfortable with object hierarchies, they're well established so that we can all understand one another's code. Well as to the answer to that - I'm not entirely sure as of yet. There are some cases in which code in object hierarchies can become very bloated, where base classes can implement code that is not used by some of it's derived classes. This makes sense however - as if a class uses most of it's base class' functionality, why re-write it just because it doesn't use some of the other base class functionality? It would add a lot of unnecessary code. But a component based engine would avoid this issue - as objects only implement the functionality or behaviour that you know they require. I've added some links to other resources and reading that might help you further understand Component-Based Architecture.


Tuesday, 2 April 2013

Kinect Full Body Collision Model

Just a quick post on something I thought I'd share, for anyone out there who's developing games for Kinect using XNA. This is a fairly simple implementation full-body collision in 2D for Kinect, using Separated axis Theory to detect collisions. A particularly in-depth and intuitive paper about Separated Axis Theory by Johnny Huynh can be found here:
http://www.jkh.me/files/tutorials/Separating%20Axis%20Theorem%20for%20Oriented%20Bounding%20Boxes.pdf  (Accessed 01/04/2013)

This paper also explains some ways to optimize the computations used within SAT and how to use it  in three-dimensional space, so is well worth a read.

To summarize, SAT can be thought of as shining a 'light' on to any two convex shapes. These shapes produce shadows along their different axes, and if all of the axes of both shapes have intersecting shadows, then we can infer that the objects are colliding with one another. If you're not familiar with Vector Projection, you may want to go and have a read-up, since that's the main mathematical principle used in SAT. My collision model simply maps the different joints, and creates object-oriented bounding-boxes for each limb, rotating the box by the angle found between the center-point between two joints and the up vector. The implementation also stores information about the velocity of a given limb, by calculating how much and in which direction the center-point of the limb has moved between each frame. This feature is designed to help with collision response, and could be used to implement some simple 2D physics, for example. The collision detection algorithm uses a Collision Tree, a collision rectangle is drawn around the entire body, and if an entity collides with this, it can then be tested against each individual limb. This improves efficiency, as the program is not having to check for collisions against each limb every single frame for objects that are no where near the player's body.



Full source code and and an executable of the sample program can be found below. If anyone uses this in their project, I do not ask to be credited. But feel free to drop me a line so I can see what you're doing with it!

Downloads:
Source Code (VS2010)
Sample Program






Friday, 11 January 2013

Theme Park Scene Complete

I have just returned to University after the christmas break, and I am now finishing up last semester's projects. The theme park scene is now complete, and a full demo video has been created, and can be viewed below.


I am happy with how the scene has come together, although there are a lot of more advanced graphics programming concepts I now wish to explore much further. The most prominent of which is tessellation of surfaces in order to achieve better lighting effects. However, from here I wish to move away from the now deprecated fixed function pipeline and begin to explore more modern OpenGL, now that I have a grasp of some basic 3D programming techniques.

Monday, 10 December 2012

Theme Park Scene - Ferris Wheel

I have now finished most of the modelling I wish to implement into my theme park scene, the final model being the ferris wheel. The following are some images of the ferris wheel.



The carts themselves are their own model, included in the ferris wheel is an array of cart models. These are then given a position at initialization in their constructor using circle approximation.

In the following few days I will begin to add more interaction to the scene, here are the planned features:

  • Improve Camera movement with pitch rotation.
  • Add extra pre-defined viewing points
  • Add Night lighting mode
  • Add buttons (in the scene) to make both carousel and ferris wheel rotate.
  • Add Wii Remote functionality using the WiiYourself library.

Thursday, 6 December 2012

Christmas Mode!

I have added a christmas mode to the Theme Park scene, now featuring a christmas tree and snowman. These models disappear when christmas mode is turned off. The tree is made of a cylinder for it's trunk, and several cylinders used to represent it's leaves, being scaled down as they go up. I would also like to add some present models to go around the tree.


Wednesday, 5 December 2012

Theme Park Scene V2.0

Since my last update, I have done quite a major rewrite of the Theme Park scene's code and class structure in order to improve it's efficiency. Originally, when creating a primitive within a model, all that would be defined within the constructor would be the type of primitive and it's color or texture. This meant that at runtime, the program rotated, translated and scaled all of these objects into place for each model, and then again to place the models in the scene. This is a very inefficient way of generating the scene on each frame, and so I thought it would be more efficient to define primitive scale and position within a model within the constructor. This means that at runtime, all that needs to be transformed are the models themselves, greatly improving efficiency.

Whilst reworking the code, I was also able to rewrite all of my models, and in doing so add more detail to them. Below are some screenshots of the new version of my Theme Park scene.
An overview of what's completed in the new scene.

The benches are now modelled using cylinders to a more realistic effect.

The carousel has been given much more detail than the original.

An addition to the scene, this will soon be a ferris wheel.

   
Another addition to the scene is flower beds.

The fountain has been given extra detail.

 The Go Kart Track is another new addition. It uses rings to make up the track and barrier, and also the tyres around the centre. These are placed using the same circle approximation algorithm used to generate discs and rings.

An image of the tyres around the centre of the Go Kart Track.

 The river has now been dropped into the ground itself. This takes out the need for a river wall, and instead fences can be used. The fence model itself is now made to be of variable size, and the number of sections required and the length of each section can be passed into it's constructor. This creates a more realistic effect than before. The bridge has yet to be remodelled, and so is omitted in this image.


Sunday, 2 December 2012

Theme Park Scene 6

I have added a fountain model to the theme park, along with an additional version of the cube primitive with no top surface. I have also added a light model that will be used several times throughout the park, and will have lights attached to them to use when night time lighting is enabled.




Below is a video of the fountain's water. It creates a slightly more realistic effect than the river,as the subdivisions are less stretched, and the surface is a square as opposed to rectangular.

Below is an overview of the main section of the theme park. With each update I will take a screenshot from the same position in the scene, to get a picture of how it is developing.

Thursday, 29 November 2012

Theme Park Scene 5

Since my last update, I have made some alterations to the disc primitive that give it a centre point. From here, I was able to create a cone primitive to be used as the roof of the carousel. I have also improve efficiency by removing the normal calculations from the disc, and simply looped through all of the vertices and assigned them to Y 1. This prevents a considerable amount of unnecessary computation taking place. I have also created a 'ring' and 'hoop' primitive. The ring is flat, the same as a disc, however has a hole of customizable size in the centre. The hoop is a 3D implementation of the ring, using two ring primitives for the top and bottom surfaces. However, my normal calculations algorithm does not yet work properly for rings or cones, and this is what I will be working on next.

The normals for the cone primitive on the roof make it appear black.

The centre point of the disc primitive can be seen in wireframe mode.

Tuesday, 27 November 2012

Theme Park Scene 4

I have successfully added a Cylinder primitive to my program. The cylinder primitive contains two disc primitives which are used to approximate a circle for the top and base of the cylinder. From here, the cylinder allocates the top and bottom vertex, then bottom and top vertex around the discs for each face. The number of faces the cylinder has can be defined in the constructor, meaning you can get various levels of smoothness. It also means the class can be used to create prisms, passing three as the number of base points for example will produce a triangular prism. From here, I can use the disc and cylinder class as a basis to build both cones and quarter cylinders (to be used as corners on a go-kart track). I can also produce cylinders with no top face to be used as park bins, and I can also begin to model lights to go around the park. Below are some screenshots of the cylinder class being used.

A cylinder with 32 base points. Any more than 32 base points means that the indices being drawn will have to use negative values, as GLbyte is being used as the type for vertex indices.

A cylinder with 64 base points. This means 256 vertices for the central faces, plus 128 for the top and bottom discs,
a total of 512 vertices.

More vertices mean a more expensive computation when generating the cylinder, but do improve it's smoothness.

A triangular prism made by passing three as the number of base points.

Saturday, 24 November 2012

Theme Park Scene 3

Today, I have added a river to the scene. The river is created by being given four initial points as it's corners, and then this quad is subdivided using a recursive member function. The number of subdivisions can be altered via a parameter passed to the class' constructor. Each vertex is then given a random height. In order to get the river to update when the camera was not moving, I had to make use of GLUT's idle function in order to re-calculate the river's vertices and normals on each frame. This meant that I also had to call redisplay on each frame, and so at the moment all geometry is being redrawn every single frame. This is not the most efficient way of going about animating the river, but it will stay in place for the time being. Every time the river is updated, it is subdivided again. However, I wish to change this and simply generate new heights for each existing vertex inside a separate function. This will make the program more efficient. Below are some images of the river, along with an image in wireframe mode so that the subdivision of the surface can be seen.




Below is a video that shows the river as it re-generates it's vertices to create a basic water effect. It is in no way a uniform or highly realistic effect, but it works for now. Video is low quality due to the screen capture software and upload process, however when in wireframe mode the subdivisions can be seen moving.


Friday, 23 November 2012

Theme Park Scene 2

Continuing my work on the Theme Park scene, I have been attempting to add a Cylinder primitive. In order to do so, I have also created a Disc primitive to be used inside the cylinder primitive. However, I am having trouble defining a draw order for the vertices that make up the faces between the rings, and have been getting some strange results from my algorithms. My intention was to use the existing vertices in the discs to create quad surfaces between each four points. I will continue work on the cylinder primitive at another time. Some pictures of the results are below.



I have also modelled a small ring toss stand to go in the park, displayed below. This is yet to be complete, as I need a cylinder primitive to finish it.




I have also modified the scene's lighting to make it brighter. In true christmas spirit, I also intend to add a 'Christmas' mode to the Theme Park, which will change the ground to snow and spawn snowmen at various locations around the scene. If I have time before the deadline, I will also add a basic snow effect.



My final addition in this update is the ability to move the camera up and down on the Y axis. However, the camera does not rotate on it's X axis, so it does not fly freely as of yet.


Wednesday, 21 November 2012

Theme Park Scene 1

I am currently developing a Theme Park scene as part of a Graphics Programming module at University. We are using OpenGL and C++. Thus far, I have a basic class structure that allows me to inherit from a 'Primitive' class, which allows me to easily create primitive shapes such as cubes or tetrahedrons. Vertex normal calculations and texture mapping is black boxed by the Primitive class, all the derived class' constructor needs to do is define the vertices, texture mapping coordinates and draw order for the vertices, all on the constructor's stack and the primitive class creates these in the heap as it's own members. I can also inherit from a Model class, and each model can contain a number of primitives to create it's geometry. The camera also moves and rotates on the XZ plane, however I hope to expand this so that the camera is free to move/rotate in all directions. The following are some screenshots of the scene at present, with a few models implemented.

This bridge will go over a river, which will have a small and very basic ripple effect created using recursive subdivision of a single quad surface.




By my next update, I hope to have implemented an algorithm to allow me to generate circles/curved surfaces in order to create a Cylinder primitive. This will require some modification of the vertex normal calculations as well.

First Post

Hello! This blog is going to be used as a portfolio of my game development work, as well as showing progress on specific projects.