BSLinksetConstraints.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 class BSLinkInfoConstraint : BSLinkInfo
  37. {
  38. public ConstraintType constraintType;
  39. public BSConstraint constraint;
  40. public OMV.Vector3 linearLimitLow;
  41. public OMV.Vector3 linearLimitHigh;
  42. public OMV.Vector3 angularLimitLow;
  43. public OMV.Vector3 angularLimitHigh;
  44. public bool useFrameOffset;
  45. public bool enableTransMotor;
  46. public float transMotorMaxVel;
  47. public float transMotorMaxForce;
  48. public float cfm;
  49. public float erp;
  50. public float solverIterations;
  51. public BSLinkInfoConstraint(BSPrimLinkable pMember)
  52. : base(pMember)
  53. {
  54. constraint = null;
  55. ResetToFixedConstraint();
  56. }
  57. // Set all the parameters for this constraint to a fixed, non-movable constraint.
  58. public void ResetToFixedConstraint()
  59. {
  60. constraintType = ConstraintType.D6_CONSTRAINT_TYPE;
  61. linearLimitLow = OMV.Vector3.Zero;
  62. linearLimitHigh = OMV.Vector3.Zero;
  63. angularLimitLow = OMV.Vector3.Zero;
  64. angularLimitHigh = OMV.Vector3.Zero;
  65. useFrameOffset = BSParam.LinkConstraintUseFrameOffset;
  66. enableTransMotor = BSParam.LinkConstraintEnableTransMotor;
  67. transMotorMaxVel = BSParam.LinkConstraintTransMotorMaxVel;
  68. transMotorMaxForce = BSParam.LinkConstraintTransMotorMaxForce;
  69. cfm = BSParam.LinkConstraintCFM;
  70. erp = BSParam.LinkConstraintERP;
  71. solverIterations = BSParam.LinkConstraintSolverIterations;
  72. }
  73. // Given a constraint, apply the current constraint parameters to same.
  74. public void SetConstraintParameters(BSConstraint constrain)
  75. {
  76. switch (constraintType)
  77. {
  78. case ConstraintType.D6_CONSTRAINT_TYPE:
  79. BSConstraint6Dof constrain6dof = constrain as BSConstraint6Dof;
  80. if (constrain6dof != null)
  81. {
  82. // zero linear and angular limits makes the objects unable to move in relation to each other
  83. constrain6dof.SetLinearLimits(linearLimitLow, linearLimitHigh);
  84. constrain6dof.SetAngularLimits(angularLimitLow, angularLimitHigh);
  85. // tweek the constraint to increase stability
  86. constrain6dof.UseFrameOffset(useFrameOffset);
  87. constrain6dof.TranslationalLimitMotor(enableTransMotor, transMotorMaxVel, transMotorMaxForce);
  88. constrain6dof.SetCFMAndERP(cfm, erp);
  89. if (solverIterations != 0f)
  90. {
  91. constrain6dof.SetSolverIterations(solverIterations);
  92. }
  93. }
  94. break;
  95. default:
  96. break;
  97. }
  98. }
  99. }
  100. public BSLinksetConstraints(BSScene scene, BSPrimLinkable parent) : base(scene, parent)
  101. {
  102. LinksetImpl = LinksetImplementation.Constraint;
  103. }
  104. // When physical properties are changed the linkset needs to recalculate
  105. // its internal properties.
  106. // This is queued in the 'post taint' queue so the
  107. // refresh will happen once after all the other taints are applied.
  108. public override void Refresh(BSPrimLinkable requestor)
  109. {
  110. base.Refresh(requestor);
  111. }
  112. private void ScheduleRebuild(BSPrimLinkable requestor)
  113. {
  114. DetailLog("{0},BSLinksetConstraint.ScheduleRebuild,,rebuilding={1},hasChildren={2},actuallyScheduling={3}",
  115. requestor.LocalID, Rebuilding, HasAnyChildren, (!Rebuilding && HasAnyChildren));
  116. // When rebuilding, it is possible to set properties that would normally require a rebuild.
  117. // If already rebuilding, don't request another rebuild.
  118. // If a linkset with just a root prim (simple non-linked prim) don't bother rebuilding.
  119. if (!Rebuilding && HasAnyChildren)
  120. {
  121. // Queue to happen after all the other taint processing
  122. m_physicsScene.PostTaintObject("BSLinksetContraints.Refresh", requestor.LocalID, delegate()
  123. {
  124. if (HasAnyChildren)
  125. RecomputeLinksetConstraints();
  126. });
  127. }
  128. }
  129. // The object is going dynamic (physical). Do any setup necessary
  130. // for a dynamic linkset.
  131. // Only the state of the passed object can be modified. The rest of the linkset
  132. // has not yet been fully constructed.
  133. // Return 'true' if any properties updated on the passed object.
  134. // Called at taint-time!
  135. public override bool MakeDynamic(BSPrimLinkable child)
  136. {
  137. bool ret = false;
  138. DetailLog("{0},BSLinksetConstraints.MakeDynamic,call,IsRoot={1}", child.LocalID, IsRoot(child));
  139. if (IsRoot(child))
  140. {
  141. // The root is going dynamic. Rebuild the linkset so parts and mass get computed properly.
  142. ScheduleRebuild(LinksetRoot);
  143. }
  144. return ret;
  145. }
  146. // The object is going static (non-physical). Do any setup necessary for a static linkset.
  147. // Return 'true' if any properties updated on the passed object.
  148. // This doesn't normally happen -- OpenSim removes the objects from the physical
  149. // world if it is a static linkset.
  150. // Called at taint-time!
  151. public override bool MakeStatic(BSPrimLinkable child)
  152. {
  153. bool ret = false;
  154. DetailLog("{0},BSLinksetConstraint.MakeStatic,call,IsRoot={1}", child.LocalID, IsRoot(child));
  155. child.ClearDisplacement();
  156. if (IsRoot(child))
  157. {
  158. // Schedule a rebuild to verify that the root shape is set to the real shape.
  159. ScheduleRebuild(LinksetRoot);
  160. }
  161. return ret;
  162. }
  163. // Called at taint-time!!
  164. public override void UpdateProperties(UpdatedProperties whichUpdated, BSPrimLinkable pObj)
  165. {
  166. // Nothing to do for constraints on property updates
  167. }
  168. // Routine called when rebuilding the body of some member of the linkset.
  169. // Destroy all the constraints have have been made to root and set
  170. // up to rebuild the constraints before the next simulation step.
  171. // Returns 'true' of something was actually removed and would need restoring
  172. // Called at taint-time!!
  173. public override bool RemoveDependencies(BSPrimLinkable child)
  174. {
  175. bool ret = false;
  176. DetailLog("{0},BSLinksetConstraint.RemoveDependencies,removeChildrenForRoot,rID={1},rBody={2}",
  177. child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody.AddrString);
  178. lock (m_linksetActivityLock)
  179. {
  180. // Just undo all the constraints for this linkset. Rebuild at the end of the step.
  181. ret = PhysicallyUnlinkAllChildrenFromRoot(LinksetRoot);
  182. // Cause the constraints, et al to be rebuilt before the next simulation step.
  183. ScheduleRebuild(LinksetRoot);
  184. }
  185. return ret;
  186. }
  187. // ================================================================
  188. // Add a new child to the linkset.
  189. // Called while LinkActivity is locked.
  190. protected override void AddChildToLinkset(BSPrimLinkable child)
  191. {
  192. if (!HasChild(child))
  193. {
  194. m_children.Add(child, new BSLinkInfoConstraint(child));
  195. DetailLog("{0},BSLinksetConstraints.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID);
  196. // Cause constraints and assorted properties to be recomputed before the next simulation step.
  197. ScheduleRebuild(LinksetRoot);
  198. }
  199. return;
  200. }
  201. // Remove the specified child from the linkset.
  202. // Safe to call even if the child is not really in my linkset.
  203. protected override void RemoveChildFromLinkset(BSPrimLinkable child)
  204. {
  205. if (m_children.Remove(child))
  206. {
  207. BSPrimLinkable rootx = LinksetRoot; // capture the root and body as of now
  208. BSPrimLinkable childx = child;
  209. DetailLog("{0},BSLinksetConstraints.RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}",
  210. childx.LocalID,
  211. rootx.LocalID, rootx.PhysBody.AddrString,
  212. childx.LocalID, childx.PhysBody.AddrString);
  213. m_physicsScene.TaintedObject("BSLinksetConstraints.RemoveChildFromLinkset", delegate()
  214. {
  215. PhysicallyUnlinkAChildFromRoot(rootx, childx);
  216. });
  217. // See that the linkset parameters are recomputed at the end of the taint time.
  218. ScheduleRebuild(LinksetRoot);
  219. }
  220. else
  221. {
  222. // Non-fatal occurance.
  223. // PhysicsScene.Logger.ErrorFormat("{0}: Asked to remove child from linkset that was not in linkset", LogHeader);
  224. }
  225. return;
  226. }
  227. // Create a constraint between me (root of linkset) and the passed prim (the child).
  228. // Called at taint time!
  229. private void PhysicallyLinkAChildToRoot(BSPrimLinkable rootPrim, BSPrimLinkable childPrim)
  230. {
  231. // Don't build the constraint when asked. Put it off until just before the simulation step.
  232. Refresh(rootPrim);
  233. }
  234. // Create a static constraint between the two passed objects
  235. private BSConstraint BuildConstraint(BSPrimLinkable rootPrim, BSLinkInfo li)
  236. {
  237. BSLinkInfoConstraint liConstraint = li as BSLinkInfoConstraint;
  238. if (liConstraint == null)
  239. return null;
  240. // Zero motion for children so they don't interpolate
  241. li.member.ZeroMotion(true);
  242. BSConstraint constrain = null;
  243. switch (liConstraint.constraintType)
  244. {
  245. case ConstraintType.D6_CONSTRAINT_TYPE:
  246. // Relative position normalized to the root prim
  247. // Essentually a vector pointing from center of rootPrim to center of li.member
  248. OMV.Vector3 childRelativePosition = liConstraint.member.Position - rootPrim.Position;
  249. // real world coordinate of midpoint between the two objects
  250. OMV.Vector3 midPoint = rootPrim.Position + (childRelativePosition / 2);
  251. DetailLog("{0},BSLinksetConstraint.BuildConstraint,taint,root={1},rBody={2},child={3},cBody={4},rLoc={5},cLoc={6},midLoc={7}",
  252. rootPrim.LocalID,
  253. rootPrim.LocalID, rootPrim.PhysBody.AddrString,
  254. liConstraint.member.LocalID, liConstraint.member.PhysBody.AddrString,
  255. rootPrim.Position, liConstraint.member.Position, midPoint);
  256. // create a constraint that allows no freedom of movement between the two objects
  257. // http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=4818
  258. constrain = new BSConstraint6Dof(
  259. m_physicsScene.World, rootPrim.PhysBody, liConstraint.member.PhysBody, midPoint, true, true );
  260. /* NOTE: below is an attempt to build constraint with full frame computation, etc.
  261. * Using the midpoint is easier since it lets the Bullet code manipulate the transforms
  262. * of the objects.
  263. * Code left for future programmers.
  264. // ==================================================================================
  265. // relative position normalized to the root prim
  266. OMV.Quaternion invThisOrientation = OMV.Quaternion.Inverse(rootPrim.Orientation);
  267. OMV.Vector3 childRelativePosition = (liConstraint.member.Position - rootPrim.Position) * invThisOrientation;
  268. // relative rotation of the child to the parent
  269. OMV.Quaternion childRelativeRotation = invThisOrientation * liConstraint.member.Orientation;
  270. OMV.Quaternion inverseChildRelativeRotation = OMV.Quaternion.Inverse(childRelativeRotation);
  271. DetailLog("{0},BSLinksetConstraint.PhysicallyLinkAChildToRoot,taint,root={1},child={2}", rootPrim.LocalID, rootPrim.LocalID, liConstraint.member.LocalID);
  272. constrain = new BS6DofConstraint(
  273. PhysicsScene.World, rootPrim.Body, liConstraint.member.Body,
  274. OMV.Vector3.Zero,
  275. OMV.Quaternion.Inverse(rootPrim.Orientation),
  276. OMV.Vector3.Zero,
  277. OMV.Quaternion.Inverse(liConstraint.member.Orientation),
  278. true,
  279. true
  280. );
  281. // ==================================================================================
  282. */
  283. break;
  284. default:
  285. break;
  286. }
  287. liConstraint.SetConstraintParameters(constrain);
  288. m_physicsScene.Constraints.AddConstraint(constrain);
  289. return constrain;
  290. }
  291. // Remove linkage between the linkset root and a particular child
  292. // The root and child bodies are passed in because we need to remove the constraint between
  293. // the bodies that were present at unlink time.
  294. // Called at taint time!
  295. private bool PhysicallyUnlinkAChildFromRoot(BSPrimLinkable rootPrim, BSPrimLinkable childPrim)
  296. {
  297. bool ret = false;
  298. DetailLog("{0},BSLinksetConstraint.PhysicallyUnlinkAChildFromRoot,taint,root={1},rBody={2},child={3},cBody={4}",
  299. rootPrim.LocalID,
  300. rootPrim.LocalID, rootPrim.PhysBody.AddrString,
  301. childPrim.LocalID, childPrim.PhysBody.AddrString);
  302. // Find the constraint for this link and get rid of it from the overall collection and from my list
  303. if (m_physicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.PhysBody, childPrim.PhysBody))
  304. {
  305. // Make the child refresh its location
  306. m_physicsScene.PE.PushUpdate(childPrim.PhysBody);
  307. ret = true;
  308. }
  309. return ret;
  310. }
  311. // Remove linkage between myself and any possible children I might have.
  312. // Returns 'true' of any constraints were destroyed.
  313. // Called at taint time!
  314. private bool PhysicallyUnlinkAllChildrenFromRoot(BSPrimLinkable rootPrim)
  315. {
  316. DetailLog("{0},BSLinksetConstraint.PhysicallyUnlinkAllChildren,taint", rootPrim.LocalID);
  317. return m_physicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.PhysBody);
  318. }
  319. // Call each of the constraints that make up this linkset and recompute the
  320. // various transforms and variables. Create constraints of not created yet.
  321. // Called before the simulation step to make sure the constraint based linkset
  322. // is all initialized.
  323. // Called at taint time!!
  324. private void RecomputeLinksetConstraints()
  325. {
  326. float linksetMass = LinksetMass;
  327. LinksetRoot.UpdatePhysicalMassProperties(linksetMass, true);
  328. DetailLog("{0},BSLinksetConstraint.RecomputeLinksetConstraints,set,rBody={1},linksetMass={2}",
  329. LinksetRoot.LocalID, LinksetRoot.PhysBody.AddrString, linksetMass);
  330. try
  331. {
  332. Rebuilding = true;
  333. // There is no reason to build all this physical stuff for a non-physical linkset.
  334. if (!LinksetRoot.IsPhysicallyActive)
  335. {
  336. DetailLog("{0},BSLinksetConstraint.RecomputeLinksetCompound,notPhysical", LinksetRoot.LocalID);
  337. return; // Note the 'finally' clause at the botton which will get executed.
  338. }
  339. ForEachLinkInfo((li) =>
  340. {
  341. // A child in the linkset physically shows the mass of the whole linkset.
  342. // This allows Bullet to apply enough force on the child to move the whole linkset.
  343. // (Also do the mass stuff before recomputing the constraint so mass is not zero.)
  344. li.member.UpdatePhysicalMassProperties(linksetMass, true);
  345. BSConstraint constrain;
  346. if (!m_physicsScene.Constraints.TryGetConstraint(LinksetRoot.PhysBody, li.member.PhysBody, out constrain))
  347. {
  348. // If constraint doesn't exist yet, create it.
  349. constrain = BuildConstraint(LinksetRoot, li);
  350. }
  351. constrain.RecomputeConstraintVariables(linksetMass);
  352. // PhysicsScene.PE.DumpConstraint(PhysicsScene.World, constrain.Constraint); // DEBUG DEBUG
  353. return false; // 'false' says to keep processing other members
  354. });
  355. }
  356. finally
  357. {
  358. Rebuilding = false;
  359. }
  360. }
  361. }
  362. }