1
0

BSActorLockAxis.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. OMV.Vector3 linearLow = new OMV.Vector3(-10000f, -10000f, -10000f);
  113. OMV.Vector3 linearHigh = new OMV.Vector3(10000f, 10000f, 10000f);
  114. if (m_controllingPrim.LockedLinearAxis.X != BSPhysObject.FreeAxis)
  115. {
  116. linearLow.X = m_controllingPrim.RawPosition.X;
  117. linearHigh.X = m_controllingPrim.RawPosition.X;
  118. }
  119. if (m_controllingPrim.LockedLinearAxis.Y != BSPhysObject.FreeAxis)
  120. {
  121. linearLow.Y = m_controllingPrim.RawPosition.Y;
  122. linearHigh.Y = m_controllingPrim.RawPosition.Y;
  123. }
  124. if (m_controllingPrim.LockedLinearAxis.Z != BSPhysObject.FreeAxis)
  125. {
  126. linearLow.Z = m_controllingPrim.RawPosition.Z;
  127. linearHigh.Z = m_controllingPrim.RawPosition.Z;
  128. }
  129. axisConstrainer.SetLinearLimits(linearLow, linearHigh);
  130. // Angular with some axis locked
  131. float fPI = (float)Math.PI;
  132. OMV.Vector3 angularLow = new OMV.Vector3(-fPI, -fPI, -fPI);
  133. OMV.Vector3 angularHigh = new OMV.Vector3(fPI, fPI, fPI);
  134. if (m_controllingPrim.LockedAngularAxis.X != BSPhysObject.FreeAxis)
  135. {
  136. angularLow.X = 0f;
  137. angularHigh.X = 0f;
  138. }
  139. if (m_controllingPrim.LockedAngularAxis.Y != BSPhysObject.FreeAxis)
  140. {
  141. angularLow.Y = 0f;
  142. angularHigh.Y = 0f;
  143. }
  144. if (m_controllingPrim.LockedAngularAxis.Z != BSPhysObject.FreeAxis)
  145. {
  146. angularLow.Z = 0f;
  147. angularHigh.Z = 0f;
  148. }
  149. if (!axisConstrainer.SetAngularLimits(angularLow, angularHigh))
  150. {
  151. m_physicsScene.DetailLog("{0},BSActorLockAxis.AddAxisLockConstraint,failedSetAngularLimits", m_controllingPrim.LocalID);
  152. }
  153. m_physicsScene.DetailLog("{0},BSActorLockAxis.AddAxisLockConstraint,create,linLow={1},linHi={2},angLow={3},angHi={4}",
  154. m_controllingPrim.LocalID, linearLow, linearHigh, angularLow, angularHigh);
  155. // Constants from one of the posts mentioned above and used in Bullet's ConstraintDemo.
  156. axisConstrainer.TranslationalLimitMotor(true /* enable */, 5.0f, 0.1f);
  157. axisConstrainer.RecomputeConstraintVariables(m_controllingPrim.RawMass);
  158. }
  159. }
  160. private void RemoveAxisLockConstraint()
  161. {
  162. if (LockAxisConstraint != null)
  163. {
  164. m_physicsScene.Constraints.RemoveAndDestroyConstraint(LockAxisConstraint);
  165. LockAxisConstraint = null;
  166. m_physicsScene.DetailLog("{0},BSActorLockAxis.RemoveAxisLockConstraint,destroyingConstraint", m_controllingPrim.LocalID);
  167. }
  168. }
  169. }
  170. }