Solved

Creating realistic asteroids

Posted on 1998-11-15
17
361 Views
Last Modified: 2012-06-22
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
Comment
Question by:a121496
  • 7
  • 6
  • 4
17 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1177969
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 Comment

by:a121496
ID: 1177970
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 Comment

by:a121496
ID: 1177971
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:nietod
ID: 1177972
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
 
LVL 1

Accepted Solution

by:
Booth882 earned 50 total points
ID: 1177973
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
 
LVL 1

Expert Comment

by:Booth882
ID: 1177974
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
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177975
>> 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
 
LVL 1

Expert Comment

by:Booth882
ID: 1177976
sorry nietod I was writing all that before I saw your comments (if you will notice the times on them).  
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177977
I see, I thought you had missunderstood mine.
0
 

Author Comment

by:a121496
ID: 1177978
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" =)
0
 

Author Comment

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

Author Comment

by:a121496
ID: 1177980
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177981
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
 
LVL 1

Expert Comment

by:Booth882
ID: 1177982
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177983
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 Comment

by:a121496
ID: 1177984
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 Comment

by:a121496
ID: 1177985
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question