BS6DofConstraint.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 BS6DofConstraint : 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 BS6DofConstraint(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 BS6DofConstraint(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. "[BULLETSIM 6DOF CONSTRAINT]", world.worldID,
  70. obj1.ID, obj1.ptr.ToString("X"), obj2.ID, obj2.ptr.ToString("X"));
  71. m_enabled = false;
  72. }
  73. else
  74. {
  75. m_constraint = new BulletConstraint(
  76. BulletSimAPI.Create6DofConstraintToPoint2(m_world.ptr, m_body1.ptr, m_body2.ptr,
  77. joinPoint,
  78. useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies));
  79. world.physicsScene.DetailLog("{0},BS6DofConstraint,createMidPoint,wID={1}, csrt={2}, rID={3}, rBody={4}, cID={5}, cBody={6}",
  80. BSScene.DetailLogZero, world.worldID, m_constraint.ptr.ToString("X"),
  81. obj1.ID, obj1.ptr.ToString("X"), obj2.ID, obj2.ptr.ToString("X"));
  82. if (m_constraint.ptr == IntPtr.Zero)
  83. {
  84. world.physicsScene.Logger.ErrorFormat("{0} Failed creation of 6Dof constraint. rootID={1}, childID={2}",
  85. LogHeader, obj1.ID, obj2.ID);
  86. m_enabled = false;
  87. }
  88. else
  89. {
  90. m_enabled = true;
  91. }
  92. }
  93. }
  94. public bool SetFrames(Vector3 frameA, Quaternion frameArot, Vector3 frameB, Quaternion frameBrot)
  95. {
  96. bool ret = false;
  97. if (m_enabled)
  98. {
  99. BulletSimAPI.SetFrames2(m_constraint.ptr, frameA, frameArot, frameB, frameBrot);
  100. ret = true;
  101. }
  102. return ret;
  103. }
  104. public bool SetCFMAndERP(float cfm, float erp)
  105. {
  106. bool ret = false;
  107. if (m_enabled)
  108. {
  109. BulletSimAPI.SetConstraintParam2(m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_STOP_CFM, cfm, ConstraintParamAxis.AXIS_ALL);
  110. BulletSimAPI.SetConstraintParam2(m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_STOP_ERP, erp, ConstraintParamAxis.AXIS_ALL);
  111. BulletSimAPI.SetConstraintParam2(m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_CFM, cfm, ConstraintParamAxis.AXIS_ALL);
  112. ret = true;
  113. }
  114. return ret;
  115. }
  116. public bool UseFrameOffset(bool useOffset)
  117. {
  118. bool ret = false;
  119. float onOff = useOffset ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse;
  120. if (m_enabled)
  121. ret = BulletSimAPI.UseFrameOffset2(m_constraint.ptr, onOff);
  122. return ret;
  123. }
  124. public bool TranslationalLimitMotor(bool enable, float targetVelocity, float maxMotorForce)
  125. {
  126. bool ret = false;
  127. float onOff = enable ? ConfigurationParameters.numericTrue : ConfigurationParameters.numericFalse;
  128. if (m_enabled)
  129. ret = BulletSimAPI.TranslationalLimitMotor2(m_constraint.ptr, onOff, targetVelocity, maxMotorForce);
  130. return ret;
  131. }
  132. public bool SetBreakingImpulseThreshold(float threshold)
  133. {
  134. bool ret = false;
  135. if (m_enabled)
  136. ret = BulletSimAPI.SetBreakingImpulseThreshold2(m_constraint.ptr, threshold);
  137. return ret;
  138. }
  139. }
  140. }