Main Categories: | Landscape | Fenceposts | Wildlife | Trees | Mushrooms | Art | Games || All Posts ||
for daily images, visit my ||Tumblr||

Tuesday, 4 March 2014

Spirals

I wrote a while back about how elegant spirals can be in computer games - in particular, they are useful if you want to iteratively cover a space with evenly distributed points, with the range expanding out from a single starting location and with one growing tip - I have used them in the past for searching for a suitable placement point in a landscape for a game object, but I'm sure there are many other applications.


Anyway, I didn't include any code in that post, so here below is a Spiral function that is ready to use. This is written in C# for Unity, but should be easy enough to convert to whatever you happen to be using.

Vector3 Spiral( float dist, float spacing ) {
float rad = Mathf.Sqrt(  dist / (Mathf.PI*spacing ));
float angle = Mathf.PI * 2.0f * rad;
 
Vector3 retVal = Vector3.zero;
retVal.x = spacing * rad * Mathf.Sin( angle );
retVal.z = spacing * rad * Mathf.Cos( angle );
return retVal;
}



'dist' is the distance along the line of the spiral from the origin, and 'spacing' is how far apart you want the coils of the spiral to be from each other (this spiral has the handy attribute of maintaining a constant radial distance between each concentric coil.)

To achieve an even distribution of points, increment the distance param by the value of spacing - that way points will be equally distant form their neighbours on the line as they are from the neighbouring coils.

Here is a screen shot of the points generated by calling Spiral( dist, 1.0f ) with dist = [0,1,2,3...] As you can see, the unit spheres are packed pretty close to 1 unit apart:



Changing the spacing to 2.0f and iterating Spiral( dist, 2.0f) with  dist = [0,2,4,6,8...] will lead to each of the unit spheres having 1 unit's space between them:



...Spirals!



No comments: