<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sgt. Conker &#187; ZuneHD</title>
	<atom:link href="http://www.sgtconker.com/category/xna/zunehd/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sgtconker.com</link>
	<description>We are &#34;absolutely fine&#34;</description>
	<lastBuildDate>Thu, 09 Sep 2010 19:48:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Article: Draw me a line (Zune HD)</title>
		<link>http://www.sgtconker.com/2010/03/article-draw-me-a-line-zune-hd/</link>
		<comments>http://www.sgtconker.com/2010/03/article-draw-me-a-line-zune-hd/#comments</comments>
		<pubDate>Sun, 07 Mar 2010 16:43:11 +0000</pubDate>
		<dc:creator>Sgt. Conker</dc:creator>
				<category><![CDATA[2D]]></category>
		<category><![CDATA[Articles]]></category>
		<category><![CDATA[Touch]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[ZuneHD]]></category>
		<category><![CDATA[Zune HD]]></category>

		<guid isPermaLink="false">http://www.sgtconker.com/?p=1236</guid>
		<description><![CDATA[by Sgt. Conker

This article will show you how to create a dotted line based on touch input from the Zune HD.
This can be very useful for indicating to the user a path a game entity is going to take.
If you've ever played Flight Control for the iPhone you'll know exactly what I mean.

Firstly, if we [...]]]></description>
			<content:encoded><![CDATA[<h4 style="text-align: center;">by <a href="http://www.conkerjo.com/">Sgt. Conker</a></h4>
<p><a href="http://www.sgtconker.com/wp-content/uploads/2010/03/bots.png"><img src="http://www.sgtconker.com/wp-content/uploads/2010/03/bots-300x169.png" alt="" title="bots" width="300" height="169" class="alignright size-medium wp-image-1240" /></a></p>
<p>This article will show you how to create a dotted line based on touch input from the Zune HD.<br />
This can be very useful for indicating to the user a path a game entity is going to take.<br />
If you've ever played <a href="http://www.iphonehacks.com/images/09-Apr/flight-control-iphone-app-1.jpg">Flight Control</a> for the iPhone you'll know exactly what I mean.</p>
<p><span id="more-1236"></span></p>
<p>Firstly, if we were doing this in a real game, we would most likely have some sort of engine.<br />
I've built this sample with a very small Micro Engine. This Micro Engine simply contains a list of World Entities and with each update/draw call it loops through each entity requesting it to update and draw.<br />
The Micro Engine has a main MicroEngine class and an Entity class which holds position/size/texture etc.</p>
<p>I won't explain this in detail here as it's out of scope of the article.</p>
<p>The first thing we should do, is make this work with a mouse on Windows. This will help us develop the feature much faster and we will add Zune HD touch input later.</p>
<p>Because we're using SpriteBatch, we'll also need a texture to use as the dotted line. Our texture will be one of the dashes and we will draw it multiple times to form a dotted line. This texture looks like this</p>
<p>In our main game loop we first call update to the MicroEngine class so that it can update the mouse input state.</p>
<pre class="brush: csharp;">this.engine.Update(gameTime);</pre>
<p>Next we need to detect that there has been a new touch from the mouse. Our engine contains a WasNewTouch method which checks the last and current state of the mouse to provide the result.</p>
<pre class="brush: csharp;">
if (engine.WasNewTouch())
{
</pre>
<p>If there was a new touch we want to start drawing a new dotted line. I've created a custom Entity so that we can utilise the MicroEngine.<br />
Inside this custom entity which I named CustomLineEntity, we store a list of Vector2 items to reference as each dot in the line.</p>
<pre class="brush: csharp;">
public class CustomLineEntity : Entity
{
    public List&lt;Vector2&gt; Points { get; set; }

    public CustomLineEntity()
    {
        this.Points = new List&lt;Vector2&gt;();
    }
</pre>
<p>We then want to override the simple draw method of a world entity and loop through our points and draw each one.</p>
<pre class="brush: csharp;">
public override void Draw(SpriteBatch spriteBatch)
{
    for (int index = 0; index &lt; this.Points.Count; index++)
    {
        this.DrawIndex(spriteBatch, index);
    }
}
</pre>
<p>To actually draw the dot in the line, we need to work out the rotation of the dot so that it's a smooth curvy line as we draw. It's also done in the draw section so that the line can be adaptive, it's not always known what rotation the first or last items would be until the line is complete.</p>
<pre class="brush: csharp;">
private void DrawIndex(SpriteBatch spriteBatch, int index)
{
    var item = this.Points[index];
    float rotation = 0;
    Vector2 heading = Vector2.Zero;

    if (index &lt; this.Points.Count - 1)
    {
        // Get the direction of this index from the next one in the list.
        heading = this.Points[index + 1]- item;
    }
    else if (this.Points.Count &gt; 1)
    {
        // It's the last one so get the direction of this index from the previous one in the list.
        heading = this.Points[index - 1]- item;
    }

    float angle = (float)Math.Atan2(heading.Y, heading.X);
    rotation = angle;
</pre>
<p>And then simply draw the sprite at the correct position and angle.</p>
<pre class="brush: csharp;">
Vector2 origin = new Vector2(this.Texture.Width / 2, this.Texture.Height / 2);
spriteBatch.Draw(
                this.Texture,
                item,
                null,
                Color.White,
                rotation,
                origin,
                1,
                SpriteEffects.None,
                0);
</pre>
<p>Going back to the main game loop, we detected a new touch with</p>
<pre class="brush: csharp;">
if (engine.WasNewTouch())
{
</pre>
<p>Now we can create the new CustomLineEntity and give it a starting position.<br />
We also set a pickedUp flag so that we know we're in the drawing a new line state.</p>
<pre class="brush: csharp;">
   // Start the line dragging by adding a CustomLineEntity to the engines WorldEntities
   this.pickedUp = true;

   this.currentLine = new CustomLineEntity();
   this.currentLine.Texture = dot;
   this.currentLine.Position = engine.MousePosition;
   this.currentLine.Points.Add(new Vector2(engine.MousePosition.X, engine.MousePosition.Y));
   this.engine.WorldEntities.Add(this.currentLine);
}
</pre>
<p>Adding it to the WorldEntities ensures it will receive the Draw &amp; Update method calls.</p>
<p>Still in our main game update.<br />
We need to detect when the user lifts off so that we can end the drawing of the line.<br />
To make this a little more fun, we will add a bot into the game world which will follow the newly created line. We will describe the bot a little later, for now, we'll just add it to the world.</p>
<pre class="brush: csharp;">
if (engine.WasTouchReleased())
{
    //Create a new bot and stick it on the line we just created
    var bot = this.CreateBot(this.currentLine);
    this.engine.WorldEntities.Add(bot);

    this.pickedUp = false;
    this.currentLine = null;
}
</pre>
<p>The final thing in our main game update is to continue creating the line if we're moving around with our mouse.</p>
<pre class="brush: csharp;">
if (this.pickedUp)
{
    this.ContinueCreatingLine();
}
</pre>
<p>To do this we must calculate the distance between the last dot in the line and the new position of our mouse.<br />
If the distance is greater than the size of a dot, we can add a new one to the line.<br />
We also give it a little gap so that it looks prettier.</p>
<pre class="brush: csharp;">
private void ContinueCreatingLine()
{
    // Get the position of the last item in the points
    Vector2 lastPos = this.currentLine.Points[this.currentLine.Points.Count - 1];

    // If the distance is greater than the width of a dot
    if (Vector2.Distance(engine.MousePosition, lastPos) &gt; dot.Width)
    {
        // Get the direction from the last point
        Vector2 dir = engine.MousePosition - lastPos;
        dir.Normalize();

        // Push the new point out at by the width of a dot plus a gap
        int gap = 5;
        var newPosition = lastPos + (dir * (dot.Width + gap));

        // Add it on to the current lines list.
        var newPoint = new Vector2(newPosition.X, newPosition.Y);

        this.currentLine.Points.Add(newPoint);
    }
}
</pre>
<p>And that's it, we can drag our mouse cursor around the screen and draw a dashed line.<br />
As stated a little earlier we want to make this more interesting by adding something which will follow this line. Otherwise it's fairly pointless <img src='http://www.sgtconker.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Going back to the line which created a new world entity when the line was completed.</p>
<pre class="brush: csharp;">
var bot = this.CreateBot(this.currentLine);
this.engine.WorldEntities.Add(bot);
</pre>
<p>I've created a custom BotEntity which has custom update logic.</p>
<p>The creation of the BotEntity sets the current line as it's path.</p>
<pre class="brush: csharp;">
private BotEntity CreateBot(CustomLineEntity line)
{
    BotEntity bot = new BotEntity();
    bot.Texture = this.botTexture;
    bot.Size = new Point(bot.Texture.Width, bot.Texture.Height);
    bot.LineToFollow = line;
    bot.Position = line.Position;
    bot.Layer = 1;
    return bot;
}
</pre>
<p>In the main game class we also have a Clear method which clears out the world entities if you press escape.</p>
<p>Here's the complete class.</p>
<pre class="brush: csharp;">
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;
        private Texture2D dot;
        private Texture2D botTexture;
        private MicroEngine engine;

        private bool pickedUp;
        private CustomLineEntity currentLine;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = &quot;Content&quot;;
            this.IsMouseVisible = true;
            this.graphics.PreferredBackBufferWidth = 480;
            this.graphics.PreferredBackBufferHeight = 272;
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            dot = Content.Load&lt;Texture2D&gt;(&quot;dot&quot;);
            this.botTexture = Content.Load&lt;Texture2D&gt;(&quot;bot&quot;);
            this.engine = new MicroEngine();
        }

        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Clear();
            }

            this.engine.Update(gameTime);

            if (engine.WasNewTouch())
            {
                // Start the line dragging by adding a CustomLineEntity to the engines WorldEntities
                this.pickedUp = true;

                this.currentLine = new CustomLineEntity();
                this.currentLine.Texture = dot;
                this.currentLine.Position = engine.MousePosition;
                this.currentLine.Points.Add(new Vector2(engine.MousePosition.X, engine.MousePosition.Y));
                this.engine.WorldEntities.Add(this.currentLine);
            }

            if (engine.WasTouchReleased())
            {
                //Create a new bot and stick it on the line we just created
                var bot = this.CreateBot(this.currentLine);
                this.engine.WorldEntities.Add(bot);

                this.pickedUp = false;
                this.currentLine = null;
            }

            if (this.pickedUp)
            {
                this.ContinueCreatingLine();
            }

            base.Update(gameTime);
        }

        private void ContinueCreatingLine()
        {
            // Get the position of the last item in the points
            Vector2 lastPos = this.currentLine.Points[this.currentLine.Points.Count - 1];

            // If the distance is greater than the width of a dot
            if (Vector2.Distance(engine.MousePosition, lastPos) &gt; dot.Width)
            {
                // Get the direction from the last point
                Vector2 dir = engine.MousePosition - lastPos;
                dir.Normalize();

                // Push the new point out at by the width of a dot plus a gap
                int gap = 5;
                var newPosition = lastPos + (dir * (dot.Width + gap));

                // Add it on to the current lines list.
                var newPoint = new Vector2(newPosition.X, newPosition.Y);

                this.currentLine.Points.Add(newPoint);
            }
        }

        private BotEntity CreateBot(CustomLineEntity line)
        {
            BotEntity bot = new BotEntity();
            bot.Texture = this.botTexture;
            bot.Size = new Point(bot.Texture.Width, bot.Texture.Height);
            bot.LineToFollow = line;
            bot.Position = line.Position;
            bot.Layer = 1;
            return bot;
        }

        private void Clear()
        {
            this.engine.WorldEntities.Clear();
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            this.engine.Draw(this.spriteBatch);

            base.Draw(gameTime);
        }
    }
</pre>
<p>The bot is a little more complex and is slightly out of scope of the article. But i'll briefly go over what this entity does.</p>
<p>On creation it was supplied a CustomLineEntity class to use as it's path.<br />
We want the bot to turn as it goes around the curvy line as it moves along it.<br />
For this we use the TurnToFace and WrapAngle methods from the Chase And Evade sample on creators</p>
<p>http://creators.xna.com/en-US/sample/chaseevade</p>
<p>In the main Update of the BotEntity we first check if we have a valid target. If we don't we must be just starting or we've got to the end.<br />
Use the first point in the list as our reference point.</p>
<pre class="brush: csharp;">
public override void Update(float elapsed)
{
    if (!this.destinationReached &amp;&amp; this.LineToFollow != null)
    {
        if (!this.target.HasValue)
        {
            if (this.LineToFollow.Points.Count &gt; 0)
            {
                this.target = this.LineToFollow.Points[0].Position;
                this.targetIndex = 0;
            }
        }
</pre>
<p>If the bot does have a valid target, we should move towards it and turn smoothly.<br />
If the distance is short enough to be considered reached, we move onto the next target.<br />
If there's no next target available, we're at our destination.</p>
<pre class="brush: csharp;">
this.MoveToTarget(elapsed);
if (Vector2.Distance(this.Position, this.target.Value) &lt; distThreshold)
{
    if (targetIndex + 1 &lt; this.LineToFollow.Points.Count)
    {
        this.targetIndex++;
        this.target = this.LineToFollow.Points[this.targetIndex].Position;
    }
    else
    {
        this.destinationReached = true;
    }
}
</pre>
<p>I'm not going to print out the TurnToFace method here, but this is how we use it to turn and move towards our target.<br />
We get the direction between our current position and our target.<br />
We then use the simple calculation of direction * speed * time to move it smoothly towards the target.<br />
And finally we set the rotation using the TurnToFace method.</p>
<pre class="brush: csharp;">
private void MoveToTarget(float elapsed)
{
    var direction = this.target.Value - this.Position;
    if (direction == Vector2.Zero)
    {
        return;
    }

    direction.Normalize();

    const float speed = 50;
    const float turnSpeed = 0.025f;
    this.Position += direction * speed * elapsed;
    this.Rotation = TurnToFace(this.Position, this.target.Value, this.Rotation, turnSpeed);
}
</pre>
<p>If we now run our sample we can drag a curvy dotted line onto the screen with our mouse and a world object will follow it nicely <img src='http://www.sgtconker.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  cool uh?</p>
<p>Now we need to make this work on the Zune.<br />
First we must create a new project type for Zune. this is easy using the built in visual studio create copy feature.</p>
<p>Luckily for us, the Micro Engine already supports Zune and has the code to treat a touch just like a mouse touch.</p>
<p>It has a few #if(ZUNE) batches of code inside WasTouchReleased and WasNewTouch</p>
<p>They look like this</p>
<pre class="brush: csharp;">
public bool WasNewTouch()
{
    if (this.LastMouseState.LeftButton == ButtonState.Released &amp;&amp;
        this.CurrentMouseState.LeftButton == ButtonState.Pressed)
    {
        return true;
    }
#if(ZUNE)
    if (this.currentTouchState != null)
    {
        if (currentTouchState.State == TouchLocationState.Pressed)
        {
            if (lastTouchState != null)
            {
                if (lastTouchState.State == TouchLocationState.Released)
                {
                    return true;
                }
            }
            else
            {
                return true;
            }
        }
    }
#endif

    return false;
}

public bool WasTouchReleased()
{
    if (this.LastMouseState.LeftButton == ButtonState.Pressed &amp;&amp;
        this.CurrentMouseState.LeftButton == ButtonState.Released)
    {
        return true;
    }
#if(ZUNE)
    if (this.currentTouchState != null)
    {
        if (currentTouchState.State == TouchLocationState.Released)
        {
            return true;
        }
    }
#endif

    return false;
}
</pre>
<p>This now runs on the Zune HD <img src='http://www.sgtconker.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>I'm sure there are lots of improvements that can be made here. But it's good enough for my prototype <img src='http://www.sgtconker.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
The main one I would like to improve on in future would be the smoothness of the curve much like the Flight Control example at the beginning.</p>
<p>Here's the full source code<br />
<a href="http://www.sgtconker.com/Downloads/articles/DrawMeALine.zip">Download Source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sgtconker.com/2010/03/article-draw-me-a-line-zune-hd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display Orientation on the ZuneHD</title>
		<link>http://www.sgtconker.com/2010/01/display-orientation-on-the-zunehd/</link>
		<comments>http://www.sgtconker.com/2010/01/display-orientation-on-the-zunehd/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 08:19:07 +0000</pubDate>
		<dc:creator>Captain ZSquare</dc:creator>
				<category><![CDATA[2D]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Input]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[ZuneHD]]></category>
		<category><![CDATA[FGF]]></category>
		<category><![CDATA[John Sedlak]]></category>
		<category><![CDATA[MVP]]></category>
		<category><![CDATA[Zune HD]]></category>

		<guid isPermaLink="false">http://www.sgtconker.com/?p=941</guid>
		<description><![CDATA[John Sedlak has two posts about using his FGF framework while working on the Zune HD and dealing with orientation.
He talks about how to support rotating and resizing of the display on the Zune HD (which supports both landscape and portrait gaming) and then continues with more explanations.
]]></description>
			<content:encoded><![CDATA[<p>John Sedlak has two posts about using his FGF framework while working on the Zune HD and dealing with orientation.</p>
<p>He talks about <a href="http://jsedlak.org/2010/01/11/fgf-display-orientation-on-the-zune-hd/">how to support rotating and resizing of the display on the Zune HD</a> (which supports both landscape and portrait gaming) and then continues with <a href="http://jsedlak.org/2010/01/12/fgf-oriented-drawing/">more explanations</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sgtconker.com/2010/01/display-orientation-on-the-zunehd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Article: Zune HD Accelerometer Basics</title>
		<link>http://www.sgtconker.com/2009/10/zune-hd-accelerometer-basics-placeholder-2/</link>
		<comments>http://www.sgtconker.com/2009/10/zune-hd-accelerometer-basics-placeholder-2/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 13:01:22 +0000</pubDate>
		<dc:creator>Sgt. Conker</dc:creator>
				<category><![CDATA[Accelerometer]]></category>
		<category><![CDATA[Articles]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[ZuneHD]]></category>
		<category><![CDATA[Tiny Engine]]></category>

		<guid isPermaLink="false">http://sgt.conkerjo.com/?p=16</guid>
		<description><![CDATA[Start at the beginning, learn how to use the Accelerometer for the ZuneHD using XNA 3.1]]></description>
			<content:encoded><![CDATA[<h4 style="text-align: center;">by <a href="http://www.conkerjo.com/">Conkerjo</a></h4>
<p><a href="http://www.conkerjo.com/wp-content/uploads/2009/10/mrtiny.png"><img class="alignright" style="display: inline; border: 0px initial initial;" title="mrtiny" src="http://www.conkerjo.com/wp-content/uploads/2009/10/mrtiny_thumb.png" border="0" alt="mrtiny" width="94" height="171" /></a><br />
This article explains the very basics of using the accelerometer on the ZuneHD using XNA 3.1<br />
<span id="more-16"></span></p>
<p><strong>Note</strong><br />
<em>This sample uses a build of the TinyEngine at the time it was written.</em></p>
<p>In this sample I want to show how to move a sprite around using only the Accelerometer for input. We will utilize some of the features of TinyEngine to get us up and running quickly.</p>
<p>XNA Zune Extensions provide a static class called Accelerometer. It provides a GetState method which returns the current state of the Accelerometer as a AccelerometerState type.</p>
<pre class="brush: csharp;">
	public struct AccelerometerState
    {
        public Vector3 Acceleration { get; }

        public bool IsConnected { get; }

        public Matrix GetRotation();
    }
</pre>
<p>Note that this class is not available on Windows and must be wrapped in a #if ZUNE conditional compilation statement if trying to develop a cross-platform game.<br />
The Acceleration is a 3 dimensional value of the direction the Accelerometer is moving at the time.&lt; /br&gt;<br />
The GetRotation gives us a Matrix of the rotation of the device. This is great information for tilting the view to the direction of the device, but that's out of scope for this article.</p>
<p>To use this in our code, as we're only moving a simple 2D sprite, we only need the X &amp; Y of the the Acceleration to apply to the Sprites move method.</p>
<pre class="brush: csharp;">
    AccelerometerState state = Accelerometer.GetState();
    Vector2 accel = new Vector2(state.Acceleration.X, -state.Acceleration.Y);
    this.mrTiny.Move(accel * 100 * elapsed);
</pre>
<p>The Move method here just simply adds the passed in Vector2 to the current Position of the Sprite.</p>
<p>We then add a Clamp check so that the Sprite doesnt go off screen&lt; /br&gt;<br />
Again, TinyEngine provides us with the Bounds of the Screen. With TinyEngine 0,0 is the center of the screen and not the XNA default of top left.</p>
<pre class="brush: csharp;">
    this.mrTiny.PositionX = MathHelper.Clamp(this.mrTiny.PositionX, this.ScreenBounds.Left, this.ScreenBounds.Right);
    this.mrTiny.PositionY = MathHelper.Clamp(this.mrTiny.PositionY, this.ScreenBounds.Top, this.ScreenBounds.Bottom);
</pre>
<p>And there we have it, you can tilt your Zune HD to move around Mr Tiny.</p>
<p>The code is linked just below.</p>
<p><a href="http://www.conkerjo.com/Downloads/TinyEngine.zip" target="_blank">Source Code (Includes Tiny Engine Source)</a></p>
<pre>Conkerjo - www.conkerjo.com - www.twitter.com/conkerjo (Come up with some generic way to show the author)</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sgtconker.com/2009/10/zune-hd-accelerometer-basics-placeholder-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
