BSConstraint6Dof.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.PhysicsModule.BulletS
  32. {
  33. public class BSConstraint6Dof : BSConstraint
  34. {
  35. private static string LogHeader = "[BULLETSIM 6DOF CONSTRAINT]";
  36. public override ConstraintType Type { get { return ConstraintType.D6_CONSTRAINT_TYPE; } }
  37. public BSConstraint6Dof(BulletWorld world, BulletBody obj1, BulletBody obj2) :base(world)
  38. {
  39. m_body1 = obj1;
  40. m_body2 = obj2;
  41. m_enabled = false;
  42. }
  43. // Create a btGeneric6DofConstraint
  44. public BSConstraint6Dof(BulletWorld world, BulletBody obj1, BulletBody obj2,
  45. Vector3 frame1, Quaternion frame1rot,
  46. Vector3 frame2, Quaternion frame2rot,
  47. bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
  48. : base(world)
  49. {
  50. m_body1 = obj1;
  51. m_body2 = obj2;
  52. m_constraint = PhysicsScene.PE.Create6DofConstraint(m_world, m_body1, m_body2,
  53. frame1, frame1rot,
  54. frame2, frame2rot,
  55. useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies);
  56. m_enabled = true;
  57. PhysicsScene.DetailLog("{0},BS6DofConstraint,create,wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}",
  58. m_body1.ID, world.worldID,
  59. obj1.ID, obj1.AddrString, obj2.ID, obj2.AddrString);
  60. PhysicsScene.DetailLog("{0},BS6DofConstraint,create, f1Loc={1},f1Rot={2},f2Loc={3},f2Rot={4},usefA={5},disCol={6}",
  61. m_body1.ID, frame1, frame1rot, frame2, frame2rot, useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies);
  62. }
  63. // 6 Dof constraint based on a midpoint between the two constrained bodies
  64. public BSConstraint6Dof(BulletWorld world, BulletBody obj1, BulletBody obj2,
  65. Vector3 joinPoint,
  66. bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
  67. : base(world)
  68. {
  69. m_body1 = obj1;
  70. m_body2 = obj2;
  71. if (!obj1.HasPhysicalBody || !obj2.HasPhysicalBody)
  72. {
  73. world.physicsScene.DetailLog("{0},BS6DOFConstraint,badBodyPtr,wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}",
  74. BSScene.DetailLogZero, world.worldID,
  75. obj1.ID, obj1.AddrString, obj2.ID, obj2.AddrString);
  76. world.physicsScene.Logger.ErrorFormat("{0} Attempt to build 6DOF constraint with missing bodies: wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}",
  77. LogHeader, world.worldID, obj1.ID, obj1.AddrString, obj2.ID, obj2.AddrString);
  78. m_enabled = false;
  79. }
  80. else
  81. {
  82. m_constraint = PhysicsScene.PE.Create6DofConstraintToPoint(m_world, m_body1, m_body2,
  83. joinPoint,
  84. useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies);
  85. PhysicsScene.DetailLog("{0},BS6DofConstraint,createMidPoint,wID={1}, csrt={2}, rID={3}, rBody={4}, cID={5}, cBody={6}",
  86. m_body1.ID, world.worldID, m_constraint.AddrString,
  87. obj1.ID, obj1.AddrString, obj2.ID, obj2.AddrString);
  88. if (!m_constraint.HasPhysicalConstraint)
  89. {
  90. world.physicsScene.Logger.ErrorFormat("{0} Failed creation of 6Dof constraint. rootID={1}, childID={2}",
  91. LogHeader, obj1.ID, obj2.ID);
  92. m_enabled = false;
  93. }
  94. else
  95. {
  96. m_enabled = true;
  97. }
  98. }
  99. }
  100. // A 6 Dof constraint that is fixed in the world and constrained to a on-the-fly created static object
  101. public BSConstraint6Dof(BulletWorld world, BulletBody obj1, Vector3 frameInBloc, Quaternion frameInBrot,
  102. bool useLinearReferenceFrameB, bool disableCollisionsBetweenLinkedBodies)
  103. : base(world)
  104. {
  105. m_body1 = obj1;
  106. m_body2 = obj1; // Look out for confusion down the road
  107. m_constraint = PhysicsScene.PE.Create6DofConstraintFixed(m_world, m_body1,
  108. frameInBloc, frameInBrot,
  109. useLinearReferenceFrameB, disableCollisionsBetweenLinkedBodies);
  110. m_enabled = true;
  111. PhysicsScene.DetailLog("{0},BS6DofConstraint,createFixed,wID={1},rID={2},rBody={3}",
  112. m_body1.ID, world.worldID, obj1.ID, obj1.AddrString);
  113. PhysicsScene.DetailLog("{0},BS6DofConstraint,createFixed, fBLoc={1},fBRot={2},usefA={3},disCol={4}",
  114. m_body1.ID, frameInBloc, frameInBrot, useLinearReferenceFrameB, disableCollisionsBetweenLinkedBodies);
  115. }
  116. public bool SetFrames(Vector3 frameA, Quaternion frameArot, Vector3 frameB, Quaternion frameBrot)
  117. {
  118. bool ret = false;
  119. if (m_enabled)
  120. {
  121. PhysicsScene.PE.SetFrames(m_constraint, frameA, frameArot, frameB, frameBrot);
  122. ret = true;
  123. }
  124. return ret;
  125. }
  126. public bool SetCFMAndERP(float cfm, float erp)
  127. {
  128. bool ret = false;
  129. if (m_enabled)
  130. {
  131. PhysicsScene.PE.SetConstraintParam(m_constraint, ConstraintParams.BT_CONSTRAINT_STOP_CFM, cfm, ConstraintParamAxis.AXIS_ALL);
  132. PhysicsScene.PE.SetConstraintParam(m_constraint, ConstraintParams.BT_CONSTRAINT_STOP_ERP, erp, ConstraintParamAxis.AXIS_ALL);
  133. PhysicsScene.PE.SetConstraintParam(m_constraint, ConstraintParams.BT_CONSTRAINT_CFM, cfm, ConstraintParamAxis.AXIS_ALL);
  134. ret = true;
  135. }
  136. return ret;
  137. }
  138. public bool UseFrameOffset(bool useOffset)
  139. {
  140. bool ret = false;
  141. float onOff = useOffset ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse;
  142. if (m_enabled)
  143. ret = PhysicsScene.PE.UseFrameOffset(m_constraint, onOff);
  144. return ret;
  145. }
  146. public bool TranslationalLimitMotor(bool enable, float targetVelocity, float maxMotorForce)
  147. {
  148. bool ret = false;
  149. float onOff = enable ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse;
  150. if (m_enabled)
  151. {
  152. ret = PhysicsScene.PE.TranslationalLimitMotor(m_constraint, onOff, targetVelocity, maxMotorForce);
  153. m_world.physicsScene.DetailLog("{0},BS6DOFConstraint,TransLimitMotor,enable={1},vel={2},maxForce={3}",
  154. BSScene.DetailLogZero, enable, targetVelocity, maxMotorForce);
  155. }
  156. return ret;
  157. }
  158. public bool SetBreakingImpulseThreshold(float threshold)
  159. {
  160. bool ret = false;
  161. if (m_enabled)
  162. ret = PhysicsScene.PE.SetBreakingImpulseThreshold(m_constraint, threshold);
  163. return ret;
  164. }
  165. }
  166. }