1
0

BSLinksetConstraints.cs 45 KB

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