• Status: Solved
• Priority: Medium
• Security: Public
• Views: 391

# Creating realistic asteroids

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

point[n].x = rand () % 25 - rand () % 25;
point[n].y = rand () % 25 - rand () % 25;

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.
0
a121496
• 7
• 6
• 4
1 Solution

Commented:
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

Author 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

Author 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

Commented:
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.

0

Commented:
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

#include <math.h>

const float Pi = 3.1415f
float Angle = 0.0f;
float Offset = 0.0f;

// 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);

point[p].x = cos(Angle + Offset);
point[p].y = sin(Angle + Offset);

Angle += Pi / 3;
}
0

Commented:
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:

for...
{

point[p].x = cos(Angle + Offset) * Radius;
point[p].y = sin(Angle + Offset) * Radius;
}

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.

let me know if you have any questions
0

Commented:
>> 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.
0

Commented:
sorry nietod I was writing all that before I saw your comments (if you will notice the times on them).
0

Commented:
I see, I thought you had missunderstood mine.
0

Author Commented:
Yes, Booth's approach and nietod's next suggestion are what I should have done.

double Angle, Offset;

for (int p = 0; p < 6; p++)
{ points[p].x = (long) (cos (Angle) * (rand () % 40));
points[p].y = (long) (sin (Angle) * -(rand () % 40));
Angle += DEG2RAD * (360 / (rand () % 2 + 5));
}

I had defined DEG2RAD elsewhere in the program just FYI.  This seems to generate what I wanted so here is your "A" =)
0

Author Commented:
nietod - I have posted a separate 50 point question for you since your idea of the fractals is a good one =)
0

Author 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 =)
0

Commented:
Thanks

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).
0

Commented:
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?
0

Commented:
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

Author 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:

http://www.experts-exchange.com/Q.10098679

Another easy one if you know all about collision detection =)
0

Author Commented:
Ignore that post above, I figured out how to do it.  I hope nobody was writing on it when I deleted it... =(
0

## Featured Post

• 7
• 6
• 4
Tackle projects and never again get stuck behind a technical roadblock.