BSActorLockAxis.cs 9.6 KB

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