BSConstraintCollection.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 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 BulletSim m_world;
  41. public BSConstraintCollection(BulletSim 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. foreach (BSConstraint cons in m_constraints)
  53. {
  54. cons.Dispose();
  55. }
  56. m_constraints.Clear();
  57. }
  58. public BSConstraint CreateConstraint(BulletSim world, BulletBody obj1, BulletBody obj2,
  59. Vector3 frame1, Quaternion frame1rot,
  60. Vector3 frame2, Quaternion frame2rot)
  61. {
  62. BSConstraint constrain = new BSConstraint(world, obj1, obj2, frame1, frame1rot, frame2, frame2rot);
  63. this.AddConstraint(constrain);
  64. return constrain;
  65. }
  66. public bool AddConstraint(BSConstraint cons)
  67. {
  68. // There is only one constraint between any bodies. Remove any old just to make sure.
  69. RemoveAndDestroyConstraint(cons.Body1, cons.Body2);
  70. m_constraints.Add(cons);
  71. return true;
  72. }
  73. // Get the constraint between two bodies. There can be only one.
  74. // Return 'true' if a constraint was found.
  75. public bool TryGetConstraint(BulletBody body1, BulletBody body2, out BSConstraint returnConstraint)
  76. {
  77. bool found = false;
  78. BSConstraint foundConstraint = null;
  79. uint lookingID1 = body1.ID;
  80. uint lookingID2 = body2.ID;
  81. ForEachConstraint(delegate(BSConstraint constrain)
  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. }
  89. return found;
  90. });
  91. returnConstraint = foundConstraint;
  92. return found;
  93. }
  94. // Remove any constraint between the passed bodies.
  95. // Presumed there is only one such constraint possible.
  96. // Return 'true' if a constraint was found and destroyed.
  97. public bool RemoveAndDestroyConstraint(BulletBody body1, BulletBody body2)
  98. {
  99. // return BulletSimAPI.RemoveConstraint(m_world.ID, obj1.ID, obj2.ID);
  100. bool ret = false;
  101. BSConstraint constrain;
  102. if (this.TryGetConstraint(body1, body2, out constrain))
  103. {
  104. // remove the constraint from our collection
  105. m_constraints.Remove(constrain);
  106. // tell the engine that all its structures need to be freed
  107. constrain.Dispose();
  108. // we destroyed something
  109. ret = true;
  110. }
  111. return ret;
  112. }
  113. // Remove all constraints that reference the passed body.
  114. // Return 'true' if any constraints were destroyed.
  115. public bool RemoveAndDestroyConstraint(BulletBody body1)
  116. {
  117. // return BulletSimAPI.RemoveConstraintByID(m_world.ID, obj.ID);
  118. List<BSConstraint> toRemove = new List<BSConstraint>();
  119. uint lookingID = body1.ID;
  120. ForEachConstraint(delegate(BSConstraint constrain)
  121. {
  122. if (constrain.Body1.ID == lookingID || constrain.Body2.ID == lookingID)
  123. {
  124. toRemove.Add(constrain);
  125. }
  126. return false;
  127. });
  128. lock (m_constraints)
  129. {
  130. foreach (BSConstraint constrain in toRemove)
  131. {
  132. m_constraints.Remove(constrain);
  133. constrain.Dispose();
  134. }
  135. }
  136. return (toRemove.Count > 0);
  137. }
  138. public bool RecalculateAllConstraints()
  139. {
  140. foreach (BSConstraint constrain in m_constraints)
  141. {
  142. constrain.CalculateTransforms();
  143. }
  144. return true;
  145. }
  146. // Lock the constraint list and loop through it.
  147. // The constraint action returns 'true' if it wants the loop aborted.
  148. private void ForEachConstraint(ConstraintAction action)
  149. {
  150. lock (m_constraints)
  151. {
  152. foreach (BSConstraint constrain in m_constraints)
  153. {
  154. if (action(constrain))
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. }