/// <inheritdoc /> protected override void PostStep() { if (Input.GetKeyDown(KeyCode.J)) { if (_rope != null) { World.DestroyJoint(_rope); _rope = null; } else { _rope = World.CreateJoint(_ropeDef); } } DrawString("Press (j) to toggle the rope joint."); if (_rope != null) { DrawString("Rope ON"); } else { DrawString("Rope OFF"); } }
/// <inheritdoc /> public override void JointDestroyed(Joint joint) { for (var i = 0; i < 8; ++i) { if (_joints[i] == joint) { _joints[i] = null; break; } } }
/// <inheritdoc /> /// <inheritdoc /> public override void OnKeyDown(KeyboardKeyEventArgs key) { if (key.Key == Key.J) { if (_rope != null) { World.DestroyJoint(_rope); _rope = null; } else { _rope = World.CreateJoint(_ropeDef); } } }
/// <inheritdoc /> /// <inheritdoc /> public override void OnKeyDown(KeyInputEventArgs keyInput) { if (keyInput.Key == KeyCodes.J) { if (_rope != null) { World.DestroyJoint(_rope); _rope = null; } else { _rope = World.CreateJoint(_ropeDef); } } }
/// <inheritdoc /> protected override void OnStep() { if (Input.GetKeyDown(KeyCode.J)) { if (_rope != null) { World.DestroyJoint(_rope); _rope = null; } else { _rope = World.CreateJoint(_ropeDef); } } }
public RopeJoint() { Body ground; { var bd = new BodyDef(); ground = World.CreateBody(bd); var shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.125f); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 20.0f; fd.Friction = 0.2f; var filter = fd.Filter; filter.CategoryBits = 0x0001; filter.MaskBits = 0xFFFF & ~0x0002; fd.Filter = filter; var jd = new RevoluteJointDef(); jd.CollideConnected = false; const int N = 10; const float y = 15.0f; _ropeDef.LocalAnchorA.Set(0.0f, y); var prevBody = ground; for (var i = 0; i < N; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(0.5f + 1.0f * i, y); if (i == N - 1) { shape.SetAsBox(1.5f, 1.5f); fd.Density = 100.0f; filter = fd.Filter; filter.CategoryBits = 0x0002; fd.Filter = filter; bd.Position.Set(1.0f * i, y); bd.AngularDamping = 0.4f; } var body = World.CreateBody(bd); body.CreateFixture(fd); var anchor = new Vector2(i, y); jd.Initialize(prevBody, body, anchor); World.CreateJoint(jd); prevBody = body; } _ropeDef.LocalAnchorB.SetZero(); var extraLength = 0.01f; _ropeDef.MaxLength = N - 1.0f + extraLength; _ropeDef.BodyB = prevBody; } { _ropeDef.BodyA = ground; _rope = World.CreateJoint(_ropeDef); } }
/// <inheritdoc /> public void Dispose() { Other = null; Joint = null; Node = null; }
public GearJoint(GearJointDef def) : base(def) { _joint1 = def.Joint1; _joint2 = def.Joint2; _typeA = _joint1.JointType; _typeB = _joint2.JointType; Debug.Assert(_typeA == JointType.RevoluteJoint || _typeA == JointType.PrismaticJoint); Debug.Assert(_typeB == JointType.RevoluteJoint || _typeB == JointType.PrismaticJoint); Single coordinateA, coordinateB; // TODO_ERIN there might be some problem with the joint edges in b2Joint. _bodyC = _joint1.BodyA; BodyA = _joint1.BodyB; // Get geometry of joint1 var xfA = BodyA.Transform; var aA = BodyA.Sweep.A; var xfC = _bodyC.Transform; var aC = _bodyC.Sweep.A; if (_typeA == JointType.RevoluteJoint) { var revolute = (RevoluteJoint)def.Joint1; _localAnchorC = revolute.LocalAnchorA; _localAnchorA = revolute.LocalAnchorB; _referenceAngleA = revolute.ReferenceAngle; _localAxisC.SetZero(); coordinateA = aA - aC - _referenceAngleA; } else { var prismatic = (PrismaticJoint)def.Joint1; _localAnchorC = prismatic.LocalAnchorA; _localAnchorA = prismatic.LocalAnchorB; _referenceAngleA = prismatic.ReferenceAngle; _localAxisC = prismatic.LocalXAxisA; var pC = _localAnchorC; var pA = MathUtils.MulT( xfC.Rotation, MathUtils.Mul(xfA.Rotation, _localAnchorA) + (xfA.Position - xfC.Position)); coordinateA = MathUtils.Dot(pA - pC, _localAxisC); } _bodyD = _joint2.BodyA; BodyB = _joint2.BodyB; // Get geometry of joint2 var xfB = BodyB.Transform; var aB = BodyB.Sweep.A; var xfD = _bodyD.Transform; var aD = _bodyD.Sweep.A; if (_typeB == JointType.RevoluteJoint) { var revolute = (RevoluteJoint)def.Joint2; _localAnchorD = revolute.LocalAnchorA; _localAnchorB = revolute.LocalAnchorB; _referenceAngleB = revolute.ReferenceAngle; _localAxisD.SetZero(); coordinateB = aB - aD - _referenceAngleB; } else { var prismatic = (PrismaticJoint)def.Joint2; _localAnchorD = prismatic.LocalAnchorA; _localAnchorB = prismatic.LocalAnchorB; _referenceAngleB = prismatic.ReferenceAngle; _localAxisD = prismatic.LocalXAxisA; var pD = _localAnchorD; var pB = MathUtils.MulT( xfD.Rotation, MathUtils.Mul(xfB.Rotation, _localAnchorB) + (xfB.Position - xfD.Position)); coordinateB = MathUtils.Dot(pB - pD, _localAxisD); } _ratio = def.Ratio; _constant = coordinateA + _ratio * coordinateB; _impulse = 0.0f; }