|
Canvas UserControl Collision Checking in Silverlight VB.Net (Silverlight) |
|
|
Written by: Justin Rich
Canvas UserControl Collision Checking in Silverlight VB.NetCollision checking in programming is important especially with a Framework for a system like Silverlight where you can dynamically create, drag, & drop canvases or usercontrols randomly. You will want to be able to check to see if the canvas you dropped is overlapping the other canvas.
Here's a quick and dirty function to determine if they're overlapping in VB.Net:
Private Function Collision(ByVal oCtrl1 As UserControl, ByVal oCtrl2 As UserControl) As Boolean Dim left1 As Double = CType(oCtrl1.GetValue(Canvas.LeftProperty), Double) Dim left2 As Double = CType(oCtrl2.GetValue(Canvas.LeftProperty), Double) Dim top1 As Double = CType(oCtrl1.GetValue(Canvas.TopProperty), Double) Dim top2 As Double = CType(oCtrl2.GetValue(Canvas.TopProperty), Double) Dim collided As Boolean = Not (left1 > left2 + oCtrl2.ActualWidth Or left1 + oCtrl1.ActualWidth < left2 Or top1 > top2 + oCtrl2.ActualHeight Or top1 + oCtrl1.ActualHeight < top2) Return collided End Function
This function will return True if they overlap and False if they don't. A good practice to avoid checking all objects completely is to check to see if they're even in the same area of the canvas before pushing every sprite or object through this comparison. You don't need to compare them if they're not closer than their widths.
A good way to handle your usercontrols is with a List object which will be covered in a later article. |
|
|
|
|
|