BSLinkset.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyrightD
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. using OMV = OpenMetaverse;
  31. namespace OpenSim.Region.Physics.BulletSPlugin
  32. {
  33. public abstract class BSLinkset
  34. {
  35. // private static string LogHeader = "[BULLETSIM LINKSET]";
  36. public enum LinksetImplementation
  37. {
  38. Constraint = 0, // linkset tied together with constraints
  39. Compound = 1, // linkset tied together as a compound object
  40. Manual = 2 // linkset tied together manually (code moves all the pieces)
  41. }
  42. // Create the correct type of linkset for this child
  43. public static BSLinkset Factory(BSScene physScene, BSPrimLinkable parent)
  44. {
  45. BSLinkset ret = null;
  46. switch (parent.LinksetType)
  47. {
  48. case LinksetImplementation.Constraint:
  49. ret = new BSLinksetConstraints(physScene, parent);
  50. break;
  51. case LinksetImplementation.Compound:
  52. ret = new BSLinksetCompound(physScene, parent);
  53. break;
  54. case LinksetImplementation.Manual:
  55. // ret = new BSLinksetManual(physScene, parent);
  56. break;
  57. default:
  58. ret = new BSLinksetCompound(physScene, parent);
  59. break;
  60. }
  61. if (ret == null)
  62. {
  63. physScene.Logger.ErrorFormat("[BULLETSIM LINKSET] Factory could not create linkset. Parent name={1}, ID={2}", parent.Name, parent.LocalID);
  64. }
  65. return ret;
  66. }
  67. public class BSLinkInfo
  68. {
  69. public BSPrimLinkable member;
  70. public BSLinkInfo(BSPrimLinkable pMember)
  71. {
  72. member = pMember;
  73. }
  74. public virtual void ResetLink() { }
  75. public virtual void SetLinkParameters(BSConstraint constrain) { }
  76. // Returns 'true' if physical property updates from the child should be reported to the simulator
  77. public virtual bool ShouldUpdateChildProperties() { return false; }
  78. }
  79. public LinksetImplementation LinksetImpl { get; protected set; }
  80. public BSPrimLinkable LinksetRoot { get; protected set; }
  81. protected BSScene m_physicsScene { get; private set; }
  82. static int m_nextLinksetID = 1;
  83. public int LinksetID { get; private set; }
  84. // The children under the root in this linkset.
  85. // protected HashSet<BSPrimLinkable> m_children;
  86. protected Dictionary<BSPrimLinkable, BSLinkInfo> m_children;
  87. // We lock the diddling of linkset classes to prevent any badness.
  88. // This locks the modification of the instances of this class. Changes
  89. // to the physical representation is done via the tainting mechenism.
  90. protected object m_linksetActivityLock = new Object();
  91. // We keep the prim's mass in the linkset structure since it could be dependent on other prims
  92. public float LinksetMass { get; protected set; }
  93. public virtual bool LinksetIsColliding { get { return false; } }
  94. public OMV.Vector3 CenterOfMass
  95. {
  96. get { return ComputeLinksetCenterOfMass(); }
  97. }
  98. public OMV.Vector3 GeometricCenter
  99. {
  100. get { return ComputeLinksetGeometricCenter(); }
  101. }
  102. protected BSLinkset(BSScene scene, BSPrimLinkable parent)
  103. {
  104. // A simple linkset of one (no children)
  105. LinksetID = m_nextLinksetID++;
  106. // We create LOTS of linksets.
  107. if (m_nextLinksetID <= 0)
  108. m_nextLinksetID = 1;
  109. m_physicsScene = scene;
  110. LinksetRoot = parent;
  111. m_children = new Dictionary<BSPrimLinkable, BSLinkInfo>();
  112. LinksetMass = parent.RawMass;
  113. Rebuilding = false;
  114. parent.ClearDisplacement();
  115. }
  116. // Link to a linkset where the child knows the parent.
  117. // Parent changing should not happen so do some sanity checking.
  118. // We return the parent's linkset so the child can track its membership.
  119. // Called at runtime.
  120. public BSLinkset AddMeToLinkset(BSPrimLinkable child)
  121. {
  122. lock (m_linksetActivityLock)
  123. {
  124. // Don't add the root to its own linkset
  125. if (!IsRoot(child))
  126. AddChildToLinkset(child);
  127. LinksetMass = ComputeLinksetMass();
  128. }
  129. return this;
  130. }
  131. // Remove a child from a linkset.
  132. // Returns a new linkset for the child which is a linkset of one (just the
  133. // orphened child).
  134. // Called at runtime.
  135. public BSLinkset RemoveMeFromLinkset(BSPrimLinkable child, bool inTaintTime)
  136. {
  137. lock (m_linksetActivityLock)
  138. {
  139. if (IsRoot(child))
  140. {
  141. // Cannot remove the root from a linkset.
  142. return this;
  143. }
  144. RemoveChildFromLinkset(child, inTaintTime);
  145. LinksetMass = ComputeLinksetMass();
  146. }
  147. // The child is down to a linkset of just itself
  148. return BSLinkset.Factory(m_physicsScene, child);
  149. }
  150. // Return 'true' if the passed object is the root object of this linkset
  151. public bool IsRoot(BSPrimLinkable requestor)
  152. {
  153. return (requestor.LocalID == LinksetRoot.LocalID);
  154. }
  155. public int NumberOfChildren { get { return m_children.Count; } }
  156. // Return 'true' if this linkset has any children (more than the root member)
  157. public bool HasAnyChildren { get { return (m_children.Count > 0); } }
  158. // Return 'true' if this child is in this linkset
  159. public bool HasChild(BSPrimLinkable child)
  160. {
  161. bool ret = false;
  162. lock (m_linksetActivityLock)
  163. {
  164. ret = m_children.ContainsKey(child);
  165. }
  166. return ret;
  167. }
  168. // Perform an action on each member of the linkset including root prim.
  169. // Depends on the action on whether this should be done at taint time.
  170. public delegate bool ForEachMemberAction(BSPrimLinkable obj);
  171. public virtual bool ForEachMember(ForEachMemberAction action)
  172. {
  173. bool ret = false;
  174. lock (m_linksetActivityLock)
  175. {
  176. action(LinksetRoot);
  177. foreach (BSPrimLinkable po in m_children.Keys)
  178. {
  179. if (action(po))
  180. break;
  181. }
  182. }
  183. return ret;
  184. }
  185. public bool TryGetLinkInfo(BSPrimLinkable child, out BSLinkInfo foundInfo)
  186. {
  187. bool ret = false;
  188. BSLinkInfo found = null;
  189. lock (m_linksetActivityLock)
  190. {
  191. ret = m_children.TryGetValue(child, out found);
  192. }
  193. foundInfo = found;
  194. return ret;
  195. }
  196. // Perform an action on each member of the linkset including root prim.
  197. // Depends on the action on whether this should be done at taint time.
  198. public delegate bool ForEachLinkInfoAction(BSLinkInfo obj);
  199. public virtual bool ForEachLinkInfo(ForEachLinkInfoAction action)
  200. {
  201. bool ret = false;
  202. lock (m_linksetActivityLock)
  203. {
  204. foreach (BSLinkInfo po in m_children.Values)
  205. {
  206. if (action(po))
  207. break;
  208. }
  209. }
  210. return ret;
  211. }
  212. // Check the type of the link and return 'true' if the link is flexible and the
  213. // updates from the child should be sent to the simulator so things change.
  214. public virtual bool ShouldReportPropertyUpdates(BSPrimLinkable child)
  215. {
  216. bool ret = false;
  217. BSLinkInfo linkInfo;
  218. if (m_children.TryGetValue(child, out linkInfo))
  219. {
  220. ret = linkInfo.ShouldUpdateChildProperties();
  221. }
  222. return ret;
  223. }
  224. // Called after a simulation step to post a collision with this object.
  225. // Return 'true' if linkset processed the collision. 'false' says the linkset didn't have
  226. // anything to add for the collision and it should be passed through normal processing.
  227. // Default processing for a linkset.
  228. public virtual bool HandleCollide(uint collidingWith, BSPhysObject collidee,
  229. OMV.Vector3 contactPoint, OMV.Vector3 contactNormal, float pentrationDepth)
  230. {
  231. bool ret = false;
  232. // prims in the same linkset cannot collide with each other
  233. BSPrimLinkable convCollidee = collidee as BSPrimLinkable;
  234. if (convCollidee != null && (LinksetID == convCollidee.Linkset.LinksetID))
  235. {
  236. // By returning 'true', we tell the caller the collision has been 'handled' so it won't
  237. // do anything about this collision and thus, effectivily, ignoring the collision.
  238. ret = true;
  239. }
  240. else
  241. {
  242. // Not a collision between members of the linkset. Must be a real collision.
  243. // So the linkset root can know if there is a collision anywhere in the linkset.
  244. LinksetRoot.SomeCollisionSimulationStep = m_physicsScene.SimulationStep;
  245. }
  246. return ret;
  247. }
  248. // I am the root of a linkset and a new child is being added
  249. // Called while LinkActivity is locked.
  250. protected abstract void AddChildToLinkset(BSPrimLinkable child);
  251. // I am the root of a linkset and one of my children is being removed.
  252. // Safe to call even if the child is not really in my linkset.
  253. protected abstract void RemoveChildFromLinkset(BSPrimLinkable child, bool inTaintTime);
  254. // When physical properties are changed the linkset needs to recalculate
  255. // its internal properties.
  256. // May be called at runtime or taint-time.
  257. public virtual void Refresh(BSPrimLinkable requestor)
  258. {
  259. LinksetMass = ComputeLinksetMass();
  260. }
  261. // Flag denoting the linkset is in the process of being rebuilt.
  262. // Used to know not the schedule a rebuild in the middle of a rebuild.
  263. protected bool Rebuilding { get; set; }
  264. // The object is going dynamic (physical). Do any setup necessary
  265. // for a dynamic linkset.
  266. // Only the state of the passed object can be modified. The rest of the linkset
  267. // has not yet been fully constructed.
  268. // Return 'true' if any properties updated on the passed object.
  269. // Called at taint-time!
  270. public abstract bool MakeDynamic(BSPrimLinkable child);
  271. // The object is going static (non-physical). Do any setup necessary
  272. // for a static linkset.
  273. // Return 'true' if any properties updated on the passed object.
  274. // Called at taint-time!
  275. public abstract bool MakeStatic(BSPrimLinkable child);
  276. // Called when a parameter update comes from the physics engine for any object
  277. // of the linkset is received.
  278. // Passed flag is update came from physics engine (true) or the user (false).
  279. // Called at taint-time!!
  280. public abstract void UpdateProperties(UpdatedProperties whichUpdated, BSPrimLinkable physObject);
  281. // Routine used when rebuilding the body of the root of the linkset
  282. // Destroy all the constraints have have been made to root.
  283. // This is called when the root body is changing.
  284. // Returns 'true' of something was actually removed and would need restoring
  285. // Called at taint-time!!
  286. public abstract bool RemoveDependencies(BSPrimLinkable child);
  287. // ================================================================
  288. // Some physical setting happen to all members of the linkset
  289. public virtual void SetPhysicalFriction(float friction)
  290. {
  291. ForEachMember((member) =>
  292. {
  293. if (member.PhysBody.HasPhysicalBody)
  294. m_physicsScene.PE.SetFriction(member.PhysBody, friction);
  295. return false; // 'false' says to continue looping
  296. }
  297. );
  298. }
  299. public virtual void SetPhysicalRestitution(float restitution)
  300. {
  301. ForEachMember((member) =>
  302. {
  303. if (member.PhysBody.HasPhysicalBody)
  304. m_physicsScene.PE.SetRestitution(member.PhysBody, restitution);
  305. return false; // 'false' says to continue looping
  306. }
  307. );
  308. }
  309. public virtual void SetPhysicalGravity(OMV.Vector3 gravity)
  310. {
  311. ForEachMember((member) =>
  312. {
  313. if (member.PhysBody.HasPhysicalBody)
  314. m_physicsScene.PE.SetGravity(member.PhysBody, gravity);
  315. return false; // 'false' says to continue looping
  316. }
  317. );
  318. }
  319. public virtual void ComputeAndSetLocalInertia(OMV.Vector3 inertiaFactor, float linksetMass)
  320. {
  321. ForEachMember((member) =>
  322. {
  323. if (member.PhysBody.HasPhysicalBody)
  324. {
  325. OMV.Vector3 inertia = m_physicsScene.PE.CalculateLocalInertia(member.PhysShape.physShapeInfo, linksetMass);
  326. member.Inertia = inertia * inertiaFactor;
  327. m_physicsScene.PE.SetMassProps(member.PhysBody, linksetMass, member.Inertia);
  328. m_physicsScene.PE.UpdateInertiaTensor(member.PhysBody);
  329. DetailLog("{0},BSLinkset.ComputeAndSetLocalInertia,m.mass={1}, inertia={2}", member.LocalID, linksetMass, member.Inertia);
  330. }
  331. return false; // 'false' says to continue looping
  332. }
  333. );
  334. }
  335. public virtual void SetPhysicalCollisionFlags(CollisionFlags collFlags)
  336. {
  337. ForEachMember((member) =>
  338. {
  339. if (member.PhysBody.HasPhysicalBody)
  340. m_physicsScene.PE.SetCollisionFlags(member.PhysBody, collFlags);
  341. return false; // 'false' says to continue looping
  342. }
  343. );
  344. }
  345. public virtual void AddToPhysicalCollisionFlags(CollisionFlags collFlags)
  346. {
  347. ForEachMember((member) =>
  348. {
  349. if (member.PhysBody.HasPhysicalBody)
  350. m_physicsScene.PE.AddToCollisionFlags(member.PhysBody, collFlags);
  351. return false; // 'false' says to continue looping
  352. }
  353. );
  354. }
  355. public virtual void RemoveFromPhysicalCollisionFlags(CollisionFlags collFlags)
  356. {
  357. ForEachMember((member) =>
  358. {
  359. if (member.PhysBody.HasPhysicalBody)
  360. m_physicsScene.PE.RemoveFromCollisionFlags(member.PhysBody, collFlags);
  361. return false; // 'false' says to continue looping
  362. }
  363. );
  364. }
  365. // ================================================================
  366. protected virtual float ComputeLinksetMass()
  367. {
  368. float mass = LinksetRoot.RawMass;
  369. if (HasAnyChildren)
  370. {
  371. lock (m_linksetActivityLock)
  372. {
  373. foreach (BSPrimLinkable bp in m_children.Keys)
  374. {
  375. mass += bp.RawMass;
  376. }
  377. }
  378. }
  379. return mass;
  380. }
  381. // Computes linkset's center of mass in world coordinates.
  382. protected virtual OMV.Vector3 ComputeLinksetCenterOfMass()
  383. {
  384. OMV.Vector3 com;
  385. lock (m_linksetActivityLock)
  386. {
  387. com = LinksetRoot.Position * LinksetRoot.RawMass;
  388. float totalMass = LinksetRoot.RawMass;
  389. foreach (BSPrimLinkable bp in m_children.Keys)
  390. {
  391. com += bp.Position * bp.RawMass;
  392. totalMass += bp.RawMass;
  393. }
  394. if (totalMass != 0f)
  395. com /= totalMass;
  396. }
  397. return com;
  398. }
  399. protected virtual OMV.Vector3 ComputeLinksetGeometricCenter()
  400. {
  401. OMV.Vector3 com;
  402. lock (m_linksetActivityLock)
  403. {
  404. com = LinksetRoot.Position;
  405. foreach (BSPrimLinkable bp in m_children.Keys)
  406. {
  407. com += bp.Position;
  408. }
  409. com /= (m_children.Count + 1);
  410. }
  411. return com;
  412. }
  413. #region Extension
  414. public virtual object Extension(string pFunct, params object[] pParams)
  415. {
  416. return null;
  417. }
  418. #endregion // Extension
  419. // Invoke the detailed logger and output something if it's enabled.
  420. protected void DetailLog(string msg, params Object[] args)
  421. {
  422. if (m_physicsScene.PhysicsLogging.Enabled)
  423. m_physicsScene.DetailLog(msg, args);
  424. }
  425. }
  426. }