Game Development Reference
In-Depth Information
The resetParticle method sets the position of the particle to
the current camera position and sets the velocity of the particle to the
direction that the camera is looking times one hundred units. In this
way, the “bullets” will fire in the direction we are looking. We color the
particles green.
void ParticleGun::resetParticle(Attribute* attribute)
{
attribute->_isAlive = true;
D3DXVECTOR3 cameraPos;
_camera->getPosition(&cameraPos);
D3DXVECTOR3 cameraDir;
_camera->getLook(&cameraDir);
// change to camera position
attribute->_position = cameraPos;
attribute->_position.y -= 1.0f; // slightly below camera so it's
// like we're carrying gun
// travels in the direction the camera is looking
attribute->_velocity = cameraDir * 100.0f;
// green
attribute->_color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);
attribute->_age = 0.0f;
attribute->_lifeTime = 1.0f; // lives for 1 seconds
}
The update method updates the position of the particle and kills the
particle if it has aged to death. Afterward, we search the particle list
and remove any dead particles.
void ParticleGun::update(float timeDelta)
{
std::list<Attribute>::iterator i;
for(i = _particles.begin(); i != _particles.end(); i++)
{
i->_position += i->_velocity * timeDelta;
i->_age += timeDelta;
if(i->_age > i->_lifeTime) // kill
i->_isAlive = false;
}
removeDeadParticles();
}
Search WWH ::




Custom Search