private void initParticle(Particle particle) { // Init the position of the particle. This is based on the source position of the particle // emitter // plus a configured variance. The random_minus_1_to_1 macro allows the number to be both // positive // and negative particle.position.x = (float) (sourcePosition.x + sourcePositionVariance.x * random_minus_1_to_1()); particle.position.y = (float) (sourcePosition.y + sourcePositionVariance.y * random_minus_1_to_1()); particle.startPos.x = sourcePosition.x; particle.startPos.y = sourcePosition.y; // Init the direction of the particle. The newAngle is calculated using the angle passed in and // the // angle variance. float newAngle = (float) degreesToRadians((float) (angle + angleVariance * random_minus_1_to_1())); // Create a new Vector2 using the newAngle Vector2 vector = new Vector2((float) Math.cos(newAngle), (float) Math.sin(newAngle)); // Calculate the vectorSpeed using the speed and speedVariance which has been passed in float vectorSpeed = (float) (speed + speedVariance * random_minus_1_to_1()); // The particles direction vector is calculated by taking the vector calculated above and // multiplying that by the speed particle.direction = Vector2.multiply(vector, vectorSpeed); // Set the default diameter of the particle from the source position particle.radius = (float) (maxRadius + maxRadiusVariance * random_minus_1_to_1()); particle.radiusDelta = (maxRadius / particleLifespan) * (1.0f / MAXIMUM_UPDATE_RATE); particle.angle = degreesToRadians((float) (angle + angleVariance * random_minus_1_to_1())); particle.degreesPerSecond = degreesToRadians( (float) (rotatePerSecond + rotatePerSecondVariance * random_minus_1_to_1())); particle.radialAcceleration = radialAcceleration; particle.tangentialAcceleration = tangentialAcceleration; // Calculate the particles life span using the life span and variance passed in particle.timeToLive = (float) Math.max(0, particleLifespan + particleLifespanVariance * random_minus_1_to_1()); // Calculate the particle size using the start and finish particle sizes float particleStartSize = (float) (startParticleSize + startParticleSizeVariance * random_minus_1_to_1()); float particleFinishSize = (float) (finishParticleSize + finishParticleSizeVariance * random_minus_1_to_1()); particle.particleSizeDelta = ((particleFinishSize - particleStartSize) / particle.timeToLive) * (1.0f / MAXIMUM_UPDATE_RATE); particle.particleSize = Math.max(0, particleStartSize); // Calculate the color the particle should have when it starts its life. All the elements // of the start color passed in along with the variance are used to calculate the star color Color4f start = new Color4f(0, 0, 0, 0); start.red = (float) (startColor.red + startColorVariance.red * random_minus_1_to_1()); start.green = (float) (startColor.green + startColorVariance.green * random_minus_1_to_1()); start.blue = (float) (startColor.blue + startColorVariance.blue * random_minus_1_to_1()); start.alpha = (float) (startColor.alpha + startColorVariance.alpha * random_minus_1_to_1()); // Calculate the color the particle should be when its life is over. This is done the same // way as the start color above Color4f end = new Color4f(0, 0, 0, 0); end.red = (float) (finishColor.red + finishColorVariance.red * random_minus_1_to_1()); end.green = (float) (finishColor.green + finishColorVariance.green * random_minus_1_to_1()); end.blue = (float) (finishColor.blue + finishColorVariance.blue * random_minus_1_to_1()); end.alpha = (float) (finishColor.alpha + finishColorVariance.alpha * random_minus_1_to_1()); // Calculate the delta which is to be applied to the particles color during each cycle of its // life. The delta calculation uses the life span of the particle to make sure that the // particles color will transition from the start to end color during its life time. As the // game // loop is using a fixed delta value we can calculate the delta color once saving cycles in the // update method particle.color = start; particle.deltaColor.red = ((end.red - start.red) / particle.timeToLive) * (1.0f / MAXIMUM_UPDATE_RATE); particle.deltaColor.green = ((end.green - start.green) / particle.timeToLive) * (1.0f / MAXIMUM_UPDATE_RATE); particle.deltaColor.blue = ((end.blue - start.blue) / particle.timeToLive) * (1.0f / MAXIMUM_UPDATE_RATE); particle.deltaColor.alpha = ((end.alpha - start.alpha) / particle.timeToLive) * (1.0f / MAXIMUM_UPDATE_RATE); // Calculate the rotation float startA = (float) (rotationStart + rotationStartVariance * random_minus_1_to_1()); float endA = (float) (rotationEnd + rotationEndVariance * random_minus_1_to_1()); particle.rotation = startA; particle.rotationDelta = (endA - startA) / particle.timeToLive; }