BSPrimLinkable.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. public BSLinkset Linkset { get; set; }
  38. // The index of this child prim.
  39. public int LinksetChildIndex { get; set; }
  40. public BSLinksetInfo LinksetInfo { get; set; }
  41. public BSPrimLinkable(uint localID, String primName, BSScene parent_scene, OMV.Vector3 pos, OMV.Vector3 size,
  42. OMV.Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical)
  43. : base(localID, primName, parent_scene, pos, size, rotation, pbs, pisPhysical)
  44. {
  45. Linkset = BSLinkset.Factory(PhysScene, this);
  46. PhysScene.TaintedObject("BSPrimLinksetCompound.Refresh", delegate()
  47. {
  48. Linkset.Refresh(this);
  49. });
  50. }
  51. public override void Destroy()
  52. {
  53. Linkset = Linkset.RemoveMeFromLinkset(this);
  54. base.Destroy();
  55. }
  56. public override void link(Manager.PhysicsActor obj)
  57. {
  58. BSPrimLinkable parent = obj as BSPrimLinkable;
  59. if (parent != null)
  60. {
  61. BSPhysObject parentBefore = Linkset.LinksetRoot;
  62. int childrenBefore = Linkset.NumberOfChildren;
  63. Linkset = parent.Linkset.AddMeToLinkset(this);
  64. DetailLog("{0},BSPrimLinkset.link,call,parentBefore={1}, childrenBefore=={2}, parentAfter={3}, childrenAfter={4}",
  65. LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren);
  66. }
  67. return;
  68. }
  69. public override void delink()
  70. {
  71. // TODO: decide if this parent checking needs to happen at taint time
  72. // Race condition here: if link() and delink() in same simulation tick, the delink will not happen
  73. BSPhysObject parentBefore = Linkset.LinksetRoot;
  74. int childrenBefore = Linkset.NumberOfChildren;
  75. Linkset = Linkset.RemoveMeFromLinkset(this);
  76. DetailLog("{0},BSPrimLinkset.delink,parentBefore={1},childrenBefore={2},parentAfter={3},childrenAfter={4}, ",
  77. LocalID, parentBefore.LocalID, childrenBefore, Linkset.LinksetRoot.LocalID, Linkset.NumberOfChildren);
  78. return;
  79. }
  80. // When simulator changes position, this might be moving a child of the linkset.
  81. public override OMV.Vector3 Position
  82. {
  83. get { return base.Position; }
  84. set
  85. {
  86. base.Position = value;
  87. PhysScene.TaintedObject("BSPrimLinkset.setPosition", delegate()
  88. {
  89. Linkset.UpdateProperties(UpdatedProperties.Position, this);
  90. });
  91. }
  92. }
  93. // When simulator changes orientation, this might be moving a child of the linkset.
  94. public override OMV.Quaternion Orientation
  95. {
  96. get { return base.Orientation; }
  97. set
  98. {
  99. base.Orientation = value;
  100. PhysScene.TaintedObject("BSPrimLinkset.setOrientation", delegate()
  101. {
  102. Linkset.UpdateProperties(UpdatedProperties.Orientation, this);
  103. });
  104. }
  105. }
  106. public override float TotalMass
  107. {
  108. get { return Linkset.LinksetMass; }
  109. }
  110. public override void UpdatePhysicalParameters()
  111. {
  112. base.UpdatePhysicalParameters();
  113. // Recompute any linkset parameters.
  114. // When going from non-physical to physical, this re-enables the constraints that
  115. // had been automatically disabled when the mass was set to zero.
  116. // For compound based linksets, this enables and disables interactions of the children.
  117. if (Linkset != null) // null can happen during initialization
  118. Linkset.Refresh(this);
  119. }
  120. protected override void MakeDynamic(bool makeStatic)
  121. {
  122. base.MakeDynamic(makeStatic);
  123. if (makeStatic)
  124. Linkset.MakeStatic(this);
  125. else
  126. Linkset.MakeDynamic(this);
  127. }
  128. // Body is being taken apart. Remove physical dependencies and schedule a rebuild.
  129. protected override void RemoveDependencies()
  130. {
  131. Linkset.RemoveDependencies(this);
  132. base.RemoveDependencies();
  133. }
  134. public override void UpdateProperties(EntityProperties entprop)
  135. {
  136. if (Linkset.IsRoot(this))
  137. {
  138. // Properties are only updated for the roots of a linkset.
  139. // TODO: this will have to change when linksets are articulated.
  140. base.UpdateProperties(entprop);
  141. }
  142. /*
  143. else
  144. {
  145. // For debugging, report the movement of children
  146. DetailLog("{0},BSPrim.UpdateProperties,child,pos={1},orient={2},vel={3},accel={4},rotVel={5}",
  147. LocalID, entprop.Position, entprop.Rotation, entprop.Velocity,
  148. entprop.Acceleration, entprop.RotationalVelocity);
  149. }
  150. */
  151. // The linkset might like to know about changing locations
  152. Linkset.UpdateProperties(UpdatedProperties.EntPropUpdates, this);
  153. }
  154. public override bool Collide(uint collidingWith, BSPhysObject collidee,
  155. OMV.Vector3 contactPoint, OMV.Vector3 contactNormal, float pentrationDepth)
  156. {
  157. // prims in the same linkset cannot collide with each other
  158. BSPrimLinkable convCollidee = collidee as BSPrimLinkable;
  159. if (convCollidee != null && (this.Linkset.LinksetID == convCollidee.Linkset.LinksetID))
  160. {
  161. return false;
  162. }
  163. // TODO: handle collisions of other objects with with children of linkset.
  164. // This is a problem for LinksetCompound since the children are packed into the root.
  165. return base.Collide(collidingWith, collidee, contactPoint, contactNormal, pentrationDepth);
  166. }
  167. }
  168. }