I am looking for an algorithm to generate
spheres, cylinders, and arcs with triangles
only. I want to provide the dimensions of the shape (ie. radius, coordinates, height) and have an object composed of triangles.
Basically what Im trying to do is do some collision detection using coldet. From my understanding, you have to have a shape consisting of triangles and… I want to test collisions on these shapes.
If there is an easier way, please let me know. I just figured that there was a script or something already out there to do the shape conversions to triangles and that it would be pretty painless.
Thanks for any help.
Maybe useless but the red book (and in the sample gl code in the sdk) has recursive funcs for sphere generation.
You could take a look at the GLUT sourcecode (or even use GLUT), since it contains glutSolidSphere() which generates spheres build out of QUADs.
And it is quite easy to split each quad in two triangles using the same vertices.
If you’re just going to use it for collision detection, composing those primitives into triangles is a bad way of doing it. You get less exact shapes, and they’re much slower to test against. Instead, use specialised code to check for intersections between spheres, cylinders and other simple geometric primitives. Check out http://www.realtimerendering.com/int for lots of intersection formulas and code.
Thanks for the suggestions guys! I’ll let you know how things turn out.
Well, just in case anyone is interested…
Cylinders were pretty easy to triangulate! All you need to do is:
Divide a circle into ‘x’ parts. 32 looked pretty nice.
Get the sin and cos for each of the 32 sections and you have your (x,y) pair for your triangles. Store these in an array.
Draw triangles between each x,y pair, the x,y pair next to it on the circle(ie x+1, y+1), and the center of the circle. If your circle is looking jagged, add more points.
Do the same thing for the bottom of the circle, but translate into the screen a bit on the Z axis (behind the 1st circle).
Now, go through each pair again, only this time your triangles go between the two circles. You draw triangles between:
a) (x,y), (x+1, y+1) in the top circle and
(x,y) in the bottom circle.
b) (x,y), (x+1, y+1) in the bottom circle,
(x+1, y+1) in the top circle…
Now you have a cylinder looking ‘at’ you. It will look like a circle until you rotate the object. Best/easiest way to do this is to modify one of the nehe tutorials so it can be interactive…
This is one of those ‘a picture is worth a thousand words’ kind of messages, but just read it again and you will get what I’m trying to say!
I found this code for spheres… http://www.cs.unc.edu/~jon/