BsHingeJoint.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsJoint.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Physics
  9. * @{
  10. */
  11. struct HINGE_JOINT_DESC;
  12. /**
  13. * Hinge joint removes all but a single rotation degree of freedom from its two attached bodies (for example a door
  14. * hinge).
  15. */
  16. class BS_CORE_EXPORT HingeJoint : public Joint
  17. {
  18. public:
  19. /** Flags that control hinge joint options. */
  20. enum class Flag
  21. {
  22. Limit = 0x1, /** Joint limit is enabled. */
  23. Drive = 0x2 /** Joint drive is enabled. */
  24. };
  25. /** Properties of a drive that drives the joint's angular velocity towards a paricular value. */
  26. struct Drive
  27. {
  28. /** Target speed of the joint. */
  29. float speed = 0.0f;
  30. /** Maximum torque the drive is allowed to apply .*/
  31. float forceLimit = FLT_MAX;
  32. /** Scales the velocity of the first body, and its response to drive torque is scaled down. */
  33. float gearRatio = 1.0f;
  34. /**
  35. * If the joint is moving faster than the drive's target speed, the drive will try to break. If you don't want
  36. * the breaking to happen set this to true.
  37. */
  38. bool freeSpin = false;
  39. bool operator==(const Drive& other) const
  40. {
  41. return speed == other.speed && forceLimit == other.forceLimit && gearRatio == other.gearRatio &&
  42. freeSpin && other.freeSpin;
  43. }
  44. };
  45. public:
  46. HingeJoint(const HINGE_JOINT_DESC& desc) { }
  47. virtual ~HingeJoint() { }
  48. /** Returns the current angle between the two attached bodes. */
  49. virtual Radian getAngle() const = 0;
  50. /** Returns the current angular speed of the joint. */
  51. virtual float getSpeed() const = 0;
  52. /**
  53. * Returns the limit of the joint. Limit constrains the motion to the specified angle range. You must enable the
  54. * limit flag on the joint in order for this to be recognized.
  55. *
  56. * @see LimitAngularRange
  57. */
  58. virtual LimitAngularRange getLimit() const = 0;
  59. /**
  60. * Sets the limit of the joint. Limit constrains the motion to the specified angle range. You must enable the limit
  61. * flag on the joint in order for this to be recognized.
  62. *
  63. * @see LimitAngularRange
  64. */
  65. virtual void setLimit(const LimitAngularRange& limit) = 0;
  66. /**
  67. * Returns the drive properties of the joint. It drives the joint's angular velocity towards a particular value. You
  68. * must enable the drive flag on the joint in order for the drive to be active.
  69. *
  70. * @see HingeJoint::Drive
  71. */
  72. virtual Drive getDrive() const = 0;
  73. /**
  74. * Sets the drive properties of the joint. It drives the joint's angular velocity towards a particular value. You
  75. * must enable the drive flag on the joint in order for the drive to be active.
  76. *
  77. * @see HingeJoint::Drive
  78. */
  79. virtual void setDrive(const Drive& drive) = 0;
  80. /** Enables or disables a flag that controls joint behaviour. */
  81. virtual void setFlag(Flag flag, bool enabled) = 0;
  82. /** Checks is the specified option enabled. */
  83. virtual bool hasFlag(Flag flag) const = 0;
  84. /** Creates a new hinge joint. */
  85. static SPtr<HingeJoint> create(const HINGE_JOINT_DESC& desc);
  86. };
  87. /** Structure used for initializing a new HingeJoint. */
  88. struct HINGE_JOINT_DESC : JOINT_DESC
  89. {
  90. HingeJoint::Drive drive;
  91. LimitAngularRange limit;
  92. HingeJoint::Flag flag = (HingeJoint::Flag)0;
  93. };
  94. /** @} */
  95. }