1
0

BSConstraint.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 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. BSScene.DetailLogZero,
  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. ret = PhysicsScene.PE.SetLinearLimits(m_constraint, low, high);
  74. return ret;
  75. }
  76. public virtual bool SetAngularLimits(Vector3 low, Vector3 high)
  77. {
  78. bool ret = false;
  79. if (m_enabled)
  80. {
  81. ret = PhysicsScene.PE.SetAngularLimits(m_constraint, low, high);
  82. }
  83. return ret;
  84. }
  85. public virtual bool SetSolverIterations(float cnt)
  86. {
  87. bool ret = false;
  88. if (m_enabled)
  89. {
  90. PhysicsScene.PE.SetConstraintNumSolverIterations(m_constraint, cnt);
  91. ret = true;
  92. }
  93. return ret;
  94. }
  95. public virtual bool CalculateTransforms()
  96. {
  97. bool ret = false;
  98. if (m_enabled)
  99. {
  100. // Recompute the internal transforms
  101. PhysicsScene.PE.CalculateTransforms(m_constraint);
  102. ret = true;
  103. }
  104. return ret;
  105. }
  106. // Reset this constraint making sure it has all its internal structures
  107. // recomputed and is enabled and ready to go.
  108. public virtual bool RecomputeConstraintVariables(float mass)
  109. {
  110. bool ret = false;
  111. if (m_enabled)
  112. {
  113. ret = CalculateTransforms();
  114. if (ret)
  115. {
  116. // Setting an object's mass to zero (making it static like when it's selected)
  117. // automatically disables the constraints.
  118. // If the link is enabled, be sure to set the constraint itself to enabled.
  119. PhysicsScene.PE.SetConstraintEnable(m_constraint, BSParam.NumericBool(true));
  120. }
  121. else
  122. {
  123. m_world.physicsScene.Logger.ErrorFormat("{0} CalculateTransforms failed. A={1}, B={2}", LogHeader, Body1.ID, Body2.ID);
  124. }
  125. }
  126. return ret;
  127. }
  128. }
  129. }