BSPrimDisplaced.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.PhysicsModules.SharedBase;
  34. using OMV = OpenMetaverse;
  35. namespace OpenSim.Region.PhysicsModule.BulletS
  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. Vector3 comDisp;
  78. if (UserSetCenterOfMassDisplacement.HasValue)
  79. comDisp = (OMV.Vector3)UserSetCenterOfMassDisplacement;
  80. else
  81. comDisp = centerOfMassDisplacement;
  82. // Eliminate any jitter caused be very slight differences in masses and positions
  83. if (comDisp.ApproxEquals(Vector3.Zero, 0.01f) )
  84. comDisp = Vector3.Zero;
  85. DetailLog("{0},BSPrimDisplaced.SetEffectiveCenterOfMassDisplacement,userSet={1},comDisp={2}",
  86. LocalID, UserSetCenterOfMassDisplacement.HasValue, comDisp);
  87. if ( !comDisp.ApproxEquals(PositionDisplacement, 0.01f) )
  88. {
  89. // Displacement setting is changing.
  90. // The relationship between the physical object and simulated object must be aligned.
  91. PositionDisplacement = comDisp;
  92. this.ForcePosition = RawPosition;
  93. }
  94. return PositionDisplacement;
  95. }
  96. // 'ForcePosition' is the one way to set the physical position of the body in the physics engine.
  97. // Displace the simulator idea of position (center of root prim) to the physical position.
  98. public override Vector3 ForcePosition
  99. {
  100. get {
  101. OMV.Vector3 physPosition = PhysScene.PE.GetPosition(PhysBody);
  102. if (PositionDisplacement != OMV.Vector3.Zero)
  103. {
  104. // If there is some displacement, return the physical position (center-of-mass)
  105. // location minus the displacement to give the center of the root prim.
  106. OMV.Vector3 displacement = PositionDisplacement * ForceOrientation;
  107. DetailLog("{0},BSPrimDisplaced.ForcePosition,get,physPos={1},disp={2},simPos={3}",
  108. LocalID, physPosition, displacement, physPosition - displacement);
  109. physPosition -= displacement;
  110. }
  111. RawPosition = physPosition;
  112. return physPosition;
  113. }
  114. set
  115. {
  116. if (PositionDisplacement != OMV.Vector3.Zero)
  117. {
  118. // This value is the simulator's idea of where the prim is: the center of the root prim
  119. RawPosition = value;
  120. // Move the passed root prim postion to the center-of-mass position and set in the physics engine.
  121. OMV.Vector3 displacement = PositionDisplacement * RawOrientation;
  122. OMV.Vector3 displacedPos = RawPosition + displacement;
  123. DetailLog("{0},BSPrimDisplaced.ForcePosition,set,simPos={1},disp={2},physPos={3}",
  124. LocalID, RawPosition, displacement, displacedPos);
  125. if (PhysBody.HasPhysicalBody)
  126. {
  127. PhysScene.PE.SetTranslation(PhysBody, displacedPos, RawOrientation);
  128. ActivateIfPhysical(false);
  129. }
  130. }
  131. else
  132. {
  133. base.ForcePosition = value;
  134. }
  135. }
  136. }
  137. // These are also overridden by BSPrimLinkable if the prim can be part of a linkset
  138. public override OMV.Vector3 CenterOfMass
  139. {
  140. get { return RawPosition; }
  141. }
  142. public override OMV.Vector3 GeometricCenter
  143. {
  144. get { return RawPosition; }
  145. }
  146. public override void UpdateProperties(EntityProperties entprop)
  147. {
  148. // Undo any center-of-mass displacement that might have been done.
  149. if (PositionDisplacement != OMV.Vector3.Zero)
  150. {
  151. // The origional shape was offset from 'zero' by PositionDisplacement.
  152. // These physical location must be back converted to be centered around the displaced
  153. // root shape.
  154. // Move the returned center-of-mass location to the root prim location.
  155. OMV.Vector3 displacement = PositionDisplacement * entprop.Rotation;
  156. OMV.Vector3 displacedPos = entprop.Position - displacement;
  157. DetailLog("{0},BSPrimDisplaced.UpdateProperties,physPos={1},disp={2},simPos={3}",
  158. LocalID, entprop.Position, displacement, displacedPos);
  159. entprop.Position = displacedPos;
  160. }
  161. base.UpdateProperties(entprop);
  162. }
  163. }
  164. }