Jump to content
  • Advertisement
Sign in to follow this  
Hitodenashi

XNA 4.0 Particle class for 2D game

This topic is 2283 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am working on a 2D game in XNA 4.0, and I'm trying to get a particle class to work. All of the particles are created and rendered, but they do not update their positions. Here is the code I am using, If anyone could help me figure out why this isn't working it would be a great help.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace Game1
{
public enum ParticleDetail { LOW, MEDIUM, HIGH };
public enum ParticleType { DIRECTED, RANDOM };
class ParticleEmitter
{
private struct particle
{
public bool alive;
public int duration;
public Vector2 pos;
public Vector2 vel;
public float scale;
public void Update(ref GameTime gameTime)
{
if (this.alive)
{
this.pos += this.vel;
this.duration = this.duration - gameTime.ElapsedGameTime.Milliseconds;
if (this.duration <= 0)
{
this.alive = false;
}
}
}
public void Render(ref Texture2D texture, ref SpriteBatch spriteBatch)
{
if (this.alive)
{
int height = texture.Height / 2;
int width = texture.Width / 2;
Vector2 origin = new Vector2(width, height);
spriteBatch.Draw(texture, this.pos, null, Color.White, 0, origin, this.scale, SpriteEffects.None, 0);
}
}
};
protected Random rnd { get; private set; }
public Texture2D texture;
private List<particle> ParticleList;
public void Init()
{
rnd = new Random();
ParticleList = new List<particle>();
}
public void CreateParticles(ParticleDetail detailLevel, ParticleType type,
Vector2 Position, Vector2 Direction, int MaxAngleDeg)
{
switch (detailLevel)
{
case ParticleDetail.LOW:
{
for (short i = 0; i < 10; i++)
{
int tAngle = rnd.Next((int)MaxAngleDeg * 2);
tAngle = MaxAngleDeg - tAngle;
float Angle = (float)tAngle * (3.14f / 180.0f);
float x = (float)(Direction.X * Math.Cos(Angle) - Direction.Y * Math.Sin(Angle));
float y = (float)(Direction.X * Math.Sin(Angle) + Direction.Y * Math.Cos(Angle));
particle p = new particle();
p.alive = true;
p.duration = 1000;
p.pos = Position;
p.vel = new Vector2(x, y);
p.vel.Normalize();
p.vel *= 10.0f;
p.scale = 0.5f;
ParticleList.Add(p);
}
} break;
case ParticleDetail.MEDIUM:
{
for (short i = 0; i < 20; i++)
{
int tAngle = rnd.Next((int)MaxAngleDeg * 2);
tAngle = MaxAngleDeg - tAngle;
float Angle = (float)tAngle * (3.14f / 180.0f);
int x = (int)(Direction.X * Math.Cos(Angle) - Direction.Y * Math.Sin(Angle));
int y = (int)(Direction.X * Math.Sin(Angle) + Direction.Y * Math.Cos(Angle));
particle p = new particle();
p.alive = true;
p.duration = 1000;
p.pos = Position;
p.vel = new Vector2(x, y);
p.vel.Normalize();
p.vel *= 10.0f;
p.scale = 1.0f;
ParticleList.Add(p);
}
}break;
case ParticleDetail.HIGH:
{
for (short i = 0; i < 30; i++)
{
int tAngle = rnd.Next((int)MaxAngleDeg * 2);
tAngle = MaxAngleDeg - tAngle;
float Angle = (float)tAngle * (3.14f / 180.0f);
int x = (int)(Direction.X * Math.Cos(Angle) - Direction.Y * Math.Sin(Angle));
int y = (int)(Direction.X * Math.Sin(Angle) + Direction.Y * Math.Cos(Angle));
particle p = new particle();
p.alive = true;
p.duration = 1000;
p.pos = Position;
p.vel = new Vector2(x, y);
p.vel.Normalize();
p.vel *= 10.0f;
p.scale = 1.0f;
ParticleList.Add(p);
}
}break;
default:
{}break;
};
}
public void UpdateParticles(ref GameTime gameTime)
{
foreach (particle p in ParticleList)
{
p.Update(ref gameTime);
}
}
public void RenderParticles(ref SpriteBatch spriteBatch)
{
foreach (particle p in ParticleList)
{
p.Render(ref texture, ref spriteBatch);
}
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
Please, use the code command. The code is difficult to follow without indentation.

Are you sure that p.vel is different than 0? What happen if you put an arbitrary number, let say (0.1, 0.1).

Share this post


Link to post
Share on other sites
I used the break points in this to step through the code, and it says that the p.vel has a value, but it doesn't seem to want to update anyway.

Share this post


Link to post
Share on other sites
public int duration; should be float or double. EDIT: Sorry, I didn't see that you use milliseconds.

Do you try a velocity like (0.1, 0.1)? We need to be sure where you have the problem. Use something like this.pos += new Vector2(0.1f, 0.1f);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!