Link to home
Start Free TrialLog in
Avatar of a121496
a121496

asked on

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.
Avatar of nietod
nietod

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.
Avatar of a121496

ASKER

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?
Avatar of a121496

ASKER

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?
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.

ASKER CERTIFIED SOLUTION
Avatar of Booth882
Booth882

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:

int Radius

for...
{
   Radius = rand() % 25;
   
   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
>> 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.
sorry nietod I was writing all that before I saw your comments (if you will notice the times on them).  
I see, I thought you had missunderstood mine.
Avatar of a121496

ASKER

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

Booth: I have adapted your algorithm to this:

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" =)
Avatar of a121496

ASKER

nietod - I have posted a separate 50 point question for you since your idea of the fractals is a good one =)
Avatar of a121496

ASKER

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 =)
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).  
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.
Avatar of a121496

ASKER

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:

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

Another easy one if you know all about collision detection =)
Avatar of a121496

ASKER

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