Good random projecticles for asteroids?

Started by
5 comments, last by Zeraan 21 years, 5 months ago
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
Advertisement
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
I sorta understood that, but the problem is that my game is 2D, not 3D, and there''s only the xvel and yvel.


Beyaan
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.
It's not what you're taught, it's what you learn.
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().
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
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

This topic is closed to new replies.

Advertisement