/// <summary> /// Returns a guaranteed unique name for an element in the canvas /// </summary> public static void EnsureUniqueNames(Canvas cnv, bool setTag) { foreach (UIElement element in cnv.Children) { string thisName = element.GetValue(Canvas.NameProperty).ToString(); string newName = thisName; if (thisName == string.Empty) { continue; } newName = EnsureUniqueNameSingle(_parentCanvas, newName); element.SetValue(Canvas.NameProperty, newName); if (setTag) { element.SetValue(Canvas.TagProperty, thisName); } // we also need to update contoller body names for joins, static bodies foreach (UIElement elementSub in cnv.Children) { if (elementSub is PhysicsJoint) { PhysicsJoint thisJoint = elementSub as PhysicsJoint; if (thisJoint.BodyOne == thisName) { thisJoint.BodyOne = newName; } if (thisJoint.BodyTwo == thisName) { thisJoint.BodyTwo = newName; } } if (elementSub is PhysicsStaticHolder) { PhysicsStaticHolder thisHolder = elementSub as PhysicsStaticHolder; if (thisHolder.Body == thisName) { thisHolder.Body = newName; } } PhysicsJointMain physJoint = elementSub.GetValue(PhysicsJointMain.PhysicsJointProperty) as PhysicsJointMain; if (physJoint != null) { if (physJoint.BodyOne == thisName) { physJoint.BodyOne = newName; } if (physJoint.BodyTwo == thisName) { physJoint.BodyTwo = newName; } } } } }
public void AddJoint(PhysicsJointMain joint) { string bodyOne = joint.BodyOne; string bodyTwo = joint.BodyTwo; int collisionGroup = joint.CollisionGroup; bool isAngleSpringEnabled = joint.AngleSpringEnabled; float springConstant = joint.AngleSpringConstant; float dampeningConstant = joint.AngleSpringDampningConstant; float angleLowerLimit = (float)joint.AngleLowerLimit; float angleUpperLimit = (float)joint.AngleUpperLimit; Point center = joint.GetCenter(); Vector2 ptCollisionCenter = new Vector2((float)center.X, (float)center.Y); if (!PhysicsObjects.ContainsKey(bodyOne)) { throw new Exception("Cannot add joint for an invalid BodyOne value of '" + bodyOne + "'. If using Behaviors, did you forgot to add a PhysicsObjectBehavior?"); } if (!PhysicsObjects.ContainsKey(bodyTwo)) { throw new Exception("Cannot add joint for an invalid BodyTwo value of '" + bodyTwo + "'. If using Behaviors, did you forgot to add a PhysicsObjectBehavior?"); } Body body1 = PhysicsObjects[bodyOne].BodyObject; Body body2 = PhysicsObjects[bodyTwo].BodyObject; Geom geom1 = PhysicsObjects[bodyOne].GeometryObject; Geom geom2 = PhysicsObjects[bodyTwo].GeometryObject; RevoluteJoint revoluteJoint = JointFactory.Instance.CreateRevoluteJoint(Simulator, body1, body2, ptCollisionCenter); if (isAngleSpringEnabled) { AngleSpring angleSpring = new AngleSpring(body1, body2, springConstant, dampeningConstant); Simulator.Add(angleSpring); } if (angleUpperLimit != -1 && angleLowerLimit != -1) { float upperAngle = (float)DegreesToRadians(angleUpperLimit); float lowerAngle = (float)DegreesToRadians(angleLowerLimit); AngleLimitJoint angleLimitJoint = new AngleLimitJoint(body1, body2, lowerAngle, upperAngle); Simulator.Add(angleLimitJoint); } if (collisionGroup > 0) { geom1.CollisionGroup = collisionGroup; geom2.CollisionGroup = collisionGroup; } // get rid of the UI representation of the joint joint.VisualElement.Visibility = Visibility.Collapsed; }
/// <summary> /// Adds ALL named XAML elements into the Physics simulation. /// </summary> /// <param name="cnvContainer">The container to add items from.</param> public void AddPhysicsBodyForCanvasWithBehaviors(Canvas cnvContainer) { #if SILVERLIGHT for (int i = 0; i < cnvContainer.Children.Count(); i++) #else for (int i = 0; i < cnvContainer.Children.Count; i++) #endif { FrameworkElement element = cnvContainer.Children[i] as FrameworkElement; string elemName = element.GetValue(Canvas.NameProperty).ToString(); element.SetValue(Canvas.TagProperty, elemName); } PhysicsControllerMain.EnsureUniqueNames(cnvContainer, false); List <PhysicsObjectMain> objectsToAdd = new List <PhysicsObjectMain>(); #if SILVERLIGHT for (int i = 0; i < cnvContainer.Children.Count() - 1; i++) #else for (int i = 0; i < cnvContainer.Children.Count - 1; i++) #endif { FrameworkElement element = cnvContainer.Children[i] as FrameworkElement; PhysicsObjectMain physObject = element.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain; if (physObject != null) { objectsToAdd.Add(physObject); } } foreach (PhysicsObjectMain physObject in objectsToAdd) { AddPhysicsBody(physObject); } #if SILVERLIGHT for (int i = cnvContainer.Children.Count() - 1; i >= 0; i--) #else for (int i = cnvContainer.Children.Count - 1; i >= 0; i--) #endif { FrameworkElement element = cnvContainer.Children[i] as FrameworkElement; PhysicsJointMain joint = element.GetValue(PhysicsJointMain.PhysicsJointProperty) as PhysicsJointMain; if (joint != null) { AddJoint(joint); } } }
public static void SetPhysicsJoint(DependencyObject target, PhysicsJointMain value) { target.SetValue(PhysicsJointMain.PhysicsJointProperty, value); }