BSActorLockAxis.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.Linq;
  30. using System.Text;
  31. using OMV = OpenMetaverse;
  32. namespace OpenSim.Region.Physics.BulletSPlugin
  33. {
  34. public class BSActorLockAxis : BSActor
  35. {
  36. BSConstraint LockAxisConstraint = null;
  37. // The lock access flags (which axises were locked) when the contraint was built.
  38. // Used to see if locking has changed since when the constraint was built.
  39. OMV.Vector3 LockAxisLinearFlags;
  40. OMV.Vector3 LockAxisAngularFlags;
  41. public BSActorLockAxis(BSScene physicsScene, BSPhysObject pObj, string actorName)
  42. : base(physicsScene, pObj, actorName)
  43. {
  44. m_physicsScene.DetailLog("{0},BSActorLockAxis,constructor", m_controllingPrim.LocalID);
  45. LockAxisConstraint = null;
  46. // we place our constraint just before the simulation step to make sure the linkset is complete
  47. m_physicsScene.BeforeStep += PhysicsScene_BeforeStep;
  48. }
  49. // BSActor.isActive
  50. public override bool isActive
  51. {
  52. get { return Enabled && m_controllingPrim.IsPhysicallyActive; }
  53. }
  54. // Release any connections and resources used by the actor.
  55. // BSActor.Dispose()
  56. public override void Dispose()
  57. {
  58. m_physicsScene.BeforeStep -= PhysicsScene_BeforeStep;
  59. RemoveAxisLockConstraint();
  60. }
  61. // Called when physical parameters (properties set in Bullet) need to be re-applied.
  62. // Called at taint-time.
  63. // BSActor.Refresh()
  64. public override void Refresh()
  65. {
  66. // Since the axis logging is done with a constraint, Refresh() time is good for
  67. // changing parameters but this needs to wait until the prim/linkset is physically
  68. // constructed. Therefore, the constraint itself is placed at pre-step time.
  69. /*
  70. m_physicsScene.DetailLog("{0},BSActorLockAxis,refresh,lockedLinear={1},lockedAngular={2},enabled={3},pActive={4}",
  71. m_controllingPrim.LocalID,
  72. m_controllingPrim.LockedLinearAxis,
  73. m_controllingPrim.LockedAngularAxis,
  74. Enabled, m_controllingPrim.IsPhysicallyActive);
  75. // If all the axis are free, we don't need to exist
  76. if (m_controllingPrim.LockedAngularAxis == m_controllingPrim.LockedAxisFree
  77. && m_controllingPrim.LockedLinearAxis == m_controllingPrim.LockedAxisFree)
  78. {
  79. Enabled = false;
  80. }
  81. // If the object is physically active, add the axis locking constraint
  82. if (isActive)
  83. {
  84. // Check to see if the locking parameters have changed
  85. if (m_controllingPrim.LockedLinearAxis != this.LockAxisLinearFlags
  86. || m_controllingPrim.LockedAngularAxis != this.LockAxisAngularFlags)
  87. {
  88. // The locking has changed. Remove the old constraint and build a new one
  89. RemoveAxisLockConstraint();
  90. }
  91. AddAxisLockConstraint();
  92. }
  93. else
  94. {
  95. RemoveAxisLockConstraint();
  96. }
  97. */
  98. }
  99. // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...).
  100. // Register a prestep action to restore physical requirements before the next simulation step.
  101. // Called at taint-time.
  102. // BSActor.RemoveDependencies()
  103. public override void RemoveDependencies()
  104. {
  105. RemoveAxisLockConstraint();
  106. // The pre-step action will restore the constraint of needed
  107. }
  108. private void PhysicsScene_BeforeStep(float timestep)
  109. {
  110. // If all the axis are free, we don't need to exist
  111. if (m_controllingPrim.LockedAngularAxis == m_controllingPrim.LockedAxisFree
  112. && m_controllingPrim.LockedLinearAxis == m_controllingPrim.LockedAxisFree)
  113. {
  114. Enabled = false;
  115. }
  116. // If the object is physically active, add the axis locking constraint
  117. if (isActive)
  118. {
  119. // Check to see if the locking parameters have changed
  120. if (m_controllingPrim.LockedLinearAxis != this.LockAxisLinearFlags
  121. || m_controllingPrim.LockedAngularAxis != this.LockAxisAngularFlags)
  122. {
  123. // The locking has changed. Remove the old constraint and build a new one
  124. RemoveAxisLockConstraint();
  125. }
  126. AddAxisLockConstraint();
  127. }
  128. else
  129. {
  130. RemoveAxisLockConstraint();
  131. }
  132. }
  133. private void AddAxisLockConstraint()
  134. {
  135. if (LockAxisConstraint == null)
  136. {
  137. // Lock that axis by creating a 6DOF constraint that has one end in the world and
  138. // the other in the object.
  139. // http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?p=20817
  140. // http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?p=26380
  141. // Remove any existing axis constraint (just to be sure)
  142. RemoveAxisLockConstraint();
  143. BSConstraint6Dof axisConstrainer = new BSConstraint6Dof(m_physicsScene.World, m_controllingPrim.PhysBody,
  144. OMV.Vector3.Zero, OMV.Quaternion.Identity,
  145. false /* useLinearReferenceFrameB */, true /* disableCollisionsBetweenLinkedBodies */);
  146. LockAxisConstraint = axisConstrainer;
  147. m_physicsScene.Constraints.AddConstraint(LockAxisConstraint);
  148. // Remember the clocking being inforced so we can notice if they have changed
  149. LockAxisLinearFlags = m_controllingPrim.LockedLinearAxis;
  150. LockAxisAngularFlags = m_controllingPrim.LockedAngularAxis;
  151. // The constraint is tied to the world and oriented to the prim.
  152. if (!axisConstrainer.SetLinearLimits(m_controllingPrim.LockedLinearAxisLow, m_controllingPrim.LockedLinearAxisHigh))
  153. {
  154. m_physicsScene.DetailLog("{0},BSActorLockAxis.AddAxisLockConstraint,failedSetLinearLimits",
  155. m_controllingPrim.LocalID);
  156. }
  157. if (!axisConstrainer.SetAngularLimits(m_controllingPrim.LockedAngularAxisLow, m_controllingPrim.LockedAngularAxisHigh))
  158. {
  159. m_physicsScene.DetailLog("{0},BSActorLockAxis.AddAxisLockConstraint,failedSetAngularLimits",
  160. m_controllingPrim.LocalID);
  161. }
  162. m_physicsScene.DetailLog("{0},BSActorLockAxis.AddAxisLockConstraint,create,linLow={1},linHi={2},angLow={3},angHi={4}",
  163. m_controllingPrim.LocalID,
  164. m_controllingPrim.LockedLinearAxisLow,
  165. m_controllingPrim.LockedLinearAxisHigh,
  166. m_controllingPrim.LockedAngularAxisLow,
  167. m_controllingPrim.LockedAngularAxisHigh);
  168. // Constants from one of the posts mentioned above and used in Bullet's ConstraintDemo.
  169. axisConstrainer.TranslationalLimitMotor(true /* enable */, 5.0f, 0.1f);
  170. axisConstrainer.RecomputeConstraintVariables(m_controllingPrim.RawMass);
  171. }
  172. }
  173. private void RemoveAxisLockConstraint()
  174. {
  175. if (LockAxisConstraint != null)
  176. {
  177. m_physicsScene.Constraints.RemoveAndDestroyConstraint(LockAxisConstraint);
  178. LockAxisConstraint = null;
  179. m_physicsScene.DetailLog("{0},BSActorLockAxis.RemoveAxisLockConstraint,destroyingConstraint", m_controllingPrim.LocalID);
  180. }
  181. }
  182. }
  183. }