0
\$\begingroup\$

I'm making a small game where the player shoots projectiles by clicking the mouse. Everything worked fine before adding in a camera. Now the position of the new projectiles is always off.

shoot code:

    if (mouseState.LeftButton == ButtonState.Pressed)
        {
            Vector2 mousePos = new Vector2(mx, my);
            Vector2 worldPosition = Vector2.Transform(mousePos, 
            Matrix.Invert(game.camera.transform));

            Shoot(new Vector2(mx, my), new Vector2(pos.X + 8 * scale.X, pos.Y + 8 * scale.Y), 
            gameTime);
            buttonTimer--;
        }

Camera code:

    public class Camera
    {
    Handler handler;

    public Matrix transform { get; private set; }

    public Camera (Handler handler)
    {
        this.handler = handler;
    }

    public void Update (GameTime gameTime)
    {
        foreach (Player p in handler.players)
            Follow(p);
    }

    public void Follow(Player target)
    {
        var offset = Matrix.CreateTranslation(1200 / 2, 800 / 2, 0);

        var position = transform = Matrix.CreateTranslation(
            -target.pos.X - ((16 * target.scale.X) / 2),
            -target.pos.Y - ((16 * target.scale.Y) / 2),
            0);

        transform = position * offset;
    }
}

Bullet code:

    public class Projectile
{
    public bool isActive = true;

    Game1 game;
    Handler handler;
    Spritesheet ss;

    public Vector2 pos;
    public Vector2 scale;

    int type;

    public Vector2 vel;
    public Vector2 direction;
    public Vector2 origin;

    long startTime = 0;

    float speed = 14;

    int particleTimer = 0;

    Rectangle Bounds;

    public Projectile(Vector2 pos, Vector2 scale, int type, Game1 game, Handler handler)
    {
        this.pos = pos;
        this.scale = scale;
        this.type = type;
        this.game = game;
        this.handler = handler;

        this.ss = game.bullet_ss;

        origin = pos;
    }

    public void LoadContent(ContentManager content)
    {

    }

    public void Update(GameTime gameTime)
    {
        pos += direction * speed;
        CreateParticles();

        Collision();

        Bounds = new Rectangle((int)pos.X, (int)pos.Y, (int)(8 * scale.X), (int)(8 * scale.Y));
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(ss.frames[type, 0], pos, null, null, null, 0, scale);
    }

    private void Collision()
    {
        if (type == 0)
        {
            foreach (Platform p in handler.platforms)
            {
                if (this.Bounds.Intersects(p.Bounds)) isActive = false;
            }
        }
    }

    private void CreateParticles()
    {
        if (particleTimer > 2)
        {
            for (int i = 0; i < 3; i++)
            {
                Vector2 position = new Vector2(handler.rand.Next(8) * scale.X, handler.rand.Next(8) * 
                scale.Y);
                Vector2 v = new Vector2(handler.rand.Next(8), handler.rand.Next(8));
                Particle p = new Particle(position + pos, v, new Vector2(4, 4), 0, game, handler);
                handler.particles.Add(p);
            }

            particleTimer = 0;
        }
        particleTimer++;
    }

What fixes can I implement so that the projectiles are created in the correct positon?

\$\endgroup\$
1
  • \$\begingroup\$ Can you describe in more detail why the position of the bullets looks off? \$\endgroup\$
    – Steven
    Nov 16 '20 at 7:37

Your Answer

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

Browse other questions tagged or ask your own question.