<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Rotating A Sprite Towards An Object In XNA</title>
	<atom:link href="http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna/feed" rel="self" type="application/rss+xml" />
	<link>http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna</link>
	<description>Call Yourself.</description>
	<lastBuildDate>Fri, 09 Dec 2011 02:29:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Shaan Verma</title>
		<link>http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna/comment-page-1#comment-473</link>
		<dc:creator>Shaan Verma</dc:creator>
		<pubDate>Thu, 27 Oct 2011 01:21:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.berecursive.com/blog/?p=8#comment-473</guid>
		<description>This is the best tutorial I have ever seen. Thank you very much!</description>
		<content:encoded><![CDATA[<p>This is the best tutorial I have ever seen. Thank you very much!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jacob</title>
		<link>http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna/comment-page-1#comment-468</link>
		<dc:creator>Jacob</dc:creator>
		<pubDate>Fri, 21 Oct 2011 05:34:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.berecursive.com/blog/?p=8#comment-468</guid>
		<description>I noticed that it bounced back and forth so i messed with the code and came up with a small adjustment :). 

Instead of:
SquarePosition.X -= (float)(3 * Math.Cos(rotation));
SquarePosition.Y -= (float)(3 * Math.Sin(rotation));

Use:
if (XDistance &gt;= 3 &#124; YDistance &gt;= 3 &#124; XDistance &lt;= -3 &#124; YDistance &lt;= -3 )
{
SquarePosition.X -= (float)(3 * Math.Cos(rotation));
SquarePosition.Y -= (float)(3 * Math.Sin(rotation));
}
else
{
SquarePosition.X -= (float)(0 * Math.Cos(rotation));
SquarePosition.Y -= (float)(0 * Math.Sin(rotation));
}

Mess around with the = numbers to get a different range. Enjoy.</description>
		<content:encoded><![CDATA[<p>I noticed that it bounced back and forth so i messed with the code and came up with a small adjustment <img src='http://www.berecursive.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . </p>
<p>Instead of:<br />
SquarePosition.X -= (float)(3 * Math.Cos(rotation));<br />
SquarePosition.Y -= (float)(3 * Math.Sin(rotation));</p>
<p>Use:<br />
if (XDistance &gt;= 3 | YDistance &gt;= 3 | XDistance &lt;= -3 | YDistance &lt;= -3 )<br />
{<br />
SquarePosition.X -= (float)(3 * Math.Cos(rotation));<br />
SquarePosition.Y -= (float)(3 * Math.Sin(rotation));<br />
}<br />
else<br />
{<br />
SquarePosition.X -= (float)(0 * Math.Cos(rotation));<br />
SquarePosition.Y -= (float)(0 * Math.Sin(rotation));<br />
}</p>
<p>Mess around with the = numbers to get a different range. Enjoy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: josh</title>
		<link>http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna/comment-page-1#comment-412</link>
		<dc:creator>josh</dc:creator>
		<pubDate>Mon, 22 Aug 2011 20:27:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.berecursive.com/blog/?p=8#comment-412</guid>
		<description>Thanks! this helped a lot (especially only being 15 :P )</description>
		<content:encoded><![CDATA[<p>Thanks! this helped a lot (especially only being 15 <img src='http://www.berecursive.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wzade</title>
		<link>http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna/comment-page-1#comment-318</link>
		<dc:creator>Wzade</dc:creator>
		<pubDate>Mon, 09 May 2011 19:14:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.berecursive.com/blog/?p=8#comment-318</guid>
		<description>Thanks for the tutorial, works like a charm! :)</description>
		<content:encoded><![CDATA[<p>Thanks for the tutorial, works like a charm! <img src='http://www.berecursive.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Patrick</title>
		<link>http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna/comment-page-1#comment-306</link>
		<dc:creator>Patrick</dc:creator>
		<pubDate>Fri, 29 Apr 2011 18:29:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.berecursive.com/blog/?p=8#comment-306</guid>
		<description>In order to do that you need to clamp the rotation to maximum and a minimum</description>
		<content:encoded><![CDATA[<p>In order to do that you need to clamp the rotation to maximum and a minimum</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daumier</title>
		<link>http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna/comment-page-1#comment-255</link>
		<dc:creator>Daumier</dc:creator>
		<pubDate>Tue, 01 Feb 2011 19:32:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.berecursive.com/blog/?p=8#comment-255</guid>
		<description>Hi, 

This code helped a lot in a project I&#039;m working on.
But I have a question, is it possible to &quot;Lock&quot; the rotation?

I&#039;m doing a shoot&#039;em up game, where the player is faced upwards, since he is moving in that direction. Aiming and rotation of the player sprite is controlled by the mouse, but I don&#039;t want the Player to be able to rotate around.

Basivally I only want him/her to aim in a cone, upwards like:  \/

So can you somehow stop the rotation? 
By lots of &quot;if(){}&quot;?

Thanks in advance!</description>
		<content:encoded><![CDATA[<p>Hi, </p>
<p>This code helped a lot in a project I&#8217;m working on.<br />
But I have a question, is it possible to &#8220;Lock&#8221; the rotation?</p>
<p>I&#8217;m doing a shoot&#8217;em up game, where the player is faced upwards, since he is moving in that direction. Aiming and rotation of the player sprite is controlled by the mouse, but I don&#8217;t want the Player to be able to rotate around.</p>
<p>Basivally I only want him/her to aim in a cone, upwards like:  \/</p>
<p>So can you somehow stop the rotation?<br />
By lots of &#8220;if(){}&#8221;?</p>
<p>Thanks in advance!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zack</title>
		<link>http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna/comment-page-1#comment-248</link>
		<dc:creator>Zack</dc:creator>
		<pubDate>Fri, 28 Jan 2011 18:42:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.berecursive.com/blog/?p=8#comment-248</guid>
		<description>JUST NORMALIZE THE VECTOR!

Position += Vector2.Normalize(SqurePosition - Mouse);</description>
		<content:encoded><![CDATA[<p>JUST NORMALIZE THE VECTOR!</p>
<p>Position += Vector2.Normalize(SqurePosition &#8211; Mouse);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: owen barker</title>
		<link>http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna/comment-page-1#comment-131</link>
		<dc:creator>owen barker</dc:creator>
		<pubDate>Thu, 18 Nov 2010 07:39:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.berecursive.com/blog/?p=8#comment-131</guid>
		<description>You should make your monsters and player as their own class. Reason because, that code you just displayed could get complex. AS for the people new to xna, its good code practices. Ive seen people code like this and get angry a couple months later, as they require to modify 80% of their code to make in function all through the application.

Learn OOP fast, it helps later on in xna game development, or basicly code 101.

As for your demo code, its pretty neat. 

-- Xna 4.0, windows phone 7 is the future?</description>
		<content:encoded><![CDATA[<p>You should make your monsters and player as their own class. Reason because, that code you just displayed could get complex. AS for the people new to xna, its good code practices. Ive seen people code like this and get angry a couple months later, as they require to modify 80% of their code to make in function all through the application.</p>
<p>Learn OOP fast, it helps later on in xna game development, or basicly code 101.</p>
<p>As for your demo code, its pretty neat. </p>
<p>&#8211; Xna 4.0, windows phone 7 is the future?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy</title>
		<link>http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna/comment-page-1#comment-102</link>
		<dc:creator>Andy</dc:creator>
		<pubDate>Wed, 13 Oct 2010 20:25:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.berecursive.com/blog/?p=8#comment-102</guid>
		<description>I have implemented alot of the code you have provided her but would like to make my soldier shoot bullets.  Do you have any advice?

My code so far:

namespace LastManDead
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        //Variables
        
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D soldier;
        Texture2D monster;
        Texture2D aim;
        Texture2D background;
        Vector2 soldierPosition;
        Vector2 monsterPosition;
        Vector2 aimPosition;
        float rotation;
        float mrotation;
        float timer = 0f;
        float mtimer = 0f;
        float interval = 200f;
        float minterval = 200f;
        Point framesize = new Point(60, 60);
        Point mFramesize = new Point(60, 60);
        int currentFrame = 0;
        int mcurrentFrame = 0;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = &quot;Content&quot;;
        }

        protected override void Initialize()
        {
            //make the mouse invisible!
            this.IsMouseVisible = false;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
            soldier = Content.Load(@&quot;Images\Soldier&quot;);
            monster = Content.Load(@&quot;Images\Monster&quot;);
            aim = Content.Load(@&quot;Images\Aim&quot;);
            background = Content.Load(@&quot;Images\skullfield&quot;);
            soldierPosition = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed &#124;&#124; Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            //Soldier Movement!!!!!
            
            //Store the mouse state
            MouseState mouse = Mouse.GetState();

            aimPosition.X = mouse.X;
            aimPosition.Y = mouse.Y;

            //Calculate the distance from the square to the mouse&#039;s X and Y position
            float XDistance = soldierPosition.X - mouse.X;
            float YDistance = soldierPosition.Y - mouse.Y;

            //Calculate the required rotation by doing a two-variable arc-tan
            rotation = (float)Math.Atan2(YDistance, XDistance);

            if (XDistance &gt; 20 &#124;&#124; XDistance  20 &#124;&#124; YDistance  interval)
                {
                    //If is incrememnt current frame
                    ++currentFrame;

                    //Check frame is within direction frames, if not set back to standing
                    if (currentFrame &gt;= 2)
                    {
                        currentFrame = 0;
                    }
                    //Reset timer
                    timer = 0f;
                }
            }
            else
                currentFrame = 0;

            //Monster Movement!!!!!!

            Rectangle soldierRect = new Rectangle((int)soldierPosition.X, (int)soldierPosition.Y, framesize.X, framesize.Y);

            float mXDistance = monsterPosition.X - soldierRect.Location.X;
            float mYDistance = monsterPosition.Y - soldierRect.Location.Y;

            //Calculate the required rotation by doing a two-variable arc-tan
            mrotation = (float)Math.Atan2(mYDistance, mXDistance);

            //Move soldier towards mouse by closing the gap 3 pixels per update
            monsterPosition.X -= (float)(1 * Math.Cos(mrotation));
            monsterPosition.Y -= (float)(1 * Math.Sin(mrotation));
            


            //Incremement timer using paramater
            mtimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;


            //Check if timer is greater than interval
            if (mtimer &gt; minterval)
            {
                //If is incrememnt current frame
                ++mcurrentFrame;

                //Check frame is within direction frames, if not set back to standing
                if (mcurrentFrame &gt;= 4)
                {
                    mcurrentFrame = 0;
                }
                //Reset timer
                mtimer = 0f;
            }


            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend,SpriteSortMode.FrontToBack,
                SaveStateMode.None);



            spriteBatch.Draw(aim,aimPosition, new Rectangle(0, 0, 20, 20), Color.White,
                0,new Vector2(aim.Height /2,aim.Width/2),1, SpriteEffects.None, 0);

            spriteBatch.Draw(soldier, soldierPosition, new Rectangle(currentFrame * framesize.X,
                0, framesize.X, framesize.Y), Color.White, rotation,
                new Vector2(framesize.X / 2, framesize.Y / 2), .7f, SpriteEffects.None, 0);

            spriteBatch.Draw(background, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
                Color.White);

            spriteBatch.Draw(monster, monsterPosition, new Rectangle(mcurrentFrame * mFramesize.X,
                0, mFramesize.X, mFramesize.Y), Color.White, mrotation,
                new Vector2(mFramesize.X / 2, mFramesize.Y / 2), .85f, SpriteEffects.None, 0);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}</description>
		<content:encoded><![CDATA[<p>I have implemented alot of the code you have provided her but would like to make my soldier shoot bullets.  Do you have any advice?</p>
<p>My code so far:</p>
<p>namespace LastManDead<br />
{<br />
    public class Game1 : Microsoft.Xna.Framework.Game<br />
    {<br />
        //Variables</p>
<p>        GraphicsDeviceManager graphics;<br />
        SpriteBatch spriteBatch;<br />
        Texture2D soldier;<br />
        Texture2D monster;<br />
        Texture2D aim;<br />
        Texture2D background;<br />
        Vector2 soldierPosition;<br />
        Vector2 monsterPosition;<br />
        Vector2 aimPosition;<br />
        float rotation;<br />
        float mrotation;<br />
        float timer = 0f;<br />
        float mtimer = 0f;<br />
        float interval = 200f;<br />
        float minterval = 200f;<br />
        Point framesize = new Point(60, 60);<br />
        Point mFramesize = new Point(60, 60);<br />
        int currentFrame = 0;<br />
        int mcurrentFrame = 0;</p>
<p>        public Game1()<br />
        {<br />
            graphics = new GraphicsDeviceManager(this);<br />
            Content.RootDirectory = &#8220;Content&#8221;;<br />
        }</p>
<p>        protected override void Initialize()<br />
        {<br />
            //make the mouse invisible!<br />
            this.IsMouseVisible = false;<br />
            base.Initialize();<br />
        }</p>
<p>        protected override void LoadContent()<br />
        {<br />
            spriteBatch = new SpriteBatch(GraphicsDevice);</p>
<p>            soldier = Content.Load(@&#8221;Images\Soldier&#8221;);<br />
            monster = Content.Load(@&#8221;Images\Monster&#8221;);<br />
            aim = Content.Load(@&#8221;Images\Aim&#8221;);<br />
            background = Content.Load(@&#8221;Images\skullfield&#8221;);<br />
            soldierPosition = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);<br />
        }</p>
<p>        protected override void UnloadContent()<br />
        {<br />
        }</p>
<p>        protected override void Update(GameTime gameTime)<br />
        {<br />
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))<br />
                this.Exit();</p>
<p>            //Soldier Movement!!!!!</p>
<p>            //Store the mouse state<br />
            MouseState mouse = Mouse.GetState();</p>
<p>            aimPosition.X = mouse.X;<br />
            aimPosition.Y = mouse.Y;</p>
<p>            //Calculate the distance from the square to the mouse&#8217;s X and Y position<br />
            float XDistance = soldierPosition.X &#8211; mouse.X;<br />
            float YDistance = soldierPosition.Y &#8211; mouse.Y;</p>
<p>            //Calculate the required rotation by doing a two-variable arc-tan<br />
            rotation = (float)Math.Atan2(YDistance, XDistance);</p>
<p>            if (XDistance &gt; 20 || XDistance  20 || YDistance  interval)<br />
                {<br />
                    //If is incrememnt current frame<br />
                    ++currentFrame;</p>
<p>                    //Check frame is within direction frames, if not set back to standing<br />
                    if (currentFrame &gt;= 2)<br />
                    {<br />
                        currentFrame = 0;<br />
                    }<br />
                    //Reset timer<br />
                    timer = 0f;<br />
                }<br />
            }<br />
            else<br />
                currentFrame = 0;</p>
<p>            //Monster Movement!!!!!!</p>
<p>            Rectangle soldierRect = new Rectangle((int)soldierPosition.X, (int)soldierPosition.Y, framesize.X, framesize.Y);</p>
<p>            float mXDistance = monsterPosition.X &#8211; soldierRect.Location.X;<br />
            float mYDistance = monsterPosition.Y &#8211; soldierRect.Location.Y;</p>
<p>            //Calculate the required rotation by doing a two-variable arc-tan<br />
            mrotation = (float)Math.Atan2(mYDistance, mXDistance);</p>
<p>            //Move soldier towards mouse by closing the gap 3 pixels per update<br />
            monsterPosition.X -= (float)(1 * Math.Cos(mrotation));<br />
            monsterPosition.Y -= (float)(1 * Math.Sin(mrotation));</p>
<p>            //Incremement timer using paramater<br />
            mtimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;</p>
<p>            //Check if timer is greater than interval<br />
            if (mtimer &gt; minterval)<br />
            {<br />
                //If is incrememnt current frame<br />
                ++mcurrentFrame;</p>
<p>                //Check frame is within direction frames, if not set back to standing<br />
                if (mcurrentFrame &gt;= 4)<br />
                {<br />
                    mcurrentFrame = 0;<br />
                }<br />
                //Reset timer<br />
                mtimer = 0f;<br />
            }</p>
<p>            base.Update(gameTime);<br />
        }</p>
<p>        protected override void Draw(GameTime gameTime)<br />
        {<br />
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);</p>
<p>            spriteBatch.Begin(SpriteBlendMode.AlphaBlend,SpriteSortMode.FrontToBack,<br />
                SaveStateMode.None);</p>
<p>            spriteBatch.Draw(aim,aimPosition, new Rectangle(0, 0, 20, 20), Color.White,<br />
                0,new Vector2(aim.Height /2,aim.Width/2),1, SpriteEffects.None, 0);</p>
<p>            spriteBatch.Draw(soldier, soldierPosition, new Rectangle(currentFrame * framesize.X,<br />
                0, framesize.X, framesize.Y), Color.White, rotation,<br />
                new Vector2(framesize.X / 2, framesize.Y / 2), .7f, SpriteEffects.None, 0);</p>
<p>            spriteBatch.Draw(background, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),<br />
                Color.White);</p>
<p>            spriteBatch.Draw(monster, monsterPosition, new Rectangle(mcurrentFrame * mFramesize.X,<br />
                0, mFramesize.X, mFramesize.Y), Color.White, mrotation,<br />
                new Vector2(mFramesize.X / 2, mFramesize.Y / 2), .85f, SpriteEffects.None, 0);</p>
<p>            spriteBatch.End();</p>
<p>            base.Draw(gameTime);<br />
        }<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: XNA User</title>
		<link>http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna/comment-page-1#comment-31</link>
		<dc:creator>XNA User</dc:creator>
		<pubDate>Sat, 26 Dec 2009 21:36:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.berecursive.com/blog/?p=8#comment-31</guid>
		<description>Oh dude this is Epic, searched so much for the solution, got there calculating an own type of Vector angles, and now the Solution for both (Drawing angle = rotation, moving direction)
is there in 3 lines of code. Dunno which Blog this is but i start loving the Author :)</description>
		<content:encoded><![CDATA[<p>Oh dude this is Epic, searched so much for the solution, got there calculating an own type of Vector angles, and now the Solution for both (Drawing angle = rotation, moving direction)<br />
is there in 3 lines of code. Dunno which Blog this is but i start loving the Author <img src='http://www.berecursive.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>

