#### Archived

This topic is now archived and is closed to further replies.

# Good random projecticles for asteroids?

This topic is 5748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m working on my game (below in my signature if you want to see) and I can''t make a good enough random velocity generator for my asteroids when they break apart. They only go in certain directions. How do I do it so it''s really random? This is what I''m using:

direction = rand()%32;
if(direction <= 7)
{
percentage = direction / 8;
percentage2 = 1.00-percentage;
direction = rand()%5+1; //for velocites

randx = (sqrt((direction * direction) * percentage));
randy = (sqrt((direction * direction) * percentage2));
}
else if(direction >= 8 && direction <= 15)
{
percentage = (direction / 8) - 1.00;
percentage2 = 1.00-percentage;
direction = rand()%5+1; //for velocites

randx = (sqrt((direction * direction) * percentage));
randy = -1*(sqrt((direction * direction) * percentage2));
}
else if(direction >= 16 && direction <= 23)
{
percentage = (direction / 8) - 2.00;
percentage2 = 1.00-percentage;
direction = rand()%5+1; //for velocites

randx = -1*(sqrt((direction * direction) * percentage));
randy = -1*(sqrt((direction * direction) * percentage2));
}
else if(direction >= 24 && direction <= 31)
{
percentage = (direction / 8) - 3.00;
percentage2 = 1.00-percentage;
direction = rand()%5+1; //for velocites

randx = -1*(sqrt((direction * direction) * percentage));
randy = (sqrt((direction * direction) * percentage2));
}

randx is the value of horizontal velocity, randy is vertical. direction is the direction, then the velocity. Thanks for your help.
Beyaan

##### Share on other sites
Find a point in the unit cube (between -0.5 and 0.5 for each component). If this point is not part of the sphere centered at (0,0,0) with radius 0.5, reject it and find another one. This point should be a velocity with a completely random direction. However, its magnitude will not be uniformly random. Simply normalize the velocity vector, and multiply it by another random number.

There may be an easier way... Ask again if you don''t understand my post.

Cédric

##### Share on other sites
I sorta understood that, but the problem is that my game is 2D, not 3D, and there''s only the xvel and yvel.

Beyaan

##### Share on other sites
You could take a random angle between 0 and 2pi radians and just set randx and randy according to the sin and cosine of that angle.

I wrote a little Asteroids demo and I tried to affect their velocities based on the original velocity and the impact point of the shot, with a 20 degree or so random variance. It might make it look a little more realistic, and will allow the player to place shots strategically.

##### Share on other sites
As a side note, rand()%32 doesn''t produce an even distribution of random numbers. You will get better results by using rand()/(65536/32) , assuming 0-65535 output from rand().

##### Share on other sites
quote:
Original post by Zeraan
I sorta understood that, but the problem is that my game is 2D, not 3D, and there''s only the xvel and yvel.

Sigh...

Ok, then Waverider''s solution is the best
angle = rand(); //Any range much larger than 2PI is fine
v = rand() /RAND_MAX * vmax; //Velocity range
vx = cos(angle) * v;
vy = sin(angle) * v;

Cédric

##### Share on other sites
Ok thanks guys. I didn''t know that rand()%anything isn''t a good way to distrubite it evenly. Guess I''ll have to fix that in my game. And for the random angle, thanks!

Beyaan

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 35
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631702
• Total Posts
3001812
×