BSActorMoveToTarget.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.Region.PhysicsModules.SharedBase;
  32. using OMV = OpenMetaverse;
  33. namespace OpenSim.Region.PhysicsModule.BulletS
  34. {
  35. public class BSActorMoveToTarget : BSActor
  36. {
  37. private BSVMotor m_targetMotor;
  38. public BSActorMoveToTarget(BSScene physicsScene, BSPhysObject pObj, string actorName)
  39. : base(physicsScene, pObj, actorName)
  40. {
  41. m_targetMotor = null;
  42. m_physicsScene.DetailLog("{0},BSActorMoveToTarget,constructor", m_controllingPrim.LocalID);
  43. }
  44. // BSActor.isActive
  45. public override bool isActive
  46. {
  47. // MoveToTarget only works on physical prims
  48. get { return Enabled && m_controllingPrim.IsPhysicallyActive; }
  49. }
  50. // Release any connections and resources used by the actor.
  51. // BSActor.Dispose()
  52. public override void Dispose()
  53. {
  54. Enabled = false;
  55. DeactivateMoveToTarget();
  56. }
  57. // Called when physical parameters (properties set in Bullet) need to be re-applied.
  58. // Called at taint-time.
  59. // BSActor.Refresh()
  60. public override void Refresh()
  61. {
  62. m_physicsScene.DetailLog("{0},BSActorMoveToTarget,refresh,enabled={1},active={2},target={3},tau={4}",
  63. m_controllingPrim.LocalID, Enabled, m_controllingPrim.MoveToTargetActive,
  64. m_controllingPrim.MoveToTargetTarget, m_controllingPrim.MoveToTargetTau );
  65. // If not active any more...
  66. if (!m_controllingPrim.MoveToTargetActive)
  67. {
  68. Enabled = false;
  69. }
  70. if (isActive)
  71. {
  72. ActivateMoveToTarget();
  73. }
  74. else
  75. {
  76. DeactivateMoveToTarget();
  77. }
  78. }
  79. // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...).
  80. // Register a prestep action to restore physical requirements before the next simulation step.
  81. // Called at taint-time.
  82. // BSActor.RemoveDependencies()
  83. public override void RemoveDependencies()
  84. {
  85. // Nothing to do for the moveToTarget since it is all software at pre-step action time.
  86. }
  87. // If a hover motor has not been created, create one and start the hovering.
  88. private void ActivateMoveToTarget()
  89. {
  90. if (m_targetMotor == null)
  91. {
  92. // We're taking over after this.
  93. m_controllingPrim.ZeroMotion(true);
  94. /* Someday use the PID controller
  95. m_targetMotor = new BSPIDVMotor("BSActorMoveToTarget-" + m_controllingPrim.LocalID.ToString());
  96. m_targetMotor.TimeScale = m_controllingPrim.MoveToTargetTau;
  97. m_targetMotor.Efficiency = 1f;
  98. */
  99. m_targetMotor = new BSVMotor("BSActorMoveToTarget-" + m_controllingPrim.LocalID.ToString(),
  100. m_controllingPrim.MoveToTargetTau, // timeScale
  101. BSMotor.Infinite, // decay time scale
  102. 1f // efficiency
  103. );
  104. m_targetMotor.PhysicsScene = m_physicsScene; // DEBUG DEBUG so motor will output detail log messages.
  105. m_targetMotor.SetTarget(m_controllingPrim.MoveToTargetTarget);
  106. m_targetMotor.SetCurrent(m_controllingPrim.RawPosition);
  107. // m_physicsScene.BeforeStep += Mover;
  108. m_physicsScene.BeforeStep += Mover2;
  109. }
  110. else
  111. {
  112. // If already allocated, make sure the target and other paramters are current
  113. m_targetMotor.SetTarget(m_controllingPrim.MoveToTargetTarget);
  114. m_targetMotor.SetCurrent(m_controllingPrim.RawPosition);
  115. }
  116. }
  117. private void DeactivateMoveToTarget()
  118. {
  119. if (m_targetMotor != null)
  120. {
  121. // m_physicsScene.BeforeStep -= Mover;
  122. m_physicsScene.BeforeStep -= Mover2;
  123. m_targetMotor = null;
  124. }
  125. }
  126. // Origional mover that set the objects position to move to the target.
  127. // The problem was that gravity would keep trying to push the object down so
  128. // the overall downward velocity would increase to infinity.
  129. // Called just before the simulation step.
  130. private void Mover(float timeStep)
  131. {
  132. // Don't do hovering while the object is selected.
  133. if (!isActive)
  134. return;
  135. OMV.Vector3 origPosition = m_controllingPrim.RawPosition; // DEBUG DEBUG (for printout below)
  136. // 'movePosition' is where we'd like the prim to be at this moment.
  137. OMV.Vector3 movePosition = m_controllingPrim.RawPosition + m_targetMotor.Step(timeStep);
  138. // If we are very close to our target, turn off the movement motor.
  139. if (m_targetMotor.ErrorIsZero())
  140. {
  141. m_physicsScene.DetailLog("{0},BSActorMoveToTarget.Mover,zeroMovement,movePos={1},pos={2},mass={3}",
  142. m_controllingPrim.LocalID, movePosition, m_controllingPrim.RawPosition, m_controllingPrim.Mass);
  143. m_controllingPrim.ForcePosition = m_targetMotor.TargetValue;
  144. m_controllingPrim.ForceVelocity = OMV.Vector3.Zero;
  145. // Setting the position does not cause the physics engine to generate a property update. Force it.
  146. m_physicsScene.PE.PushUpdate(m_controllingPrim.PhysBody);
  147. }
  148. else
  149. {
  150. m_controllingPrim.ForcePosition = movePosition;
  151. // Setting the position does not cause the physics engine to generate a property update. Force it.
  152. m_physicsScene.PE.PushUpdate(m_controllingPrim.PhysBody);
  153. }
  154. m_physicsScene.DetailLog("{0},BSActorMoveToTarget.Mover,move,fromPos={1},movePos={2}",
  155. m_controllingPrim.LocalID, origPosition, movePosition);
  156. }
  157. // Version of mover that applies forces to move the physical object to the target.
  158. // Also overcomes gravity so the object doesn't just drop to the ground.
  159. // Called just before the simulation step.
  160. private void Mover2(float timeStep)
  161. {
  162. // Don't do hovering while the object is selected.
  163. if (!isActive)
  164. return;
  165. OMV.Vector3 origPosition = m_controllingPrim.RawPosition; // DEBUG DEBUG (for printout below)
  166. OMV.Vector3 addedForce = OMV.Vector3.Zero;
  167. // CorrectionVector is the movement vector required this step
  168. OMV.Vector3 correctionVector = m_targetMotor.Step(timeStep, m_controllingPrim.RawPosition);
  169. // If we are very close to our target, turn off the movement motor.
  170. if (m_targetMotor.ErrorIsZero())
  171. {
  172. m_physicsScene.DetailLog("{0},BSActorMoveToTarget.Mover3,zeroMovement,pos={1},mass={2}",
  173. m_controllingPrim.LocalID, m_controllingPrim.RawPosition, m_controllingPrim.Mass);
  174. m_controllingPrim.ForcePosition = m_targetMotor.TargetValue;
  175. m_controllingPrim.ForceVelocity = OMV.Vector3.Zero;
  176. // Setting the position does not cause the physics engine to generate a property update. Force it.
  177. m_physicsScene.PE.PushUpdate(m_controllingPrim.PhysBody);
  178. }
  179. else
  180. {
  181. // First force to move us there -- the motor return a timestep scaled value.
  182. addedForce = correctionVector / timeStep;
  183. // Remove the existing velocity (only the moveToTarget force counts)
  184. addedForce -= m_controllingPrim.RawVelocity;
  185. // Overcome gravity.
  186. addedForce -= m_controllingPrim.Gravity;
  187. // Add enough force to overcome the mass of the object
  188. addedForce *= m_controllingPrim.Mass;
  189. m_controllingPrim.AddForce(addedForce, false /* pushForce */, true /* inTaintTime */);
  190. }
  191. m_physicsScene.DetailLog("{0},BSActorMoveToTarget.Mover3,move,fromPos={1},addedForce={2}",
  192. m_controllingPrim.LocalID, origPosition, addedForce);
  193. }
  194. }
  195. }