Silverlight Feeds - All your Silverlight feeds in one place.

Sponsors

Monday, September 27, 2010

Collision Detection on Windows Phone 7

by andy@andybeaulieu.com via Andy's Blog on 9/27/2010 9:24:00 PM

If you're using Silverlight on Windows Phone 7 for casual games development, performing a HitTest to determine if two elements collide is a basic need. I've blogged about how to do this in desktop Silverlight in the past, and for the most part this technique works great in WP7... EXCEPT when your app is running in Landscape orientation.

[DOWNLOAD SOURCE]

The problem in WP7 is that FindElementsInHostCoordinates will always return elements in portrait orientation, so that they are rotated 90 degrees from the top left origin. We can work around this using a RotateTransform to determine what point we want to test.

public static Point HackPointForWindowsPhoneLandscape(Point pt)

{

    double angle = 90;

    Point ptOrigin = new Point(0, 0);

 

    RotateTransform rt = new RotateTransform();

    rt.Angle = angle;

    rt.CenterX = ptOrigin.X;

    rt.CenterY = ptOrigin.Y;

    Point ptReturn = rt.Transform(pt);

 

    ptReturn.X = ptReturn.X + 480;

 

    return ptReturn;

}

In the download sample, the CollisionHelper class wraps this hack all up for you. You just need to use the CheckCollision method to see if two elements collide:

if (CollisionHelper.CheckCollision(ship, shipShell, asteroidXaml1, pathAsteroid1))

{

    txtStatus.Text = "Collision with XAML Element!";

    return;

}

 

That's it! This gives us pixel-perfect collision detection with pretty good performance.

email it!bookmark it!digg it!

Original Post: Collision Detection on Windows Phone 7

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. Silverlight Feeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on Silverlight Feeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us