Monday, June 8, 2015

Sprink!

Sprink has been released for iOS and PC!

http://www.basemetalgames.com/sprink.html

As spring awakens from a long winter's rest, Sprink awakes and begins a journey to find a sunflower! Ascend skyward through the seasons, to the stars!

Sprink is a heartwarming, family-friendly platform jumping game about rebirth, working towards your goals, and picking yourself up again when you fall - with a little help from your friends. Enjoy!

In April, I did a month-long game jam on a forum of a friend of mine (http://www.supershigi.com/forums/ - Laura Shigihara, composer for Plants vs. Zombies, game developer and all-around awesome person).  The theme for the game jam was "Spring" - so, of course, the game starts with the story of a spring awakening in the spring.

I had a lot of fun making this game.  It was difficult to get everything scheduled and done in one month, as I not only had to code the game, but also do all of the artwork, music, menus, and probably a dozen other things I have forgotten about.

The last month has been spent fixing up things for iOS, and waiting for approval on the App Store.

Overall, it went quite smoothly.  The game is fairly short, but there is a lot of heart put into it, and you can replay it a lot to try to beat your high score.  :)

Enjoy!

Thursday, January 22, 2015

Interpolation

Interpolation is basically just the creation of intermediate points between two points.  In games, this is quite often used to move an object smoothly from one place to another over several frames.

Linear Interpolation


The basic form of interpolation is linear interpolation (lerp).  This basically looks like:

p = p0*(1 - t) + p1*t

where:
p0 is the start value
p1 is the end value
t is a value between 0 and 1 representing the interpolation amount
p is the current value

This basically represents a straight line segment between p0 and p1, with t representing how far along the line we are.

An equivalent representation is:
p = p0 + (p1 - p0)*t

This form does have some possible performance advantages.  First, there is only one multiplication to perform.  Second, since p0 and p1 are constants, it is also possible to pre-compute the subtraction.

This basic formula also works for vectors, so p0 and p1 can be single floating point values, 2D or 3D points, or vectors of any other size.

This is such a common and basic function that it is an intrinsic function in shader languages.

Normalization


Quite often you will find that the values you want to use as the interpolation amount (t) are not between 0 and 1.

If you have a low and high range, it's fairly straightforward to normalize the values to a range between 0 and 1:

t = clamp((t0 - x0) / (x1 - x0))
where:
x0 is the minimum range value
x1 is the maximum range value
t0 is the current value
clamp is a function that forces values less than 0 to be come 0, and greater than 1 to become 1
t is the normalized value

Smooth Interpolation


Suppose we have three points, p0, p1, and p2.  We can interpolate between p0 and p1, and then from p1 to p2.  This allows us to follow a path through multiple points, but the results are usually not very satisfactory.  For motion of an object, changes in speed and direction are very abrupt and unnatural. 

One easy way to smooth out these transitions is to modify the value of t before doing the interpolation.  The main advantage to this is the ability to reuse the basic linear interpolation function for various non-linear interpolation methods.

Smoothstep is a commonly used function for this purpose.  The smoothstep function, with its first and second derivatives:
t = 3*t0^2 - 2*t0^3
t' = 6*t0 - 6*t0^2
t'' = 6 - 12*t0
where:
t0 is the initial value
t is the new value
t' and t'' are the first and second derivatives

Because t' (the first derivative) is zero at t0=0 and t0=1, there will be less apparent discontinuity as we pass through each point.

The second derivative is still non-zero at t0=1, which may still be perceived as a discontinuity.  In terms of physics, this would be the same as having zero velocity at the end point, but non-zero acceleration.

This function is common enough to be, like lerp, implemented in shader languages.  Implementations for smoothstep typically also include the normalization step, so t = smoothstep(0, 1, t0) is equivalent to the basic smoothstep function.

A smoother possible function is:

t = 6*t0^5 - 15*t0^4+10*t0^3
t' = 30*t0^4 - 60*t0^3 + 30*t0^2
t'' = 120*t0^3 - 180*t0^2 + 60*t0

This will have first and second derivatives equal to zero at t0=0 and t0=1.


Other Curves



There are many other possible functions that you may find useful, depending on the circumstances.  In general, any function that gives output values in the range [0,1] for input in the range [0,1] will work.

t = t0^2
Fast End - starts slowly at t=0, but increases rapidly towards t=1:

t = t0*(2-t0) = 1-(1-t0)*(1-t0)
Fast Start - essentially the inverse of Fast End

t = 2*t0^3 - 3*t0^2 + 2*t0
Fast Start End - fast start, slow middle and fast end values

t = 0.5 - cos(PI*t0)*0.5
Cosine interpolation

Conclusion


I have found it useful to have all of these curves available (and their derivatives and integrals) available in a general-purpose library.

Hopefully there is some useful information here that helps you with your own programming!