BSConstraint6Dof.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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(BulletSim world, BulletBody obj1, BulletBody obj2,
  39. Vector3 frame1, Quaternion frame1rot,
  40. Vector3 frame2, Quaternion frame2rot,
  41. bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
  42. {
  43. m_world = world;
  44. m_body1 = obj1;
  45. m_body2 = obj2;
  46. m_constraint = new BulletConstraint(
  47. BulletSimAPI.Create6DofConstraint2(m_world.ptr, m_body1.ptr, m_body2.ptr,
  48. frame1, frame1rot,
  49. frame2, frame2rot,
  50. useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies));
  51. m_enabled = true;
  52. world.physicsScene.DetailLog("{0},BS6DofConstraint,createFrame,wID={1}, rID={2}, rBody={3}, cID={4}, cBody={5}",
  53. BSScene.DetailLogZero, world.worldID,
  54. obj1.ID, obj1.ptr.ToString("X"), obj2.ID, obj2.ptr.ToString("X"));
  55. }
  56. public BSConstraint6Dof(BulletSim world, BulletBody obj1, BulletBody obj2,
  57. Vector3 joinPoint,
  58. bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
  59. {
  60. m_world = world;
  61. m_body1 = obj1;
  62. m_body2 = obj2;
  63. if (obj1.ptr == IntPtr.Zero || obj2.ptr == IntPtr.Zero)
  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.ptr.ToString("X"), obj2.ID, obj2.ptr.ToString("X"));
  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.ptr.ToString("X"), obj2.ID, obj2.ptr.ToString("X"));
  70. m_enabled = false;
  71. }
  72. else
  73. {
  74. m_constraint = new BulletConstraint(
  75. BulletSimAPI.Create6DofConstraintToPoint2(m_world.ptr, m_body1.ptr, m_body2.ptr,
  76. joinPoint,
  77. useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies));
  78. world.physicsScene.DetailLog("{0},BS6DofConstraint,createMidPoint,wID={1}, csrt={2}, rID={3}, rBody={4}, cID={5}, cBody={6}",
  79. BSScene.DetailLogZero, world.worldID, m_constraint.ptr.ToString("X"),
  80. obj1.ID, obj1.ptr.ToString("X"), obj2.ID, obj2.ptr.ToString("X"));
  81. if (m_constraint.ptr == IntPtr.Zero)
  82. {
  83. world.physicsScene.Logger.ErrorFormat("{0} Failed creation of 6Dof constraint. rootID={1}, childID={2}",
  84. LogHeader, obj1.ID, obj2.ID);
  85. m_enabled = false;
  86. }
  87. else
  88. {
  89. m_enabled = true;
  90. }
  91. }
  92. }
  93. public bool SetFrames(Vector3 frameA, Quaternion frameArot, Vector3 frameB, Quaternion frameBrot)
  94. {
  95. bool ret = false;
  96. if (m_enabled)
  97. {
  98. BulletSimAPI.SetFrames2(m_constraint.ptr, frameA, frameArot, frameB, frameBrot);
  99. ret = true;
  100. }
  101. return ret;
  102. }
  103. public bool SetCFMAndERP(float cfm, float erp)
  104. {
  105. bool ret = false;
  106. if (m_enabled)
  107. {
  108. BulletSimAPI.SetConstraintParam2(m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_STOP_CFM, cfm, ConstraintParamAxis.AXIS_ALL);
  109. BulletSimAPI.SetConstraintParam2(m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_STOP_ERP, erp, ConstraintParamAxis.AXIS_ALL);
  110. BulletSimAPI.SetConstraintParam2(m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_CFM, cfm, ConstraintParamAxis.AXIS_ALL);
  111. ret = true;
  112. }
  113. return ret;
  114. }
  115. public bool UseFrameOffset(bool useOffset)
  116. {
  117. bool ret = false;
  118. float onOff = useOffset ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse;
  119. if (m_enabled)
  120. ret = BulletSimAPI.UseFrameOffset2(m_constraint.ptr, onOff);
  121. return ret;
  122. }
  123. public bool TranslationalLimitMotor(bool enable, float targetVelocity, float maxMotorForce)
  124. {
  125. bool ret = false;
  126. float onOff = enable ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse;
  127. if (m_enabled)
  128. {
  129. ret = BulletSimAPI.TranslationalLimitMotor2(m_constraint.ptr, onOff, targetVelocity, maxMotorForce);
  130. m_world.physicsScene.DetailLog("{0},BS6DOFConstraint,TransLimitMotor,enable={1},vel={2},maxForce={3}",
  131. BSScene.DetailLogZero, enable, targetVelocity, maxMotorForce);
  132. }
  133. return ret;
  134. }
  135. public bool SetBreakingImpulseThreshold(float threshold)
  136. {
  137. bool ret = false;
  138. if (m_enabled)
  139. ret = BulletSimAPI.SetBreakingImpulseThreshold2(m_constraint.ptr, threshold);
  140. return ret;
  141. }
  142. }
  143. }