March 12th, 2009

Collision testing on a free moving object in Silverlight

All Silverlight Examples, Silverlight 2 tutorials, Silverlight C# Tutorials, Silverlight Effects, Silverllight Games, by Gavin Wignall.

The below example shows a dragable ball that when released will bounce around with a simulated gravity effect. There is also a box (‘Move me’) that you can drag that the bouncy ball will bounce off / on.

In a previous post (here) I dabbled in making a dragable bouncy ball that simulated a gravity effect when released. I have taken that project and cleaned it up a bit to get rid of most of the bugs.


I have also set up all of the controlling factors as variables so you can get in and play with the effects easier, see below for your ‘built in’ options:

//Declare the limits of where the ball can bounce
private double BounceBoundaryLeft = 10;
private double BounceBoundaryTop = 10;
private double BounceBoundaryRight = 490;
private double BounceBoundaryBottom = 390;
//Set Margin for dragging the Ball around the edge of the screen
private double DragMargin = 30;
//Declare Ball properties
private double BouncyBallWeight = 0.7;
private double BounceLevel = 0.8;
// Declare Shadow properties
private double ShadowOffset = 10;
private double ShadowHeightLimit = 300;

Get the code

Feel free to grab the code here and start playing!

Back Top

Responses to “Collision testing on a free moving object in Silverlight”

  1. not too bad… nicely done on the whole…

    Couple of bugs I noticed, though…

    1. The ball never stops bouncing! ;)
    2. The collision detection only works if the ball is going towards the box, not the other way around. So if the ball is bouncing to the left, and you try to poke it with the box, nothing happens.

    Other than that, a great start… ;)

    Whothehellisthat at March 13, 2009 11:59 am
  2. I agree, the ball must stop! The shrinking shadow under the box is a nice touch though. With a little code development, you could turn it into one of those retro video tennis games!

    TooSexyForSilverlight at March 13, 2009 6:34 pm
  3. Love Perusing your blog… always interesting. Thankyou!!!

    Free Hypnosis Scripts at January 19, 2010 7:36 pm
  4. Hi and many thanks for a intriguing web site. I appreciate what you talked about.

  1. No trackbacks yet.

Leave a Reply

Back Top