Ggcourtois

Hey all,

I would like to know if it is possible to rotate a 2d sprite at a given angle If so, how

thanks alot,

ggcourtois



Re: XNA Game Studio Express Rotating a 2d sprite

GCDev

Ggcourtois wrote:

Hey all,

I would like to know if it is possible to rotate a 2d sprite at a given angle If so, how

thanks alot,

ggcourtois

Code Snippet

#region Using Statements

using System;

using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Audio;

using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Storage;

#endregion

namespace ClickMove

{

/// <summary>

/// This is the main type for your game

/// </summary>

public class Game1 : Microsoft.Xna.Framework.Game

{

GraphicsDeviceManager graphics;

ContentManager content;

Texture2D defaultTexture;

SpriteBatch defaultSpriteBatch;

float angle;//<---------------------------**

//to track click

MouseState lastMouseState;

public Game1()

{

graphics = new GraphicsDeviceManager(this);

IsMouseVisible = true;

content = new ContentManager(Services);

}

/// <summary>

/// Allows the game to perform any initialization it needs to before starting to run.

/// This is where it can query for any required services and load any non-graphic

/// related content. Calling base.Initialize will enumerate through any components

/// and initialize them as well.

/// </summary>

protected override void Initialize()

{

// TODO: Add your initialization logic here

lastMouseState = Mouse.GetState();

base.Initialize();

}

/// <summary>

/// Load your graphics content. If loadAllContent is true, you should

/// load content from both ResourceManagementMode pools. Otherwise, just

/// load ResourceManagementMode.Manual content.

/// </summary>

/// <param name="loadAllContent">Which type of content to load.</param>

protected override void LoadGraphicsContent(bool loadAllContent)

{

if (loadAllContent)

{

// TODO: Load any ResourceManagementMode.Automatic content

defaultTexture = content.Load<Texture2D>("happy");

}

// TODO: Load any ResourceManagementMode.Manual content

defaultSpriteBatch = new SpriteBatch(graphics.GraphicsDevice);

}

/// <summary>

/// Unload your graphics content. If unloadAllContent is true, you should

/// unload content from both ResourceManagementMode pools. Otherwise, just

/// unload ResourceManagementMode.Manual content. Manual content will get

/// Disposed by the GraphicsDevice during a Reset.

/// </summary>

/// <param name="unloadAllContent">Which type of content to unload.</param>

protected override void UnloadGraphicsContent(bool unloadAllContent)

{

if (unloadAllContent == true)

{

content.Unload();

}

}

/// <summary>

/// Allows the game to run logic such as updating the world,

/// checking for collisions, gathering input and playing audio.

/// </summary>

/// <param name="gameTime">Provides a snapshot of timing values.</param>

protected override void Update(GameTime gameTime)

{

MouseState currentMouseState = Mouse.GetState();

// Allows the default game to exit on Xbox 360 and Windows

if (Keyboard.GetState().IsKeyDown(Keys.Escape))

this.Exit();

angle += 0.01f; // ****************SOME animation

lastMouseState = Mouse.GetState();

base.Update(gameTime);

}

/// <summary>

/// This is called when the game should draw itself.

/// </summary>

/// <param name="gameTime">Provides a snapshot of timing values.</param>

protected override void Draw(GameTime gameTime)

{

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

defaultSpriteBatch.Begin();

/// angle in radians and the new Vector2(16,16) mean that the center of rotation is at the center of the sprite, because my sprite is a 32x32 sqare

defaultSpriteBatch.Draw(defaultTexture, new Rectangle(50, 50, 32, 32),null, Color.White, angle, new Vector2(16,16), SpriteEffects.None, 0.0f);

defaultSpriteBatch.End();

base.Draw(gameTime);

}

}

}





Re: XNA Game Studio Express Rotating a 2d sprite

Ggcourtois

thanks