1
0

BSConstraintCollection.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 log4net;
  31. using OpenMetaverse;
  32. namespace OpenSim.Region.Physics.BulletSPlugin
  33. {
  34. public sealed class BSConstraintCollection : IDisposable
  35. {
  36. // private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  37. // private static readonly string LogHeader = "[CONSTRAINT COLLECTION]";
  38. delegate bool ConstraintAction(BSConstraint constrain);
  39. private List<BSConstraint> m_constraints;
  40. private BulletWorld m_world;
  41. public BSConstraintCollection(BulletWorld world)
  42. {
  43. m_world = world;
  44. m_constraints = new List<BSConstraint>();
  45. }
  46. public void Dispose()
  47. {
  48. this.Clear();
  49. }
  50. public void Clear()
  51. {
  52. lock (m_constraints)
  53. {
  54. foreach (BSConstraint cons in m_constraints)
  55. {
  56. cons.Dispose();
  57. }
  58. m_constraints.Clear();
  59. }
  60. }
  61. public bool AddConstraint(BSConstraint cons)
  62. {
  63. lock (m_constraints)
  64. {
  65. // There is only one constraint between any bodies. Remove any old just to make sure.
  66. RemoveAndDestroyConstraint(cons.Body1, cons.Body2);
  67. m_constraints.Add(cons);
  68. }
  69. return true;
  70. }
  71. // Get the constraint between two bodies. There can be only one.
  72. // Return 'true' if a constraint was found.
  73. public bool TryGetConstraint(BulletBody body1, BulletBody body2, out BSConstraint returnConstraint)
  74. {
  75. bool found = false;
  76. BSConstraint foundConstraint = null;
  77. uint lookingID1 = body1.ID;
  78. uint lookingID2 = body2.ID;
  79. lock (m_constraints)
  80. {
  81. foreach (BSConstraint constrain in m_constraints)
  82. {
  83. if ((constrain.Body1.ID == lookingID1 && constrain.Body2.ID == lookingID2)
  84. || (constrain.Body1.ID == lookingID2 && constrain.Body2.ID == lookingID1))
  85. {
  86. foundConstraint = constrain;
  87. found = true;
  88. break;
  89. }
  90. }
  91. }
  92. returnConstraint = foundConstraint;
  93. return found;
  94. }
  95. // Remove any constraint between the passed bodies.
  96. // Presumed there is only one such constraint possible.
  97. // Return 'true' if a constraint was found and destroyed.
  98. public bool RemoveAndDestroyConstraint(BulletBody body1, BulletBody body2)
  99. {
  100. bool ret = false;
  101. lock (m_constraints)
  102. {
  103. BSConstraint constrain;
  104. if (this.TryGetConstraint(body1, body2, out constrain))
  105. {
  106. // remove the constraint from our collection
  107. ret = RemoveAndDestroyConstraint(constrain);
  108. }
  109. }
  110. return ret;
  111. }
  112. // The constraint MUST exist in the collection
  113. // Could be called if the constraint was previously removed.
  114. // Return 'true' if the constraint was actually removed and disposed.
  115. public bool RemoveAndDestroyConstraint(BSConstraint constrain)
  116. {
  117. bool removed = false;
  118. lock (m_constraints)
  119. {
  120. // remove the constraint from our collection
  121. removed = m_constraints.Remove(constrain);
  122. }
  123. // Dispose() is safe to call multiple times
  124. constrain.Dispose();
  125. return removed;
  126. }
  127. // Remove all constraints that reference the passed body.
  128. // Return 'true' if any constraints were destroyed.
  129. public bool RemoveAndDestroyConstraint(BulletBody body1)
  130. {
  131. List<BSConstraint> toRemove = new List<BSConstraint>();
  132. uint lookingID = body1.ID;
  133. lock (m_constraints)
  134. {
  135. foreach (BSConstraint constrain in m_constraints)
  136. {
  137. if (constrain.Body1.ID == lookingID || constrain.Body2.ID == lookingID)
  138. {
  139. toRemove.Add(constrain);
  140. }
  141. }
  142. foreach (BSConstraint constrain in toRemove)
  143. {
  144. m_constraints.Remove(constrain);
  145. constrain.Dispose();
  146. }
  147. }
  148. return (toRemove.Count > 0);
  149. }
  150. public bool RecalculateAllConstraints()
  151. {
  152. bool ret = false;
  153. lock (m_constraints)
  154. {
  155. foreach (BSConstraint constrain in m_constraints)
  156. {
  157. constrain.CalculateTransforms();
  158. ret = true;
  159. }
  160. }
  161. return ret;
  162. }
  163. }
  164. }