BSConstraint.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 abstract class BSConstraint : IDisposable
  34. {
  35. private static string LogHeader = "[BULLETSIM CONSTRAINT]";
  36. protected BulletWorld m_world;
  37. protected BSScene PhysicsScene;
  38. protected BulletBody m_body1;
  39. protected BulletBody m_body2;
  40. protected BulletConstraint m_constraint;
  41. protected bool m_enabled = false;
  42. public BulletBody Body1 { get { return m_body1; } }
  43. public BulletBody Body2 { get { return m_body2; } }
  44. public BulletConstraint Constraint { get { return m_constraint; } }
  45. public abstract ConstraintType Type { get; }
  46. public bool IsEnabled { get { return m_enabled; } }
  47. public BSConstraint(BulletWorld world)
  48. {
  49. m_world = world;
  50. PhysicsScene = m_world.physicsScene;
  51. }
  52. public virtual void Dispose()
  53. {
  54. if (m_enabled)
  55. {
  56. m_enabled = false;
  57. if (m_constraint.HasPhysicalConstraint)
  58. {
  59. bool success = PhysicsScene.PE.DestroyConstraint(m_world, m_constraint);
  60. m_world.physicsScene.DetailLog("{0},BSConstraint.Dispose,taint,id1={1},body1={2},id2={3},body2={4},success={5}",
  61. m_body1.ID,
  62. m_body1.ID, m_body1.AddrString,
  63. m_body2.ID, m_body2.AddrString,
  64. success);
  65. m_constraint.Clear();
  66. }
  67. }
  68. }
  69. public virtual bool SetLinearLimits(Vector3 low, Vector3 high)
  70. {
  71. bool ret = false;
  72. if (m_enabled)
  73. {
  74. m_world.physicsScene.DetailLog("{0},BSConstraint.SetLinearLimits,taint,low={1},high={2}", m_body1.ID, low, high);
  75. ret = PhysicsScene.PE.SetLinearLimits(m_constraint, low, high);
  76. }
  77. return ret;
  78. }
  79. public virtual bool SetAngularLimits(Vector3 low, Vector3 high)
  80. {
  81. bool ret = false;
  82. if (m_enabled)
  83. {
  84. m_world.physicsScene.DetailLog("{0},BSConstraint.SetAngularLimits,taint,low={1},high={2}", m_body1.ID, low, high);
  85. ret = PhysicsScene.PE.SetAngularLimits(m_constraint, low, high);
  86. }
  87. return ret;
  88. }
  89. public virtual bool SetSolverIterations(float cnt)
  90. {
  91. bool ret = false;
  92. if (m_enabled)
  93. {
  94. PhysicsScene.PE.SetConstraintNumSolverIterations(m_constraint, cnt);
  95. ret = true;
  96. }
  97. return ret;
  98. }
  99. public virtual bool CalculateTransforms()
  100. {
  101. bool ret = false;
  102. if (m_enabled)
  103. {
  104. // Recompute the internal transforms
  105. PhysicsScene.PE.CalculateTransforms(m_constraint);
  106. ret = true;
  107. }
  108. return ret;
  109. }
  110. // Reset this constraint making sure it has all its internal structures
  111. // recomputed and is enabled and ready to go.
  112. public virtual bool RecomputeConstraintVariables(float mass)
  113. {
  114. bool ret = false;
  115. if (m_enabled)
  116. {
  117. ret = CalculateTransforms();
  118. if (ret)
  119. {
  120. // Setting an object's mass to zero (making it static like when it's selected)
  121. // automatically disables the constraints.
  122. // If the link is enabled, be sure to set the constraint itself to enabled.
  123. PhysicsScene.PE.SetConstraintEnable(m_constraint, BSParam.NumericBool(true));
  124. }
  125. else
  126. {
  127. m_world.physicsScene.Logger.ErrorFormat("{0} CalculateTransforms failed. A={1}, B={2}", LogHeader, Body1.ID, Body2.ID);
  128. }
  129. }
  130. return ret;
  131. }
  132. }
  133. }