0
\$\begingroup\$

I'm new to C# and Monogame, and I'm trying to create an effect of a 'psychic' ability, the user will press a key and a circular aura blast will emit from their position. I'm just trying to get the actual blast working before setting up the positions and all that, but my problem is when trying to get all the particles to move out from the origin of a circle to the outside.

I've never worked with circles yet and only with Rectangles so my knowledge of this is very basic. You'll probably recognise the code from a youtube channel and that's because I've been trying to learn from wherever I can, most of the time that leads me to youtube since a video demonstration works best but I digress.

This is my code for the particle generator so far.

class ParticleGenerator
{
Texture2D texture;
float spawnWidth;
float density;


List<Particles> particles = new List<Particles>();

float timer;

public ParticleGenerator(Texture2D newTexture, float newSpawnWidth, float newDensity)
{
    texture = newTexture;
    spawnWidth = newSpawnWidth;
    density = newDensity;

}

public void createParticle(GraphicsDevice graphics)
{

    particles.Add(new Particles(texture, new Vector2(graphics.Viewport.Width / 2 , graphics.Viewport.Height /2), new Vector2(5, 1)));

}

public void Update(GameTime gameTime, GraphicsDevice graphics)
{
    timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
    while (timer > 0)
    {
        timer -= 1f / density;
        createParticle(graphics);
    }

    for (int i = 0; i < particles.Count; i++)
    {
        particles[i].Update();
        if (particles[i].Position.Y > graphics.Viewport.Height)
        {
            particles.RemoveAt(i);
            i--;
        }
    }
}

public void Draw(SpriteBatch spriteBatch)
{
    foreach (Particles particle in particles)
    {
        particle.Draw(spriteBatch);
    }
}

Many thanks for any help :)

\$\endgroup\$
1
  • \$\begingroup\$ I was thinking that If I could just make a certain amount of particles, come out from the centre, instead of making an actual circle that expands. Just set one particle off in one directions from the centre? But I still don't know how to do that, would I need an array/list of coordinates/vectors and then assign each particle a corresponding coordinate? \$\endgroup\$ – vK31RON May 3 '18 at 17:34
1
\$\begingroup\$

I tend to talk to all of my objects in "Polar Coordinates" which is a vector to represent where it is in 3d space and then a "direction" in which it is facing.

I can then "point" my object in that direction, build a movement vector for it, and simply move in that direction on each update. Code that determines an angle between two points looks something like this:

    public static float AngleTo(float x1, float y1, float x2, float y2)
    {
        return (float)Math.Atan2(y1 - y2, x1 - x2);
    }

though in your case it's probably more simple than this, as you could simply create your particles an evenly divided amount of space around your object.

Unfortunately your definition for the Particle class is not here; but you should add a Movement Vector or a Rotation to it, and utilize that when you "Create" a particle to set it in motion in the correct direction.

Note: You will want to check all four bounds of the area instead of just the upper-Y now.

To build a movement vector in a direction, first determine what the movement vector is by default. Let's say that it's defaultMvec=(1,1,0). Now the code to change that movement vector is something like

mvec.X = defaultMvec.X * cos(rotation)
mvec.Y = defaultMvec.Y * sin(rotation)

Without understanding trigonometry on a reasonable level I'm not sure I can tell you why this is the case. Try to look up how Cos and Sin work in terms of "Radians" and get used to it a bit. Cos(Pi) for instance is -1, and Cos(0) is 1; and the gradiant of multipliers in between will make sense of the math I posted above.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.