Skip to main content
GameDev.net gamedev.net
🔒 Locked

object instances questions

Started by m@ Aug 20, 2007 at 9:13 AM 2 replies 1k views
Original Post
m@
m@
Hello! Just a few quick questions about object instancing... Say I want to create a sphere object, so I use opengl to define the points via a loop say. Is this an efficient way of generating an object? Also if this were so then if I was to create a grid of these spheres can I use a loop to generate the instances such as

for( int i; i > 10; ++i)
{
   sphere (sphere + char(i));
}

I know the code probably wrong, but I would like to know a good way of creating multiple instances of a class object that generates say a sphere.... If that makes any sense, or have I got the wrong idea all together?? Thanks heaps in advance.
jpetrie
jpetrie
That really depends entirely on what "sphere" is and what it does. The fact that it looks like it's a function that takes a pointer to itself renders it rather...dubious.

If you have a class called Sphere that represents the geometry of a sphere, and you want to create a list of them, simply create a bunch of them (perhaps in a loop) and store them into a vector:
std::vector< Sphere > spheres;// Perhaps in a loop...spheres.push_back( Sphere( x, y, radius ) );
Durfy
Durfy
I agree with Jpetrie. Thats how I've done it in the past.
vector spheres;

function CreateSpheres(int count, x, y, xspacing, yspacing) {
for (int i = 0; i < count; i++) {
spheres.push_back(CSphere(xspacing, yspacing));
x+= xspacing;
y+= yspacing;
}
}

something like that should work... Excuse me for any inaccuracies in syntax etc.
This is jsut one way of doing it there are many ways. Thats the beauty of programming you control the logic! You can make your spheres as many as big as colorful as you want. It is fascinating.
-durfy
m@
m@
Excellent Thanks! Exactly what I was looking for.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.