Question

I've tried Math.Atan2(futurePos.Y - pos.Y, futurePos.X - pos.X) as the rotation, but it only seems to work on the X axis.
Whereas if I do Math.Atan2(mouse.Y - pos.Y, mouse.X - pos.X) it works just fine (although I have to manage if the velocity is a negative value and even that doesn't work for two directions).

(futurePos = position + velocity)

My desired outcome here is to draw a rectangle on a particle's position whose length is in relation to the particle's velocity and whose angle points towards where the particle is going.

Was it helpful?

Solution 2

FYI, I ended up drawing a line from the object to its future position (as expected in my question).

Here's the code in the Draw method;

Vector2 nextPos = position + velocity * 7;

drawLine(spriteBatch, Game1.pixelTexture, position, nextPos, Color.FromNonPremultiplied(255, 0, 0, 100));

Here's the draw line code;

void drawLine(SpriteBatch spa, Texture2D tex, Vector2 src, Vector2 dst, Color col)
{
    Vector2 direction = dst - src;
    var angle = (float)Math.Atan2(direction.Y, direction.X);
    float distance;
    Vector2.Distance(ref src, ref dst, out distance);

    spa.Draw(tex, src, new Rectangle((int)src.X, (int)src.Y, (int)distance, 2), col, angle, Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
}

OTHER TIPS

Remember that, if your reference system is the "classic" one, in XNA the Y axis is reversed, so you have to negate everything you put as first parameter in Math.Atan2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top