Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Particle Systems for Games Particle System 3D MIM, hyeon.

Similar presentations


Presentation on theme: "1 Particle Systems for Games Particle System 3D MIM, hyeon."— Presentation transcript:

1 1 Particle Systems for Games Particle System 3D MIM, hyeon

2 2 Particle System -William T. Reeves, Particle Systems : A Technique for Modeling a Class of Fuzzy Objects, SIGGRAPH 83. – Star Trek II - The Wrath of Khan The Genesis Demo sequence, 400 Particle Systems, 750,000 Particles

3 3 Revised by hyeon Particle Types Big particles Particles that have an area greater than a pixel Small Particles Smaller than a pixel Particles have no geometry Games usually use big particles in smaller quantities. Small particles are more for off-line effects.

4 4 Revised by hyeon 2D vs. 3D particles 2D Particles Easy to make using sprite system in XNA. Kinda the default version 3D Particles Particles are placed in space More complicated to implement as we will see 2D is easy and is the basis for the XNA examples. But, the particles don’t move when you move things on the screen!

5 5 Revised by hyeon What makes up a particle Position Where the particle is on the screen Velocity How fast it is moving. A vector in pixels. private Vector2 position; private Vector2 velocity; These are the basics, but there are other things that are very common.

6 6 Revised by hyeon

7 7

8 8

9 9

10 10 Revised by hyeon

11 11 Revised by hyeon

