9.3. P ARTICLE S YSTEMS Development of a particle system.

Slides:



Advertisements
Similar presentations
7.2. AI E NGINE AND S TEERING B EHAVIOUR I Design of an AI Engine and introduction to steering in game AI.
Advertisements

7.3. S TEERING B EHAVIOUR II Steering behaviours in game AI.
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
Using effects within a SpriteBatch or as a post render effect
4.9. P ARTICLE E FFECTS Use of particle effects within games.
UFCFX5-15-3Mobile Device Development Particle Systems.
3.1. G RAPHICS I The use of images within games. Reflections and advice on the games proposed in the Week 2 Hand-in.
4.11. T REES AND V EGETATION Rendering of trees and vegetation.
1Notes  Textbook: matchmove 6.7.2, B.9. 2 Match Move  For combining CG effects with real footage, need to match synthetic camera to real camera: “matchmove”
Computer Science – Game DesignUC Santa Cruz Today Publish/Subscribe Design Pattern Delegates Sprite movement Particles.
Computer Science – Game DesignUC Santa Cruz Game Jam Two teams from CMPS 20 – Less Than Royal – Colon Trey.
Particle Systems GPU Graphics. Sample Particle System Fire and SmokeWater.
Particle Systems Final Exam Study Guide Game Design Experience Professor Jim Whitehead March 16, 2009 Creative Commons Attribution 3.0 (Except copyrighted.
Week 1 - Friday.  What did we talk about last time?  C#  SharpDX.
2D Physics and Camera Systems For CSE 3902 By: Matt Boggus.
Physically Based Animation and Modeling
Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett.
CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby.
Procedural Terrain with Stochastic Trees. WITH MAX MORRIS, CORY GAGEL, MARK WOULFE.
CSE 380 – Computer Game Programming Sprite Animation
Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to XNA Game.
Using Namepsaces  This section lists the namespaces that the application will be using frequently. Saves the programmer from specifying a fully qualified.
Geometric Transforms Changing coordinate systems.
Object Composition Interfaces Collections Covariance Object class Programming using C# LECTURE 10.
1 Useful Tools for Making Video Games Part V An overview of.
User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett.
Particle Systems Simulation for special effects.
Computer Science Term 1, 2006 Tutorial 3 Assignment 4 – Special Effects.
3.2. G RAPHICS I Alpha blending within games. An exploration of the use of alpha blending within games.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
Object Oriented Design COSC 315 Fall 2014 Bridget M. Blodgett.
CSC505 Particle Systems. CSC505 Object Representations So far we have represented (rendered) objects with –Lines –Polygons (triangles) –Curves These techniques.
XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites.
11 Lecture 3 Particle Effects References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
Particle Systems (Motion Machines of 2D Objects with Textures) Matthew K. Bowles Advanced Computer Graphics Spring 2004.
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
Games Development 2 Overview & Entity IDs and Communication CO3301 Week 1.
2.3. A RCHITECTURAL D ESIGN I Example approach for game screen management.
Point Sprites Course Information CVG: Programming 4 My Name: Mark Walsh Website: Recommended.
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Advanced techniques for the development of 2D Windows 8 games using Direct X and C++ Markus Jost CEO, Lead Programmer, Codebox GmbH
Particles and their home in Geometry Shaders Paul Taylor 2010.
Portal & Particle. Index Portal Particles Portal Room-Portal visibility system  Determine which room is shown at this time  Determine which object.
11 Writing Text Session 5.1. Session Overview  Show how fonts are managed in computers  Discover the difference between bitmap fonts and vector fonts.
Playing with Sprites. XNA Game Lifecycle In the faceBall demo program we bounced a smiley face around the graphical display against a background image.
Graphics for Games Particle Systems CO2301 Games Development 1 Week 23.
XNA Tutorial 1 For CS134 Lecture. Overview Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager.
Consumer Windows Phone Emulator Phone Emulator SamplesDocumentation GuidesCommunity Packaging and Verification Tools Windows Phone Dev Tools AppHub.
Ctrl + Z Sucheta Bhatawadekar Sucheta Bhatawadekar Dan L'Hommedieu Dan L'Hommedieu John Scott John Scott Jonathan Perkins Jonathan Perkins Aaron Cardwell.
The Stingray Example Program CMT3311. Stingray - an example 2D game May be useful as a simple case study Most 2D games need to solve generic problems.
1 Particle Systems for Games Particle System 3D MIM, hyeon.
2D Graphics CMT3311. This covers... How to make a transparent sprite How to add a sprite to your project and draw it Properties of sprites and how to.
Object-Oriented Programming Concepts
Where are we ? Setup Sprites Input Collision Drawing Sprites
Week 2 - Monday CS361.
2D Graphics and Animations in Unity 3D
CS1S467 GUI Programming LECTURE 11 Collections
Steering behaviours in game AI
2.3. Architectural Design I
Physically Based Animation and Modeling
CO1301: Games Concepts Lecture 22 Particle Systems
CO1301: Games Concepts Lecture 21 Timing + Quads + Numbers
Computer Graphics Lecture 15.
Development of a particle system
Particle Systems - A Technique for Modeling a Class of Fuzzy Objects
CO Games Concepts Week 22 Particle systems
Use of particle effects within games
Presentation transcript:

9.3. P ARTICLE S YSTEMS Development of a particle system

In lecture exploration of answers to frequently asked student questions

Development of an XNA/Java deployable particle system

A particle system can be used to simulate phenomena such as fire, smoke, explosions, moving water (e.g. fountains, waterfalls), sparks, dust, snow, rain, bullet casings, etc. Particle systems can also be used to provide special effects for spells, etc. or provide a model of fur and grass. Particle effects are widely used within games.

A particle system is typically controlled by an emitter. The emitter acts as the source for generated particles. Particles may be emitted at a constant rate or in sudden bursts. Each update/draw can be defined in terms of a simulation stage and a rendering stage. Each generated particle can have a number of defined parameters, e.g. velocity, direction, lifetime, colour, etc. Typically each parameter value is randomly selected from within some defined range.

Simulation Stage New particles are spawned if needed (alongside particle parameter selection/randomisation). Any particles that exceed their lifetime are removed from the simulation. All other particles properties are updated as needed (e.g. position, rotation, etc.) based on some simulation rules. The particles may also be tested for collision against other objects, with suitable interactions introduced Rendering stage Each particle is rendered (if in 3D typically using a textured billboard)

We will explore the XNA ParticleSample tutorial. Based upon the XNA ParticleSample tutorial Aside: The tutorial uses a SpriteBatch to render the particles – a GPU point sprite based approach is explored in a later lecture.

Each particle system comprises a number of particles. Each particle is defined with properties such as position, velocity, acceleration, rotation, lifetime, etc. When drawn each particle will display a common image that is layered on top of other particles (using either normal or additive blending). The particle properties are controlled to provide the desired effect.

public bool isActive { get { return timeSinceStart < lifetime; } } public void Initialize ( Vector2 position, Vector2 velocity, Vector2 acceleration, float lifetime, float scale, float rotationSpeed) { // Store passed values // Set time since start to zero // Randomly select rotation (0-2π radians) } public void Update(float dt) { velocity += acceleration * dt; position += velocity * dt; rotation += rotationSpeed * dt; timeSinceStart += dt; } } Definition position, velocity, rotation acceleration, and rotation speed Define how long this particle will ‘live’ before it is removed/reborn And how long it has been alive Define how long this particle will ‘live’ before it is removed/reborn And how long it has been alive Define the scale of this particle (i.e. how close/far from the camera) public class Particle { public Vector2 position ; public Vector2 velocity ; public Vector2 acceleration ; public float rotation ; public float rotationSpeed ; public float lifetime ; public float timeSinceStart ; public float scale ; Determine if this particle is alive Initialise the particle (e.g. upon particle reuse or ‘rebirth’) Called each frame to update the particle’s position and rotation Aside: Other properties can be stored within the particle.

The ParticleSystem class manages a collection of particle instances. As particles die and are reborn it is important the particle system caches and reuses particle (to reduce garbage collection churn). A particle system may have to manage particle load by controlling the number of active particles. Often the draw order for particles is important (i.e. particles drawn with normal alpha blending are drawn first before those using additive blending). The presented approach uses a generic particle system that is extended/customised for each effect. Other approaches involve having a single manager manage and draw a number of different types of particle.

public abstract class ParticleSystem : DrawableGameComponent { private Texture2D texture ; private Vector2 origin ; private const int howManyEffects ; Particle[] particles ; Queue freeParticles ; As a DrawableGameComponent, when added to a Game instance, XNA will automatically call the update and draw method Base texture used by particles in this system, alongside a defined origin for the texture Variable controlling the maximum number of effects of this type that can be ongoing Array of particles maintained by this system Queue from which new particles are born and to which dead particles are moved protected int minNumParticles ; protected int maxNumParticles ; protected float minInitialSpeed ; protected float maxInitialSpeed ; protected float minAcceleration ; protected float maxAcceleration ; protected float minRotationSpeed ; protected float maxRotationSpeed ; protected float minLifetime ; protected float maxLifetime ; protected float minScale ; protected float maxScale ; protected SpriteBlendMode spriteBlendMode; Min-max number of particles that are added per effect call Range of initial speeds, accelerations and rotational speeds. Range of lifespans – also drives alpha value and scale Range of scales – also effected by lifespan to avoid particle ‘popping’ on creation Blendmode to combined particle images

protected abstract void InitializeConstants (); protected override void LoadContent () { // Load/obtain the texture // used by the particle system // Set the origin, i.e. centre, // of the texture } Initialise the constants associated with this particle system protected ParticleSystem ( Game game, int howManyEffects) : base(game) { this.howManyEffects = howManyEffects; } public override void Initialize () { InitialiseConstants(); particles = new Particle[ howManyEffects * maxNumParticles]; freeParticles = new Queue ( howManyEffects * maxNumParticles); for (int i = 0; i < particles.Length; i++) { particles[i] = new Particle(); freeParticles.Enqueue(particles[i]); } } Create the particles and add them to the particles array and also to the free particle queue This method will be overridden by extending classes to providing initial defining values

Add the effect offered by this particle system at the specified location Remove particles from the free queue (where possible) and initialise them public void AddParticles ( Vector2 where) { int numParticles = Random.Next( minNumParticles, maxNumParticles); for (int i = 0; i < numParticles && freeParticles.Count > 0; i++) { Particle particle = freeParticles.Dequeue(); InitializeParticle(particle, where); } } protected virtual void InitializeParticle ( Particle particle, Vector2 where) { Vector2 direction = PickRandomDirection(); float velocity = Random.Next( min, max ); float acceleration = Random.Next( min, max ); float lifetime = Random.Next( min, max ); float scale = Random.Next( min, max ); float rotationSpeed = Random.Next( min, max ); particle. Initialize ( where, velocity * direction, acceleration * direction, lifetime, scale, rotationSpeed); } protected virtual Vector2 PickRandomDirection () { float angle = Random.Next(0, MathHelper.TwoPi); return new Vector2((float) Math.Cos(angle), (float)Math.Sin(angle)); } Min/max values as defined above, e.g. maxScale, minScale, etc. Randomise particle properties within defined ranges (can be overridden by extending classes)

public override void Draw (GameTime gameTime) { game.SpriteBatch.Begin(spriteBlendMode); foreach (Particle p in particles) { if (!p.Active) continue; float normalizedLifetime = p.TimeSinceStart / p.Lifetime; float alpha = 4 * normalizedLifetime * (1 - normalizedLifetime); Color color = new Color( new Vector4(1, 1, 1, alpha) ); float scale = p.Scale * (.75f +.25f * normalizedLifetime); game.SpriteBatch. Draw ( texture, p.Position, null, color, p.Rotation, origin, scale, SpriteEffects.None, 0.0f); } game.SpriteBatch.End(); } Determine how long has passed Consider each particle. This will also consider non-active particles (a performance hit). Other (more complex) approaches can be used to minimise/avoid non-active consideration. Update the particle and add to free particle queue if needed public override void Update ( GameTime gameTime) { float dt = (float)gameTime. ElapsedGameTime.TotalSeconds; foreach (Particle p in particles) { if (p.Active) { p.Update(dt); if (!p.Active) freeParticles.Enqueue(p); } } } Lifetime is normalised to be between 0 (born) and 1 (dead) and is used to calc alpha value (fade-in/out) and scale. Alpha = 0 at a lifetime of 0 and 1, and a maximum of 1 at a lifetime of 0.5 Particles slowly grow over their lifespan

Three different particle systems are defined: SmokePlumeParticleSystem ExplosionParticleSystem ExplosionSmokeParticleSystem The explosion and explosion smoke particle systems are combined to produce a full explosion effect. Each particle system customises the values within the inherited ParticleSystem class to provide the desired effect. public class SmokePlumeParticleSystem : ParticleSystem { protected override void InitializeConstants () { textureFilename = "smoke"; minInitialSpeed = 20; maxInitialSpeed = 100; minAcceleration = 0; maxAcceleration = 0; spriteBlendMode = SpriteBlendMode.AlphaBlend; }

The Game class defines three instances of the particle system: ExplosionParticleSystem explosion ; explosion = new ExplosionParticleSystem(this, 1); Components.Add(explosion); ExplosionSmokeParticleSystem smoke ;... SmokePlumeParticleSystem smokePlume ;... const float TimeBetweenExplosions = 2.0f; float timeTillExplosion = 0.0f; const float TimeBetweenSmokePlumePuffs =.5f; float timeTillPuff = 0.0f; Repeat times are defined for each particle effect (one explosion every 2 seconds, one puff of smoke every 0.5 seconds). XNA Aside: As the ParticleSystem instances extend DrawableGame- Component once added to the game, XNA will automatically call their update and draw methods, i.e. the methods do not need to be explicitly called.

Based on the currently active particle system, update the system to ensure that new effects are added as needed. protected override void Update ( GameTime gameTime) { switch (currentState) { case State.Explosions: UpdateExplosions(dt ); break; case State.SmokePlume: UpdateSmokePlume(dt ); break; } private void UpdateExplosions (float dt) { timeTillExplosion -= dt; if (timeTillExplosion < 0) { Vector2 where = Vector2.Zero; where.X = RandomBetween(0, graphics.GraphicsDevice.Viewport.Width); where.Y = RandomBetween(0, graphics.GraphicsDevice.Viewport.Height); explosion.AddParticles(where); smoke.AddParticles(where); timeTillExplosion = TimeBetweenExplosions; } } Update the currently active particle system Determine if it’s time for another explosion effect Select a random spawn location Add the fire and smoke effects Reset the time

To do: Complete Question Clinic Consider if material is of use within your game. Complete section in project document on alpha hand-in Submit alpha Hand-in if desired Today we explored: Overview of particle effects Development of an introductory particle system