BSLinksetConstraints.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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.PhysicsModule.BulletS
  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. //
  52. public OMV.Vector3 frameInAloc;
  53. public OMV.Quaternion frameInArot;
  54. public OMV.Vector3 frameInBloc;
  55. public OMV.Quaternion frameInBrot;
  56. public bool useLinearReferenceFrameA;
  57. // Spring
  58. public bool[] springAxisEnable;
  59. public float[] springDamping;
  60. public float[] springStiffness;
  61. public OMV.Vector3 springLinearEquilibriumPoint;
  62. public OMV.Vector3 springAngularEquilibriumPoint;
  63. public BSLinkInfoConstraint(BSPrimLinkable pMember)
  64. : base(pMember)
  65. {
  66. constraint = null;
  67. ResetLink();
  68. member.PhysScene.DetailLog("{0},BSLinkInfoConstraint.creation", member.LocalID);
  69. }
  70. // Set all the parameters for this constraint to a fixed, non-movable constraint.
  71. public override void ResetLink()
  72. {
  73. // constraintType = ConstraintType.D6_CONSTRAINT_TYPE;
  74. constraintType = ConstraintType.BS_FIXED_CONSTRAINT_TYPE;
  75. linearLimitLow = OMV.Vector3.Zero;
  76. linearLimitHigh = OMV.Vector3.Zero;
  77. angularLimitLow = OMV.Vector3.Zero;
  78. angularLimitHigh = OMV.Vector3.Zero;
  79. useFrameOffset = BSParam.LinkConstraintUseFrameOffset;
  80. enableTransMotor = BSParam.LinkConstraintEnableTransMotor;
  81. transMotorMaxVel = BSParam.LinkConstraintTransMotorMaxVel;
  82. transMotorMaxForce = BSParam.LinkConstraintTransMotorMaxForce;
  83. cfm = BSParam.LinkConstraintCFM;
  84. erp = BSParam.LinkConstraintERP;
  85. solverIterations = BSParam.LinkConstraintSolverIterations;
  86. frameInAloc = OMV.Vector3.Zero;
  87. frameInArot = OMV.Quaternion.Identity;
  88. frameInBloc = OMV.Vector3.Zero;
  89. frameInBrot = OMV.Quaternion.Identity;
  90. useLinearReferenceFrameA = true;
  91. springAxisEnable = new bool[6];
  92. springDamping = new float[6];
  93. springStiffness = new float[6];
  94. for (int ii = 0; ii < springAxisEnable.Length; ii++)
  95. {
  96. springAxisEnable[ii] = false;
  97. springDamping[ii] = BSAPITemplate.SPRING_NOT_SPECIFIED;
  98. springStiffness[ii] = BSAPITemplate.SPRING_NOT_SPECIFIED;
  99. }
  100. springLinearEquilibriumPoint = OMV.Vector3.Zero;
  101. springAngularEquilibriumPoint = OMV.Vector3.Zero;
  102. member.PhysScene.DetailLog("{0},BSLinkInfoConstraint.ResetLink", member.LocalID);
  103. }
  104. // Given a constraint, apply the current constraint parameters to same.
  105. public override void SetLinkParameters(BSConstraint constrain)
  106. {
  107. member.PhysScene.DetailLog("{0},BSLinkInfoConstraint.SetLinkParameters,type={1}", member.LocalID, constraintType);
  108. switch (constraintType)
  109. {
  110. case ConstraintType.BS_FIXED_CONSTRAINT_TYPE:
  111. case ConstraintType.D6_CONSTRAINT_TYPE:
  112. BSConstraint6Dof constrain6dof = constrain as BSConstraint6Dof;
  113. if (constrain6dof != null)
  114. {
  115. // NOTE: D6_SPRING_CONSTRAINT_TYPE should be updated if you change any of this code.
  116. // zero linear and angular limits makes the objects unable to move in relation to each other
  117. constrain6dof.SetLinearLimits(linearLimitLow, linearLimitHigh);
  118. constrain6dof.SetAngularLimits(angularLimitLow, angularLimitHigh);
  119. // tweek the constraint to increase stability
  120. constrain6dof.UseFrameOffset(useFrameOffset);
  121. constrain6dof.TranslationalLimitMotor(enableTransMotor, transMotorMaxVel, transMotorMaxForce);
  122. constrain6dof.SetCFMAndERP(cfm, erp);
  123. if (solverIterations != 0f)
  124. {
  125. constrain6dof.SetSolverIterations(solverIterations);
  126. }
  127. }
  128. break;
  129. case ConstraintType.D6_SPRING_CONSTRAINT_TYPE:
  130. BSConstraintSpring constrainSpring = constrain as BSConstraintSpring;
  131. if (constrainSpring != null)
  132. {
  133. // zero linear and angular limits makes the objects unable to move in relation to each other
  134. constrainSpring.SetLinearLimits(linearLimitLow, linearLimitHigh);
  135. constrainSpring.SetAngularLimits(angularLimitLow, angularLimitHigh);
  136. // tweek the constraint to increase stability
  137. constrainSpring.UseFrameOffset(useFrameOffset);
  138. constrainSpring.TranslationalLimitMotor(enableTransMotor, transMotorMaxVel, transMotorMaxForce);
  139. constrainSpring.SetCFMAndERP(cfm, erp);
  140. if (solverIterations != 0f)
  141. {
  142. constrainSpring.SetSolverIterations(solverIterations);
  143. }
  144. for (int ii = 0; ii < springAxisEnable.Length; ii++)
  145. {
  146. constrainSpring.SetAxisEnable(ii, springAxisEnable[ii]);
  147. if (springDamping[ii] != BSAPITemplate.SPRING_NOT_SPECIFIED)
  148. constrainSpring.SetDamping(ii, springDamping[ii]);
  149. if (springStiffness[ii] != BSAPITemplate.SPRING_NOT_SPECIFIED)
  150. constrainSpring.SetStiffness(ii, springStiffness[ii]);
  151. }
  152. constrainSpring.CalculateTransforms();
  153. if (springLinearEquilibriumPoint != OMV.Vector3.Zero)
  154. constrainSpring.SetEquilibriumPoint(springLinearEquilibriumPoint, springAngularEquilibriumPoint);
  155. else
  156. constrainSpring.SetEquilibriumPoint(BSAPITemplate.SPRING_NOT_SPECIFIED, BSAPITemplate.SPRING_NOT_SPECIFIED);
  157. }
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. // Return 'true' if the property updates from the physics engine should be reported
  164. // to the simulator.
  165. // If the constraint is fixed, we don't need to report as the simulator and viewer will
  166. // report the right things.
  167. public override bool ShouldUpdateChildProperties()
  168. {
  169. bool ret = true;
  170. if (constraintType == ConstraintType.BS_FIXED_CONSTRAINT_TYPE)
  171. ret = false;
  172. return ret;
  173. }
  174. }
  175. public BSLinksetConstraints(BSScene scene, BSPrimLinkable parent) : base(scene, parent)
  176. {
  177. LinksetImpl = LinksetImplementation.Constraint;
  178. }
  179. private static string LogHeader = "[BULLETSIM LINKSET CONSTRAINT]";
  180. // When physical properties are changed the linkset needs to recalculate
  181. // its internal properties.
  182. // This is queued in the 'post taint' queue so the
  183. // refresh will happen once after all the other taints are applied.
  184. public override void Refresh(BSPrimLinkable requestor)
  185. {
  186. ScheduleRebuild(requestor);
  187. base.Refresh(requestor);
  188. }
  189. private void ScheduleRebuild(BSPrimLinkable requestor)
  190. {
  191. DetailLog("{0},BSLinksetConstraint.ScheduleRebuild,,rebuilding={1},hasChildren={2},actuallyScheduling={3}",
  192. requestor.LocalID, Rebuilding, HasAnyChildren, (!Rebuilding && HasAnyChildren));
  193. // When rebuilding, it is possible to set properties that would normally require a rebuild.
  194. // If already rebuilding, don't request another rebuild.
  195. // If a linkset with just a root prim (simple non-linked prim) don't bother rebuilding.
  196. lock (this)
  197. {
  198. if (!RebuildScheduled)
  199. {
  200. if (!Rebuilding && HasAnyChildren)
  201. {
  202. RebuildScheduled = true;
  203. // Queue to happen after all the other taint processing
  204. m_physicsScene.PostTaintObject("BSLinksetContraints.Refresh", requestor.LocalID, delegate()
  205. {
  206. if (HasAnyChildren)
  207. {
  208. // Constraints that have not been changed are not rebuild but make sure
  209. // the constraint of the requestor is rebuilt.
  210. PhysicallyUnlinkAChildFromRoot(LinksetRoot, requestor);
  211. // Rebuild the linkset and all its constraints.
  212. RecomputeLinksetConstraints();
  213. }
  214. RebuildScheduled = false;
  215. });
  216. }
  217. }
  218. }
  219. }
  220. // The object is going dynamic (physical). Do any setup necessary
  221. // for a dynamic linkset.
  222. // Only the state of the passed object can be modified. The rest of the linkset
  223. // has not yet been fully constructed.
  224. // Return 'true' if any properties updated on the passed object.
  225. // Called at taint-time!
  226. public override bool MakeDynamic(BSPrimLinkable child)
  227. {
  228. bool ret = false;
  229. DetailLog("{0},BSLinksetConstraints.MakeDynamic,call,IsRoot={1}", child.LocalID, IsRoot(child));
  230. if (IsRoot(child))
  231. {
  232. // The root is going dynamic. Rebuild the linkset so parts and mass get computed properly.
  233. Refresh(LinksetRoot);
  234. }
  235. return ret;
  236. }
  237. // The object is going static (non-physical). Do any setup necessary for a static linkset.
  238. // Return 'true' if any properties updated on the passed object.
  239. // This doesn't normally happen -- OpenSim removes the objects from the physical
  240. // world if it is a static linkset.
  241. // Called at taint-time!
  242. public override bool MakeStatic(BSPrimLinkable child)
  243. {
  244. bool ret = false;
  245. DetailLog("{0},BSLinksetConstraint.MakeStatic,call,IsRoot={1}", child.LocalID, IsRoot(child));
  246. child.ClearDisplacement();
  247. if (IsRoot(child))
  248. {
  249. // Schedule a rebuild to verify that the root shape is set to the real shape.
  250. Refresh(LinksetRoot);
  251. }
  252. return ret;
  253. }
  254. // Called at taint-time!!
  255. public override void UpdateProperties(UpdatedProperties whichUpdated, BSPrimLinkable pObj)
  256. {
  257. // Nothing to do for constraints on property updates
  258. }
  259. // Routine called when rebuilding the body of some member of the linkset.
  260. // Destroy all the constraints have have been made to root and set
  261. // up to rebuild the constraints before the next simulation step.
  262. // Returns 'true' of something was actually removed and would need restoring
  263. // Called at taint-time!!
  264. public override bool RemoveDependencies(BSPrimLinkable child)
  265. {
  266. bool ret = false;
  267. DetailLog("{0},BSLinksetConstraint.RemoveDependencies,removeChildrenForRoot,rID={1},rBody={2}",
  268. child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody.AddrString);
  269. lock (m_linksetActivityLock)
  270. {
  271. // Just undo all the constraints for this linkset. Rebuild at the end of the step.
  272. ret = PhysicallyUnlinkAllChildrenFromRoot(LinksetRoot);
  273. // Cause the constraints, et al to be rebuilt before the next simulation step.
  274. Refresh(LinksetRoot);
  275. }
  276. return ret;
  277. }
  278. // ================================================================
  279. // Add a new child to the linkset.
  280. // Called while LinkActivity is locked.
  281. protected override void AddChildToLinkset(BSPrimLinkable child)
  282. {
  283. if (!HasChild(child))
  284. {
  285. m_children.Add(child, new BSLinkInfoConstraint(child));
  286. DetailLog("{0},BSLinksetConstraints.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID);
  287. // Cause constraints and assorted properties to be recomputed before the next simulation step.
  288. Refresh(LinksetRoot);
  289. }
  290. return;
  291. }
  292. // Remove the specified child from the linkset.
  293. // Safe to call even if the child is not really in my linkset.
  294. protected override void RemoveChildFromLinkset(BSPrimLinkable child, bool inTaintTime)
  295. {
  296. if (m_children.Remove(child))
  297. {
  298. BSPrimLinkable rootx = LinksetRoot; // capture the root and body as of now
  299. BSPrimLinkable childx = child;
  300. DetailLog("{0},BSLinksetConstraints.RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}",
  301. childx.LocalID,
  302. rootx.LocalID, rootx.PhysBody.AddrString,
  303. childx.LocalID, childx.PhysBody.AddrString);
  304. m_physicsScene.TaintedObject(inTaintTime, childx.LocalID, "BSLinksetConstraints.RemoveChildFromLinkset", delegate()
  305. {
  306. PhysicallyUnlinkAChildFromRoot(rootx, childx);
  307. });
  308. // See that the linkset parameters are recomputed at the end of the taint time.
  309. Refresh(LinksetRoot);
  310. }
  311. else
  312. {
  313. // Non-fatal occurance.
  314. // PhysicsScene.Logger.ErrorFormat("{0}: Asked to remove child from linkset that was not in linkset", LogHeader);
  315. }
  316. return;
  317. }
  318. // Create a constraint between me (root of linkset) and the passed prim (the child).
  319. // Called at taint time!
  320. private void PhysicallyLinkAChildToRoot(BSPrimLinkable rootPrim, BSPrimLinkable childPrim)
  321. {
  322. // Don't build the constraint when asked. Put it off until just before the simulation step.
  323. Refresh(rootPrim);
  324. }
  325. // Create a static constraint between the two passed objects
  326. private BSConstraint BuildConstraint(BSPrimLinkable rootPrim, BSLinkInfo li)
  327. {
  328. BSLinkInfoConstraint linkInfo = li as BSLinkInfoConstraint;
  329. if (linkInfo == null)
  330. return null;
  331. // Zero motion for children so they don't interpolate
  332. li.member.ZeroMotion(true);
  333. BSConstraint constrain = null;
  334. switch (linkInfo.constraintType)
  335. {
  336. case ConstraintType.BS_FIXED_CONSTRAINT_TYPE:
  337. case ConstraintType.D6_CONSTRAINT_TYPE:
  338. // Relative position normalized to the root prim
  339. // Essentually a vector pointing from center of rootPrim to center of li.member
  340. OMV.Vector3 childRelativePosition = linkInfo.member.Position - rootPrim.Position;
  341. // real world coordinate of midpoint between the two objects
  342. OMV.Vector3 midPoint = rootPrim.Position + (childRelativePosition / 2);
  343. DetailLog("{0},BSLinksetConstraint.BuildConstraint,6Dof,rBody={1},cBody={2},rLoc={3},cLoc={4},midLoc={5}",
  344. rootPrim.LocalID, rootPrim.PhysBody, linkInfo.member.PhysBody,
  345. rootPrim.Position, linkInfo.member.Position, midPoint);
  346. // create a constraint that allows no freedom of movement between the two objects
  347. // http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=4818
  348. constrain = new BSConstraint6Dof(
  349. m_physicsScene.World, rootPrim.PhysBody, linkInfo.member.PhysBody, midPoint, true, true );
  350. /* NOTE: below is an attempt to build constraint with full frame computation, etc.
  351. * Using the midpoint is easier since it lets the Bullet code manipulate the transforms
  352. * of the objects.
  353. * Code left for future programmers.
  354. // ==================================================================================
  355. // relative position normalized to the root prim
  356. OMV.Quaternion invThisOrientation = OMV.Quaternion.Inverse(rootPrim.Orientation);
  357. OMV.Vector3 childRelativePosition = (liConstraint.member.Position - rootPrim.Position) * invThisOrientation;
  358. // relative rotation of the child to the parent
  359. OMV.Quaternion childRelativeRotation = invThisOrientation * liConstraint.member.Orientation;
  360. OMV.Quaternion inverseChildRelativeRotation = OMV.Quaternion.Inverse(childRelativeRotation);
  361. DetailLog("{0},BSLinksetConstraint.PhysicallyLinkAChildToRoot,taint,root={1},child={2}", rootPrim.LocalID, rootPrim.LocalID, liConstraint.member.LocalID);
  362. constrain = new BS6DofConstraint(
  363. PhysicsScene.World, rootPrim.Body, liConstraint.member.Body,
  364. OMV.Vector3.Zero,
  365. OMV.Quaternion.Inverse(rootPrim.Orientation),
  366. OMV.Vector3.Zero,
  367. OMV.Quaternion.Inverse(liConstraint.member.Orientation),
  368. true,
  369. true
  370. );
  371. // ==================================================================================
  372. */
  373. break;
  374. case ConstraintType.D6_SPRING_CONSTRAINT_TYPE:
  375. constrain = new BSConstraintSpring(m_physicsScene.World, rootPrim.PhysBody, linkInfo.member.PhysBody,
  376. linkInfo.frameInAloc, linkInfo.frameInArot, linkInfo.frameInBloc, linkInfo.frameInBrot,
  377. linkInfo.useLinearReferenceFrameA,
  378. true /*disableCollisionsBetweenLinkedBodies*/);
  379. DetailLog("{0},BSLinksetConstraint.BuildConstraint,spring,root={1},rBody={2},child={3},cBody={4},rLoc={5},cLoc={6}",
  380. rootPrim.LocalID,
  381. rootPrim.LocalID, rootPrim.PhysBody.AddrString,
  382. linkInfo.member.LocalID, linkInfo.member.PhysBody.AddrString,
  383. rootPrim.Position, linkInfo.member.Position);
  384. break;
  385. default:
  386. break;
  387. }
  388. linkInfo.SetLinkParameters(constrain);
  389. m_physicsScene.Constraints.AddConstraint(constrain);
  390. return constrain;
  391. }
  392. // Remove linkage between the linkset root and a particular child
  393. // The root and child bodies are passed in because we need to remove the constraint between
  394. // the bodies that were present at unlink time.
  395. // Called at taint time!
  396. private bool PhysicallyUnlinkAChildFromRoot(BSPrimLinkable rootPrim, BSPrimLinkable childPrim)
  397. {
  398. bool ret = false;
  399. DetailLog("{0},BSLinksetConstraint.PhysicallyUnlinkAChildFromRoot,taint,root={1},rBody={2},child={3},cBody={4}",
  400. rootPrim.LocalID,
  401. rootPrim.LocalID, rootPrim.PhysBody.AddrString,
  402. childPrim.LocalID, childPrim.PhysBody.AddrString);
  403. // If asked to unlink root from root, just remove all the constraints
  404. if (rootPrim == childPrim || childPrim == LinksetRoot)
  405. {
  406. PhysicallyUnlinkAllChildrenFromRoot(LinksetRoot);
  407. ret = true;
  408. }
  409. else
  410. {
  411. // Find the constraint for this link and get rid of it from the overall collection and from my list
  412. if (m_physicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.PhysBody, childPrim.PhysBody))
  413. {
  414. // Make the child refresh its location
  415. m_physicsScene.PE.PushUpdate(childPrim.PhysBody);
  416. ret = true;
  417. }
  418. }
  419. return ret;
  420. }
  421. // Remove linkage between myself and any possible children I might have.
  422. // Returns 'true' of any constraints were destroyed.
  423. // Called at taint time!
  424. private bool PhysicallyUnlinkAllChildrenFromRoot(BSPrimLinkable rootPrim)
  425. {
  426. DetailLog("{0},BSLinksetConstraint.PhysicallyUnlinkAllChildren,taint", rootPrim.LocalID);
  427. return m_physicsScene.Constraints.RemoveAndDestroyConstraint(rootPrim.PhysBody);
  428. }
  429. // Call each of the constraints that make up this linkset and recompute the
  430. // various transforms and variables. Create constraints of not created yet.
  431. // Called before the simulation step to make sure the constraint based linkset
  432. // is all initialized.
  433. // Called at taint time!!
  434. private void RecomputeLinksetConstraints()
  435. {
  436. float linksetMass = LinksetMass;
  437. LinksetRoot.UpdatePhysicalMassProperties(linksetMass, true);
  438. DetailLog("{0},BSLinksetConstraint.RecomputeLinksetConstraints,set,rBody={1},linksetMass={2}",
  439. LinksetRoot.LocalID, LinksetRoot.PhysBody.AddrString, linksetMass);
  440. try
  441. {
  442. Rebuilding = true;
  443. // There is no reason to build all this physical stuff for a non-physical linkset.
  444. if (!LinksetRoot.IsPhysicallyActive || !HasAnyChildren)
  445. {
  446. DetailLog("{0},BSLinksetConstraint.RecomputeLinksetCompound,notPhysicalOrNoChildren", LinksetRoot.LocalID);
  447. return; // Note the 'finally' clause at the botton which will get executed.
  448. }
  449. ForEachLinkInfo((li) =>
  450. {
  451. // A child in the linkset physically shows the mass of the whole linkset.
  452. // This allows Bullet to apply enough force on the child to move the whole linkset.
  453. // (Also do the mass stuff before recomputing the constraint so mass is not zero.)
  454. li.member.UpdatePhysicalMassProperties(linksetMass, true);
  455. BSConstraint constrain;
  456. if (!m_physicsScene.Constraints.TryGetConstraint(LinksetRoot.PhysBody, li.member.PhysBody, out constrain))
  457. {
  458. // If constraint doesn't exist yet, create it.
  459. constrain = BuildConstraint(LinksetRoot, li);
  460. }
  461. li.SetLinkParameters(constrain);
  462. constrain.RecomputeConstraintVariables(linksetMass);
  463. // PhysicsScene.PE.DumpConstraint(PhysicsScene.World, constrain.Constraint); // DEBUG DEBUG
  464. return false; // 'false' says to keep processing other members
  465. });
  466. }
  467. finally
  468. {
  469. Rebuilding = false;
  470. }
  471. }
  472. #region Extension
  473. public override object Extension(string pFunct, params object[] pParams)
  474. {
  475. object ret = null;
  476. switch (pFunct)
  477. {
  478. // pParams = [ BSPhysObject root, BSPhysObject child, integer linkType ]
  479. case ExtendedPhysics.PhysFunctChangeLinkType:
  480. if (pParams.Length > 2)
  481. {
  482. int requestedType = (int)pParams[2];
  483. DetailLog("{0},BSLinksetConstraint.ChangeLinkType,requestedType={1}", LinksetRoot.LocalID, requestedType);
  484. if (requestedType == (int)ConstraintType.BS_FIXED_CONSTRAINT_TYPE
  485. || requestedType == (int)ConstraintType.D6_CONSTRAINT_TYPE
  486. || requestedType == (int)ConstraintType.D6_SPRING_CONSTRAINT_TYPE
  487. || requestedType == (int)ConstraintType.HINGE_CONSTRAINT_TYPE
  488. || requestedType == (int)ConstraintType.CONETWIST_CONSTRAINT_TYPE
  489. || requestedType == (int)ConstraintType.SLIDER_CONSTRAINT_TYPE)
  490. {
  491. BSPrimLinkable child = pParams[1] as BSPrimLinkable;
  492. if (child != null)
  493. {
  494. DetailLog("{0},BSLinksetConstraint.ChangeLinkType,rootID={1},childID={2},type={3}",
  495. LinksetRoot.LocalID, LinksetRoot.LocalID, child.LocalID, requestedType);
  496. m_physicsScene.TaintedObject(child.LocalID, "BSLinksetConstraint.PhysFunctChangeLinkType", delegate()
  497. {
  498. // Pick up all the constraints currently created.
  499. RemoveDependencies(child);
  500. BSLinkInfo linkInfo = null;
  501. if (TryGetLinkInfo(child, out linkInfo))
  502. {
  503. BSLinkInfoConstraint linkInfoC = linkInfo as BSLinkInfoConstraint;
  504. if (linkInfoC != null)
  505. {
  506. linkInfoC.constraintType = (ConstraintType)requestedType;
  507. ret = (object)true;
  508. DetailLog("{0},BSLinksetConstraint.ChangeLinkType,link={1},type={2}",
  509. linkInfo.member.LocalID, linkInfo.member.LocalID, linkInfoC.constraintType);
  510. }
  511. else
  512. {
  513. DetailLog("{0},BSLinksetConstraint.ChangeLinkType,linkInfoNotConstraint,childID={1}", LinksetRoot.LocalID, child.LocalID);
  514. }
  515. }
  516. else
  517. {
  518. DetailLog("{0},BSLinksetConstraint.ChangeLinkType,noLinkInfoForChild,childID={1}", LinksetRoot.LocalID, child.LocalID);
  519. }
  520. // Cause the whole linkset to be rebuilt in post-taint time.
  521. Refresh(child);
  522. });
  523. }
  524. else
  525. {
  526. DetailLog("{0},BSLinksetConstraint.SetLinkType,childNotBSPrimLinkable", LinksetRoot.LocalID);
  527. }
  528. }
  529. else
  530. {
  531. DetailLog("{0},BSLinksetConstraint.SetLinkType,illegalRequestedType,reqested={1},spring={2}",
  532. LinksetRoot.LocalID, requestedType, ((int)ConstraintType.D6_SPRING_CONSTRAINT_TYPE));
  533. }
  534. }
  535. break;
  536. // pParams = [ BSPhysObject root, BSPhysObject child ]
  537. case ExtendedPhysics.PhysFunctGetLinkType:
  538. if (pParams.Length > 0)
  539. {
  540. BSPrimLinkable child = pParams[1] as BSPrimLinkable;
  541. if (child != null)
  542. {
  543. BSLinkInfo linkInfo = null;
  544. if (TryGetLinkInfo(child, out linkInfo))
  545. {
  546. BSLinkInfoConstraint linkInfoC = linkInfo as BSLinkInfoConstraint;
  547. if (linkInfoC != null)
  548. {
  549. ret = (object)(int)linkInfoC.constraintType;
  550. DetailLog("{0},BSLinksetConstraint.GetLinkType,link={1},type={2}",
  551. linkInfo.member.LocalID, linkInfo.member.LocalID, linkInfoC.constraintType);
  552. }
  553. }
  554. }
  555. }
  556. break;
  557. // pParams = [ BSPhysObject root, BSPhysObject child, int op, object opParams, int op, object opParams, ... ]
  558. case ExtendedPhysics.PhysFunctChangeLinkParams:
  559. // There should be two parameters: the childActor and a list of parameters to set
  560. if (pParams.Length > 2)
  561. {
  562. BSPrimLinkable child = pParams[1] as BSPrimLinkable;
  563. BSLinkInfo baseLinkInfo = null;
  564. if (TryGetLinkInfo(child, out baseLinkInfo))
  565. {
  566. BSLinkInfoConstraint linkInfo = baseLinkInfo as BSLinkInfoConstraint;
  567. if (linkInfo != null)
  568. {
  569. int valueInt;
  570. float valueFloat;
  571. bool valueBool;
  572. OMV.Vector3 valueVector;
  573. OMV.Vector3 valueVector2;
  574. OMV.Quaternion valueQuaternion;
  575. int axisLow, axisHigh;
  576. int opIndex = 2;
  577. while (opIndex < pParams.Length)
  578. {
  579. int thisOp = 0;
  580. string errMsg = "";
  581. try
  582. {
  583. thisOp = (int)pParams[opIndex];
  584. DetailLog("{0},BSLinksetConstraint.ChangeLinkParams2,op={1},val={2}",
  585. linkInfo.member.LocalID, thisOp, pParams[opIndex + 1]);
  586. switch (thisOp)
  587. {
  588. case ExtendedPhysics.PHYS_PARAM_LINK_TYPE:
  589. valueInt = (int)pParams[opIndex + 1];
  590. ConstraintType valueType = (ConstraintType)valueInt;
  591. if (valueType == ConstraintType.BS_FIXED_CONSTRAINT_TYPE
  592. || valueType == ConstraintType.D6_CONSTRAINT_TYPE
  593. || valueType == ConstraintType.D6_SPRING_CONSTRAINT_TYPE
  594. || valueType == ConstraintType.HINGE_CONSTRAINT_TYPE
  595. || valueType == ConstraintType.CONETWIST_CONSTRAINT_TYPE
  596. || valueType == ConstraintType.SLIDER_CONSTRAINT_TYPE)
  597. {
  598. linkInfo.constraintType = valueType;
  599. }
  600. opIndex += 2;
  601. break;
  602. case ExtendedPhysics.PHYS_PARAM_FRAMEINA_LOC:
  603. errMsg = "PHYS_PARAM_FRAMEINA_LOC takes one parameter of type vector";
  604. valueVector = (OMV.Vector3)pParams[opIndex + 1];
  605. linkInfo.frameInAloc = valueVector;
  606. opIndex += 2;
  607. break;
  608. case ExtendedPhysics.PHYS_PARAM_FRAMEINA_ROT:
  609. errMsg = "PHYS_PARAM_FRAMEINA_ROT takes one parameter of type rotation";
  610. valueQuaternion = (OMV.Quaternion)pParams[opIndex + 1];
  611. linkInfo.frameInArot = valueQuaternion;
  612. opIndex += 2;
  613. break;
  614. case ExtendedPhysics.PHYS_PARAM_FRAMEINB_LOC:
  615. errMsg = "PHYS_PARAM_FRAMEINB_LOC takes one parameter of type vector";
  616. valueVector = (OMV.Vector3)pParams[opIndex + 1];
  617. linkInfo.frameInBloc = valueVector;
  618. opIndex += 2;
  619. break;
  620. case ExtendedPhysics.PHYS_PARAM_FRAMEINB_ROT:
  621. errMsg = "PHYS_PARAM_FRAMEINB_ROT takes one parameter of type rotation";
  622. valueQuaternion = (OMV.Quaternion)pParams[opIndex + 1];
  623. linkInfo.frameInBrot = valueQuaternion;
  624. opIndex += 2;
  625. break;
  626. case ExtendedPhysics.PHYS_PARAM_LINEAR_LIMIT_LOW:
  627. errMsg = "PHYS_PARAM_LINEAR_LIMIT_LOW takes one parameter of type vector";
  628. valueVector = (OMV.Vector3)pParams[opIndex + 1];
  629. linkInfo.linearLimitLow = valueVector;
  630. opIndex += 2;
  631. break;
  632. case ExtendedPhysics.PHYS_PARAM_LINEAR_LIMIT_HIGH:
  633. errMsg = "PHYS_PARAM_LINEAR_LIMIT_HIGH takes one parameter of type vector";
  634. valueVector = (OMV.Vector3)pParams[opIndex + 1];
  635. linkInfo.linearLimitHigh = valueVector;
  636. opIndex += 2;
  637. break;
  638. case ExtendedPhysics.PHYS_PARAM_ANGULAR_LIMIT_LOW:
  639. errMsg = "PHYS_PARAM_ANGULAR_LIMIT_LOW takes one parameter of type vector";
  640. valueVector = (OMV.Vector3)pParams[opIndex + 1];
  641. linkInfo.angularLimitLow = valueVector;
  642. opIndex += 2;
  643. break;
  644. case ExtendedPhysics.PHYS_PARAM_ANGULAR_LIMIT_HIGH:
  645. errMsg = "PHYS_PARAM_ANGULAR_LIMIT_HIGH takes one parameter of type vector";
  646. valueVector = (OMV.Vector3)pParams[opIndex + 1];
  647. linkInfo.angularLimitHigh = valueVector;
  648. opIndex += 2;
  649. break;
  650. case ExtendedPhysics.PHYS_PARAM_USE_FRAME_OFFSET:
  651. errMsg = "PHYS_PARAM_USE_FRAME_OFFSET takes one parameter of type integer (bool)";
  652. valueBool = ((int)pParams[opIndex + 1]) != 0;
  653. linkInfo.useFrameOffset = valueBool;
  654. opIndex += 2;
  655. break;
  656. case ExtendedPhysics.PHYS_PARAM_ENABLE_TRANSMOTOR:
  657. errMsg = "PHYS_PARAM_ENABLE_TRANSMOTOR takes one parameter of type integer (bool)";
  658. valueBool = ((int)pParams[opIndex + 1]) != 0;
  659. linkInfo.enableTransMotor = valueBool;
  660. opIndex += 2;
  661. break;
  662. case ExtendedPhysics.PHYS_PARAM_TRANSMOTOR_MAXVEL:
  663. errMsg = "PHYS_PARAM_TRANSMOTOR_MAXVEL takes one parameter of type float";
  664. valueFloat = (float)pParams[opIndex + 1];
  665. linkInfo.transMotorMaxVel = valueFloat;
  666. opIndex += 2;
  667. break;
  668. case ExtendedPhysics.PHYS_PARAM_TRANSMOTOR_MAXFORCE:
  669. errMsg = "PHYS_PARAM_TRANSMOTOR_MAXFORCE takes one parameter of type float";
  670. valueFloat = (float)pParams[opIndex + 1];
  671. linkInfo.transMotorMaxForce = valueFloat;
  672. opIndex += 2;
  673. break;
  674. case ExtendedPhysics.PHYS_PARAM_CFM:
  675. errMsg = "PHYS_PARAM_CFM takes one parameter of type float";
  676. valueFloat = (float)pParams[opIndex + 1];
  677. linkInfo.cfm = valueFloat;
  678. opIndex += 2;
  679. break;
  680. case ExtendedPhysics.PHYS_PARAM_ERP:
  681. errMsg = "PHYS_PARAM_ERP takes one parameter of type float";
  682. valueFloat = (float)pParams[opIndex + 1];
  683. linkInfo.erp = valueFloat;
  684. opIndex += 2;
  685. break;
  686. case ExtendedPhysics.PHYS_PARAM_SOLVER_ITERATIONS:
  687. errMsg = "PHYS_PARAM_SOLVER_ITERATIONS takes one parameter of type float";
  688. valueFloat = (float)pParams[opIndex + 1];
  689. linkInfo.solverIterations = valueFloat;
  690. opIndex += 2;
  691. break;
  692. case ExtendedPhysics.PHYS_PARAM_SPRING_AXIS_ENABLE:
  693. errMsg = "PHYS_PARAM_SPRING_AXIS_ENABLE takes two parameters of types integer and integer (bool)";
  694. valueInt = (int)pParams[opIndex + 1];
  695. valueBool = ((int)pParams[opIndex + 2]) != 0;
  696. GetAxisRange(valueInt, out axisLow, out axisHigh);
  697. for (int ii = axisLow; ii <= axisHigh; ii++)
  698. linkInfo.springAxisEnable[ii] = valueBool;
  699. opIndex += 3;
  700. break;
  701. case ExtendedPhysics.PHYS_PARAM_SPRING_DAMPING:
  702. errMsg = "PHYS_PARAM_SPRING_DAMPING takes two parameters of types integer and float";
  703. valueInt = (int)pParams[opIndex + 1];
  704. valueFloat = (float)pParams[opIndex + 2];
  705. GetAxisRange(valueInt, out axisLow, out axisHigh);
  706. for (int ii = axisLow; ii <= axisHigh; ii++)
  707. linkInfo.springDamping[ii] = valueFloat;
  708. opIndex += 3;
  709. break;
  710. case ExtendedPhysics.PHYS_PARAM_SPRING_STIFFNESS:
  711. errMsg = "PHYS_PARAM_SPRING_STIFFNESS takes two parameters of types integer and float";
  712. valueInt = (int)pParams[opIndex + 1];
  713. valueFloat = (float)pParams[opIndex + 2];
  714. GetAxisRange(valueInt, out axisLow, out axisHigh);
  715. for (int ii = axisLow; ii <= axisHigh; ii++)
  716. linkInfo.springStiffness[ii] = valueFloat;
  717. opIndex += 3;
  718. break;
  719. case ExtendedPhysics.PHYS_PARAM_SPRING_EQUILIBRIUM_POINT:
  720. errMsg = "PHYS_PARAM_SPRING_EQUILIBRIUM_POINT takes two parameters of type vector";
  721. valueVector = (OMV.Vector3)pParams[opIndex + 1];
  722. valueVector2 = (OMV.Vector3)pParams[opIndex + 2];
  723. linkInfo.springLinearEquilibriumPoint = valueVector;
  724. linkInfo.springAngularEquilibriumPoint = valueVector2;
  725. opIndex += 3;
  726. break;
  727. case ExtendedPhysics.PHYS_PARAM_USE_LINEAR_FRAMEA:
  728. errMsg = "PHYS_PARAM_USE_LINEAR_FRAMEA takes one parameter of type integer (bool)";
  729. valueBool = ((int)pParams[opIndex + 1]) != 0;
  730. linkInfo.useLinearReferenceFrameA = valueBool;
  731. opIndex += 2;
  732. break;
  733. default:
  734. break;
  735. }
  736. }
  737. catch (InvalidCastException e)
  738. {
  739. m_physicsScene.Logger.WarnFormat("{0} value of wrong type in physSetLinksetParams: {1}, err={2}",
  740. LogHeader, errMsg, e);
  741. }
  742. catch (Exception e)
  743. {
  744. m_physicsScene.Logger.WarnFormat("{0} bad parameters in physSetLinksetParams: {1}", LogHeader, e);
  745. }
  746. }
  747. }
  748. // Something changed so a rebuild is in order
  749. Refresh(child);
  750. }
  751. }
  752. break;
  753. default:
  754. ret = base.Extension(pFunct, pParams);
  755. break;
  756. }
  757. return ret;
  758. }
  759. // Bullet constraints keep some limit parameters for each linear and angular axis.
  760. // Setting same is easier if there is an easy way to see all or types.
  761. // This routine returns the array limits for the set of axis.
  762. private void GetAxisRange(int rangeSpec, out int low, out int high)
  763. {
  764. switch (rangeSpec)
  765. {
  766. case ExtendedPhysics.PHYS_AXIS_LINEAR_ALL:
  767. low = 0;
  768. high = 2;
  769. break;
  770. case ExtendedPhysics.PHYS_AXIS_ANGULAR_ALL:
  771. low = 3;
  772. high = 5;
  773. break;
  774. case ExtendedPhysics.PHYS_AXIS_ALL:
  775. low = 0;
  776. high = 5;
  777. break;
  778. default:
  779. low = high = rangeSpec;
  780. break;
  781. }
  782. return;
  783. }
  784. #endregion // Extension
  785. }
  786. }