private void SetJointTorque(RevoluteJoint joint, float torque) { float atorque; // abs(torque) float storque; // sign(torque) if (torque > 0f) { atorque = torque; storque = 1f; } else if (torque < 0f) { atorque = -torque; storque = -1f; } else { atorque = 0f; storque = 0f; } //# //# We have to enumerate various cases because we are emulating friction //# with active torque and we have to take care not to add energy to //# the system when a reversal happens inside the timestep. //# //# Also, the underlying sim does not let us apply torque directly, but //# rather we have to factor it into maxMotorTorque and motorSpeed, the //# latter of which we set to an extreme we don't normally expect to reach. //# float speed = joint.JointSpeed; float fric = __jointFriction; if (speed * storque >= 0f) //# Pushing in same direction as current motion (and against friction) { if (atorque > fric) //# With enough force to overcome friction: { joint.SetMaxMotorTorque(atorque - fric); //# Remaining torque (after friction)... joint.MotorSpeed = storque * 100f; //# serves to accelerate current motion } else //# Without enough force to overcome friction: { joint.SetMaxMotorTorque(fric - atorque); //# Remaining friction... joint.MotorSpeed = 0f; //# ...serves to stop the motion (but no more) } } else //# Pushing against current motion (and with friction) { joint.SetMaxMotorTorque(fric + atorque); //# Forces sum... joint.MotorSpeed = storque * 100f; //# ...in direction of torque //# BUG: This case unfortunately adds energy during reversals because friction //# continues to contribute energy in the same direction after the reversal. //# Is there a fix for this? } }
public override void Keyboard(System.Windows.Forms.Keys key) { switch (key) { case System.Windows.Forms.Keys.A: _leftJoint.SetMaxMotorTorque(800.0f); _leftJoint.MotorSpeed = 12.0f; break; case System.Windows.Forms.Keys.S: _leftJoint.SetMaxMotorTorque(100.0f); _leftJoint.MotorSpeed = 0.0f; break; case System.Windows.Forms.Keys.D: _leftJoint.SetMaxMotorTorque(1200.0f); _leftJoint.MotorSpeed = -36.0f; break; } }
public override void Load() { // Ground body Body ground; { ground = Physics.NewBody(m_world); // outline Physics.NewFixture(ground, Physics.NewChainShape(true, new Vector2[] { new Vector2(0.0f, -2.0f), new Vector2(8.0f, 6.0f), new Vector2(8.0f, 20.0f), new Vector2(-8.0f, 20.0f), new Vector2(-8.0f, 6.0f), }), 0); } // Flippers { var p1 = new Vector2(-2.0f, 0.0f); var p2 = new Vector2(2.0f, 0.0f); Body leftFlipper = Physics.NewBody(m_world, p1.X, p1.Y, BodyType.Dynamic); Body rightFlipper = Physics.NewBody(m_world, p2.X, p2.Y, BodyType.Dynamic); var box = Physics.NewRectangleShape(1.75f * 2, 0.1f * 2); Physics.NewFixture(leftFlipper, box, 1.0f); Physics.NewFixture(rightFlipper, box, 1.0f); lrj = Physics.NewRevoluteJoint(ground, leftFlipper, p1); lrj.SetMotorSpeed(0); lrj.SetMotorEnabled(true); lrj.SetLimitsEnabled(true); lrj.SetMaxMotorTorque(1000.0f); lrj.SetLimits(-30.0f * Mathf.PI / 180.0f, 5.0f * Mathf.PI / 180.0f); rrj = Physics.NewRevoluteJoint(ground, rightFlipper, p2); rrj.SetMotorSpeed(0); rrj.SetMotorEnabled(true); rrj.SetLimitsEnabled(true); rrj.SetMaxMotorTorque(1000.0f); rrj.SetLimits(-5.0f * Mathf.PI / 180.0f, 30.0f * Mathf.PI / 180.0f); } // Circle character { var body = Physics.NewBody(m_world, 1.0f, 15.0f, BodyType.Dynamic); body.SetBullet(true); Physics.NewFixture(body, Physics.NewCircleShape(0.2f), 1.0f); } }
public override void Load() { genDT = 0; Body ground = Physics.NewBody(m_world); { Body body = Physics.NewBody(m_world, 0.0f * scale, 10.0f * scale, BodyType.Dynamic); body.SetSleepingAllowed(false); Physics.NewFixture(body, NewRectShape(body, 10.0f * scale, 0.0f * scale, 0.5f * 2 * scale, 10.0f * 2 * scale), 5.0f * scale); Physics.NewFixture(body, NewRectShape(body, -10.0f * scale, 0.0f * scale, 0.5f * 2 * scale, 10.0f * 2 * scale), 5.0f * scale); Physics.NewFixture(body, NewRectShape(body, 0.0f * scale, 10.0f * scale, 10.0f * 2 * scale, 0.5f * 2 * scale), 5.0f * scale); Physics.NewFixture(body, NewRectShape(body, 0.0f * scale, -10.0f * scale, 10.0f * 2 * scale, 0.5f * 2 * scale), 5.0f * scale); //RevoluteJoint jd = Physics.NewRevoluteJoint(ground, body, // new Vector2(0.0f, 10.0f) * scale, // new Vector2(0.0f, 0.0f) * scale // ); RevoluteJoint jd = Physics.NewRevoluteJoint(ground, body, new Vector2(0.0f, 10.0f) * scale, new Vector2(0.0f, 10.0f) * scale ); jd.SetMotorSpeed(-0.05f * Mathf.PI); jd.SetMaxMotorTorque(1e8f * scale); jd.SetMotorEnabled(true); } m_count = 0; }