Пример #1
0
        private void ParseJoint(V2DJointKind jointKind, Instance inst)
        {
            List<V2DJoint> joints = curDef.Joints; //List<V2DJoint>
            string nm = inst.Name;
            int firstUs = nm.IndexOf('_');
            string jName = (firstUs > -1) ? nm.Substring(0, firstUs) : nm;
            string targName = (firstUs > -1) ? nm.Substring(firstUs + 1) : "";

            Point p = new Point(
                inst.Transformations[0].Matrix.TranslateX,
                inst.Transformations[0].Matrix.TranslateY);

            V2DJoint joint = joints.Find(jt => jt.Name == jName);
            if (joint == null)
            {
                joint = new V2DJoint();
                joint.Name = jName;
                joint.Type = jointKind;
                joint.X2 = p.X;
                joint.Y2 = p.Y;
                if (inst.Transformations.Count > 1) // pulley defined by joints on frame 2
                {
                    joint.GroundAnchor1X = inst.Transformations[1].Matrix.TranslateX;
                    joint.GroundAnchor1Y = inst.Transformations[1].Matrix.TranslateY;
                }
            }
            else
            {
                joint.X2 = p.X;
                joint.Y2 = p.Y;
                if (inst.Transformations.Count > 1) // pulley defined by joints on frame 2
                {
                    joint.GroundAnchor2X = inst.Transformations[1].Matrix.TranslateX;
                    joint.GroundAnchor2Y = inst.Transformations[1].Matrix.TranslateY;
                }
            }
        }
Пример #2
0
 private void GenerateJointActionData(V2DJoint jnt)
 {
     foreach (string k in codeData.Keys)
     {
         if (jnt.Name == k)
         {
             Dictionary<string, string> d = codeData[k];
             foreach (string s in d.Keys)
             {
                 if (jnt.data.ContainsKey(s))
                 {
                     jnt.data[s] = d[s];
                 }
                 else
                 {
                     jnt.data.Add(s, d[s]);
                 }
             }
         }
     }
 }
Пример #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;
        }
Пример #4
0
        private void EnsureBodyTags(V2DJoint j)
        {
            bool hasB1 = j.data.ContainsKey("body1");
            bool hasB2 = j.data.ContainsKey("body2");
            bool b1FromCode = hasB1;

            if (!hasB1 || !hasB2)
            {
                List<Body2D> hits = FindTargetUnderPoint(j.Locations[0]);
                if (!hasB1 && hits.Count > 0)
                {
                    j.data.Add("body1", hits[0].InstanceName);
                    hasB1 = true;
                }

                if (!hasB2)
                {
                    Body2D b2 = null;
                    if (j.Locations.Count > 1)// && j.JointKind == JointKind.Distance)
                    {
                        List<Body2D> b2L = FindTargetUnderPoint(j.Locations[1]);

                        if (b2L.Count > 0)
                        {
                            b2 = b2L[0];
                        }
                        else
                        {
                            // ground
                        }
                    }
                    else if (hits.Count > 1)
                    {
                        b2 = hits[1];
                    }
                    else if (b1FromCode && hits.Count > 0)
                    {
                        b2 = hits[0];
                    }

                    if (!hasB1)
                    {
                        j.data.Add("body1", V2D.ROOT_NAME);
                    }

                    if (b2 != null)
                    {
                        j.data.Add("body2", b2.InstanceName);
                        hasB2 = true;
                    }
                    else
                    {
                        j.data.Add("body2", V2D.ROOT_NAME);
                    }
                }
            }
            GenerateJointActionData(j, curDef);

            // flip if a ground ref (I think all joints to ground use ground on body 1..?)
            if (j.data["body2"] == V2D.ROOT_NAME)
            {
                string temp = j.data["body2"];
                j.data["body2"] = j.data["body1"];
                j.data["body1"] = temp;
                if (j.Locations.Count > 1)
                {
                    Point temp2 = j.Locations[0];
                    j.Locations[0] = j.Locations[1];
                    j.Locations[1] = temp2;
                }
            }
        }
Пример #5
0
        public virtual V2DJoint GetV2DJoint()
        {
            V2DJoint result = new V2DJoint();
            result.Type = this.JointKind;
            result.Name = this.Name;

            foreach (string k in data.Keys)
            {
                Type t = result.GetType();
                FieldInfo fi = t.GetField(k, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                if (fi != null)
                {
                    Type ft = fi.FieldType;
                    string val = data[k];
                    Object o = Convert.ChangeType(val, fi.FieldType);
                    fi.SetValue(result, o);
                }
            }
            return result;
        }