Ejemplo n.º 1
0
        internal GearJoint(GearJointDef def)
            : base(def)
        {
            JointType type1 = def.joint1.JointType;
            JointType type2 = def.joint2.JointType;

            Debug.Assert(type1 == JointType.Revolute || type1 == JointType.Prismatic);
            Debug.Assert(type2 == JointType.Revolute || type2 == JointType.Prismatic);
            Debug.Assert(def.joint1.GetBodyA().GetType() == BodyType.Static);
            Debug.Assert(def.joint2.GetBodyA().GetType() == BodyType.Static);

            _revolute1  = null;
            _prismatic1 = null;
            _revolute2  = null;
            _prismatic2 = null;

            float coordinate1, coordinate2;

            _ground1 = def.joint1.GetBodyA();
            _bodyA   = def.joint1.GetBodyB();
            if (type1 == JointType.Revolute)
            {
                _revolute1     = (RevoluteJoint)def.joint1;
                _groundAnchor1 = _revolute1._localAnchor1;
                _localAnchor1  = _revolute1._localAnchor2;
                coordinate1    = _revolute1.GetJointAngle();
            }
            else
            {
                _prismatic1    = (PrismaticJoint)def.joint1;
                _groundAnchor1 = _prismatic1._localAnchor1;
                _localAnchor1  = _prismatic1._localAnchor2;
                coordinate1    = _prismatic1.GetJointTranslation();
            }

            _ground2 = def.joint2.GetBodyA();
            _bodyB   = def.joint2.GetBodyB();
            if (type2 == JointType.Revolute)
            {
                _revolute2     = (RevoluteJoint)def.joint2;
                _groundAnchor2 = _revolute2._localAnchor1;
                _localAnchor2  = _revolute2._localAnchor2;
                coordinate2    = _revolute2.GetJointAngle();
            }
            else
            {
                _prismatic2    = (PrismaticJoint)def.joint2;
                _groundAnchor2 = _prismatic2._localAnchor1;
                _localAnchor2  = _prismatic2._localAnchor2;
                coordinate2    = _prismatic2.GetJointTranslation();
            }

            _ratio = def.ratio;

            _ant = coordinate1 + _ratio * coordinate2;

            _impulse = 0.0f;
        }
        internal GearJoint(GearJointDef def)
            : base(def)
        {
            JointType type1 = def.joint1.JointType;
            JointType type2 = def.joint2.JointType;

            Debug.Assert(type1 == JointType.Revolute || type1 == JointType.Prismatic);
            Debug.Assert(type2 == JointType.Revolute || type2 == JointType.Prismatic);
            Debug.Assert(def.joint1.GetBodyA().GetType() == BodyType.Static);
            Debug.Assert(def.joint2.GetBodyA().GetType() == BodyType.Static);

            _revolute1 = null;
            _prismatic1 = null;
            _revolute2 = null;
            _prismatic2 = null;

            float coordinate1, coordinate2;

            _ground1 = def.joint1.GetBodyA();
            _bodyA = def.joint1.GetBodyB();
            if (type1 == JointType.Revolute)
            {
                _revolute1 = (RevoluteJoint)def.joint1;
                _groundAnchor1 = _revolute1._localAnchor1;
                _localAnchor1 = _revolute1._localAnchor2;
                coordinate1 = _revolute1.GetJointAngle();
            }
            else
            {
                _prismatic1 = (PrismaticJoint)def.joint1;
                _groundAnchor1 = _prismatic1._localAnchor1;
                _localAnchor1 = _prismatic1._localAnchor2;
                coordinate1 = _prismatic1.GetJointTranslation();
            }

            _ground2 = def.joint2.GetBodyA();
            _bodyB = def.joint2.GetBodyB();
            if (type2 == JointType.Revolute)
            {
                _revolute2 = (RevoluteJoint)def.joint2;
                _groundAnchor2 = _revolute2._localAnchor1;
                _localAnchor2 = _revolute2._localAnchor2;
                coordinate2 = _revolute2.GetJointAngle();
            }
            else
            {
                _prismatic2 = (PrismaticJoint)def.joint2;
                _groundAnchor2 = _prismatic2._localAnchor1;
                _localAnchor2 = _prismatic2._localAnchor2;
                coordinate2 = _prismatic2.GetJointTranslation();
            }

            _ratio = def.ratio;

            _ant = coordinate1 + _ratio * coordinate2;

            _impulse = 0.0f;
        }
