Game Development Reference
In-Depth Information
for(inti=0;i<numParticles; i++)
addParticle();
}
Also notice that we specify the size of the vertex buffer, the batch size,
and the starting offset.
The resetParticle method creates a snowflake with a random
x- and z-coordinate position inside the bounding box and sets the
y-coordinate to be equal to the top of the bounding box. It then gives
the snowflakes a velocity so that the snowflakes fall downward and
slightly toward the left. The snowflakes are colored white:
void Snow::resetParticle(Attribute* attribute)
{
attribute->_isAlive = true;
// get random x, z coordinate for the position of the snowflake.
d3d::GetRandomVector(
&attribute->_position,
&_boundingBox._min,
&_boundingBox._max);
// no randomness for height (y-coordinate). Snowflake
// always starts at the top of bounding box.
attribute->_position.y = _boundingBox._max.y;
// snowflakes fall downward and slightly to the left
attribute->_velocity.x = d3d::GetRandomFloat(0.0f, 1.0f)*-3.0f;
attribute->_velocity.y = d3d::GetRandomFloat(0.0f, 1.0f)*-10.0f;
attribute->_velocity.z = 0.0f;
// white snowflake
attribute->_color = d3d::WHITE;
}
The update method updates the position of the particle and then tests
if the particle has gone outside the system's bounding box. If it has
gone outside the bounding box, we respawn it.
void Snow::update(float timeDelta)
{
std::list<Attribute>::iterator i;
for(i = _particles.begin(); i != _particles.end(); i++)
{
i->_position += i->_velocity * timeDelta;
// is the point outside bounds?
if( _boundingBox.isPointInside( i->_position ) == false )
{
// nope so kill it, but we want to recycle dead
// particles, so respawn it instead.
resetParticle( &(*i) );
}
}
}
Search WWH ::




Custom Search