hinge_joint.script 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. local frontwheel = "frontwheel#collisionobject" -- <1>
  2. local backwheel = "backwheel#collisionobject"
  3. local body = "body#collisionobject"
  4. local center_anchor = vmath.vector3(0, 0, 0)
  5. local frontwheel_anchor = vmath.vector3(60, -60, 0)
  6. local backwheel_anchor = vmath.vector3(-60, -60, 0)
  7. local hinge_props = { enable_motor = true, enable_limit = false, max_motor_torque = 3000, motor_speed = 1 * 2 * math.pi}
  8. function init(self)
  9. msg.post(".", "acquire_input_focus") -- <2>
  10. self.forward = true -- <3>
  11. physics.create_joint(physics.JOINT_TYPE_HINGE, frontwheel, "frontwheel", center_anchor, body, frontwheel_anchor, hinge_props) -- <4>
  12. physics.create_joint(physics.JOINT_TYPE_HINGE, backwheel, "backwheel", center_anchor, body, backwheel_anchor, hinge_props)
  13. end
  14. function on_input(self, action_id, action)
  15. if action_id == hash("touch") and action.pressed then -- <5>
  16. self.forward = not self.forward -- <6>
  17. if self.forward then -- <7>
  18. hinge_props.motor_speed = 5 * 2 * math.pi -- <8>
  19. else -- <9>
  20. hinge_props.motor_speed = -5 * 2 * math.pi -- <10>
  21. end
  22. physics.set_joint_properties(frontwheel, "frontwheel", hinge_props) -- <11>
  23. physics.set_joint_properties(backwheel, "backwheel", hinge_props)
  24. end
  25. end
  26. --[[
  27. 1. Store collision objects ids, vectors used for anchors and hinge properties used for creating joints in local variables.
  28. 2. Tell the engine that this object ("." is shorthand for the current game object) should listen to input. Any input will be received in the `on_input()` function.
  29. 3. Set a flag self.forward used to define the direction of the rotation of the joined wheels.
  30. 4. Create a joint of type "hinge" (a revolute joint, a pin or an axle) between a center of frontwheel collision object and an anchor ((-60,-60) from the center) on body collision object with provided properties. Do the same for the second wheel.
  31. 5. If we receive input (touch or mouse click) we switch the direction of rotation of the wheels.
  32. 6. Negate the current flag defining the direction.
  33. 7. If the direction flag is true, we are going forward.
  34. 8. Set the motor_speed property to 5 revolutions per second in clockwise direction.
  35. 9. If the direction flag is false, we are going backward.
  36. 10. Set the motor_speed property to 5 revolutions per second in counter-clockwise direction.
  37. 11. Set the new properties with changed speed for the joints.
  38. --]]