12 12 Revised by hyeon More complete particle public class Particle { private Vector2 position; private Vector2 velocity; private Vector2 acceleration; private float lifetime; private float age; private float scale; private float orientation; private float angularVelocity; /// /// Position of the particle in space /// public Vector2 Position { get { return position; } set { position = value; } } /// /// 2D particle velocity /// public Vector2 Velocity { get { return velocity; } set { velocity = value; } } // and more access properties…

13 13 Revised by hyeon Lifetime private float lifetime; private float age; Particles have a lifetime. They exist for a specific period of time, then disappear. They will fade out over the lifetime.

14 14 Revised by hyeon Scale and orientation Scale is how big particle is Orientation is angle to draw it Angular velocity is any spin private float scale; private float orientation; private float angularVelocity;

15 15 Revised by hyeon Initialization Function public void Initialize(Vector2 position, Vector2 velocity, Vector2 acceleration, float lifetime, float scale, float rotationSpeed, float orientation) { // set the values to the requested values this.Position = position; this.Velocity = velocity; this.Acceleration = acceleration; this.Lifetime = lifetime; this.Scale = scale; this.AngularVelocity = rotationSpeed; this.Age = 0.0f; this.Orientation = orientation; }

16 16 Revised by hyeon Particle updates (Euler Steps) /// /// Update for the particle. Does an Euler step. /// /// Time step public void Update(float delta) { // Update velocity Velocity += Acceleration * delta; // Update position Position += Velocity * delta; // Update orientation Orientation += AngularVelocity * delta; // Update age Age += delta; }

17 17 1 차 테일러 - 절단 오차 (truncation error)

18 18 Adaptive stepsize 두 구간 Δt Δt/2 et = |v1 - v2| h_new = h_old*sqrt(eto/et); dtnew = dt * sqrt(eto/et); 새 구간의 크기 if(dtnew < dt) // 더 작다면 루젠 쿠타 4 차

19 19 Revised by hyeon The ParticleSystem class We have a list of active particles. We add to it when we generate particles and we iterate over it to draw the particles. Note that there may be many particles created and destroyed at a rapid rate. Managed code systems use garbage collection. They don’t like things that allocate and deallocate at a very high rate of speed. This can cause pauses as the garbage collector churns! We’re going to keep a fixed number of particles that we just reuse rather than creating new ones all of the time. There will be live particles and available particles. Any suggested data structures? Note that lifetimes may vary.

20 20 Elements in the Particle System Particle System has role of managing and drawing particles – Features Scale Position of emitter Texture – Method Create and Dispose Particles Update Particles Render Particles

21 21 Revised by hyeon Lists of particles // The list of live particles private LinkedList liveParticles = new LinkedList (); // A list of available particles private LinkedList availableParticles = new LinkedList (); private void Initialize() { InitializeConstants(); for (int i = 0; i < howManyEffects * maxNumParticles; i++) { availableParticles.AddLast(new Particle()); } howManyEffects is how many active explosions or smoke puffs active at any time. maxNumParticles is the maximum particles for an effect.

22 22 Revised by hyeon Initializing Particles protected virtual void InitializeParticle(Particle p, Vector2 where) { // Determine the initial particle direction Vector2 direction = PickParticleDirection(); // pick some random values for our particle float velocity = RandomBetween(minInitialSpeed, maxInitialSpeed); float acceleration = RandomBetween(minAcceleration, maxAcceleration); float lifetime = RandomBetween(minLifetime, maxLifetime); float scale = RandomBetween(minScale, maxScale); float rotationSpeed = RandomBetween(minRotationSpeed, maxRotationSpeed); float orientation = RandomBetween(0, (float)Math.PI * 2); // then initialize it with those random values. initialize will save those, // and make sure it is marked as active. p.Initialize( where, velocity * direction, acceleration * direction, lifetime, scale, rotationSpeed, orientation); }

23 23 Revised by hyeon Update public void Update(double deltaTime) { float delta = (float)deltaTime; for (LinkedListNode node = liveParticles.First; node != null; ) { LinkedListNode nextNode = node.Next; node.Value.Update(delta); if(!node.Value.Active) { liveParticles.Remove(node); availableParticles.AddLast(node); } node = nextNode; }

24 24 Revised by hyeon Implementation

25 25 Revised by hyeon 3D Particles The same basic concepts apply, except that you have to:  Position, velocity, acceleration are all 3D, now  Draw texture mapped rectangles for particles  Use the alpha blending and turn off writing the depth buffer  Billboard the particles to face the viewer at all times 3D Particles will stay at the correct world position as you rotate the camera and are sensitive to occlusion

26 26 Particle and PointSprite Before DX 8.0, using Billboard ( always looks up the camera) Constructed by four vertices

27 27 Revised by hyeon Billboarding Forcing something to always face the view. Or more precisely: Forcing something to always face in direction (0, 0, 1) in the view coordinate system. Before we do anything, our particle texture is facing in the direction (0, 0, 1). W is a translation only, not a rotation. Any ideas?

28 28 Revised by hyeon Billboarding The view matrix will rotate us from the direction we want to be. We’ll just pre-rotate in the opposite direction. Matrix invView = camera.View; invView.Translation = Vector3.Zero; invView = Matrix.Invert(invView); Matrix world = Matrix.CreateScale(scale) * Matrix.CreateRotationZ(p.Orientation) * invView * Matrix.CreateTranslation(p.Position); effect.Parameters["World"].SetValue(world);

29 29 Revised by hyeon PointSprite

30 30 Revised by hyeon Draw – the Particle loop foreach (Particle3d p in liveParticles) { // Life time as a value from 0 to 1 float normalizedLifetime = p.Age / p.Lifetime; float alpha = 4 * normalizedLifetime * (1 - normalizedLifetime); // make particles grow as they age. they'll start at 75% of their size, // and increase to 100% once they're finished. float scale = p.Scale * (.75f +.25f * normalizedLifetime); Matrix world = Matrix.CreateScale(scale) * Matrix.CreateRotationZ(p.Orientation) * invView * Matrix.CreateTranslation(p.Position); effect.Parameters["World"].SetValue(world); effect.Parameters["Alpha"].SetValue(alpha); effect.Begin(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Begin(); device.DrawUserIndexedPrimitives (PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3); pass.End(); } effect.End(); }

31 31 Revised by hyeon Particles also useful for  Fog with varying densities  Fountains  Rain  Snow  Dust  Clouds  Falling leaves  Blood spray  Etc…

32 32 Revised by hyeon Other things you can do Gravity – Just add an acceleration of (0, -980, 0). Bounce – If Y goes below a certain amount, mirror the Y for the position and reverse the Y for the velocity. Bounce with loss – After you bounce, multiply the velocity by a constant < 1 Energy – Add a value to the particle to indicate it’s energy. Lose energy over time and change the color based on the energy. Particles can be emitted in a shape. Replace your character with a bunch of particles in her shape, then let gravity do what it does best.


Download ppt "1 Particle Systems for Games Particle System 3D MIM, hyeon."

Similar presentations


Ads by Google