Procedural Track Generation in Unity3D

This post is basically how to generate a procedural/dynamic road or a  track at run time in Unity3D.

Work-flow:








Prerequisite: Check out this link

Final Result:



This mesh is actually an extrusion of the following 2D surface -














In 3D, this is how it looks -










Explanation of terms -
Block - 1,2,..n - The entire track is built of a sequence of building blocks. This is the repeating entity in the track

Points - P1, P2.. Pn - Points that define the track (input for this generator)

Sequence - S1, S2,.. Sn - The track can be thought of these 8 sequences of vertices (4 are shown above). Each sequence originates from the corners of the flat 2D surface above

Lets call this Track-Generator, hmmm.. let's see.. GUS ( I'm sure AlterVik would have given it a better name... eviiiiiiiil).

Gus needs an Array of Vector3, which is the list of Points described above. GUS generates the track along these points. This means that when GUS gets hungry, you create a spline (line) in your fav 3D editor and export the points and feed it to GUS.

How GUS works - 
First of all GUS assumes this is not a loop (To loop, GUS expects you to mutate it and create GEORGE, GEORGE LOOGUS). Secondly, GUS expects at least 2 points. Anyway...

1. Gus calculates the number of blocks required as ( # of Points -1 ) and calls it "n" [ n = #Points -1 ]

2. Gus knows there are 8 Sequences of vertices in track as calls it "S"[ S = 8]

3. Gus then identifies number of vertices required as 18 + (n-1) x 8
[ 16 for 1st block, +8 for each after ]

Now GUS loops S=8 times (for each Sequence required) -
   For this Sequence
       GUS finds out offset [width (w) and height (h)] of the sequence from the center
        For each Block
           GUS finds out in which direction the track is moving [ P(n+1) - P(n)]
           GUS finds out Left Direction from this [ See Note at the end ]
           GUS adds point [ P + Left * w + Up * h]

Now it's time for GUS to identify the Triangles...
GUS knows there are n x 16 Tris, hence n x 16  x 3 points to define all the Tris.













Like before, GUS loops S=8 times (for each Sequence required) -
  For this Sequence
     For each Block
        Green Tri's points - (i)th and (i+1)th vertex from Sequence S, (i)th from S + 1
        Red Tri's points - (i+1)th  vertex from Sequence S, (i)th and (i+1)th from S + 1

That's it. I haven't explained the complete workflow as given in 1st fig, you can refer the code below and this is in no way optimized and bug free... so basically WTF License.

Notes:
1. While calculating the direction in which the Track is being generated, GUS assumes that the Road is not banked. Reason being, GUS only has a line as direction, there can be infinite "Up" vectors. Hence, GUS assumes "Up" to be global "Up" axis. Based on this, GUS does Cross-product of "Up" and "Forward" to get "Left".

To have banking, points given to GUS should also have associated Left or Up direction data. In short, GUS should be mutated into BOGUS - to calculate direction based on BOth these data.

2. GUS no like UV mapping for now.

Code
Save this link.
Add the script to a game object and set the width and height parameters.


If it helped you in some way, leave a comment so that I know someone out there is actually reading all this x_X

Yowza year for the Gaming Industry

Everything's going web-based, things I probably never imagined.

When I started with Unity3D I thought, well here's a platform for both Windows and Web. All we need to do is develop for one and the other fits right in. Last year,  to develop 3d on the web, there weren't much options - it was either Unity or Flash (software based rendering of course)

Times have changed... drastically. Or was I not paying attention? x_x

As of now, these are the following ways to publish 3D games on the web -
Unity 3D
Flash 11 (Molehill as of now)
Silverlight 5
WebGL

Lets take out WebGL, cos I feel it's not mature enough (read browser compatibility)

As of 2010, whatever 3D you'd do on the web would leave out an important target - Linux.

Adobe, being the target of sick-mindless-cribbing -whoa-HTML5-will-kill-it people has proved once again what it is to be a visionary.

Talk about development options for Flash 11 - Unity3D, Epic's UDK and Away 3D.
[ I was wondering why Unity came up with this Flash export option. They expected Epic to? ]

Silverlight 5 RC also means you can have your XNA games on the Web! ( I really never imagined this day would come)

Looks like "Browser" is indeed becoming a Platform. When Google said this,  it sounded ridiculous to me. Well now I'm a little.. never mind.. not gonna admit it. Well, Is Google Chrome headed in the right direction then? I don't know actually.. but I have this zoinks-scoob-something's-fishy feeling that it's gonna revolutionize gaming platform.

My predictions - 
1. Chrome is gonna change the equation for WebGL ( it's the only thing that can actually be 100% platform agnostic).
2. Chrome and Android are gonna leverage the same tech for making web = mobile equation true

Can't wait for Google to release their Game Engine (hope it's not in Go.. fingers crossed)

Air Hockey in Unity3D

Simple prototype I made in Unity3D. (Requires Unity web-player plugin)



Things to ponder -
It's a pain in the brain (O_o) to configure the Physics engine to work for simple things like this, that too when scaled to real world.


First i had to setup a few  parameters to tell Physx to consider collision at very small dimensions


Then I had to tell it to use "Bouncy" material


Then I had to tell it to confine rotation and stuff so that Puck doesn't fly-off.



I still couldn't get the desired effect, and like icing on cake - there is no "Cylinder" collider in Unity, nor a 2D-Circle thingy. So eventually, i was frustrated with all this, did my very-simple-stupid-flawed Circle-circle collision x_x

This proto has NO rigid bodies at all, the physics engine is probably like "Dude.. I'm bored >.<"

Lessons learnt -
To make small games like these, it's probably a good idea to scale everything up

Setup game dimension (screen width & height) before-hand and use the same settings while developing / testing

Programming the AI (so to speak) / NPCs is always more difficult than what you initially assume

Dropbox is awesome to host your blog content

Failure

"Ever tried. Ever failed. No matter. Try Again. Fail again. Fail better." – Samuel Beckett