In this article I will be talking much more about Particle Animations.
In the previous article
you've seen how to show a particle in XNA. That was a simple application. But this time we will be making a much more detailed animation using 3D models & Particles.
This sample contains 2 particle effects & a rotating 3D model.
The Red Comet will crush to Earth and it will explode :) Theres no reality but I believe it will be a nice sample to show Particle Animations on demonstration.
World 3D Model:
Lets create the project and see how its done.
First of all we need to add some variables:
Vector3 modelPosition = Vector3.Backward;
float modelRotation = 0.0f;
Vector3 cameraPosition = new Vector3(100, 100, 1500);
int spx, spy;
Vector2 spritePosition = new Vector2(400, 400);
myModel is the Model Object we will be using for 3D Rotating World Model.
modelPosition will help us to store the position infos of the 3d Model.
modelRotation will help us in which speed we wish to rotate it.Lesser values will make it rotate slower.
Spx & Spy are the position values of SpriteBatch which will be used for positioning Particles.
Boom is the boolean value which query whether red comet has crushed the World or not.
"a" variable is the counter as you will know from the previous sample.
spritePosition is the variable we use for storing the particles' positions.
Ani_2 is the counter variable for Explosion Particle.
boom_part is the explosion particle.
Update Game1 as:
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
Window.Title = "Boom!";
We have added a Title to the XNA Window named "Boom!"
Update LoadContent as:
protected override void LoadContent()
spriteBatch = new SpriteBatch(GraphicsDevice);
myModel = Content.Load<Model>("Model\\world");
myTexture = Content.Load<Texture2D>("particles//Comet//comet_1");
boom_part = Content.Load<Texture2D>("particles//Explosion//explosion_1");
aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
We have added the start values of 3d model and particle textures.
Update the "Update" function as:
protected override void Update(GameTime gameTime)
modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds *
KeyboardState keystat = Keyboard.GetState();
spx += 1;
spy += 1;
else if (keystat.IsKeyDown(Keys.Down))
spx -= 1;
spy -= 1;
if (ani_2 == 50)
ani_2 = 49;
boom_part = Content.Load<Texture2D>("particles//Explosion//explosion_" + ani_2);
if (a == 50)
a = 15;
myTexture = Content.Load<Texture2D>("particles//Comet//comet_" + a);
if (spx > 75)
boom = true;
If we press "UP" key the red comet will be going enroute World. And Down key will be the reverse one.
And if boom is true which means if red comet crushed then world will explode.
If spx value is bigger than 75 then it means it will crush to the World.
This is a simple sample to demonstrate what we can do with Particles so truth be told I have made it as easy as possible. You can use Collision Detection if you want. But I didnt need it in this sample.
And finally Update our Draw Function as:
protected override void Draw(GameTime gameTime)
Matrix transforms = new Matrix[myModel.Bones.Count];
foreach (ModelMesh mesh in myModel.Meshes)
foreach (BasicEffect effect in mesh.Effects)
effect.World = transforms[mesh.ParentBone.Index] *
effect.View = Matrix.CreateLookAt(cameraPosition,
effect.PreferPerPixelLighting = true;
effect.Projection = Matrix.CreatePerspectiveFieldOfView(
spriteBatch.Draw(myTexture, new Rectangle(spx, spy, 300, 300), Color.White);
spriteBatch.Draw(boom_part, new Rectangle(spx + 175, spy + 50, 300, 300), Color.White);
This code whows a simple model loading. While boom is false we tell 3D World to rotate and red comet to continue flying.
If boom is true then the red comet and 3D World will be visible instead explosion particle will show itself.
Lets run the sample and see what happens.
By pressing Up Key the red comet will come closer to the world.and when came to near, it will explode as shown below:
Yeap Red Comet crushed and we all died. We're in heaven right now. Nice joke, isn't it.
This sample showed that using 2D Particle Animations you can perfect your game and insert action which will keep the player to continue play your game.
I hope this mini article-sample demonstration helped you and opened your mind to insert nice effects in your game project.