Monday, September 29, 2008

Things have been moving along in the as-yet-unnamed 2d project. Whilst making the particle system I came up with a small template class that I think is really rather useful. I needed a way to blend between a list of values of arbitrary type. The only type specific functionality required was a linear interpolation (Lerp) function... so a delegate was required. The beauty of it is that the Lerp method signature is pretty standard across the XNA framework, so I stuck with that. Here's the code (damn I really need a better way to post code):

// Generic class to store a list of times & values and
// interpolate between them.
public class FunctionLinear<T>
{
// The method signature for interpolating between two values of T
public delegate T Lerp<T>(T a, T b, float amt);

// Lerp function provided by the user
Lerp<T> Lerper;

// The sorted list of items
SortedList<float, T> Items = new SortedList<float, T>();

public FunctionLinear(Lerp<T> lerpMethod)
{
Lerper = lerpMethod;
}

public void AddItem(float time, T item)
{
Items.Add(time, item);
}

public T GetValue(float time)
{
if (Items.Count == 0)
return default(T);

// Determine the Items before and after the given time
int i = 0;
while (i <> Items.Keys[i + 1])
i++;

if (i >= Items.Count - 1)
return Items.Values[i - 1];

// Get proportion between values
float amt = (time - Items.Keys[i]) / (Items.Keys[i + 1] - Items.Keys[i]);

// Call the interpolator
return Lerper(Items.Values[i], Items.Values[i + 1], amt);
}
}

Thats it. As an example, here's how you would use it to create a function that describe a square in 2 seconds:

FunctionLinear f = new FunctionLinear(Vector2.Lerp);
f.AddItem(0.0f, Vector2.Zero);
f.AddItem(0.5f, Vector2.UnitX);
f.AddItem(1.0f, Vector2.One);
f.AddItem(1.5f, Vector2.UnitY);
f.AddItem(2.0f, Vector2.Zero);

Monday, September 15, 2008



Since I'm majorly embarrassed at how few posts make it to this blog, here's a little snippet of what I've been doing. Work on my XNA 3d engine has stalled, due to being temporarily replaced by the above 2D project.

I am lucky enough to work with 8 digital artists. Hearing about a local game competition (gamejam.org) got us inspired. A few of us decided to give it a go but realised that there was no way near enough time to get it done. Then our Dream Build Play dreams suffered the same fate. So, in the interest of this project at least seeing some light of day, above is a sneak preview of... (CENSORED). Sorry, the name is too cool to let out of the bag yet. No doubt I'll reveal it in a future blog post, and I'll get permission from the artist to blog about this project.