Thread: should this algorithm work?

  1. #1
    Join Date
    Jan 2003

    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

  2. #2
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    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.
    Last edited by dbgt goten; 03-24-2003 at 10:15 AM.

  3. #3
    Join Date
    Jan 2003
    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

    	Position.x = ((int)rand()%1200) - 600;
    	Position.y = ((int)rand()%1200) - 600;
    	Position.z = ((int)rand()%1200) - 600;
    	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.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    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.

  5. #5
    Join Date
    Jan 2003
    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


    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 {
    Vector Position;

    class Meteor: public GameEntity {
    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).
    Last edited by Silvercord; 03-24-2003 at 01:35 PM.

  6. #6
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    I know i'll get flamed for this but whenever I try to insert a picture
    it asks for an address or something. What do I do?

    Don't be angry I didn't answer your question; Be happy I bumped it up!
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  7. #7
    Join Date
    Jan 2003
    Its okay I've solved the problem anyway. What do you mean by address?? A web address? I honestly don't know what you mean. I know that when you aren't logged in you cannot have the options of posting pictures anyway. Also you must make sure you are uploading a supported file type (gif jpg png txt zip bmp jpeg cpp c h hpp)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. If you are employed as a programmer, please look
    By Flood Fighter in forum A Brief History of
    Replies: 10
    Last Post: 09-28-2004, 02:35 AM
  3. Database Search Algorithm
    By Krupux in forum C Programming
    Replies: 1
    Last Post: 08-28-2003, 09:57 PM
  4. Algorithm question
    By PJYelton in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 10:52 AM
  5. string searching
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-07-2001, 09:59 AM