BSConstraint.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 BulletSim m_world;
  37. protected BulletBody m_body1;
  38. protected BulletBody m_body2;
  39. protected BulletConstraint m_constraint;
  40. protected bool m_enabled = false;
  41. public BSConstraint()
  42. {
  43. }
  44. public virtual void Dispose()
  45. {
  46. if (m_enabled)
  47. {
  48. m_enabled = false;
  49. if (m_constraint.ptr != IntPtr.Zero)
  50. {
  51. bool success = BulletSimAPI.DestroyConstraint2(m_world.ptr, m_constraint.ptr);
  52. m_world.physicsScene.DetailLog("{0},BSConstraint.Dispose,taint,id1={1},body1={2},id2={3},body2={4},success={5}",
  53. BSScene.DetailLogZero,
  54. m_body1.ID, m_body1.ptr.ToString("X"),
  55. m_body2.ID, m_body2.ptr.ToString("X"),
  56. success);
  57. m_constraint.ptr = System.IntPtr.Zero;
  58. }
  59. }
  60. }
  61. public BulletBody Body1 { get { return m_body1; } }
  62. public BulletBody Body2 { get { return m_body2; } }
  63. public BulletConstraint Constraint { get { return m_constraint; } }
  64. public abstract ConstraintType Type { get; }
  65. public virtual bool SetLinearLimits(Vector3 low, Vector3 high)
  66. {
  67. bool ret = false;
  68. if (m_enabled)
  69. ret = BulletSimAPI.SetLinearLimits2(m_constraint.ptr, low, high);
  70. return ret;
  71. }
  72. public virtual bool SetAngularLimits(Vector3 low, Vector3 high)
  73. {
  74. bool ret = false;
  75. if (m_enabled)
  76. ret = BulletSimAPI.SetAngularLimits2(m_constraint.ptr, low, high);
  77. return ret;
  78. }
  79. public virtual bool SetSolverIterations(float cnt)
  80. {
  81. bool ret = false;
  82. if (m_enabled)
  83. {
  84. BulletSimAPI.SetConstraintNumSolverIterations2(m_constraint.ptr, cnt);
  85. ret = true;
  86. }
  87. return ret;
  88. }
  89. public virtual bool CalculateTransforms()
  90. {
  91. bool ret = false;
  92. if (m_enabled)
  93. {
  94. // Recompute the internal transforms
  95. BulletSimAPI.CalculateTransforms2(m_constraint.ptr);
  96. ret = true;
  97. }
  98. return ret;
  99. }
  100. // Reset this constraint making sure it has all its internal structures
  101. // recomputed and is enabled and ready to go.
  102. public virtual bool RecomputeConstraintVariables(float mass)
  103. {
  104. bool ret = false;
  105. if (m_enabled)
  106. {
  107. ret = CalculateTransforms();
  108. if (ret)
  109. {
  110. // Setting an object's mass to zero (making it static like when it's selected)
  111. // automatically disables the constraints.
  112. // If the link is enabled, be sure to set the constraint itself to enabled.
  113. BulletSimAPI.SetConstraintEnable2(m_constraint.ptr, m_world.physicsScene.NumericBool(true));
  114. }
  115. else
  116. {
  117. m_world.physicsScene.Logger.ErrorFormat("{0} CalculateTransforms failed. A={1}, B={2}", LogHeader, Body1.ID, Body2.ID);
  118. }
  119. }
  120. return ret;
  121. }
  122. }
  123. }