BSPrimDisplaced.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 copyright
  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.Reflection;
  30. using System.Runtime.InteropServices;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Physics.Manager;
  34. using OMV = OpenMetaverse;
  35. namespace OpenSim.Region.Physics.BulletSPlugin
  36. {
  37. public class BSPrimDisplaced : BSPrim
  38. {
  39. // The purpose of this subclass is to do any mapping between what the simulator thinks
  40. // the prim position and orientation is and what the physical position/orientation.
  41. // This difference happens because Bullet assumes the center-of-mass is the <0,0,0>
  42. // of the prim/linkset. The simulator, on the other hand, tracks the location of
  43. // the prim/linkset by the location of the root prim. So, if center-of-mass is anywhere
  44. // but the origin of the root prim, the physical origin is displaced from the simulator origin.
  45. //
  46. // This routine works by capturing ForcePosition and
  47. // adjusting the simulator values (being set) into the physical values.
  48. // The conversion is also done in the opposite direction (physical origin -> simulator origin).
  49. //
  50. // The updateParameter call is also captured and the values from the physics engine
  51. // are converted into simulator origin values before being passed to the base
  52. // class.
  53. // PositionDisplacement is the vehicle relative distance from the root prim position to the center-of-mass.
  54. public virtual OMV.Vector3 PositionDisplacement { get; set; }
  55. public BSPrimDisplaced(uint localID, String primName, BSScene parent_scene, OMV.Vector3 pos, OMV.Vector3 size,
  56. OMV.Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical)
  57. : base(localID, primName, parent_scene, pos, size, rotation, pbs, pisPhysical)
  58. {
  59. ClearDisplacement();
  60. }
  61. // Clears any center-of-mass displacement introduced by linksets, etc.
  62. // Does not clear the displacement set by the user.
  63. public void ClearDisplacement()
  64. {
  65. if (UserSetCenterOfMassDisplacement.HasValue)
  66. PositionDisplacement = (OMV.Vector3)UserSetCenterOfMassDisplacement;
  67. else
  68. PositionDisplacement = OMV.Vector3.Zero;
  69. }
  70. // Set this sets and computes the displacement from the passed prim to the center-of-mass.
  71. // A user set value for center-of-mass overrides whatever might be passed in here.
  72. // The displacement is in local coordinates (relative to root prim in linkset oriented coordinates).
  73. // Returns the relative offset from the root position to the center-of-mass.
  74. // Called at taint time.
  75. public virtual Vector3 SetEffectiveCenterOfMassDisplacement(Vector3 centerOfMassDisplacement)
  76. {
  77. PhysScene.AssertInTaintTime("BSPrimDisplaced.SetEffectiveCenterOfMassDisplacement");
  78. Vector3 comDisp;
  79. if (UserSetCenterOfMassDisplacement.HasValue)
  80. comDisp = (OMV.Vector3)UserSetCenterOfMassDisplacement;
  81. else
  82. comDisp = centerOfMassDisplacement;
  83. // Eliminate any jitter caused be very slight differences in masses and positions
  84. if (comDisp.ApproxEquals(Vector3.Zero, 0.01f) )
  85. comDisp = Vector3.Zero;
  86. DetailLog("{0},BSPrimDisplaced.SetEffectiveCenterOfMassDisplacement,userSet={1},comDisp={2}",
  87. LocalID, UserSetCenterOfMassDisplacement.HasValue, comDisp);
  88. if ( !comDisp.ApproxEquals(PositionDisplacement, 0.01f) )
  89. {
  90. // Displacement setting is changing.
  91. // The relationship between the physical object and simulated object must be aligned.
  92. PositionDisplacement = comDisp;
  93. this.ForcePosition = RawPosition;
  94. }
  95. return PositionDisplacement;
  96. }
  97. // 'ForcePosition' is the one way to set the physical position of the body in the physics engine.
  98. // Displace the simulator idea of position (center of root prim) to the physical position.
  99. public override Vector3 ForcePosition
  100. {
  101. get {
  102. OMV.Vector3 physPosition = PhysScene.PE.GetPosition(PhysBody);
  103. if (PositionDisplacement != OMV.Vector3.Zero)
  104. {
  105. // If there is some displacement, return the physical position (center-of-mass)
  106. // location minus the displacement to give the center of the root prim.
  107. OMV.Vector3 displacement = PositionDisplacement * ForceOrientation;
  108. DetailLog("{0},BSPrimDisplaced.ForcePosition,get,physPos={1},disp={2},simPos={3}",
  109. LocalID, physPosition, displacement, physPosition - displacement);
  110. physPosition -= displacement;
  111. }
  112. RawPosition = physPosition;
  113. return physPosition;
  114. }
  115. set
  116. {
  117. if (PositionDisplacement != OMV.Vector3.Zero)
  118. {
  119. // This value is the simulator's idea of where the prim is: the center of the root prim
  120. RawPosition = value;
  121. // Move the passed root prim postion to the center-of-mass position and set in the physics engine.
  122. OMV.Vector3 displacement = PositionDisplacement * RawOrientation;
  123. OMV.Vector3 displacedPos = RawPosition + displacement;
  124. DetailLog("{0},BSPrimDisplaced.ForcePosition,set,simPos={1},disp={2},physPos={3}",
  125. LocalID, RawPosition, displacement, displacedPos);
  126. if (PhysBody.HasPhysicalBody)
  127. {
  128. PhysScene.PE.SetTranslation(PhysBody, displacedPos, RawOrientation);
  129. ActivateIfPhysical(false);
  130. }
  131. }
  132. else
  133. {
  134. base.ForcePosition = value;
  135. }
  136. }
  137. }
  138. // These are also overridden by BSPrimLinkable if the prim can be part of a linkset
  139. public override OMV.Vector3 CenterOfMass
  140. {
  141. get { return RawPosition; }
  142. }
  143. public override OMV.Vector3 GeometricCenter
  144. {
  145. get { return RawPosition; }
  146. }
  147. public override void UpdateProperties(EntityProperties entprop)
  148. {
  149. // Undo any center-of-mass displacement that might have been done.
  150. if (PositionDisplacement != OMV.Vector3.Zero)
  151. {
  152. // The origional shape was offset from 'zero' by PositionDisplacement.
  153. // These physical location must be back converted to be centered around the displaced
  154. // root shape.
  155. // Move the returned center-of-mass location to the root prim location.
  156. OMV.Vector3 displacement = PositionDisplacement * entprop.Rotation;
  157. OMV.Vector3 displacedPos = entprop.Position - displacement;
  158. DetailLog("{0},BSPrimDisplaced.UpdateProperties,physPos={1},disp={2},simPos={3}",
  159. LocalID, entprop.Position, displacement, displacedPos);
  160. entprop.Position = displacedPos;
  161. }
  162. base.UpdateProperties(entprop);
  163. }
  164. }
  165. }