I am working on this game in which there are simple graphics. I am trying to create a good algorithm for making asteroids. The asteroids are drawn using the Polygon function, so as you can imagine they don't look that good.
Here is my current plan. I have an array of 6 POINT structures. These POINTS are relative to the center of the asteroid. So the coordinates in them can be positive or negative. To pick the points, I simply pick random numbers like
This, however, creates some pretty strange asteroids (i.e. not real at all). I am wondering, is there a way to create decent looking asteroids at run time, or do I have to pick design the asteroids myself, and then at run time have it randomly pick a shape similar to the real asteroids game.
I would use a fractals to generate bitmap images when the program initializes. Then use the bitmaps. Get a book on graphics programming using fractals for examples.
0
a121496Author Commented:
That's really a good idea, the thought never even crossed my mind. But I would rather use knowledge I already have since this project, like many others, is just for fun. For now at least, do you know of any other, simpler ways using just your basic polygons?
0
a121496Author Commented:
You see, the problem is with the coordinates and the way they are connected. Let's say you have points A,B,C,and D
A B
| X |
C D
That's how they are connected sometimes, forming two triangles which looks really bad. Is there some way I can adjust the creation of the coordinates so this does not happen? i.e. create the coordinates in order: A,B,C, then D rather than, say, A,D,B,C?
0
Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.
You could just let the points lie on 6 equally spaced lines that radiat from the origin and use a rangom number to calculate how far the points are from the origin. (Then with a little trig, you could convert that to X.Y values).
If you wanted to get a little more fancy you allow the points to move within a triangle that is 1/6th of the place. i.e would let the points move off those radiating lines so long as they don't cross the sextant used by the adjacent points. To do that, I would calculate the distance the point is from the origine, then calculate the width of the sextant at that distance. Then calculate a randome number less that that width to figure out how far the point moves from the line at the center of its sextant.
I dont know how regular you want them, but you could use sin and cos functions found in #include <math.h>. if you are using a 6pointed object you could do something like
// Angle will be the base angle of each point, since the math.h functions work with radians, this value will be incrememted by Pi / 3 through each cycle of the loop. Offset is a random number in the range of the Angle value and the next Angle value. this way each consecutive point succeeds the next around a circle, and no point crosses any other.
for(int p = 0; p < 6; p++)
{
Offset = rand() % (Pi / 3);
the problem with that algorithm is that even though the points are randomly spaced, they all lie on the same circle, so all of your asteroids will have a roughly circular shape. to remedy this you can add another variable that is a modifier you multiply by to get the distance away from the center of the circle and get it randomly like:
this way you get irregular yet unintersecting points. and you can adjust the irregularity of it up and down by modifying the way you get the random value.
>> the problem with that algorithm is that even though the points are randomly
>> spaced, they all lie on the same circle, so all of your asteroids will have a
>> roughly circular shape. to remedy this you can add another variable
>> that is a modifier you multiply by to get the distance away from the
>> center of the circle and get it randomly like:
That is precislely what I suggested. i.e. have the points lie on 6 radial lines at randomly determined distances from the origin.
I had defined DEG2RAD elsewhere in the program just FYI. This seems to generate what I wanted so here is your "A" =)
0
a121496Author Commented:
nietod - I have posted a separate 50 point question for you since your idea of the fractals is a good one =)
0
a121496Author Commented:
BTW, in case either of you happen to know the answer, is there someway I can just draw the outline of the polygon rather than have it filled as well? I am trying to get this to look fairly similar to the original one. Should I just use the Polyline function for it? That must be it, stupid question... either way, if either of you have anything further to add, I'm still here =)
If speed is important, use the fact that the sin and cos of each of the 6 angles will be constant, so calculate them once and put them in a table (array).
the only drawing tool I have used is OpenGL, and with this you can specify GL_POLYGON or GL_LINELOOP. is there something analogous in your drawing tool?
PolyLine() will just draw the outline. But I think you will have to add a copy of the first point to the end to make it contect back and close, otherwise the last segment will be missing.
0
a121496Author Commented:
Yes, Polyline did it, and you're right, I did have to add that extra point at the end. But in any case, thank you for all of your help, both of you! Now you can answer my next question: