Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
 public Point GetCenter()
 {
     return(_physicsJointMain.GetCenter());
 }