BSLinksetCompound.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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.Framework;
  31. using OMV = OpenMetaverse;
  32. namespace OpenSim.Region.Physics.BulletSNPlugin
  33. {
  34. // When a child is linked, the relationship position of the child to the parent
  35. // is remembered so the child's world position can be recomputed when it is
  36. // removed from the linkset.
  37. sealed class BSLinksetCompoundInfo : BSLinksetInfo
  38. {
  39. public OMV.Vector3 OffsetPos;
  40. public OMV.Quaternion OffsetRot;
  41. public BSLinksetCompoundInfo(OMV.Vector3 p, OMV.Quaternion r)
  42. {
  43. OffsetPos = p;
  44. OffsetRot = r;
  45. }
  46. public override void Clear()
  47. {
  48. OffsetPos = OMV.Vector3.Zero;
  49. OffsetRot = OMV.Quaternion.Identity;
  50. }
  51. public override string ToString()
  52. {
  53. StringBuilder buff = new StringBuilder();
  54. buff.Append("<p=");
  55. buff.Append(OffsetPos.ToString());
  56. buff.Append(",r=");
  57. buff.Append(OffsetRot.ToString());
  58. buff.Append(">");
  59. return buff.ToString();
  60. }
  61. };
  62. public sealed class BSLinksetCompound : BSLinkset
  63. {
  64. private static string LogHeader = "[BULLETSIM LINKSET COMPOUND]";
  65. public BSLinksetCompound(BSScene scene, BSPhysObject parent) : base(scene, parent)
  66. {
  67. }
  68. // For compound implimented linksets, if there are children, use compound shape for the root.
  69. public override BSPhysicsShapeType PreferredPhysicalShape(BSPhysObject requestor)
  70. {
  71. // Returning 'unknown' means we don't have a preference.
  72. BSPhysicsShapeType ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  73. if (IsRoot(requestor) && HasAnyChildren)
  74. {
  75. ret = BSPhysicsShapeType.SHAPE_COMPOUND;
  76. }
  77. // DetailLog("{0},BSLinksetCompound.PreferredPhysicalShape,call,shape={1}", LinksetRoot.LocalID, ret);
  78. return ret;
  79. }
  80. // When physical properties are changed the linkset needs to recalculate
  81. // its internal properties.
  82. public override void Refresh(BSPhysObject requestor)
  83. {
  84. base.Refresh(requestor);
  85. // Something changed so do the rebuilding thing
  86. // ScheduleRebuild();
  87. }
  88. // Schedule a refresh to happen after all the other taint processing.
  89. private void ScheduleRebuild(BSPhysObject requestor)
  90. {
  91. DetailLog("{0},BSLinksetCompound.ScheduleRebuild,,rebuilding={1}",
  92. requestor.LocalID, Rebuilding);
  93. // When rebuilding, it is possible to set properties that would normally require a rebuild.
  94. // If already rebuilding, don't request another rebuild.
  95. if (!Rebuilding)
  96. {
  97. PhysicsScene.PostTaintObject("BSLinksetCompound.ScheduleRebuild", LinksetRoot.LocalID, delegate()
  98. {
  99. if (HasAnyChildren)
  100. RecomputeLinksetCompound();
  101. });
  102. }
  103. }
  104. // The object is going dynamic (physical). Do any setup necessary
  105. // for a dynamic linkset.
  106. // Only the state of the passed object can be modified. The rest of the linkset
  107. // has not yet been fully constructed.
  108. // Return 'true' if any properties updated on the passed object.
  109. // Called at taint-time!
  110. public override bool MakeDynamic(BSPhysObject child)
  111. {
  112. bool ret = false;
  113. DetailLog("{0},BSLinksetCompound.MakeDynamic,call,IsRoot={1}", child.LocalID, IsRoot(child));
  114. if (IsRoot(child))
  115. {
  116. // The root is going dynamic. Make sure mass is properly set.
  117. ScheduleRebuild(LinksetRoot);
  118. }
  119. else
  120. {
  121. // The origional prims are removed from the world as the shape of the root compound
  122. // shape takes over.
  123. BulletSimAPI.AddToCollisionFlags2(child.PhysBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE);
  124. BulletSimAPI.ForceActivationState2(child.PhysBody.ptr, ActivationState.DISABLE_SIMULATION);
  125. // We don't want collisions from the old linkset children.
  126. BulletSimAPI.RemoveFromCollisionFlags2(child.PhysBody.ptr, CollisionFlags.BS_SUBSCRIBE_COLLISION_EVENTS);
  127. child.PhysBody.collisionType = CollisionType.LinksetChild;
  128. ret = true;
  129. }
  130. return ret;
  131. }
  132. // The object is going static (non-physical). Do any setup necessary for a static linkset.
  133. // Return 'true' if any properties updated on the passed object.
  134. // This doesn't normally happen -- OpenSim removes the objects from the physical
  135. // world if it is a static linkset.
  136. // Called at taint-time!
  137. public override bool MakeStatic(BSPhysObject child)
  138. {
  139. bool ret = false;
  140. DetailLog("{0},BSLinksetCompound.MakeStatic,call,IsRoot={1}", child.LocalID, IsRoot(child));
  141. if (IsRoot(child))
  142. {
  143. ScheduleRebuild(LinksetRoot);
  144. }
  145. else
  146. {
  147. // The non-physical children can come back to life.
  148. BulletSimAPI.RemoveFromCollisionFlags2(child.PhysBody.ptr, CollisionFlags.CF_NO_CONTACT_RESPONSE);
  149. child.PhysBody.collisionType = CollisionType.LinksetChild;
  150. // Don't force activation so setting of DISABLE_SIMULATION can stay if used.
  151. BulletSimAPI.Activate2(child.PhysBody.ptr, false);
  152. ret = true;
  153. }
  154. return ret;
  155. }
  156. public override void UpdateProperties(BSPhysObject updated, bool physicalUpdate)
  157. {
  158. // The user moving a child around requires the rebuilding of the linkset compound shape
  159. // One problem is this happens when a border is crossed -- the simulator implementation
  160. // is to store the position into the group which causes the move of the object
  161. // but it also means all the child positions get updated.
  162. // What would cause an unnecessary rebuild so we make sure the linkset is in a
  163. // region before bothering to do a rebuild.
  164. if (!IsRoot(updated)
  165. && !physicalUpdate
  166. && PhysicsScene.TerrainManager.IsWithinKnownTerrain(LinksetRoot.RawPosition))
  167. {
  168. updated.LinksetInfo = null;
  169. ScheduleRebuild(updated);
  170. }
  171. }
  172. // Routine called when rebuilding the body of some member of the linkset.
  173. // Since we don't keep in world relationships, do nothing unless it's a child changing.
  174. // Returns 'true' of something was actually removed and would need restoring
  175. // Called at taint-time!!
  176. public override bool RemoveBodyDependencies(BSPrim child)
  177. {
  178. bool ret = false;
  179. DetailLog("{0},BSLinksetCompound.RemoveBodyDependencies,refreshIfChild,rID={1},rBody={2},isRoot={3}",
  180. child.LocalID, LinksetRoot.LocalID, LinksetRoot.PhysBody.ptr.ToString(), IsRoot(child));
  181. if (!IsRoot(child))
  182. {
  183. // Because it is a convenient time, recompute child world position and rotation based on
  184. // its position in the linkset.
  185. RecomputeChildWorldPosition(child, true);
  186. }
  187. // Cannot schedule a refresh/rebuild here because this routine is called when
  188. // the linkset is being rebuilt.
  189. // InternalRefresh(LinksetRoot);
  190. return ret;
  191. }
  192. // Companion to RemoveBodyDependencies(). If RemoveBodyDependencies() returns 'true',
  193. // this routine will restore the removed constraints.
  194. // Called at taint-time!!
  195. public override void RestoreBodyDependencies(BSPrim child)
  196. {
  197. }
  198. // When the linkset is built, the child shape is added to the compound shape relative to the
  199. // root shape. The linkset then moves around but this does not move the actual child
  200. // prim. The child prim's location must be recomputed based on the location of the root shape.
  201. private void RecomputeChildWorldPosition(BSPhysObject child, bool inTaintTime)
  202. {
  203. BSLinksetCompoundInfo lci = child.LinksetInfo as BSLinksetCompoundInfo;
  204. if (lci != null)
  205. {
  206. if (inTaintTime)
  207. {
  208. OMV.Vector3 oldPos = child.RawPosition;
  209. child.ForcePosition = LinksetRoot.RawPosition + lci.OffsetPos;
  210. child.ForceOrientation = LinksetRoot.RawOrientation * lci.OffsetRot;
  211. DetailLog("{0},BSLinksetCompound.RecomputeChildWorldPosition,oldPos={1},lci={2},newPos={3}",
  212. child.LocalID, oldPos, lci, child.RawPosition);
  213. }
  214. else
  215. {
  216. // TaintedObject is not used here so the raw position is set now and not at taint-time.
  217. child.Position = LinksetRoot.RawPosition + lci.OffsetPos;
  218. child.Orientation = LinksetRoot.RawOrientation * lci.OffsetRot;
  219. }
  220. }
  221. else
  222. {
  223. // This happens when children have been added to the linkset but the linkset
  224. // has not been constructed yet. So like, at taint time, adding children to a linkset
  225. // and then changing properties of the children (makePhysical, for instance)
  226. // but the post-print action of actually rebuilding the linkset has not yet happened.
  227. // PhysicsScene.Logger.WarnFormat("{0} Restoring linkset child position failed because of no relative position computed. ID={1}",
  228. // LogHeader, child.LocalID);
  229. DetailLog("{0},BSLinksetCompound.recomputeChildWorldPosition,noRelativePositonInfo", child.LocalID);
  230. }
  231. }
  232. // ================================================================
  233. // Add a new child to the linkset.
  234. // Called while LinkActivity is locked.
  235. protected override void AddChildToLinkset(BSPhysObject child)
  236. {
  237. if (!HasChild(child))
  238. {
  239. m_children.Add(child);
  240. DetailLog("{0},BSLinksetCompound.AddChildToLinkset,call,child={1}", LinksetRoot.LocalID, child.LocalID);
  241. // Rebuild the compound shape with the new child shape included
  242. ScheduleRebuild(child);
  243. }
  244. return;
  245. }
  246. // Remove the specified child from the linkset.
  247. // Safe to call even if the child is not really in the linkset.
  248. protected override void RemoveChildFromLinkset(BSPhysObject child)
  249. {
  250. if (m_children.Remove(child))
  251. {
  252. DetailLog("{0},BSLinksetCompound.RemoveChildFromLinkset,call,rID={1},rBody={2},cID={3},cBody={4}",
  253. child.LocalID,
  254. LinksetRoot.LocalID, LinksetRoot.PhysBody.ptr.ToString(),
  255. child.LocalID, child.PhysBody.ptr.ToString());
  256. // Cause the child's body to be rebuilt and thus restored to normal operation
  257. RecomputeChildWorldPosition(child, false);
  258. child.ForceBodyShapeRebuild(false);
  259. if (!HasAnyChildren)
  260. {
  261. // The linkset is now empty. The root needs rebuilding.
  262. LinksetRoot.ForceBodyShapeRebuild(false);
  263. }
  264. else
  265. {
  266. // Rebuild the compound shape with the child removed
  267. ScheduleRebuild(child);
  268. }
  269. }
  270. return;
  271. }
  272. // Called before the simulation step to make sure the compound based linkset
  273. // is all initialized.
  274. // Constraint linksets are rebuilt every time.
  275. // Note that this works for rebuilding just the root after a linkset is taken apart.
  276. // Called at taint time!!
  277. private void RecomputeLinksetCompound()
  278. {
  279. try
  280. {
  281. // Suppress rebuilding while rebuilding
  282. Rebuilding = true;
  283. // Cause the root shape to be rebuilt as a compound object with just the root in it
  284. LinksetRoot.ForceBodyShapeRebuild(true);
  285. DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,start,rBody={1},rShape={2},numChildren={3}",
  286. LinksetRoot.LocalID, LinksetRoot.PhysBody, LinksetRoot.PhysShape, NumberOfChildren);
  287. // Add a shape for each of the other children in the linkset
  288. ForEachMember(delegate(BSPhysObject cPrim)
  289. {
  290. if (!IsRoot(cPrim))
  291. {
  292. // Compute the displacement of the child from the root of the linkset.
  293. // This info is saved in the child prim so the relationship does not
  294. // change over time and the new child position can be computed
  295. // when the linkset is being disassembled (the linkset may have moved).
  296. BSLinksetCompoundInfo lci = cPrim.LinksetInfo as BSLinksetCompoundInfo;
  297. if (lci == null)
  298. {
  299. // Each child position and rotation is given relative to the root.
  300. OMV.Quaternion invRootOrientation = OMV.Quaternion.Inverse(LinksetRoot.RawOrientation);
  301. OMV.Vector3 displacementPos = (cPrim.RawPosition - LinksetRoot.RawPosition) * invRootOrientation;
  302. OMV.Quaternion displacementRot = cPrim.RawOrientation * invRootOrientation;
  303. // Save relative position for recomputing child's world position after moving linkset.
  304. lci = new BSLinksetCompoundInfo(displacementPos, displacementRot);
  305. cPrim.LinksetInfo = lci;
  306. DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,creatingRelPos,lci={1}", cPrim.LocalID, lci);
  307. }
  308. DetailLog("{0},BSLinksetCompound.RecomputeLinksetCompound,addMemberToShape,mID={1},mShape={2},dispPos={3},dispRot={4}",
  309. LinksetRoot.LocalID, cPrim.LocalID, cPrim.PhysShape, lci.OffsetPos, lci.OffsetRot);
  310. if (cPrim.PhysShape.isNativeShape)
  311. {
  312. // A native shape is turning into a hull collision shape because native
  313. // shapes are not shared so we have to hullify it so it will be tracked
  314. // and freed at the correct time. This also solves the scaling problem
  315. // (native shapes scaled but hull/meshes are assumed to not be).
  316. // TODO: decide of the native shape can just be used in the compound shape.
  317. // Use call to CreateGeomNonSpecial().
  318. BulletShape saveShape = cPrim.PhysShape;
  319. cPrim.PhysShape.Clear(); // Don't let the create free the child's shape
  320. // PhysicsScene.Shapes.CreateGeomNonSpecial(true, cPrim, null);
  321. PhysicsScene.Shapes.CreateGeomMeshOrHull(cPrim, null);
  322. BulletShape newShape = cPrim.PhysShape;
  323. cPrim.PhysShape = saveShape;
  324. BulletSimAPI.AddChildShapeToCompoundShape2(LinksetRoot.PhysShape.ptr, newShape.ptr, lci.OffsetPos, lci.OffsetRot);
  325. }
  326. else
  327. {
  328. // For the shared shapes (meshes and hulls), just use the shape in the child.
  329. // The reference count added here will be decremented when the compound shape
  330. // is destroyed in BSShapeCollection (the child shapes are looped over and dereferenced).
  331. if (PhysicsScene.Shapes.ReferenceShape(cPrim.PhysShape))
  332. {
  333. PhysicsScene.Logger.ErrorFormat("{0} Rebuilt sharable shape when building linkset! Region={1}, primID={2}, shape={3}",
  334. LogHeader, PhysicsScene.RegionName, cPrim.LocalID, cPrim.PhysShape);
  335. }
  336. BulletSimAPI.AddChildShapeToCompoundShape2(LinksetRoot.PhysShape.ptr, cPrim.PhysShape.ptr, lci.OffsetPos, lci.OffsetRot);
  337. }
  338. }
  339. return false; // 'false' says to move onto the next child in the list
  340. });
  341. // With all of the linkset packed into the root prim, it has the mass of everyone.
  342. LinksetMass = LinksetMass;
  343. LinksetRoot.UpdatePhysicalMassProperties(LinksetMass, true);
  344. }
  345. finally
  346. {
  347. Rebuilding = false;
  348. }
  349. BulletSimAPI.RecalculateCompoundShapeLocalAabb2(LinksetRoot.PhysShape.ptr);
  350. // DEBUG: see of inter-linkset collisions are causing problems for constraint linksets.
  351. // BulletSimAPI.SetCollisionFilterMask2(LinksetRoot.BSBody.ptr,
  352. // (uint)CollisionFilterGroups.LinksetFilter, (uint)CollisionFilterGroups.LinksetMask);
  353. }
  354. }
  355. }