public void ApplyAttribtues(PulleyJointDef j) { j.collideConnected = collideConnected; if (maxLength1 != 0.0f) { j.maxLengthA = maxLength1; } if (maxLength2 != 0.0f) { j.maxLengthB = maxLength2; } if (ratio != 1.0f) { j.ratio = ratio; } }
internal PulleyJoint(PulleyJointDef def) : base(def) { _groundAnchor1 = def.groundAnchorA; _groundAnchor2 = def.groundAnchorB; _localAnchor1 = def.localAnchorA; _localAnchor2 = def.localAnchorB; Debug.Assert(def.ratio != 0.0f); _ratio = def.ratio; _ant = def.lengthA + _ratio * def.lengthB; _maxLength1 = Math.Min(def.maxLengthA, _ant - _ratio * PulleyJointDef.b2_minPulleyLength); _maxLength2 = Math.Min(def.maxLengthB, (_ant - PulleyJointDef.b2_minPulleyLength) / _ratio); _impulse = 0.0f; _limitImpulse1 = 0.0f; _limitImpulse2 = 0.0f; }
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; }