giving something back to the Flash community

Distributing N-points on a sphere

Tags: , , ,

distributing an unknown amount of points on a spherical shape

as far as I know, no 3D engine can handle the triangulated sphere or geosphere that one can findin 3DSmax
it basically looks like this (sorry for the crappy picture):

it’s made of triangles and as you may have noticed, triangles are good.

as opposed to the whatever sphere (again sorry for the crappy picture):

in the current engines (I think that) you have to do something like a whatever sphere (giving a height segments number and a whatever ‘slice’ number )

It happened quite often at work that I’ve been asked to ditribute a given number of objects on a sphere. When I had to do so, I usually performed a good ol’hack like counting the vertices of a sphere, then creating some temporary triangles every N face, reorient them to finally collect their centers to pin an object there…
so I was extremely glad yesterday morning when I found this : nodename (check out Alan’s work, it’s really worth it).

especially as I’ve been asked to do exactly this (distribute a given number of points on a sphere) no later than yesterday noon.

I told them: “ok, I found this article this morning but the guy ( Alan ) doesn’t release the actionscript code so I’ll have to port it from C++. give me the afternoon and I’ll let you know what happened”.

edit: I had a chance to check Alan’s code (all you’ve got to do is ask for it ^^) and it’s really worth being seen for it’s more than a straight port. He really “worked it around”.

ok, now check it out:

the distribution algorithm

Technically it’s a kind of spring mass system that works in 3D pushing and pulling vertices in all directions based on their energy. check out nodename’s post for more info.
I changed the algorithm a bit so that you can input a random set of points that will be nicely sorted and returned. there are 2 params vertices, the unsorted array, nstep which is the maximum amount of recursions. in the demo, I’m using 10 recursions which is a good ratio performances/quality.
Of course the higher the recursion amount, the nicest the result (the more violent for the processor though). and also, the algorithm normalizes the position of each vertex so after the distribution, I perform a rescale on every vertex. As they all have the same raidus, it’s not that painful.

and here’s the zip of that unbelieivable demo

The zip file includes my wonderful 3D particles engine (/*cough*/) absolutely uncommented and messy as hell yet it gives an idea of an implementation ( check the addVertice method in Scene3D). of course you can put the Distibution algorithm inside the render loop which gives a ‘shlobolob’ effect when adding a particle but tends to come absolutely unstable for an unknown reason.

Hope you’ll like it, cheers

Tags: , , ,

© 2009 HIDIHO!. All Rights Reserved.

This blog is powered by Wordpress and Magatheme by Bryan Helmig.