Ejemplo n.º 3
0
        public static Joint AddJoint(this IJointable ithis, V2DJoint joint, float offsetX, float offsetY)
        {
            Joint result = null;
            JointDef jointDef = null;
            //Body targ0 = ithis.VScreen.bodyMap[joint.Body1];
            //Body targ1 = ithis.VScreen.bodyMap[joint.Body2];
            Body targ0 = GetBody(ithis, joint.Body1);
            Body targ1 = GetBody(ithis, joint.Body2);

            // gears need the first body static
            if (targ0 != null && targ1 != null && targ1.GetType() == BodyType.Static && targ0.GetType() != BodyType.Static)
            {
                Body temp = targ0;
                targ0 = targ1;
                targ1 = temp;
            }

            Vector2 pt0 = new Vector2(joint.X + offsetX, joint.Y + offsetY);

            string name = joint.Name;

            Vector2 anchor0 = new Vector2(pt0.X / V2DScreen.WorldScale, pt0.Y / V2DScreen.WorldScale);
            Vector2 anchor1 = new Vector2();

            switch (joint.Type)
            {
                case V2DJointKind.Distance:
                    Vector2 pt1 = new Vector2(joint.X2 + offsetX, joint.Y2 + offsetY);
                    anchor1 = new Vector2(pt1.X / V2DScreen.WorldScale, pt1.Y / V2DScreen.WorldScale);

                    DistanceJointDef dj = new DistanceJointDef();
                    dj.Initialize(targ0, targ1, anchor0, anchor1);
                    dj.collideConnected = joint.CollideConnected;
                    dj.dampingRatio = joint.DampingRatio;
                    dj.frequencyHz = joint.FrequencyHz;
                    if (joint.Length != -1)
                    {
                        dj.length = joint.Length / V2DScreen.WorldScale;
                    }

                    jointDef = dj;
                    break;

                case V2DJointKind.Revolute:
                    float rot0 = joint.Min; //(typeof(joint["min"]) == "string") ? parseFloat(joint["min"]) / 180 * Math.PI : joint["min"];
                    float rot1 = joint.Max; //(typeof(joint["max"]) == "string") ? parseFloat(joint["max"]) / 180 * Math.PI : joint["max"];

                    RevoluteJointDef rj = new RevoluteJointDef();
                    rj.Initialize(targ0, targ1, anchor0);
                    rj.lowerAngle = rot0;
                    rj.upperAngle = rot1;

                    rj.enableLimit = rot0 != 0 && rot1 != 0;
                    rj.maxMotorTorque = joint.MaxMotorTorque;
                    rj.motorSpeed = joint.MotorSpeed;
                    rj.enableMotor = joint.EnableMotor;

                    jointDef = rj;
                    break;

                case V2DJointKind.Prismatic:
                    float axisX = joint.AxisX;
                    float axisY = joint.AxisY;
                    float min = joint.Min;
                    float max = joint.Max;

                    PrismaticJointDef pj = new PrismaticJointDef();
                    Vector2 worldAxis = new Vector2(axisX, axisY);
                    pj.Initialize(targ0, targ1, anchor0, worldAxis);
                    pj.lowerTranslation = min / V2DScreen.WorldScale;
                    pj.upperTranslation = max / V2DScreen.WorldScale;

                    pj.enableLimit = joint.EnableLimit;
                    pj.maxMotorForce = joint.MaxMotorTorque;
                    pj.motorSpeed = joint.MotorSpeed;
                    pj.enableMotor = joint.EnableMotor;

                    jointDef = pj;
                    break;

                case V2DJointKind.Pully:
                    Vector2 pt2 = new Vector2(joint.X2 + offsetX, joint.Y2 + offsetY);
                    anchor1 = new Vector2(pt2.X / V2DScreen.WorldScale, pt2.Y / V2DScreen.WorldScale);

                    Vector2 groundAnchor0 = new Vector2(joint.GroundAnchor1X / V2DScreen.WorldScale, joint.GroundAnchor1Y / V2DScreen.WorldScale);

                    Vector2 groundAnchor1 = new Vector2(joint.GroundAnchor2X / V2DScreen.WorldScale, joint.GroundAnchor2Y / V2DScreen.WorldScale);

                    float max0 = joint.MaxLength1;
                    float max1 = joint.MaxLength2;

                    float rat = joint.Ratio;

                    PulleyJointDef puj = new PulleyJointDef();
                    puj.Initialize(targ0, targ1, groundAnchor0, groundAnchor1, anchor0, anchor1, rat);
                    puj.maxLengthA = (max0 + max1) / V2DScreen.WorldScale;
                    puj.maxLengthB = (max0 + max1) / V2DScreen.WorldScale;

                    puj.collideConnected = joint.CollideConnected;

                    jointDef = puj;
                    break;

                case V2DJointKind.Gear:
                    GearJointDef gj = new GearJointDef();
                    gj.bodyA = targ0;
                    gj.bodyB = targ1;
                    gj.joint1 = GetFirstGearableJoint(targ0.GetJointList());
                    gj.joint2 = GetFirstGearableJoint(targ1.GetJointList());
                    gj.ratio = joint.Ratio;
                    jointDef = gj;
                    break;
            }

            if (jointDef != null)
            {
                result = SetJointWithReflection(ithis, name, jointDef);

                if (result != null)
                {
                    Dictionary<string, string> dict = new Dictionary<string, string>();
                    dict["name"] = name;
                    result.SetUserData(dict);
                }
            }

            return result;
        }