/// <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;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Adds a single joint objectt from a Canvas into the simulation.
        /// </summary>
        /// <param name="cnvContainer">The Canvas to add joints from</param>
        public void AddJoint(PhysicsJoint joint)
        {
            if (!PhysicsObjects.ContainsKey(joint.BodyOne))
            {
                throw new Exception("A PhysicsJoint exists with an invalid BodyOne value of '" + joint.BodyOne + "'.");
            }
            if (!PhysicsObjects.ContainsKey(joint.BodyTwo))
            {
                throw new Exception("A PhysicsJoint exists with an invalid BodyTwo value of '" + joint.BodyTwo + "'.");
            }


            Body body1 = PhysicsObjects[joint.BodyOne].BodyObject;
            Body body2 = PhysicsObjects[joint.BodyTwo].BodyObject;

            Geom geom1 = PhysicsObjects[joint.BodyOne].GeometryObject;
            Geom geom2 = PhysicsObjects[joint.BodyTwo].GeometryObject;


            Vector2 ptCollisionCenter = new Vector2((float)(joint.GetCenter().X), (float)(joint.GetCenter().Y));

            RevoluteJoint revoluteJoint = JointFactory.Instance.CreateRevoluteJoint(Simulator, body1, body2, ptCollisionCenter);

            joint.RevoluteJointObject = revoluteJoint;

            if (joint.AngleSpringEnabled)
            {
                float       springConstant    = joint.AngleSpringConstant;
                float       dampeningConstant = joint.AngleSpringDampningConstant;
                AngleSpring angleSpring       = new AngleSpring(body1, body2, springConstant, dampeningConstant);
                Simulator.Add(angleSpring);
            }

            if (joint.CollisionGroup > 0)
            {
                geom1.CollisionGroup = joint.CollisionGroup;
                geom2.CollisionGroup = joint.CollisionGroup;
            }

            joint.Visibility = Visibility.Collapsed;
        }
 /// <summary>
 /// Adds a single joint objectt from a Canvas into the simulation.
 /// </summary>
 /// <param name="cnvContainer">The Canvas to add joints from</param>
 public void AddJoint(PhysicsJoint joint)
 {
     _physicsControllerMain.AddJoint(joint);
 }
        /// <summary>
        /// Adds a Physics Joint to the simulation
        /// </summary>
        /// <param name="joint"></param>
        public void AddPhysicsJoint(PhysicsJoint joint)
        {
            string bodyOne = joint.BodyOne;
            string bodyTwo = joint.BodyTwo;
            short collisionGroup = Convert.ToInt16(joint.CollisionGroup);
            bool isAngleSpringEnabled = joint.AngleSpringEnabled;
            float springConstant = (float)joint.AngleSpringConstant;
            float angleLowerLimit = (float)joint.AngleLowerLimit;
            float angleUpperLimit = (float)joint.AngleUpperLimit;


            Point center = joint.GetCenter();
            xna.Vector2 ptCollisionCenter = new xna.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;

            xna.Vector2 ptCollisionCenterA = _boundaryHelper.ScreenToWorld(ptCollisionCenter);
            xna.Vector2 ptCollisionCenterB = _boundaryHelper.ScreenToWorld(ptCollisionCenter);

            // account for offset within body
            ptCollisionCenterA -= body1.Position;
            ptCollisionCenterB -= body2.Position;

            // DEMO: (5) Create Joints
            if (joint.IsWeldJoint)
            {
                WeldJoint weldJoint = new WeldJoint(body1, body2, ptCollisionCenterA, ptCollisionCenterB);
                Simulator.AddJoint(weldJoint);
            }
            else
                if (joint.IsDistanceJoint)
                {
                    DistanceJoint distanceJoint = new DistanceJoint(body1, body2, ptCollisionCenterA, ptCollisionCenterB);

                    Simulator.AddJoint(distanceJoint);

                }
                else
                {
                    RevoluteJoint revoluteJoint = new RevoluteJoint(body1, body2, ptCollisionCenterA, ptCollisionCenterB);

                    Simulator.AddJoint(revoluteJoint);

                    if (isAngleSpringEnabled)
                    {
                        AngleJoint aj = new AngleJoint(body1, body2);
                        aj.TargetAngle = 0;
                        aj.Softness = springConstant;


                        Simulator.AddJoint(aj);

                    }

                    if (angleUpperLimit != -1 && angleLowerLimit != -1)
                    {
                        float upperAngle = (float)PhysicsUtilities.DegreesToRadians(angleUpperLimit);
                        float lowerAngle = (float)PhysicsUtilities.DegreesToRadians(angleLowerLimit);

                        revoluteJoint.LimitEnabled = true;
                        revoluteJoint.LowerLimit = lowerAngle;
                        revoluteJoint.UpperLimit = upperAngle;
                    }
                }

            if (collisionGroup > 0)
            {

                foreach (Fixture f in PhysicsObjects[bodyOne].BodyObject.FixtureList)
                {
                    f.CollisionGroup = collisionGroup;
                }
                foreach (Fixture f in PhysicsObjects[bodyTwo].BodyObject.FixtureList)
                {
                    f.CollisionGroup = collisionGroup;
                }
            }

            // get rid of the UI representation of the joint
            joint.Visibility = Visibility.Collapsed;

        }
Example #5
0
        private static void BodyTwoChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            PhysicsJoint joint = obj as PhysicsJoint;

            joint.JointMain.BodyTwo = Convert.ToString(args.NewValue);
        }
Example #6
0
        private static void CollisionGroupChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            PhysicsJoint joint = obj as PhysicsJoint;

            joint.JointMain.CollisionGroup = Convert.ToInt32(args.NewValue);
        }
Example #7
0
        private static void AngleSpringDampningConstantChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            PhysicsJoint joint = obj as PhysicsJoint;

            joint.JointMain.AngleSpringDampningConstant = Convert.ToInt32(args.NewValue);
        }
Example #8
0
        private static void AngleSpringEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            PhysicsJoint joint = obj as PhysicsJoint;

            joint.JointMain.AngleSpringEnabled = Convert.ToBoolean(args.NewValue);
        }