BSActorMoveToTarget.cs 9.4 KB

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