1
0

BSActorLockAxis.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. public BSActorLockAxis(BSScene physicsScene, BSPhysObject pObj, string actorName)
  38. : base(physicsScene, pObj, actorName)
  39. {
  40. m_physicsScene.DetailLog("{0},BSActorLockAxis,constructor", m_controllingPrim.LocalID);
  41. LockAxisConstraint = null;
  42. }
  43. // BSActor.isActive
  44. public override bool isActive
  45. {
  46. get { return Enabled && m_controllingPrim.IsPhysicallyActive; }
  47. }
  48. // Release any connections and resources used by the actor.
  49. // BSActor.Dispose()
  50. public override void Dispose()
  51. {
  52. RemoveAxisLockConstraint();
  53. }
  54. // Called when physical parameters (properties set in Bullet) need to be re-applied.
  55. // Called at taint-time.
  56. // BSActor.Refresh()
  57. public override void Refresh()
  58. {
  59. m_physicsScene.DetailLog("{0},BSActorLockAxis,refresh,lockedAxis={1},enabled={2},pActive={3}",
  60. m_controllingPrim.LocalID, m_controllingPrim.LockedAngularAxis, Enabled, m_controllingPrim.IsPhysicallyActive);
  61. // If all the axis are free, we don't need to exist
  62. if (m_controllingPrim.LockedAngularAxis == m_controllingPrim.LockedAxisFree)
  63. {
  64. Enabled = false;
  65. }
  66. // If the object is physically active, add the axis locking constraint
  67. if (isActive)
  68. {
  69. AddAxisLockConstraint();
  70. }
  71. else
  72. {
  73. RemoveAxisLockConstraint();
  74. }
  75. }
  76. // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...).
  77. // Register a prestep action to restore physical requirements before the next simulation step.
  78. // Called at taint-time.
  79. // BSActor.RemoveDependencies()
  80. public override void RemoveDependencies()
  81. {
  82. if (LockAxisConstraint != null)
  83. {
  84. // If a constraint is set up, remove it from the physical scene
  85. RemoveAxisLockConstraint();
  86. // Schedule a call before the next simulation step to restore the constraint.
  87. m_physicsScene.PostTaintObject("BSActorLockAxis:" + ActorName, m_controllingPrim.LocalID, delegate()
  88. {
  89. Refresh();
  90. });
  91. }
  92. }
  93. private void AddAxisLockConstraint()
  94. {
  95. if (LockAxisConstraint == null)
  96. {
  97. // Lock that axis by creating a 6DOF constraint that has one end in the world and
  98. // the other in the object.
  99. // http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?p=20817
  100. // http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?p=26380
  101. // Remove any existing axis constraint (just to be sure)
  102. RemoveAxisLockConstraint();
  103. BSConstraint6Dof axisConstrainer = new BSConstraint6Dof(m_physicsScene.World, m_controllingPrim.PhysBody,
  104. OMV.Vector3.Zero, OMV.Quaternion.Identity,
  105. false /* useLinearReferenceFrameB */, true /* disableCollisionsBetweenLinkedBodies */);
  106. LockAxisConstraint = axisConstrainer;
  107. m_physicsScene.Constraints.AddConstraint(LockAxisConstraint);
  108. // The constraint is tied to the world and oriented to the prim.
  109. // Free to move linearly in the region
  110. OMV.Vector3 linearLow = OMV.Vector3.Zero;
  111. OMV.Vector3 linearHigh = m_physicsScene.TerrainManager.DefaultRegionSize;
  112. if (m_controllingPrim.LockedLinearAxis.X != BSPhysObject.FreeAxis)
  113. {
  114. linearLow.X = m_controllingPrim.RawPosition.X;
  115. linearHigh.X = m_controllingPrim.RawPosition.X;
  116. }
  117. if (m_controllingPrim.LockedLinearAxis.Y != BSPhysObject.FreeAxis)
  118. {
  119. linearLow.Y = m_controllingPrim.RawPosition.Y;
  120. linearHigh.Y = m_controllingPrim.RawPosition.Y;
  121. }
  122. if (m_controllingPrim.LockedLinearAxis.Z != BSPhysObject.FreeAxis)
  123. {
  124. linearLow.Z = m_controllingPrim.RawPosition.Z;
  125. linearHigh.Z = m_controllingPrim.RawPosition.Z;
  126. }
  127. axisConstrainer.SetLinearLimits(linearLow, linearHigh);
  128. // Angular with some axis locked
  129. float fPI = (float)Math.PI;
  130. OMV.Vector3 angularLow = new OMV.Vector3(-fPI, -fPI, -fPI);
  131. OMV.Vector3 angularHigh = new OMV.Vector3(fPI, fPI, fPI);
  132. if (m_controllingPrim.LockedAngularAxis.X != BSPhysObject.FreeAxis)
  133. {
  134. angularLow.X = 0f;
  135. angularHigh.X = 0f;
  136. }
  137. if (m_controllingPrim.LockedAngularAxis.Y != BSPhysObject.FreeAxis)
  138. {
  139. angularLow.Y = 0f;
  140. angularHigh.Y = 0f;
  141. }
  142. if (m_controllingPrim.LockedAngularAxis.Z != BSPhysObject.FreeAxis)
  143. {
  144. angularLow.Z = 0f;
  145. angularHigh.Z = 0f;
  146. }
  147. if (!axisConstrainer.SetAngularLimits(angularLow, angularHigh))
  148. {
  149. m_physicsScene.DetailLog("{0},BSActorLockAxis.AddAxisLockConstraint,failedSetAngularLimits", m_controllingPrim.LocalID);
  150. }
  151. m_physicsScene.DetailLog("{0},BSActorLockAxis.AddAxisLockConstraint,create,linLow={1},linHi={2},angLow={3},angHi={4}",
  152. m_controllingPrim.LocalID, linearLow, linearHigh, angularLow, angularHigh);
  153. // Constants from one of the posts mentioned above and used in Bullet's ConstraintDemo.
  154. axisConstrainer.TranslationalLimitMotor(true /* enable */, 5.0f, 0.1f);
  155. axisConstrainer.RecomputeConstraintVariables(m_controllingPrim.RawMass);
  156. }
  157. }
  158. private void RemoveAxisLockConstraint()
  159. {
  160. if (LockAxisConstraint != null)
  161. {
  162. m_physicsScene.Constraints.RemoveAndDestroyConstraint(LockAxisConstraint);
  163. LockAxisConstraint = null;
  164. m_physicsScene.DetailLog("{0},BSActorLockAxis.RemoveAxisLockConstraint,destroyingConstraint", m_controllingPrim.LocalID);
  165. }
  166. }
  167. }
  168. }