Circular explosion with definable points

Started by
11 comments, last by Emergent 14 years, 4 months ago
You don't need to compute cosine and sine over and over, or even look them up over and over. You just need to build a rotation matrix (requires one sine and one cosine of the angular increment) and then apply it over and over. Equivalently you can do this using complex numbers instead of matrices and vectors.

(Pseudo?)code:
void explode(nPoints){   dTheta = 2*PI/nPoints;   R = rotationMatrix2d(dTheta);   Vector2d p(1, 0);   for(int i=0; i < nPoints; ++i)   {      createExplosionParticleAtPoint(p);      p = R*p; // '*' here is overloaded to do matrix multiplication   }}


where

                       [ cos(θ)   -sin(θ) ]rotationMatrix2d(θ) =  [                  ]   .                       [ sin(θ)    cos(θ) ]


This kind of approach in other situations opens you up to nasty rounding problems, but given the number of particles per circle (< a few hundred, I'm guessing), the accuracy of the data types (floats I'm guessing; maybe doubles), and the purpose (graphics -- to look good) these won't accumulate fast enough to matter,

[Edited by - Emergent on December 2, 2009 7:29:38 PM]
Advertisement
Not to nitpick, but i'm nit picking :)

nPoints = 2;
dTheta = 2*PI/(nPoints-1);

dTheta then equals 2*PI, rather than PI.
Quote:Original post by bzroom
Not to nitpick, but i'm nit picking :)

nPoints = 2;
dTheta = 2*PI/(nPoints-1);

dTheta then equals 2*PI, rather than PI.


Good call. Should be divided by nPoints, not nPoints-1. I'd used the number of segments between n points on a line segment (n-1) rather than on a circle (n). Edited my original code; fixed now.

This topic is closed to new replies.

Advertisement