16.12.2012 Views

Trent Polack's OpenGL Game Programming Tutorials

Trent Polack's OpenGL Game Programming Tutorials

Trent Polack's OpenGL Game Programming Tutorials

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Jeff Molofee's <strong>OpenGL</strong> Windows Tutorial #3<br />

// The Emitter Has A Direction. This Is Where We Find It:<br />

random_yaw = (float)(RANDOM_FLOAT*PI*2.0f);<br />

random_pitch= (float)(DEG_TO_RAD(RANDOM_FLOAT*((parent->angle))));<br />

// The Following Code Uses Spherical Coordinates To Randomize<br />

// The Velocity Vector Of The Particle<br />

velocity.vertex[0]=(cosf(random_pitch))*(parent->velocity.vertex[0]);<br />

velocity.vertex[1]=(sinf(random_pitch)*cosf(random_yaw))*(parent-<br />

>velocity.vertex[1]);<br />

velocity.vertex[2]=(sinf(random_pitch)*sinf(random_yaw))*(parent-<br />

>velocity.vertex[2]);<br />

// Velocity At This Point Is Just A Direction (Normalized<br />

// Vector) And Needs To Be Multiplied By The Speed<br />

// Component To Be Legit.<br />

new_speed= ((parent->speed)+(RANDOM_FLOAT*(parent->speed_counter)));<br />

CHECK_RANGE(new_speed, MIN_SPEED, MAX_SPEED);<br />

velocity.vertex[0]*= new_speed;<br />

velocity.vertex[1]*= new_speed;<br />

velocity.vertex[2]*= new_speed;<br />

// Set The Particle's Parent System<br />

Set_ParentSystem(parent);<br />

}<br />

Phew, I am glad that function is over with! For a quick note, CHECK_RANGE is a macro that checks to make sure<br />

that a variable (the first argument) is within a minimum (second argument) and the maximum (last argument)<br />

range. Also, look at these 3 lines:<br />

velocity.vertex[0]=(cosf(random_pitch))*(parent->velocity.vertex[0]);<br />

velocity.vertex[1]=(sinf(random_pitch)*cosf(random_yaw))*(parent-<br />

>velocity.vertex[1]);<br />

velocity.vertex[2]=(sinf(random_pitch)*sinf(random_yaw))*(parent-<br />

>velocity.vertex[2]);<br />

This may be a speed caution, but it also creates the exact effect that we want. I tried using our SIN and COS<br />

tables, but they created explosions that were TOO perfect (it basically created a perfect sphere, instead of a sphere<br />

with 'bumps'). So, sometimes it is necessary to do dynamic calculations.<br />

Next on our list is to show the particle's update function. This function updates the particle that the class currently<br />

represents ONLY (this is all handled within the particle system's update function). Lets go through this function bit<br />

by bit (or, since this is a pretty large function, byte by byte):<br />

bool PARTICLE::<br />

Update(float time_counter)<br />

{<br />

static VERTEX3D attract_location;<br />

static VECTOR3D attract_normal;<br />

This is the start of our update function, we are going to be using the two variables shown here for our particle<br />

attraction calculations (this is one of the FEW special effects that I have included within the engine, and it is for the<br />

particle's attraction to the emitter).<br />

// Age The Particle By The Time Counter<br />

age+= time_counter;<br />

if(age>=dying_age)<br />

http://nehe.gamedev.net/gametutorials/lesson03.asp (5 of 14) [20/08/2001 22:34:18]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!