BSLinksetConstraints.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 OMV = OpenMetaverse;
  31. namespace OpenSim.Region.Physics.BulletSPlugin
  32. {
  33. public sealed class BSLinksetConstraints : BSLinkset
  34. {
  35. // private static string LogHeader = "[BULLETSIM LINKSET CONSTRAINTS]";
  36. public BSLinksetConstraints(BSScene scene, BSPrimLinkable parent) : base(scene, parent)
  37. {
  38. }
  39. // When physical properties are changed the linkset needs to recalculate
  40. // its internal properties.
  41. // This is queued in the 'post taint' queue so the
  42. // refresh will happen once after all the other taints are applied.
  43. public override void Refresh(BSPrimLinkable requestor)
  44. {
  45. base.Refresh(requestor);
  46. if (HasAnyChildren && IsRoot(requestor))
  47. {
  48. // Queue to happen after all the other taint processing
  49. m_physicsScene.PostTaintObject("BSLinksetContraints.Refresh", requestor.LocalID, delegate()
  50. {
  51. if (HasAnyChildren && IsRoot(requestor))
  52. RecomputeLinksetConstraints();
  53. });
  54. }
  55. }
  56. // The object is going dynamic (physical). Do any setup necessary
  57. // for a dynamic linkset.
  58. // Only the state of the passed object can be modified. The rest of the linkset
  59. // has not yet been fully constructed.
  60. // Return 'true' if any properties updated on the passed object.
  61. // Called at taint-time!
  62. public override bool MakeDynamic(BSPrimLinkable child)
  63. {
  64. // What is done for each object in BSPrim is what we want.
  65. return false;
  66. }
  67. // The object is going static (non-physical). Do any setup necessary for a static linkset.
  68. // Return 'true' if any properties updated on the passed object.
  69. // This doesn't normally happen -- OpenSim removes the objects from the physical
  70. // world if it is a static linkset.
  71. // Called at taint-time!
  72. public override bool MakeStatic(BSPrimLinkable child)
  73. {
  74. // What is done for each object in BSPrim is what we want.
  75. return false;
  76. }
  77. // Called at taint-time!!
  78. public override void UpdateProperties(UpdatedProperties whichUpdated, BSPrimLinkable pObj)
  79. {
  80. // Nothing to do for constraints on property updates
  81. }
  82. // Routine called when rebuilding the body of some member of the linkset.
  83. // Destroy all the constraints have have been made to root and set
  84. // up to rebuild the constraints before the next simulation step.
  85. // Returns 'true' of something was actually removed and would need restoring
  86. // Called at taint-time!!
  87. public override bool RemoveDependencies(BSPrimLinkable child)
  88. {
  89. bool ret = false;
  90. DetailLog("{0},BSLinksetConstraint.RemoveDependencies,removeChildrenForRoot,rID={1},rBody={2}",
  91. child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody.AddrString);
  92. lock (m_linksetActivityLock)
  93. {
  94. // Just undo all the constraints for this linkset. Rebuild at the end of the step.
  95. ret = PhysicallyUnlinkAllChildrenFromRoot(LinksetRoot);
  96. // Cause the constraints, et al to be rebuilt before the next simulation step.
  97. Refresh(LinksetRoot);
  98. }
  99. return ret;
  100. }
  101. // ================================================================
  102. // Add a new child to the linkset.
  103. // Called while LinkActivity is locked.
  104. protected override void AddChildToLinkset(BSPrimLinkable child)
  105. {
  106. if (!HasChild(child))
  107. {
  108. m_children.Add(child);
  109. DetailLog("{0},BSLinksetConstraints.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID);
  110. // Cause constraints and assorted properties to be recomputed before the next simulation step.
  111. Refresh(LinksetRoot);
  112. }
  113. return;
  114. }
  115. // Remove the specified child from the linkset.
  116. // Safe to call even if the child is not really in my linkset.
  117. protected override void RemoveChildFromLinkset(BSPrimLinkable child)
  118. {
  119. if (m_children.Remove(child))
  120. {
  121. BSPrimLinkable rootx = LinksetRoot; // capture the root and body as of now
  122. BSPrimLinkable childx = child;
  123. DetailLog("{0},BSLinksetConstraints.RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}",
  124. childx.LocalID,
  125. rootx.LocalID, rootx.PhysBody.AddrString,
  126. childx.LocalID, childx.PhysBody.AddrString);
  127. m_physicsScene.TaintedObject("BSLinksetConstraints.RemoveChildFromLinkset", delegate()
  128. {
  129. PhysicallyUnlinkAChildFromRoot(rootx, childx);
  130. });
  131. // See that the linkset parameters are recomputed at the end of the taint time.
  132. Refresh(LinksetRoot);
  133. }
  134. else
  135. {
  136. // Non-fatal occurance.
  137. // PhysicsScene.Logger.ErrorFormat("{0}: Asked to remove child from linkset that was not in linkset", LogHeader);
  138. }
  139. return;
  140. }
  141. // Create a constraint between me (root of linkset) and the passed prim (the child).
  142. // Called at taint time!
  143. private void PhysicallyLinkAChildToRoot(BSPrimLinkable rootPrim, BSPrimLinkable childPrim)
  144. {
  145. // Don't build the constraint when asked. Put it off until just before the simulation step.
  146. Refresh(rootPrim);
  147. }
  148. private BSConstraint BuildConstraint(BSPrimLinkable rootPrim, BSPrimLinkable childPrim)
  149. {
  150. // Zero motion for children so they don't interpolate
  151. childPrim.ZeroMotion(true);
  152. // Relative position normalized to the root prim
  153. // Essentually a vector pointing from center of rootPrim to center of childPrim
  154. OMV.Vector3 childRelativePosition = childPrim.Position - rootPrim.Position;
  155. // real world coordinate of midpoint between the two objects
  156. OMV.Vector3 midPoint = rootPrim.Position + (childRelativePosition / 2);
  157. DetailLog("{0},BSLinksetConstraint.BuildConstraint,taint,root={1},rBody={2},child={3},cBody={4},rLoc={5},cLoc={6},midLoc={7}",
  158. rootPrim.LocalID,
  159. rootPrim.LocalID, rootPrim.PhysBody.AddrString,
  160. childPrim.LocalID, childPrim.PhysBody.AddrString,
  161. rootPrim.Position, childPrim.Position, midPoint);
  162. // create a constraint that allows no freedom of movement between the two objects
  163. // http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=4818
  164. BSConstraint6Dof constrain = new BSConstraint6Dof(
  165. m_physicsScene.World, rootPrim.PhysBody, childPrim.PhysBody, midPoint, true, true );
  166. // PhysicsScene.World, childPrim.BSBody, rootPrim.BSBody, midPoint, true, true );
  167. /* NOTE: below is an attempt to build constraint with full frame computation, etc.
  168. * Using the midpoint is easier since it lets the Bullet code manipulate the transforms
  169. * of the objects.
  170. * Code left for future programmers.
  171. // ==================================================================================
  172. // relative position normalized to the root prim
  173. OMV.Quaternion invThisOrientation = OMV.Quaternion.Inverse(rootPrim.Orientation);
  174. OMV.Vector3 childRelativePosition = (childPrim.Position - rootPrim.Position) * invThisOrientation;
  175. // relative rotation of the child to the parent
  176. OMV.Quaternion childRelativeRotation = invThisOrientation * childPrim.Orientation;
  177. OMV.Quaternion inverseChildRelativeRotation = OMV.Quaternion.Inverse(childRelativeRotation);
  178. DetailLog("{0},BSLinksetConstraint.PhysicallyLinkAChildToRoot,taint,root={1},child={2}", rootPrim.LocalID, rootPrim.LocalID, childPrim.LocalID);
  179. BS6DofConstraint constrain = new BS6DofConstraint(
  180. PhysicsScene.World, rootPrim.Body, childPrim.Body,
  181. OMV.Vector3.Zero,
  182. OMV.Quaternion.Inverse(rootPrim.Orientation),
  183. OMV.Vector3.Zero,
  184. OMV.Quaternion.Inverse(childPrim.Orientation),
  185. true,
  186. true
  187. );
  188. // ==================================================================================
  189. */
  190. m_physicsScene.Constraints.AddConstraint(constrain);
  191. // zero linear and angular limits makes the objects unable to move in relation to each other
  192. constrain.SetLinearLimits(OMV.Vector3.Zero, OMV.Vector3.Zero);
  193. constrain.SetAngularLimits(OMV.Vector3.Zero, OMV.Vector3.Zero);
  194. // tweek the constraint to increase stability
  195. constrain.UseFrameOffset(BSParam.LinkConstraintUseFrameOffset);
  196. constrain.TranslationalLimitMotor(BSParam.LinkConstraintEnableTransMotor,
  197. BSParam.LinkConstraintTransMotorMaxVel,
  198. BSParam.LinkConstraintTransMotorMaxForce);
  199. constrain.SetCFMAndERP(BSParam.LinkConstraintCFM, BSParam.LinkConstraintERP);
  200. if (BSParam.LinkConstraintSolverIterations != 0f)
  201. {
  202. constrain.SetSolverIterations(BSParam.LinkConstraintSolverIterations);
  203. }
  204. return constrain;
  205. }
  206. // Remove linkage between the linkset root and a particular child
  207. // The root and child bodies are passed in because we need to remove the constraint between
  208. // the bodies that were present at unlink time.
  209. // Called at taint time!
  210. private bool PhysicallyUnlinkAChildFromRoot(BSPrimLinkable rootPrim, BSPrimLinkable childPrim)
  211. {
  212. bool ret = false;
  213. DetailLog("{0},BSLinksetConstraint.PhysicallyUnlinkAChildFromRoot,taint,root={1},rBody={2},child={3},cBody={4}",
  214. rootPrim.LocalID,
  215. rootPrim.LocalID, rootPrim.PhysBody.AddrString,
  216. childPrim.LocalID, childPrim.PhysBody.AddrString);
  217. // Find the constraint for this link and get rid of it from the overall collection and from my list
  218. if (m_physicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.PhysBody, childPrim.PhysBody))
  219. {
  220. // Make the child refresh its location
  221. m_physicsScene.PE.PushUpdate(childPrim.PhysBody);
  222. ret = true;
  223. }
  224. return ret;
  225. }
  226. // Remove linkage between myself and any possible children I might have.
  227. // Returns 'true' of any constraints were destroyed.
  228. // Called at taint time!
  229. private bool PhysicallyUnlinkAllChildrenFromRoot(BSPrimLinkable rootPrim)
  230. {
  231. DetailLog("{0},BSLinksetConstraint.PhysicallyUnlinkAllChildren,taint", rootPrim.LocalID);
  232. return m_physicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.PhysBody);
  233. }
  234. // Call each of the constraints that make up this linkset and recompute the
  235. // various transforms and variables. Create constraints of not created yet.
  236. // Called before the simulation step to make sure the constraint based linkset
  237. // is all initialized.
  238. // Called at taint time!!
  239. private void RecomputeLinksetConstraints()
  240. {
  241. float linksetMass = LinksetMass;
  242. LinksetRoot.UpdatePhysicalMassProperties(linksetMass, true);
  243. DetailLog("{0},BSLinksetConstraint.RecomputeLinksetConstraints,set,rBody={1},linksetMass={2}",
  244. LinksetRoot.LocalID, LinksetRoot.PhysBody.AddrString, linksetMass);
  245. foreach (BSPrimLinkable child in m_children)
  246. {
  247. // A child in the linkset physically shows the mass of the whole linkset.
  248. // This allows Bullet to apply enough force on the child to move the whole linkset.
  249. // (Also do the mass stuff before recomputing the constraint so mass is not zero.)
  250. child.UpdatePhysicalMassProperties(linksetMass, true);
  251. BSConstraint constrain;
  252. if (!m_physicsScene.Constraints.TryGetConstraint(LinksetRoot.PhysBody, child.PhysBody, out constrain))
  253. {
  254. // If constraint doesn't exist yet, create it.
  255. constrain = BuildConstraint(LinksetRoot, child);
  256. }
  257. constrain.RecomputeConstraintVariables(linksetMass);
  258. // PhysicsScene.PE.DumpConstraint(PhysicsScene.World, constrain.Constraint); // DEBUG DEBUG
  259. }
  260. }
  261. }
  262. }