Quadra

Quadra Offshore

 
  • Increase font size
  • Default font size
  • Decrease font size
Home Silverlight Tutorial 2

Silverlight Tutorial: Checking if objects are overlapping

E-mail Print PDF

Silverlight Tutorial: Check if two Objects are overlapping, and by how much.

This tutorial has been updated to Silverlight 2.0! 

There are lots of applications in checking if two objects are overlapping. In games, for example. In drawing applications. For this tutorial, we will use it to implement a basic type of Recycle Bin, so we can throw away objects on the canvas.

Take a look at the demo running here. You can download the source here

To do this, we can write a class; Overlap.

Our overlap constructor will take two objects, and cast them to UIElement:

<Overlap.cs>

This will allow us to pass different kinds of objects to the class; Canvas’, Rectangles, etc., and get their coordinates with the GetValue Method:

Now we can do a simple check to compare if we have any overlapping:

Finally, we can get the percentage overlapping:


Finally, we just need to implement this check, so we wire up a MouseMove event to the main (and only) Canvas of the app in Xaml, and fill in some C#:

<Page.xaml.cs>

Here, we are to changing the Skew, and Scale of the object (a camera) moving over the delete bin according to the amount of overlap between the two. This leads to the effect that the camera is going into the bin. We also want to make the delete bin “pop up” a little bit if the camera is hovering over it.