Snake on a Sphere: Procedural Meshes

The main character of my game "Snake on a Sphere" is a snake ... on a sphere but what people might not be aware of is that this character is entirely generated in code.

3b968.gif

Part of the reason is that I'm not that proficient with modeling, articulating, and animating. But even if I was, there's a lot of power in creating the mesh in code. So roughly, there are two things you have to do:

  • Define the vertices of the mesh.
  • Define the triangles of the mesh.

In my case, I wanted to have a snake, so it's roughly a squiggly cylinder. The way I created that was to define a spine that trails behind the head and then for each point in that spine, I define a coordinate system:

  • "Forward" is in the direction between each point and the point in front of it.
  • "Up" is away from the center of the sphere.
  • "Right" is the vector cross product of the previous two.

Now we can define a circle using "Up" and "Right" and then connect up neighboring circles with the appropriate triangles between circles and voila, we have a snake! Well there's a little more to it with shaping of the head and such, but that's the basic idea. You can see more details in the code for my snake. Enjoy!