# should this algorithm work?

• 03-24-2003
Silvercord
should this algorithm work?
I am trying to randomly place stars a fixed distance away from a fixed point in space so it creates a sphere-like formation. So far it seems to be working, as in the stars look like they're in a sphere (as shown in the picture)

Here is my algorithm:
randomly choose x, y and z positions so that it is between positive and negative 600
Normalize the Position, this means it will have a distance of one from the center of the sphere (this is where I'm not sure if its true, I'm trying to test but I am getting screwy values)
Then multiply the position by the distance away from the center that you want (again, i am getting screwy values testing but my math in the testing phase could be wrong)

Does the algorithm seem like it should work? The sphere to the left is what I'm creating, but I'm not sure if the distance from the center to each star is what I want
• 03-24-2003
Jeremy G
Im not quite sure what your normalyzing, the center position of the sphere or the star points of the spear. Im not shure. Any who, really shouldn't have to normalize anything. Generally you only normalize triangle vectors for easing translation/rotation.

EDIT: Oh yeah, it would be far more helpful if you posted the code you used to generate the points on the surface of the sphere. FAR more helpful.
• 03-24-2003
Silvercord
Well I generate points randomly on some scale. I then normalize the points on the sphere, and then multiply by the fixed distance to ensure it is x units away from the center. Here is the code

Code:

```        Position.x = ((int)rand()%1200) - 600;         Position.y = ((int)rand()%1200) - 600;         Position.z = ((int)rand()%1200) - 600;         Position.Normalize();         Position.x *= 1000;         Position.y *= 1000;         Position.z *= 1000;```
I didn't use my own operators because at the time I wrote this I was modifying my vector class. What this does is generate a position. Each component of the vector is assigned a random value from -600 to 600. It is then normalized so that the point is exactly 1 unit away from the center (that is the thinking anyway, I'm not sure if it works because i get screwy values when I am testing, and I'm still fixing the math on that aspect). Once the point is normalized, the vector is multiplied by the distance it is supposed to be from the center, in this case 1000.
• 03-24-2003
PJYelton
As long as your normalization is correct and actually produces a vector of unit length 1, then I don't see any reason why this wouldn't work.

Post your math if it is giving you problems.
• 03-24-2003
Silvercord
well I just wanted someone to say "yes that should work". This stuff is simple enough that i don't want to rely on anyone for a while :) but thx pelton

EDIT:

Ok I've fixed the problem. It was actually with the way I built my classes. I'm not sure why I didn't receive an error on this. Anyway every single object in the world that you can see is a GameEntity (base class). Due to some culling routines I decided to make my Position vector be a base class member so I could loop through every 'gameentity' object and determine if it's visible. Well lo and behold I didn't take out the Position vector from my meteor class, so I had something like this:
class GameEntity {
public:
//blah
Vector Position;
}

class Meteor: public GameEntity {
public:
//blah
Vector Position; //screwed up
}

Therein lay the problem, hence the screwy values (I was really accessing jibberish). I probably got a warning about this, but I had like 34 errors altogether when I performed a complete build (truncation of various variables and the whole bit, stuff I really shouldn't even be warned about imo).
• 03-24-2003
Stan100
I know i'll get flamed for this but whenever I try to insert a picture