Spherical Distribution of Explosion Particles
by Rob Green · 07/04/2005 (2:07 pm) · 5 comments
In game/fx/particleEngine.cc change the following method
from
to
void ParticleEmitter::emitParticles(const Point3F& rCenter,
const Point3F& rNormal,
const F32 radius,
const Point3F& velocity,
S32 count)from
Point3F pos = axisx * (radius * (1 - (2 * sgRandom.randF())));
pos += axisy * (radius * (1 - (2 * sgRandom.randF())));
pos += axisz * (radius * sgRandom.randF());to
F32 angle = 360 * sgRandom.randF();
F32 length = radius * sgRandom.randF();
F32 maxZ = mSqrt((radius * radius) - (length * length));
Point3F pos = axisx * (length * mCos(angle));
pos += axisy * (length * mSin(angle));
pos += axisz * (maxZ * sgRandom.randF());
#2
07/06/2005 (10:56 pm)
Thanks, you just saved me several hours of painstaking work!
#3
07/07/2005 (9:50 am)
This should definitly make it into head. Hope some GG staffers see this :)
#4
07/18/2005 (9:24 am)
With this code you'll end up with a lot more particles in the center than the outer edge of the sphere. If you want even distribution, you can pick a random point within the box, then throw away any points that fall outside the radius. Using this method will also let you avoid calls to mCos, mSin, and mSqrt. I'll post code when I get home.
#5
07/18/2005 (11:49 am)
Here it is. I had to move a few things around so this is the whole loop. You can change the number of retries if you want, or go with just one try, but then the number of particles that actually show up will be less predictable. for (S32 i = 0; i < count; i++) {
float radiusSquared = radius * radius;
int retries;
int maxRetries = 25;
Point3F pos;
for(retries = 0; retries < maxRetries; retries++)
{
pos = axisx * (radius * (1 - (2 * sgRandom.randF())));
pos += axisy * (radius * (1 - (2 * sgRandom.randF())));
pos += axisz * (radius * sgRandom.randF());
if(pos.lenSquared() <= radiusSquared)
break;
}
if(retries < maxRetries)
{
Point3F axis = pos;
axis.normalize();
pos += rCenter;
addParticle(pos, axis, velocity, axisz);
}
} 
Torque Owner Qing -- Paul Rosenberry