0
\$\begingroup\$

I do not understand how the rotation of the image i have works. Is it something to do with the ThumbSticks on the Xbox Pad? or properties of a float? if someone could explain to me what is actually happening here i would be very greatful.

The image is of a Gun. I would like to add in shooting guided by the thumbsticks for enemies above the player, so understanding this is crucial.

The image and coding has its own class. So in Game1.cs only initialised, WMpos set to the character and update and draw methods referenced, so does not affect rotation.

This is all the relevant code for it here:

public Texture2D WMtxr;
public Vector2 WMorigin;
public float WMrotation;

public void UpdateMe(GameTime gameTime)
    {
        WMorigin = new Vector2(WMtxr.Width - 100, WMtxr.Height - 36);
        if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y < -0.1)
        {
            WMrotation += 0.03f;
        }
        if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y > 0.1)
        {
            WMrotation -= 0.03f;
        } 
    }

public void DrawMe(SpriteBatch sb)
    {
        sb.Draw(WMtxr, WMpos, null, Color.White, WMrotation, WMorigin, 1f, SpriteEffects.None, 0);
    }
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The WMRotation variable you have here is being used to drive the rotation.

The rotation is changing in response to the thumbsticks because you have some logic in your update that is modifying the WMRotation variable in response to thumbstick input.

If you were to remove this logic and just add a small amount to WMRotation every update, then the image would rotate at a constant rate.

e.g.

// Note this might not compile exactly as written, I haven't written XNA code
// in years, and I don't have it installed right now. 
// This is the basic gist of it though
public void UpdateMe(GameTime gameTime)
{
    const float deltaTimeSeconds = gameTime.ElapsedGameTime.Seconds;

    WMorigin = new Vector2(WMtxr.Width - 100, WMtxr.Height - 36);

    // Rotate approximately 45 degrees per second
    // (0.785398 is approx 45 degrees in radians)
    WMrotation += 0.785398 * deltaTimeSeconds;
}

But why does WMRotation change the rotation? The sprite is drawn rotated here because WMRotation is being passed into SpriteBatch.Draw here in your draw function

public void DrawMe(SpriteBatch sb)
{
    //                                         | WMrotation passed in here
    //                                         v
    sb.Draw(WMtxr, WMpos, null, Color.White, WMrotation, WMorigin, 1f, SpriteEffects.None, 0);
}

See the documentation for SpriteBatch.Draw here (there are several versions of the function you can call) https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritebatch.draw.aspx

Note: There are other versions of the function you can call, depending on which properties of the sprite you care about. https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritebatch.draw.aspx

A simplified high-level explanation of what SpriteBatch.Draw is doing here, is drawing a square on the screen made out of four vertices. The vertex positions are rotated internally using the rotation value that you pass in. This explanation of SpriteBatch.Draw is skipping over a lot of implementation details, but you don't really need to care too much about the details at this point.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thank you for your help, i appreciate it greatly! :). Looking at the documentation helped. sometimes its hard trying to find what i am actually needing to look for \$\endgroup\$
    – Lonchenzo
    Commented Apr 14, 2017 at 20:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .