BSConstraint6Dof.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyrightD
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. using OpenMetaverse;
  31. namespace OpenSim.Region.Physics.BulletSPlugin
  32. {
  33. public sealed class BSConstraint6Dof : BSConstraint
  34. {
  35. private static string LogHeader = "[BULLETSIM 6DOF CONSTRAINT]";
  36. public override ConstraintType Type { get { return ConstraintType.D6_CONSTRAINT_TYPE; } }
  37. // Create a btGeneric6DofConstraint
  38. public BSConstraint6Dof(BulletWorld world, BulletBody obj1, BulletBody obj2,
  39. Vector3 frame1, Quaternion frame1rot,
  40. Vector3 frame2, Quaternion frame2rot,
  41. bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
  42. : base(world)
  43. {
  44. m_body1 = obj1;
  45. m_body2 = obj2;
  46. m_constraint = PhysicsScene.PE.Create6DofConstraint(m_world, m_body1, m_body2,
  47. frame1, frame1rot,
  48. frame2, frame2rot,
  49. useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies);
  50. m_enabled = true;
  51. world.physicsScene.DetailLog("{0},BS6DofConstraint,createFrame,wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}",
  52. BSScene.DetailLogZero, world.worldID,
  53. obj1.ID, obj1.AddrString, obj2.ID, obj2.AddrString);
  54. }
  55. // 6 Dof constraint based on a midpoint between the two constrained bodies
  56. public BSConstraint6Dof(BulletWorld world, BulletBody obj1, BulletBody obj2,
  57. Vector3 joinPoint,
  58. bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
  59. : base(world)
  60. {
  61. m_body1 = obj1;
  62. m_body2 = obj2;
  63. if (!obj1.HasPhysicalBody || !obj2.HasPhysicalBody)
  64. {
  65. world.physicsScene.DetailLog("{0},BS6DOFConstraint,badBodyPtr,wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}",
  66. BSScene.DetailLogZero, world.worldID,
  67. obj1.ID, obj1.AddrString, obj2.ID, obj2.AddrString);
  68. world.physicsScene.Logger.ErrorFormat("{0} Attempt to build 6DOF constraint with missing bodies: wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}",
  69. LogHeader, world.worldID, obj1.ID, obj1.AddrString, obj2.ID, obj2.AddrString);
  70. m_enabled = false;
  71. }
  72. else
  73. {
  74. m_constraint = PhysicsScene.PE.Create6DofConstraintToPoint(m_world, m_body1, m_body2,
  75. joinPoint,
  76. useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies);
  77. PhysicsScene.DetailLog("{0},BS6DofConstraint,createMidPoint,wID={1}, csrt={2}, rID={3}, rBody={4}, cID={5}, cBody={6}",
  78. BSScene.DetailLogZero, world.worldID, m_constraint.AddrString,
  79. obj1.ID, obj1.AddrString, obj2.ID, obj2.AddrString);
  80. if (!m_constraint.HasPhysicalConstraint)
  81. {
  82. world.physicsScene.Logger.ErrorFormat("{0} Failed creation of 6Dof constraint. rootID={1}, childID={2}",
  83. LogHeader, obj1.ID, obj2.ID);
  84. m_enabled = false;
  85. }
  86. else
  87. {
  88. m_enabled = true;
  89. }
  90. }
  91. }
  92. // A 6 Dof constraint that is fixed in the world and constrained to a on-the-fly created static object
  93. public BSConstraint6Dof(BulletWorld world, BulletBody obj1, Vector3 frameInBloc, Quaternion frameInBrot,
  94. bool useLinearReferenceFrameB, bool disableCollisionsBetweenLinkedBodies)
  95. : base(world)
  96. {
  97. m_body1 = obj1;
  98. m_body2 = obj1; // Look out for confusion down the road
  99. m_constraint = PhysicsScene.PE.Create6DofConstraintFixed(m_world, m_body1,
  100. frameInBloc, frameInBrot,
  101. useLinearReferenceFrameB, disableCollisionsBetweenLinkedBodies);
  102. m_enabled = true;
  103. world.physicsScene.DetailLog("{0},BS6DofConstraint,createFixed,wID={1},rID={2},rBody={3}",
  104. BSScene.DetailLogZero, world.worldID, obj1.ID, obj1.AddrString);
  105. }
  106. public bool SetFrames(Vector3 frameA, Quaternion frameArot, Vector3 frameB, Quaternion frameBrot)
  107. {
  108. bool ret = false;
  109. if (m_enabled)
  110. {
  111. PhysicsScene.PE.SetFrames(m_constraint, frameA, frameArot, frameB, frameBrot);
  112. ret = true;
  113. }
  114. return ret;
  115. }
  116. public bool SetCFMAndERP(float cfm, float erp)
  117. {
  118. bool ret = false;
  119. if (m_enabled)
  120. {
  121. PhysicsScene.PE.SetConstraintParam(m_constraint, ConstraintParams.BT_CONSTRAINT_STOP_CFM, cfm, ConstraintParamAxis.AXIS_ALL);
  122. PhysicsScene.PE.SetConstraintParam(m_constraint, ConstraintParams.BT_CONSTRAINT_STOP_ERP, erp, ConstraintParamAxis.AXIS_ALL);
  123. PhysicsScene.PE.SetConstraintParam(m_constraint, ConstraintParams.BT_CONSTRAINT_CFM, cfm, ConstraintParamAxis.AXIS_ALL);
  124. ret = true;
  125. }
  126. return ret;
  127. }
  128. public bool UseFrameOffset(bool useOffset)
  129. {
  130. bool ret = false;
  131. float onOff = useOffset ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse;
  132. if (m_enabled)
  133. ret = PhysicsScene.PE.UseFrameOffset(m_constraint, onOff);
  134. return ret;
  135. }
  136. public bool TranslationalLimitMotor(bool enable, float targetVelocity, float maxMotorForce)
  137. {
  138. bool ret = false;
  139. float onOff = enable ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse;
  140. if (m_enabled)
  141. {
  142. ret = PhysicsScene.PE.TranslationalLimitMotor(m_constraint, onOff, targetVelocity, maxMotorForce);
  143. m_world.physicsScene.DetailLog("{0},BS6DOFConstraint,TransLimitMotor,enable={1},vel={2},maxForce={3}",
  144. BSScene.DetailLogZero, enable, targetVelocity, maxMotorForce);
  145. }
  146. return ret;
  147. }
  148. public bool SetBreakingImpulseThreshold(float threshold)
  149. {
  150. bool ret = false;
  151. if (m_enabled)
  152. ret = PhysicsScene.PE.SetBreakingImpulseThreshold(m_constraint, threshold);
  153. return ret;
  154. }
  155. }
  156. }