BSPrimLinkable.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.Linq;
  30. using System.Text;
  31. using OpenSim.Framework;
  32. using OMV = OpenMetaverse;
  33. namespace OpenSim.Region.Physics.BulletSPlugin
  34. {
  35. public class BSPrimLinkable : BSPrimDisplaced
  36. {
  37. // The purpose of this subclass is to add linkset functionality to the prim. This overrides
  38. // operations necessary for keeping the linkset created and, additionally, this
  39. // calls the linkset implementation for its creation and management.
  40. // This adds the overrides for link() and delink() so the prim is linkable.
  41. public BSLinkset Linkset { get; set; }
  42. // The index of this child prim.
  43. public int LinksetChildIndex { get; set; }
  44. public BSLinkset.LinksetImplementation LinksetType { get; set; }
  45. public BSPrimLinkable(uint localID, String primName, BSScene parent_scene, OMV.Vector3 pos, OMV.Vector3 size,
  46. OMV.Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical)
  47. : base(localID, primName, parent_scene, pos, size, rotation, pbs, pisPhysical)
  48. {
  49. // Default linkset implementation for this prim
  50. LinksetType = (BSLinkset.LinksetImplementation)BSParam.LinksetImplementation;
  51. Linkset = BSLinkset.Factory(PhysScene, this);
  52. PhysScene.TaintedObject("BSPrimLinksetCompound.Refresh", delegate()
  53. {
  54. Linkset.Refresh(this);
  55. });
  56. }
  57. public override void Destroy()
  58. {
  59. Linkset = Linkset.RemoveMeFromLinkset(this);
  60. base.Destroy();
  61. }
  62. public override void link(Manager.PhysicsActor obj)
  63. {
  64. BSPrimLinkable parent = obj as BSPrimLinkable;
  65. if (parent != null)
  66. {
  67. BSPhysObject parentBefore = Linkset.LinksetRoot; // DEBUG
  68. int childrenBefore = Linkset.NumberOfChildren; // DEBUG
  69. Linkset = parent.Linkset.AddMeToLinkset(this);
  70. DetailLog("{0},BSPrimLinkset.link,call,parentBefore={1}, childrenBefore=={2}, parentAfter={3}, childrenAfter={4}",
  71. LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren);
  72. }
  73. return;
  74. }
  75. public override void delink()
  76. {
  77. // TODO: decide if this parent checking needs to happen at taint time
  78. // Race condition here: if link() and delink() in same simulation tick, the delink will not happen
  79. BSPhysObject parentBefore = Linkset.LinksetRoot; // DEBUG
  80. int childrenBefore = Linkset.NumberOfChildren; // DEBUG
  81. Linkset = Linkset.RemoveMeFromLinkset(this);
  82. DetailLog("{0},BSPrimLinkset.delink,parentBefore={1},childrenBefore={2},parentAfter={3},childrenAfter={4}, ",
  83. LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren);
  84. return;
  85. }
  86. // When simulator changes position, this might be moving a child of the linkset.
  87. public override OMV.Vector3 Position
  88. {
  89. get { return base.Position; }
  90. set
  91. {
  92. base.Position = value;
  93. PhysScene.TaintedObject("BSPrimLinkset.setPosition", delegate()
  94. {
  95. Linkset.UpdateProperties(UpdatedProperties.Position, this);
  96. });
  97. }
  98. }
  99. // When simulator changes orientation, this might be moving a child of the linkset.
  100. public override OMV.Quaternion Orientation
  101. {
  102. get { return base.Orientation; }
  103. set
  104. {
  105. base.Orientation = value;
  106. PhysScene.TaintedObject("BSPrimLinkset.setOrientation", delegate()
  107. {
  108. Linkset.UpdateProperties(UpdatedProperties.Orientation, this);
  109. });
  110. }
  111. }
  112. public override float TotalMass
  113. {
  114. get { return Linkset.LinksetMass; }
  115. }
  116. public override OMV.Vector3 CenterOfMass
  117. {
  118. get { return Linkset.CenterOfMass; }
  119. }
  120. public override OMV.Vector3 GeometricCenter
  121. {
  122. get { return Linkset.GeometricCenter; }
  123. }
  124. // Refresh the linkset structure and parameters when the prim's physical parameters are changed.
  125. public override void UpdatePhysicalParameters()
  126. {
  127. base.UpdatePhysicalParameters();
  128. // Recompute any linkset parameters.
  129. // When going from non-physical to physical, this re-enables the constraints that
  130. // had been automatically disabled when the mass was set to zero.
  131. // For compound based linksets, this enables and disables interactions of the children.
  132. if (Linkset != null) // null can happen during initialization
  133. Linkset.Refresh(this);
  134. }
  135. // When the prim is made dynamic or static, the linkset needs to change.
  136. protected override void MakeDynamic(bool makeStatic)
  137. {
  138. base.MakeDynamic(makeStatic);
  139. if (Linkset != null) // null can happen during initialization
  140. {
  141. if (makeStatic)
  142. Linkset.MakeStatic(this);
  143. else
  144. Linkset.MakeDynamic(this);
  145. }
  146. }
  147. // Body is being taken apart. Remove physical dependencies and schedule a rebuild.
  148. protected override void RemoveDependencies()
  149. {
  150. Linkset.RemoveDependencies(this);
  151. base.RemoveDependencies();
  152. }
  153. // Called after a simulation step for the changes in physical object properties.
  154. // Do any filtering/modification needed for linksets.
  155. public override void UpdateProperties(EntityProperties entprop)
  156. {
  157. if (Linkset.IsRoot(this))
  158. {
  159. // Properties are only updated for the roots of a linkset.
  160. // TODO: this will have to change when linksets are articulated.
  161. base.UpdateProperties(entprop);
  162. }
  163. /*
  164. else
  165. {
  166. // For debugging, report the movement of children
  167. DetailLog("{0},BSPrim.UpdateProperties,child,pos={1},orient={2},vel={3},accel={4},rotVel={5}",
  168. LocalID, entprop.Position, entprop.Rotation, entprop.Velocity,
  169. entprop.Acceleration, entprop.RotationalVelocity);
  170. }
  171. */
  172. // The linkset might like to know about changing locations
  173. Linkset.UpdateProperties(UpdatedProperties.EntPropUpdates, this);
  174. }
  175. // Called after a simulation step to post a collision with this object.
  176. // This returns 'true' if the collision has been queued and the SendCollisions call must
  177. // be made at the end of the simulation step.
  178. public override bool Collide(uint collidingWith, BSPhysObject collidee,
  179. OMV.Vector3 contactPoint, OMV.Vector3 contactNormal, float pentrationDepth)
  180. {
  181. bool ret = false;
  182. // Ask the linkset if it wants to handle the collision
  183. if (!Linkset.HandleCollide(collidingWith, collidee, contactPoint, contactNormal, pentrationDepth))
  184. {
  185. // The linkset didn't handle it so pass the collision through normal processing
  186. ret = base.Collide(collidingWith, collidee, contactPoint, contactNormal, pentrationDepth);
  187. }
  188. return ret;
  189. }
  190. // A linkset reports any collision on any part of the linkset.
  191. public long SomeCollisionSimulationStep = 0;
  192. public override bool HasSomeCollision
  193. {
  194. get
  195. {
  196. return (SomeCollisionSimulationStep == PhysScene.SimulationStep) || base.IsColliding;
  197. }
  198. set
  199. {
  200. if (value)
  201. SomeCollisionSimulationStep = PhysScene.SimulationStep;
  202. else
  203. SomeCollisionSimulationStep = 0;
  204. base.HasSomeCollision = value;
  205. }
  206. }
  207. // Convert the existing linkset of this prim into a new type.
  208. public bool ConvertLinkset(BSLinkset.LinksetImplementation newType)
  209. {
  210. bool ret = false;
  211. if (LinksetType != newType)
  212. {
  213. // Set the implementation type first so the call to BSLinkset.Factory gets the new type.
  214. this.LinksetType = newType;
  215. BSLinkset oldLinkset = this.Linkset;
  216. BSLinkset newLinkset = BSLinkset.Factory(PhysScene, this);
  217. this.Linkset = newLinkset;
  218. // Pick up any physical dependencies this linkset might have in the physics engine.
  219. oldLinkset.RemoveDependencies(this);
  220. // Create a list of the children (mainly because can't interate through a list that's changing)
  221. List<BSPrimLinkable> children = new List<BSPrimLinkable>();
  222. oldLinkset.ForEachMember((child) =>
  223. {
  224. if (!oldLinkset.IsRoot(child))
  225. children.Add(child);
  226. return false; // 'false' says to continue to next member
  227. });
  228. // Remove the children from the old linkset and add to the new (will be a new instance from the factory)
  229. foreach (BSPrimLinkable child in children)
  230. {
  231. oldLinkset.RemoveMeFromLinkset(child);
  232. newLinkset.AddMeToLinkset(child);
  233. child.Linkset = newLinkset;
  234. }
  235. // Force the shape and linkset to get reconstructed
  236. newLinkset.Refresh(this);
  237. this.ForceBodyShapeRebuild(true /* inTaintTime */);
  238. }
  239. return ret;
  240. }
  241. }
  242. }