Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Creating realistic asteroids

Posted on 1998-11-15
Medium Priority
385 Views
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
Question by:a121496
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 7
• 6
• 4

LVL 22

Expert Comment

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

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

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

LVL 22

Expert Comment

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

Booth882 earned 200 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

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:

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

LVL 22

Expert Comment

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

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

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

Author Comment

ID: 1177978
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 Comment

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

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

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

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

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

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

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

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. â€¦
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article locâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month9 days, 2 hours left to enroll