BSLinksetConstraints.cs 45 KB

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