XNA - How to place my particle engine/emitter on a bouncing ball?

100 views Asked by At

I have a particle engine which creates an emitter at my mouse position.

particleEngine.EmitterLocation = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);

It's in the Update method in Game1.cs.

I have another class which is called Ball.cs with its bouncing physics, and Texture2D texture; Vector2 position.

Now how do I make the emitter / particles follow the ball instead?

1

There are 1 answers

1
Sane On BEST ANSWER

pass a reference to the particleEngine to the Ball class and set the EmitterLocation to the ball's location.

Example:

Game1, Initialize (for instance):

ParticleEngine particleEngine = new ParticleEngine();
Ball ball = new Ball(particleEngine);

In the Ball class:

class Ball
{
    ParticleEngine particleEngine;
    Vector2 position;

    public Ball(ParticleEngine particleEngine)
    {
        this.particleEngine = particleEngine;
    }

    public void Update(GameTime gameTime)
    {
        //Update position
        particleEngine.EmitterLocation = new Vector2(this.position.X, this.position.Y);
    }
}

I don't know how your particle engine works or anything about your code structure, but with the information given I did my best to implement an understandable example.