BSLinksetConstraints.cs 15 KB

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