Sign in to follow this  
c4c0d3m0n

Explosion code

Recommended Posts

c4c0d3m0n    100
Hello all. I've created a simple function a long time ago that could create an explosion of particles in 2D. In order to calculate the velocity in x and y directions from an angle, I used this code:
    velx = velocity * cos(torad(angle));
    vely = velocity * sin(torad(angle));
In order to create the explosion, I'd provide a random angle (float) and a random velocity (float) capped at a maximum. How would I tackle the maths if I want to do a similar explosion in 3D? I guess I need two random angles, since one won't cut it for all three dimensions. But how would I combine them?

Share this post


Link to post
Share on other sites
Tromack    122
http://en.wikipedia.org/wiki/Spherical_coordinates

You are correct that you would need 2 angles, azimuth and elevation (or zenith depending on what you want your 0 to mean).

Share this post


Link to post
Share on other sites
JimmyDeemo    156
I'm just gonna put this one out there, as i'm just thinking on my feet here.

You could pick a random point, then work out the vector between that and the center of the explosion, normalise it to give you the direction then apply a random magnitude to it.

Share this post


Link to post
Share on other sites
MJP    19786
You can use spherical coordinates with rho == 1 (what you did there in 2D was actually use polar coordinates). Converting from spherical to cartesian is pretty easy, the equations are listed on the wikipedia entry. However there's a trick: simply generating two random angles (for phi and theta) won't get you a uniform distribution on a sphere. this page and this page talk about what happens with the naive approach. The easiest way to get around this is to do what the second page suggests:


// Assume that RandFloat returns a random floating-point number within the specified range

float phi = RandFloat(0, 2 * pi);
float theta = acos(1 - RandFloat(0, 2));
velx = velocity * sin(phi) * cos(theta);
vely = velocity * sin(phi) * sin(theta);
velz = velocity * cos(phi);

Share this post


Link to post
Share on other sites
c4c0d3m0n    100
After doing reading and some testing, I've found the trigonometric approach to be very hard. I really like Jimmy's idea, and that's why I chose to go for it. This is my current explosion code:

void ParticleSystem::addExplosion( const Particle& ptcl, float maxvel, int magnitude )
{
for( int i = 0; i < magnitude; i++ ) {
// Velocity is between maxvel/2 and maxvel
float random_vel = (float)rand()/RAND_MAX * (maxvel/2) + (maxvel/2);

// Create a random point - rand()-rand() also creates negative numbers
Vector random_point( rand()-rand(), rand()-rand(), rand()-rand() );
// Take the unit vector and multiply it times the random velocity
random_point = random_point.unit() * random_vel;

// Add the particle and set its velocity
system.push_back( ptcl );
system.back().vel = random_point;

}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this