/// <summary>
        /// Adds a single XAML element into the Physics simulation.
        /// </summary>
        /// <param name="cnvContainer">The container to add items from.</param>
        public PhysicsSprite AddPhysicsBody(UIElement element, UIElement boundaryElement)
        {
            string thisName = element.GetValue(Canvas.NameProperty).ToString();

            element.SetValue(Canvas.TagProperty, thisName);

            // special case: if we are adding a UserControl _AND_ the UserControl contains one or more
            // Physics Controllers, then we handle that UserControl as a "nested" canvas!
            if (element is UserControl)
            {
                Canvas cnvNestedPhysics = FindPhysicsCanvas(element as UserControl);
                if (cnvNestedPhysics != null)
                {
                    EnsureUniqueNames(cnvNestedPhysics);
                    AddPhysicsBodyForCanvas(cnvNestedPhysics);
                    return(null);
                }
            }

            // look to see if we have processed this object before, and if so,
            // use its point collection instead of recalculating
            List <Point> points = FindMatchingUIElement(element);

            string        nameKey = (string)element.GetValue(Canvas.NameProperty);
            PhysicsSprite polygon = new PhysicsSprite(Simulator, _parentCanvas, element, points, DebugMode, (float)FrictionDefault, boundaryElement);

            polygon.Name = nameKey;
            if (polygon.OutlinePoints != null)
            {
                polygon.Collision += new PhysicsSprite.CollisionHandler(polygon_Collision);
                _parentCanvas.Children.Add(polygon);
                PhysicsObjects.Add(polygon.Name, polygon);
                polygon.Update();
            }

            //if (MousePickEnabled)
            //{
            polygon.MouseLeftButtonDown += new MouseButtonEventHandler(polygon_MouseLeftButtonDown);
            polygon.MouseLeftButtonUp   += new MouseButtonEventHandler(polygon_MouseLeftButtonUp);
            polygon.MouseMove           += new MouseEventHandler(polygon_MouseMove);
            //}

            return(polygon);
        }
        public PhysicsSprite AddPhysicsBody(PhysicsObjectMain physObject)
        {
            FrameworkElement element = physObject.VisualElement;

            string thisName = element.GetValue(Canvas.NameProperty).ToString();

            element.SetValue(Canvas.TagProperty, thisName);

            // special case: if we are adding a UserControl _AND_ the UserControl contains one or more
            // Physics Controllers, then we handle that UserControl as a "nested" canvas!
            if (BoundaryHelper.IsInsideNestedUsercontrol(element))  //element is UserControl)
            {
                UserControl ucParent         = BoundaryHelper.GetParentUserControl(element);
                Canvas      cnvNestedPhysics = FindPhysicsCanvas(ucParent);
                if (cnvNestedPhysics != null)
                {
                    EnsureUniqueNames(cnvNestedPhysics);
                    if (element is UserControl)
                    {
                        AddPhysicsBodyForCanvasWithBehaviors(cnvNestedPhysics);
                        return(null);
                    }
                }
            }

            // look to see if we have processed this object before, and if so,
            // use its point collection instead of recalculating
            List <Point> points = FindMatchingUIElement(element);

            string           nameKey         = (string)element.GetValue(Canvas.NameProperty);
            FrameworkElement boundaryElement = null;

            if (physObject.BoundaryElement != null && physObject.BoundaryElement != string.Empty)
            {
                boundaryElement = element.FindName(physObject.BoundaryElement) as FrameworkElement;
            }

            if (nameKey == string.Empty)
            {
                nameKey      = GetUniqueSpriteName();
                element.Name = nameKey;
            }

            PhysicsSprite sprite = new PhysicsSprite(Simulator, _parentCanvas, element, points, DebugMode, (float)FrictionDefault, boundaryElement);

            sprite.Name = nameKey;
            if (sprite.OutlinePoints != null)
            {
                sprite.Collision += new PhysicsSprite.CollisionHandler(polygon_Collision);
                _parentCanvas.Children.Add(sprite);
                PhysicsObjects.Add(sprite.Name, sprite);
                sprite.Update();
            }

            sprite.MouseLeftButtonDown += new MouseButtonEventHandler(polygon_MouseLeftButtonDown);
            sprite.MouseLeftButtonUp   += new MouseButtonEventHandler(polygon_MouseLeftButtonUp);
            sprite.MouseMove           += new MouseEventHandler(polygon_MouseMove);

            sprite.GeometryObject.CollisionGroup = physObject.CollisionGroup;
            sprite.BodyObject.IsStatic           = physObject.IsStatic;

            if (physObject.RestitutionCoefficient != 0)
            {
                sprite.GeometryObject.RestitutionCoefficient = (float)physObject.RestitutionCoefficient;
            }
            if (physObject.FrictionCoefficient != 0)
            {
                sprite.GeometryObject.FrictionCoefficient = (float)physObject.FrictionCoefficient;
            }
            if (physObject.MomentOfIntertia != 0)
            {
                sprite.BodyObject.MomentOfInertia = (float)physObject.MomentOfIntertia;
            }
            if (physObject.Mass != 0)
            {
                sprite.BodyObject.Mass = (float)physObject.Mass;
            }

            return(sprite);
        }
        /// <summary>
        /// Adds a single Physics Sprite into the Physics simulation.
        /// </summary>
        /// <param name="cnvContainer">The container to add items from.</param>
        public PhysicsSprite AddPhysicsSprite(PhysicsSprite element, bool enablePhysics)
        {
            if (enablePhysics)
            {
                string thisName = element.GetValue(Canvas.NameProperty).ToString();
                element.SetValue(Canvas.TagProperty, thisName); // save original name in tag

                if ((element.Parent as Panel).Children.IndexOf(element) < 0)
                    throw new Exception("Could not find element " + thisName + " in the parent");
                (element.Parent as Panel).Children.Remove(element);

                thisName = PhysicsUtilities.EnsureUniqueName(this, element, thisName);  // get a unique name in the game canvas
                element.Name = thisName;

                //PhysicsUtilities.DebugVisualTree(this, 1);

                this.Children.Add(element);

                PhysicsObjects.Add(thisName, element);
                element.Update();

                element.Collision += new PhysicsSprite.CollisionHandler(polygon_Collision);
            }

            if (MousePickEnabled)
            {
                element.MousePickEnabled = true;
            }

            return element;

        }