Moving Space Dust Effect         How to code a particle effect attached to a moving SceneNode, so that the particle effect follows it

This snippet shows how to code a particle effect attached to a moving SceneNode, so that the particle effect follows it.
Perfect for a space dust effect, or for rain.

Captain Nemo:

Here is a snippet from my own code. The particle system is attached to a child scene node of the camera. If a particle is further away from the camera than some specified distance I mirror it along its direction vector to the camera. E.g. if the particle is in front of the camera I position it behind the camera, if it's to the left I position it to the right etc.

This code also changes the size of the particles to avoid them becoming too large if they are close to the camera.

```bool XXX::frameStarted(const FrameEvent& evt)
{
const float maxDist  = 250.0;
const float mirrorDist = maxDist*0.99;
const float dimFactor = 0.8*0.005*0.005;
const float maxDist2 = maxDist*maxDist;
Camera* cam = mCamera->getCamera();
const Vector3& camPos = cam->getWorldPosition();

ParticleIterator pit = mParticleSystem->_getIterator();

while (!pit.end())
{
Particle* particle = pit.getNext();
Vector3& pos = particle->position;
particle->timeToLive = 999999.0f;
Vector3 pDir = pos-camPos;
float dist = pDir.squaredLength();
float dim = dist*dimFactor;
particle->setDimensions(dim, dim);

if (dist > maxDist2) {
pDir.normalise();
Vector3 p = camPos-pDir*mirrorDist;
particle->position = p;
}
}

return true;
}```

Copied with permission from this topic: http://www.ogre3d.org/phpBB2/viewtopic.php?t=19122

37 online users