Search code examples
c#canvasxnasprite

How to draw a sprite on a moving canvas


I generally figure this sort of thing out normally but I am stumped. I suspect theres a mathematical combination I have missed but anyway.

I have a moving background (currently goes up and down from top to bottom) I have a moving object (currently moves left and right from the centre of the canvas programatically).

So this is the question, How can I make an object move relatively to the position on the canvas in x and y directions?

Here is my relevant codes:

//Helper method
    private Vector2 CalculateDirection()
    {
        Vector2 calculatedDirection = new Vector2((float)Math.Cos(direction),
            (float)Math.Sin(direction));
        calculatedDirection.Normalize();
        return calculatedDirection;
    }

object on canvas

    public void Update(GameTime gameTime, Vector2 center)
    {
        this.currentCentre = originalCentre - center;
        //movement logic here
        Vector2 calculatedDirection = CalculateDirection();
        //deltaTime = ((float)gameTime.ElapsedGameTime.TotalMilliseconds) / 15f;

        if (speed > 0f || speed < 0f)
        {
            ///TODO: work this out!!
            Velocity = calculatedDirection * speed;
            float dir = (originalCentre.Y - currentCentre.Y);
            position.X += Velocity.X * (1.0f - 0.9f);
            position.Y = dir;// *(1.0f - 0.9f); 
        }
    }

canvas moving methods

    private void determinePitchSize()
    {
        int newHeight = Convert.ToInt32(pitch.Height * ratio);
        this.canvas = new Rectangle(
            0, posHeight,
            device.PresentationParameters.BackBufferWidth,
            newHeight
            );
    }

    public void increasePosHeight()
    {
        posHeight++;
    }

    public void decreasePosHeight()
    {
        posHeight--;
    }

    private void determineDirection()
    {
        if (!direction)
        {
            if (this.canvas.Height + this.canvas.Y <= this.screenY)
                direction = true;
        }
        else
        {
            if (this.canvas.Y >= 0)
                direction = false;
        }
    }
    private void useDirection()
    {
        this.determineDirection();

        if (direction)
            this.increasePosHeight();
        else decreasePosHeight();
    }

If you need any more info I can add it here.

Thanks


Solution

  • Ok so thanks to Nico, I was able to answer this.

        Vector2 Velocity { get; set; }
        Vector2 relative { get; set; }
        public void Update(GameTime gameTime, Vector2 center)
        {
    
            this.currentCentre = center;
    
            Vector2 calculatedDirection = CalculateDirection();
    
    
            if (speed > 0f || speed < 0f)
            {
                Velocity = calculatedDirection * speed * 0.1f;
                relative = relative - Velocity;
                position = currentCentre + relative;
    
            }
        }
    

    The velocity creates object movement to test that it ends up in a different place.

    Relative starts at 0,0 (the center) and is adjusted by the velocity.

    Position is then set to the centre plus the relative position. which has been set by the velocity.