1
0

BSActorHover.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 BSActorHover : BSActor
  36. {
  37. private BSFMotor m_hoverMotor;
  38. public BSActorHover(BSScene physicsScene, BSPhysObject pObj, string actorName)
  39. : base(physicsScene, pObj, actorName)
  40. {
  41. m_hoverMotor = null;
  42. m_physicsScene.DetailLog("{0},BSActorHover,constructor", m_controllingPrim.LocalID);
  43. }
  44. // BSActor.isActive
  45. public override bool isActive
  46. {
  47. get { return Enabled; }
  48. }
  49. // Release any connections and resources used by the actor.
  50. // BSActor.Dispose()
  51. public override void Dispose()
  52. {
  53. Enabled = false;
  54. DeactivateHover();
  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},BSActorHover,refresh", m_controllingPrim.LocalID);
  62. // If not active any more, turn me off
  63. if (!m_controllingPrim.HoverActive)
  64. {
  65. SetEnabled(false);
  66. }
  67. // If the object is physically active, add the hoverer prestep action
  68. if (isActive)
  69. {
  70. ActivateHover();
  71. }
  72. else
  73. {
  74. DeactivateHover();
  75. }
  76. }
  77. // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...).
  78. // Register a prestep action to restore physical requirements before the next simulation step.
  79. // Called at taint-time.
  80. // BSActor.RemoveDependencies()
  81. public override void RemoveDependencies()
  82. {
  83. // Nothing to do for the hoverer since it is all software at pre-step action time.
  84. }
  85. // If a hover motor has not been created, create one and start the hovering.
  86. private void ActivateHover()
  87. {
  88. if (m_hoverMotor == null)
  89. {
  90. // Turning the target on
  91. m_hoverMotor = new BSFMotor("BSActorHover",
  92. m_controllingPrim.HoverTau, // timeScale
  93. BSMotor.Infinite, // decay time scale
  94. 1f // efficiency
  95. );
  96. m_hoverMotor.SetTarget(ComputeCurrentHoverHeight());
  97. m_hoverMotor.SetCurrent(m_controllingPrim.RawPosition.Z);
  98. m_hoverMotor.PhysicsScene = m_physicsScene; // DEBUG DEBUG so motor will output detail log messages.
  99. m_physicsScene.BeforeStep += Hoverer;
  100. }
  101. }
  102. private void DeactivateHover()
  103. {
  104. if (m_hoverMotor != null)
  105. {
  106. m_physicsScene.BeforeStep -= Hoverer;
  107. m_hoverMotor = null;
  108. }
  109. }
  110. // Called just before the simulation step. Update the vertical position for hoverness.
  111. private void Hoverer(float timeStep)
  112. {
  113. // Don't do hovering while the object is selected.
  114. if (!isActive)
  115. return;
  116. m_hoverMotor.SetCurrent(m_controllingPrim.RawPosition.Z);
  117. m_hoverMotor.SetTarget(ComputeCurrentHoverHeight());
  118. float targetHeight = m_hoverMotor.Step(timeStep);
  119. // 'targetHeight' is where we'd like the Z of the prim to be at this moment.
  120. // Compute the amount of force to push us there.
  121. float moveForce = (targetHeight - m_controllingPrim.RawPosition.Z) * m_controllingPrim.RawMass;
  122. // Undo anything the object thinks it's doing at the moment
  123. moveForce = -m_controllingPrim.RawVelocity.Z * m_controllingPrim.Mass;
  124. m_physicsScene.PE.ApplyCentralImpulse(m_controllingPrim.PhysBody, new OMV.Vector3(0f, 0f, moveForce));
  125. m_physicsScene.DetailLog("{0},BSPrim.Hover,move,targHt={1},moveForce={2},mass={3}",
  126. m_controllingPrim.LocalID, targetHeight, moveForce, m_controllingPrim.RawMass);
  127. }
  128. // Based on current position, determine what we should be hovering at now.
  129. // Must recompute often. What if we walked offa cliff>
  130. private float ComputeCurrentHoverHeight()
  131. {
  132. float ret = m_controllingPrim.HoverHeight;
  133. float groundHeight = m_physicsScene.TerrainManager.GetTerrainHeightAtXYZ(m_controllingPrim.RawPosition);
  134. switch (m_controllingPrim.HoverType)
  135. {
  136. case PIDHoverType.Ground:
  137. ret = groundHeight + m_controllingPrim.HoverHeight;
  138. break;
  139. case PIDHoverType.GroundAndWater:
  140. float waterHeight = m_physicsScene.TerrainManager.GetWaterLevelAtXYZ(m_controllingPrim.RawPosition);
  141. if (groundHeight > waterHeight)
  142. {
  143. ret = groundHeight + m_controllingPrim.HoverHeight;
  144. }
  145. else
  146. {
  147. ret = waterHeight + m_controllingPrim.HoverHeight;
  148. }
  149. break;
  150. }
  151. return ret;
  152. }
  153. }
  154. }