So I installed Physics helper but the one that was offered above DOESN'T work with any windows phone build past mango. So I hunted down Physics Helper XAML which is not an install file and has to poor documentation to use it in the foreseeable future.
So I created my own XAML collision detection system. It is pretty simple, does what I need and is fast.
Most importantly though you can edit your canvas in Blend or in the WYSISYG editor and it not break the code.
First I create my nested canvas with the items I would like to be collidable
<Canvas x:Name="Collidables">
<Image x:Name="star" Width="50" Height="50" Source="images/star.png" Canvas.Left="54" Canvas.Top="-132"/>
<Image x:Name="star_Copy" Width="50" Height="50" Source="/images/star.png" Canvas.Left="158" Canvas.Top="-190"/>
<Image x:Name="star_Copy1" Width="50" Height="50" Source="/images/star.png" Canvas.Left="268" Canvas.Top="-260"/>
<Image x:Name="star_Copy2" Width="50" Height="50" Source="/images/star.png" Canvas.Left="372" Canvas.Top="-322"/>
<Image x:Name="star_Copy3" Width="50" Height="50" Source="/images/star.png" Canvas.Left="54" Canvas.Top="-390"/>
<Image x:Name="star_Copy4" Width="50" Height="50" Source="/images/star.png" Canvas.Left="158" Canvas.Top="-448"/>
<Image x:Name="star_Copy5" Width="50" Height="50" Source="/images/star.png" Canvas.Left="268" Canvas.Top="-518"/>
<Image x:Name="star_Copy6" Width="50" Height="50" Source="/images/star.png" Canvas.Left="372" Canvas.Top="-580"/>
<Image x:Name="star_Copy7" Width="50" Height="50" Source="/images/star.png" Canvas.Left="372" Canvas.Top="-798"/>
<Image x:Name="star_Copy8" Width="50" Height="50" Source="/images/star.png" Canvas.Left="268" Canvas.Top="-882"/>
<Image x:Name="star_Copy9" Width="50" Height="50" Source="/images/star.png" Canvas.Left="158" Canvas.Top="-982"/>
<Image x:Name="star_Copy10" Width="50" Height="50" Source="/images/star.png" Canvas.Left="54" Canvas.Top="-1060"/>
</canvas>
Then you can go to the code behind to do the rest ##NOTE THAT EVERY ITEM IS NAMED UNIQUELY##
Within the game loop I have the following methods declared to check for collision detection
collisionChecks();
handleCollisions();
So what is happening in the first method is this:
All the elements from the collidable canvas are pulled into a list
To save on checking items that aren't even on the screen I weed them
out by saying if the Top of the element is on the screen add it to the
collisionEnabled List otherwise forget it.
public void collisionChecks()
{
List<UIElement> AllCollidables = Collidables.Children.ToList();
collisionEnabled.Clear();
foreach (UIElement item in AllCollidables)
{
if (Canvas.GetTop((Image)item) + canvasShift > 0)
collisionEnabled.Add(item as Image);
}
}
Now we can Handle the objects that are eligible for collisions
First foreach loop checks to see if there is a collision between the player and each object that is in the collisionEnabled List.
If there is a collision it gets added to the Collisions to handle list
Once we have all the objects that are actually being collide with we can handle them individually based on their name to determine what to do with them. For example in this code I say if the name contains "star" then have it disappear and soon it will play a sound also. But I could also say if it contains "rocket" then change the player state to flying or what ever.
public void handleCollisions()
{
Rect player = new Rect();
player.X = Canvas.GetLeft(Player);
player.Y = Canvas.GetTop(Player);
player.Height = Player.Height;
player.Width = Player.Width;
foreach (Image item in collisionEnabled)
{
if (item.Visibility == Visibility.Visible)
{
Rect obj = new Rect();
obj.X = Canvas.GetLeft(item);
obj.Y = Canvas.GetTop(item) + canvasShift;
obj.Height = item.Height;
obj.Width = item.Width;
obj.Intersect(player);
if (!obj.IsEmpty)
{
collisionsToHandle.Add(item);
}
}
}
foreach (Image item in collisionsToHandle)
{
item.Visibility = Visibility.Collapsed;
if (item.Name.ToLower().Contains("star"))
{
score += 100;
}
}
collisionsToHandle.Clear();
collisionEnabled.Clear();}
This isn't the cleanest way to implement a collision detection system but it is the smallest and fastest one I have ever come up with. Hope this helps anyone else interested in simple 2D Collision detection for XAML Objects