BSPrimDisplaced.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * The quotations from http://wiki.secondlife.com/wiki/Linden_Vehicle_Tutorial
  28. * are Copyright (c) 2009 Linden Research, Inc and are used under their license
  29. * of Creative Commons Attribution-Share Alike 3.0
  30. * (http://creativecommons.org/licenses/by-sa/3.0/).
  31. */
  32. using System;
  33. using System.Collections.Generic;
  34. using System.Reflection;
  35. using System.Runtime.InteropServices;
  36. using OpenMetaverse;
  37. using OpenSim.Framework;
  38. using OpenSim.Region.Physics.Manager;
  39. using OMV = OpenMetaverse;
  40. namespace OpenSim.Region.Physics.BulletSPlugin
  41. {
  42. public class BSPrimDisplaced : BSPrim
  43. {
  44. // The purpose of this module is to do any mapping between what the simulator thinks
  45. // the prim position and orientation is and what the physical position/orientation.
  46. // This difference happens because Bullet assumes the center-of-mass is the <0,0,0>
  47. // of the prim/linkset. The simulator tracks the location of the prim/linkset by
  48. // the location of the root prim. So, if center-of-mass is anywhere but the origin
  49. // of the root prim, the physical origin is displaced from the simulator origin.
  50. //
  51. // This routine works by capturing the Force* setting of position/orientation/... and
  52. // adjusting the simulator values (being set) into the physical values.
  53. // The conversion is also done in the opposite direction (physical origin -> simulator origin).
  54. //
  55. // The updateParameter call is also captured and the values from the physics engine
  56. // are converted into simulator origin values before being passed to the base
  57. // class.
  58. public virtual OMV.Vector3 PositionDisplacement { get; set; }
  59. public virtual OMV.Quaternion OrientationDisplacement { get; set; }
  60. public BSPrimDisplaced(uint localID, String primName, BSScene parent_scene, OMV.Vector3 pos, OMV.Vector3 size,
  61. OMV.Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical)
  62. : base(localID, primName, parent_scene, pos, size, rotation, pbs, pisPhysical)
  63. {
  64. ClearDisplacement();
  65. }
  66. public void ClearDisplacement()
  67. {
  68. PositionDisplacement = OMV.Vector3.Zero;
  69. OrientationDisplacement = OMV.Quaternion.Identity;
  70. }
  71. // Set this sets and computes the displacement from the passed prim to the center-of-mass.
  72. // A user set value for center-of-mass overrides whatever might be passed in here.
  73. // The displacement is in local coordinates (relative to root prim in linkset oriented coordinates).
  74. public virtual void SetEffectiveCenterOfMassDisplacement(Vector3 centerOfMassDisplacement)
  75. {
  76. Vector3 comDisp;
  77. if (UserSetCenterOfMassDisplacement.HasValue)
  78. comDisp = (OMV.Vector3)UserSetCenterOfMassDisplacement;
  79. else
  80. comDisp = centerOfMassDisplacement;
  81. DetailLog("{0},BSPrimDisplaced.SetEffectiveCenterOfMassDisplacement,userSet={1},comDisp={2}",
  82. LocalID, UserSetCenterOfMassDisplacement.HasValue, comDisp);
  83. if (comDisp == Vector3.Zero)
  84. {
  85. // If there is no diplacement. Things get reset.
  86. PositionDisplacement = OMV.Vector3.Zero;
  87. OrientationDisplacement = OMV.Quaternion.Identity;
  88. }
  89. else
  90. {
  91. // Remember the displacement from root as well as the origional rotation of the
  92. // new center-of-mass.
  93. PositionDisplacement = comDisp;
  94. OrientationDisplacement = OMV.Quaternion.Identity;
  95. }
  96. }
  97. public override Vector3 ForcePosition
  98. {
  99. get { return base.ForcePosition; }
  100. set
  101. {
  102. if (PositionDisplacement != OMV.Vector3.Zero)
  103. {
  104. OMV.Vector3 displacedPos = value - (PositionDisplacement * RawOrientation);
  105. DetailLog("{0},BSPrimDisplaced.ForcePosition,val={1},disp={2},newPos={3}", LocalID, value, PositionDisplacement, displacedPos);
  106. base.ForcePosition = displacedPos;
  107. }
  108. else
  109. {
  110. base.ForcePosition = value;
  111. }
  112. }
  113. }
  114. public override Quaternion ForceOrientation
  115. {
  116. get { return base.ForceOrientation; }
  117. set
  118. {
  119. // TODO:
  120. base.ForceOrientation = value;
  121. }
  122. }
  123. // TODO: decide if this is the right place for these variables.
  124. // Somehow incorporate the optional settability by the user.
  125. // Is this used?
  126. public override OMV.Vector3 CenterOfMass
  127. {
  128. get { return RawPosition; }
  129. }
  130. // Is this used?
  131. public override OMV.Vector3 GeometricCenter
  132. {
  133. get { return RawPosition; }
  134. }
  135. public override void UpdateProperties(EntityProperties entprop)
  136. {
  137. // Undo any center-of-mass displacement that might have been done.
  138. if (PositionDisplacement != OMV.Vector3.Zero || OrientationDisplacement != OMV.Quaternion.Identity)
  139. {
  140. // Correct for any rotation around the center-of-mass
  141. // TODO!!!
  142. OMV.Vector3 displacedPos = entprop.Position + (PositionDisplacement * entprop.Rotation);
  143. DetailLog("{0},BSPrimDisplaced.ForcePosition,physPos={1},disp={2},newPos={3}", LocalID, entprop.Position, PositionDisplacement, displacedPos);
  144. entprop.Position = displacedPos;
  145. // entprop.Rotation = something;
  146. }
  147. base.UpdateProperties(entprop);
  148. }
  149. }
  150. }