Jump to content
  • Advertisement

Hitodenashi

Member
  • Content Count

    6
  • Joined

  • Last visited

Community Reputation

100 Neutral

About Hitodenashi

  • Rank
    Newbie
  1. Hitodenashi

    XNA 4.0 Particle class for 2D game

    Hello, just to let you guys know, I switched the particle struct to a class, and it worked. thanks for the help anyway.
  2. Hitodenashi

    XNA 4.0 Particle class for 2D game

    I use an origin different from 0,0 so I can center the sprite on the center of the texture
  3. Hitodenashi

    XNA 4.0 Particle class for 2D game

    I tried hard coding the velocity in, but it still doesn't seem to want to move
  4. Hitodenashi

    XNA 4.0 Particle class for 2D game

    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.
  5. Hitodenashi

    XNA 4.0 Particle class for 2D game

    Ok I edited it, so now it has the code done correctly.
  6. 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); } } } }
  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!