<?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>Silverlight &#124; WPF &#124; Microsoft.Net &#187; Collision</title>
	<atom:link href="http://joel.neubeck.net/tag/collision/feed/" rel="self" type="application/rss+xml" />
	<link>http://joel.neubeck.net</link>
	<description>Simplifing structure without changing results</description>
	<lastBuildDate>Wed, 26 May 2010 18:43:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Developing a Casual Games with Silverlight 2 &#8211; Module 2</title>
		<link>http://joel.neubeck.net/2008/11/casual-game-m2-expression-newsletter/</link>
		<comments>http://joel.neubeck.net/2008/11/casual-game-m2-expression-newsletter/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 17:54:59 +0000</pubDate>
		<dc:creator>joel</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[AABB]]></category>
		<category><![CDATA[casual games]]></category>
		<category><![CDATA[Collision]]></category>
		<category><![CDATA[Expression Newsletter]]></category>

		<guid isPermaLink="false">http://joel.neubeck.net/?p=319</guid>
		<description><![CDATA[This week Microsoft published the latest issue of the Expression Newsletter. If you have not seen the newsletter its a great series of articles focused on using Silverlight and the Expression tools to create amazing rich interactive solutions. In this latest issue I was asked to contribute the second of a six part series of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.microsoft.com/expression/news-press/newsletter/default.aspx"><img class="alignnone size-full wp-image-236" title="Microsoft Expression" src="http://joel.neubeck.net/wp-content/uploads/2008/09/microsoft_expression_logo.gif" alt="" width="191" height="62" /></a>This week Microsoft published the latest issue of the Expression Newsletter.  If you have not seen the newsletter its a great series of articles focused on using Silverlight and the Expression tools to create amazing rich interactive solutions.  In this latest issue I was asked to contribute the second of a six part series of articles on  <a href="http://expression.microsoft.com/en-us/dd279542.aspx">exploring the process of designing and building a casual online game in Silverlight 2.</a>.   Here is what  the series will include.</p>
<ul>
<li>Module 1: Getting Started – Architecture / framework</li>
<li><strong>Module 2: Movement and collision detection</strong></li>
<li>Module 3: Design – Sprites, boards and dialogs</li>
<li>Module 4: Animations and sound</li>
<li>Module 5: Initialization and Deployment</li>
<li>Module 6: Advanced concepts (Physics, Multiplayer, Optimization)</li>
</ul>
<p>Make sure to check out the article at <a href="http://www.microsoft.com/expression/news-press/newsletter/">http://www.microsoft.com/expression/news-press/newsletter/</a></p>
<p><code> </code></p>
<p>Code: COMING SOON</p>
]]></content:encoded>
			<wfw:commentRss>http://joel.neubeck.net/2008/11/casual-game-m2-expression-newsletter/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Silverlight 2 MVC Game Framework</title>
		<link>http://joel.neubeck.net/2008/06/silverlight-2-game-framework-example/</link>
		<comments>http://joel.neubeck.net/2008/06/silverlight-2-game-framework-example/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 22:55:08 +0000</pubDate>
		<dc:creator>joel</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[casual games]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Collision]]></category>
		<category><![CDATA[Elastic Collision]]></category>
		<category><![CDATA[Inelastic Collision]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://joel.neubeck.net/2008/06/silverlight-2-game-framework-example/</guid>
		<description><![CDATA[Last week I presented a session at DesertCodeCamp 2008 on Developing Casual Games with Silverlight 2. Excluding some technical difficulties, getting my MacBook to connect to dual projectors, the presentation went well. There is certainly interest in the community to begin using Silverlight as a casual gaming platform. The goal of this presentation was to [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I presented a session at <a href="http://desertcodecamp.com" target="_blank">DesertCodeCamp</a> 2008 on Developing Casual Games with Silverlight 2. Excluding some technical difficulties, getting my MacBook to connect to dual projectors, the presentation went well.  There is certainly interest in the community to begin using Silverlight as a casual gaming platform.</p>
<p>The goal of this presentation was to create a more “developer” focused exploration into a development architecture one could use for building a casual game. In this example, one which leverages pixel-based movement.  As with everything in development, there is no real right or wrong architecture, just proven patterns, that have proven scalability, flexibility and efficiency.</p>
<h2>Concept</h2>
<p>Build a simple game where a player has to move around a box and avoid a series of randomly moving spheres.  Each sphere can collide with both the bounds of the box, each other and the player.  The player can only be collided with 5 times or the game is over.  Player movement is controlled by hitting a combination of arrow keys which changes the vector angle of the players movement.  The players speed is consistent, until that player collides with one of the spheres.  The behavior of the collision will change based on the element being collided with..</p>
<h2>Framework</h2>
<p>Last fall Microsoft released its first version of the Model-View-Controller (MVC) framework for ASP.NET as a way to help developers implement this proven separation pattern into ASPX site design. If you have not got a chance to use I strongly recommend giving it a try.</p>
<p><a href="http://joel.neubeck.net/wp-content/uploads/2008/06/cc-game-solution.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" title="VS 2008 Solution" src="http://joel.neubeck.net/wp-content/uploads/2008/06/cc-game-solution-thumb.png" border="0" alt="VS 2008 Solution" width="241" height="594" align="left" /></a>At a very high level MVC divides an application&#8217;s implementation into three components: models, views, and controller.  This “separation” is used specifically as a way to improve a developers ability to decouple the presentation layer from the business logic.  In game development, MVC is an excellent way to build a foundation which simplifies a developers ability to control state such as movement and collision detection.  Unfortunately a formal MVC framework does not exist for Silverlight 2, but implementing the pattern is pretty simple.  The image to left illustrates how I have implemented the MCV pattern in my example project.</p>
<p>Exploring further you will see that I do not consider “page.xaml” a view. This is intentional.  page.xaml is simply a way to get my controller instantiated.  All game elements will be inserted into my “Shell.xaml” view by my controller, and this UserControl will be inserted into the page’s LayoutRoot after the page’s xaml has been loaded.  My controller will get a reference to this root UIElement via constructor dependency injection, a common approach seen in MVC implementations.  Here is the code found within page.xaml.cs.</p>
<p><br style="clear: both" /></p>

<div class="wp_syntax"><div class="code"><pre class="c-sharp" style="font-family:monospace;">public partial class Page : UserControl
{
    private Controllers.Controller _controller;
    private Views.Shell _shell = new Views.Shell();
    public Views.Shell Shell
    {
        get { return _shell; }
        set { _shell = value; }
    }
&nbsp;
    public Page()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Page_Loaded);
    }
&nbsp;
    void Page_Loaded(object sender, RoutedEventArgs e)
    {
        LayoutRoot.Children.Add(_shell);
        _controller = new Controllers.Controller(this);
        _controller.Initialize();
    }
}</pre></div></div>

<h2>Controller</h2>
<p>In this game I have a single controller which acts as the central nervous system of my game. It controls the following:</p>
<ul>
<li>Primary game timer (tick)
<ul>
<li>Moves Player, Balls and checks for Collisions</li>
</ul>
</li>
<li>Inserts the appropriate dialogs and listens to events from each
<ul>
<li>Start, End, Next Level</li>
</ul>
</li>
<li>Listening for events from my primary model
<ul>
<li>Collision, NextLevel, GameOver and StopMovement</li>
</ul>
</li>
<li>Captures the approriate keyboard inputs
<ul>
<li>Left, Right, Up, Down, Left-Up, Left-Down, Right-Up, Right-Down</li>
</ul>
</li>
</ul>
<h2>Game Model</h2>
<p>The controller instantiates and holds a reference to my primary game model which will maintain the games state and expose any methods used to interact with the “Player” and “Hazards” [ball]).  The model is the single places that knows how to start a level, insert the player and place each of the balls into a level.  When events occur like the player collides with a ball, the model fires an event to the controller indicating what transpired.  The controller decides what this means visually to the game play (updates the “Shell” to indicate a new Hit count).  The model maintains state so it will be responsible for deciding when the game is over.  A game is over when either time has expired (15 seconds) or five collisions have occurred.  As with a collision, the model will fire an event to the controller when the game is over.</p>
<h2>Behaviors</h2>
<p>Part of building this sample was to build upon some of the previous work I have done on spherical collisions.  Is previous posts, I have shown how to react to both an elastic and inelastic collision.  In this example I refer to elastic behavior as “Deflect” and inelastic as “Bump”.  The idea is that if I build an object oriented methodology of defining collision behavior, one based on inheritance, I can expand the types of hazards as I develop new behaviors.  For example, the next behavior I intend to develop is one which hazards chases the player, when the hazard gets within a specified proximity of the player.</p>
<p>Here is the game and source.</p>
<p><iframe src="/wp-content/uploads/2008/06/CodeCampGame/default.html" width="400" height="400"></iframe></p>
<p>Code: <a onclick="javascript: pageTracker._trackPageview('/code/CodeCampGame.zip');" href="/wp-content/uploads/2008/06/CodeCampGame/CodeCampGame.zip">CodeCampGame.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joel.neubeck.net/2008/06/silverlight-2-game-framework-example/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Silverlight Circular Collision Animation</title>
		<link>http://joel.neubeck.net/2008/05/silverlight-circular-collision-animation/</link>
		<comments>http://joel.neubeck.net/2008/05/silverlight-circular-collision-animation/#comments</comments>
		<pubDate>Wed, 14 May 2008 19:34:55 +0000</pubDate>
		<dc:creator>joel</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[Circular Rotation]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Collision]]></category>
		<category><![CDATA[Deflection]]></category>
		<category><![CDATA[DispatcherTimer]]></category>
		<category><![CDATA[RotateTransform]]></category>
		<category><![CDATA[TranslateTransform]]></category>

		<guid isPermaLink="false">http://joel.neubeck.net/2008/05/silverlight-circular-collision-animation/</guid>
		<description><![CDATA[Building on the sample I created on Circular Motion, in this example I add a concept of collision.&#160; In physics, deflection is the event that occurs where an object collides and bounces off of another surface.&#160; In my example, two spheres collide at a given X/Y causing each ball to reverse direction and maintains its [...]]]></description>
			<content:encoded><![CDATA[<p>Building on the sample I created on <a href="/2008/05/silverlight-circular-motion-animation/">Circular Motion</a>, in this example I add a concept of collision.&nbsp; In physics, deflection is the event that occurs where an object collides and bounces off of another surface.&nbsp; In my example, two spheres collide at a given X/Y causing each ball to reverse direction and maintains its current speed and angle.</p>
<p><iframe src="/wp-content/uploads/2008/05/CircularCollision/default.html" width="300" height="300"></iframe></p>
<p>Just as in my previous example I use a single DispatcherTimer and a generic collection of UserConrols. To keep track of the direction that each ball is traveling I added a boolean property to my &#8220;Ball&#8221; user control to determine if it was rotating clockwise.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="c-sharp" style="font-family:monospace;">void _timer_Tick(object sender, EventArgs e)
{
    foreach (Ball ball in _balls)
    {
        double x = 0.0;
        double y = 0.0;
        double angle = ball.Rotate.Angle;
&nbsp;
        CalculatePosition(ball.Clockwise, ref x, ref y, ref angle, 1);
        //update the position of the ball
        ball.Rotate.Angle = angle;
        ball.Translate.X = x;
        ball.Translate.Y = y;
&nbsp;
        //check to see if a collision has occured
        if (ball.CollisionType == Ball.CollisionTypes.Deflect)
        {
            foreach (Ball ball2 in _balls)
            {
                if (!ball.Equals(ball2))
                {
                    double b2X = 0.0;
                    double b2Y = 0.0;
                    double b2A = ball2.Rotate.Angle;
                    //if we wait until the two balls are at the same X/Y then the balls
                    //will have overlapped.  Instead, predict by looking 5 degrees ahead 
                    //then switching direction.
                    CalculatePosition(ball2.Clockwise, ref b2X, ref b2Y, ref b2A, 5);
                    if (Math.Ceiling(x).Equals(Math.Ceiling(b2X))
                        &amp;amp;&amp;amp; Math.Ceiling(y).Equals(Math.Ceiling(b2Y)))
                    {
                        ball.Clockwise = !ball.Clockwise;
                        ball2.Clockwise = !ball2.Clockwise;
                        //ball.sound.Position = TimeSpan.FromMilliseconds(0);
                        //ball.sound.Play();
                        break;
                    }
                }
            }
        }
    }
}</pre></td></tr></table></div>

<p>As you can see in the code above, I am doing a quick check to see if my rotating ball is defined to deflect upon collision. In this example, this is the only type of collision supported. In the future, I might add things like exploding on impact or magnetism upon the collision. </p>
<p>The first step in the circular rotation is to move each ball to its new X/Y position. Once moved, I check to see if any other balls overlap its current X/Y location. To make the collision more realistic (no overlap), I predict where each additional ball will exist 5 degrees forward of its current angle. This allows me to simulate the ball just touching each other before reversing directions. </p>
<p>Code: <a onclick="javascript: pageTracker._trackPageview('/code/CircularCollision.zip');" href="/wp-content/uploads/2008/05/CircularCollision/CircularCollision.zip">CircularCollision.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joel.neubeck.net/2008/05/silverlight-circular-collision-animation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
