BSActorAvatarMove.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 BSActorAvatarMove : BSActor
  36. {
  37. BSVMotor m_velocityMotor;
  38. // Set to true if we think we're going up stairs.
  39. // This state is remembered because collisions will turn on and off as we go up stairs.
  40. int m_walkingUpStairs;
  41. float m_lastStepUp;
  42. public BSActorAvatarMove(BSScene physicsScene, BSPhysObject pObj, string actorName)
  43. : base(physicsScene, pObj, actorName)
  44. {
  45. m_velocityMotor = null;
  46. m_walkingUpStairs = 0;
  47. m_physicsScene.DetailLog("{0},BSActorAvatarMove,constructor", m_controllingPrim.LocalID);
  48. }
  49. // BSActor.isActive
  50. public override bool isActive
  51. {
  52. get { return Enabled && m_controllingPrim.IsPhysicallyActive; }
  53. }
  54. // Release any connections and resources used by the actor.
  55. // BSActor.Dispose()
  56. public override void Dispose()
  57. {
  58. Enabled = false;
  59. }
  60. // Called when physical parameters (properties set in Bullet) need to be re-applied.
  61. // Called at taint-time.
  62. // BSActor.Refresh()
  63. public override void Refresh()
  64. {
  65. m_physicsScene.DetailLog("{0},BSActorAvatarMove,refresh", m_controllingPrim.LocalID);
  66. // If the object is physically active, add the hoverer prestep action
  67. if (isActive)
  68. {
  69. ActivateAvatarMove();
  70. }
  71. else
  72. {
  73. DeactivateAvatarMove();
  74. }
  75. }
  76. // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...).
  77. // Register a prestep action to restore physical requirements before the next simulation step.
  78. // Called at taint-time.
  79. // BSActor.RemoveDependencies()
  80. public override void RemoveDependencies()
  81. {
  82. // Nothing to do for the hoverer since it is all software at pre-step action time.
  83. }
  84. // Usually called when target velocity changes to set the current velocity and the target
  85. // into the movement motor.
  86. public void SetVelocityAndTarget(OMV.Vector3 vel, OMV.Vector3 targ, bool inTaintTime)
  87. {
  88. m_physicsScene.TaintedObject(inTaintTime, "BSActorAvatarMove.setVelocityAndTarget", delegate()
  89. {
  90. if (m_velocityMotor != null)
  91. {
  92. m_velocityMotor.Reset();
  93. m_velocityMotor.SetTarget(targ);
  94. m_velocityMotor.SetCurrent(vel);
  95. m_velocityMotor.Enabled = true;
  96. }
  97. });
  98. }
  99. // If a hover motor has not been created, create one and start the hovering.
  100. private void ActivateAvatarMove()
  101. {
  102. if (m_velocityMotor == null)
  103. {
  104. // Infinite decay and timescale values so motor only changes current to target values.
  105. m_velocityMotor = new BSVMotor("BSCharacter.Velocity",
  106. 0.2f, // time scale
  107. BSMotor.Infinite, // decay time scale
  108. 1f // efficiency
  109. );
  110. // _velocityMotor.PhysicsScene = PhysicsScene; // DEBUG DEBUG so motor will output detail log messages.
  111. SetVelocityAndTarget(m_controllingPrim.RawVelocity, m_controllingPrim.TargetVelocity, true /* inTaintTime */);
  112. m_physicsScene.BeforeStep += Mover;
  113. m_walkingUpStairs = 0;
  114. }
  115. }
  116. private void DeactivateAvatarMove()
  117. {
  118. if (m_velocityMotor != null)
  119. {
  120. m_physicsScene.BeforeStep -= Mover;
  121. m_velocityMotor = null;
  122. }
  123. }
  124. // Called just before the simulation step. Update the vertical position for hoverness.
  125. private void Mover(float timeStep)
  126. {
  127. // Don't do movement while the object is selected.
  128. if (!isActive)
  129. return;
  130. // TODO: Decide if the step parameters should be changed depending on the avatar's
  131. // state (flying, colliding, ...). There is code in ODE to do this.
  132. // COMMENTARY: when the user is making the avatar walk, except for falling, the velocity
  133. // specified for the avatar is the one that should be used. For falling, if the avatar
  134. // is not flying and is not colliding then it is presumed to be falling and the Z
  135. // component is not fooled with (thus allowing gravity to do its thing).
  136. // When the avatar is standing, though, the user has specified a velocity of zero and
  137. // the avatar should be standing. But if the avatar is pushed by something in the world
  138. // (raising elevator platform, moving vehicle, ...) the avatar should be allowed to
  139. // move. Thus, the velocity cannot be forced to zero. The problem is that small velocity
  140. // errors can creap in and the avatar will slowly float off in some direction.
  141. // So, the problem is that, when an avatar is standing, we cannot tell creaping error
  142. // from real pushing.
  143. // The code below uses whether the collider is static or moving to decide whether to zero motion.
  144. m_velocityMotor.Step(timeStep);
  145. m_controllingPrim.IsStationary = false;
  146. // If we're not supposed to be moving, make sure things are zero.
  147. if (m_velocityMotor.ErrorIsZero() && m_velocityMotor.TargetValue == OMV.Vector3.Zero)
  148. {
  149. // The avatar shouldn't be moving
  150. m_velocityMotor.Zero();
  151. if (m_controllingPrim.IsColliding)
  152. {
  153. // If we are colliding with a stationary object, presume we're standing and don't move around
  154. if (!m_controllingPrim.ColliderIsMoving)
  155. {
  156. m_physicsScene.DetailLog("{0},BSCharacter.MoveMotor,collidingWithStationary,zeroingMotion", m_controllingPrim.LocalID);
  157. m_controllingPrim.IsStationary = true;
  158. m_controllingPrim.ZeroMotion(true /* inTaintTime */);
  159. }
  160. // Standing has more friction on the ground
  161. if (m_controllingPrim.Friction != BSParam.AvatarStandingFriction)
  162. {
  163. m_controllingPrim.Friction = BSParam.AvatarStandingFriction;
  164. m_physicsScene.PE.SetFriction(m_controllingPrim.PhysBody, m_controllingPrim.Friction);
  165. }
  166. }
  167. else
  168. {
  169. if (m_controllingPrim.Flying)
  170. {
  171. // Flying and not collising and velocity nearly zero.
  172. m_controllingPrim.ZeroMotion(true /* inTaintTime */);
  173. }
  174. }
  175. m_physicsScene.DetailLog("{0},BSCharacter.MoveMotor,taint,stopping,target={1},colliding={2}",
  176. m_controllingPrim.LocalID, m_velocityMotor.TargetValue, m_controllingPrim.IsColliding);
  177. }
  178. else
  179. {
  180. // Supposed to be moving.
  181. OMV.Vector3 stepVelocity = m_velocityMotor.CurrentValue;
  182. if (m_controllingPrim.Friction != BSParam.AvatarFriction)
  183. {
  184. // Probably starting up walking. Set friction to moving friction.
  185. m_controllingPrim.Friction = BSParam.AvatarFriction;
  186. m_physicsScene.PE.SetFriction(m_controllingPrim.PhysBody, m_controllingPrim.Friction);
  187. }
  188. // If falling, we keep the world's downward vector no matter what the other axis specify.
  189. // The check for RawVelocity.Z < 0 makes jumping work (temporary upward force).
  190. if (!m_controllingPrim.Flying && !m_controllingPrim.IsColliding)
  191. {
  192. if (m_controllingPrim.RawVelocity.Z < 0)
  193. stepVelocity.Z = m_controllingPrim.RawVelocity.Z;
  194. // DetailLog("{0},BSCharacter.MoveMotor,taint,overrideStepZWithWorldZ,stepVel={1}", LocalID, stepVelocity);
  195. }
  196. // 'stepVelocity' is now the speed we'd like the avatar to move in. Turn that into an instantanous force.
  197. OMV.Vector3 moveForce = (stepVelocity - m_controllingPrim.RawVelocity) * m_controllingPrim.Mass;
  198. // Add special movement force to allow avatars to walk up stepped surfaces.
  199. moveForce += WalkUpStairs();
  200. m_physicsScene.DetailLog("{0},BSCharacter.MoveMotor,move,stepVel={1},vel={2},mass={3},moveForce={4}",
  201. m_controllingPrim.LocalID, stepVelocity, m_controllingPrim.RawVelocity, m_controllingPrim.Mass, moveForce);
  202. m_physicsScene.PE.ApplyCentralImpulse(m_controllingPrim.PhysBody, moveForce);
  203. }
  204. }
  205. // Decide if the character is colliding with a low object and compute a force to pop the
  206. // avatar up so it can walk up and over the low objects.
  207. private OMV.Vector3 WalkUpStairs()
  208. {
  209. OMV.Vector3 ret = OMV.Vector3.Zero;
  210. m_physicsScene.DetailLog("{0},BSCharacter.WalkUpStairs,IsColliding={1},flying={2},targSpeed={3},collisions={4},avHeight={5}",
  211. m_controllingPrim.LocalID, m_controllingPrim.IsColliding, m_controllingPrim.Flying,
  212. m_controllingPrim.TargetVelocitySpeed, m_controllingPrim.CollisionsLastTick.Count, m_controllingPrim.Size.Z);
  213. // This test is done if moving forward, not flying and is colliding with something.
  214. // Check for stairs climbing if colliding, not flying and moving forward
  215. if ( m_controllingPrim.IsColliding
  216. && !m_controllingPrim.Flying
  217. && m_controllingPrim.TargetVelocitySpeed > 0.1f )
  218. {
  219. // The range near the character's feet where we will consider stairs
  220. // float nearFeetHeightMin = m_controllingPrim.RawPosition.Z - (m_controllingPrim.Size.Z / 2f) + 0.05f;
  221. // Note: there is a problem with the computation of the capsule height. Thus RawPosition is off
  222. // from the height. Revisit size and this computation when height is scaled properly.
  223. float nearFeetHeightMin = m_controllingPrim.RawPosition.Z - (m_controllingPrim.Size.Z / 2f) - 0.05f;
  224. float nearFeetHeightMax = nearFeetHeightMin + BSParam.AvatarStepHeight;
  225. // Look for a collision point that is near the character's feet and is oriented the same as the charactor is.
  226. // Find the highest 'good' collision.
  227. OMV.Vector3 highestTouchPosition = OMV.Vector3.Zero;
  228. foreach (KeyValuePair<uint, ContactPoint> kvp in m_controllingPrim.CollisionsLastTick.m_objCollisionList)
  229. {
  230. // Don't care about collisions with the terrain
  231. if (kvp.Key > m_physicsScene.TerrainManager.HighestTerrainID)
  232. {
  233. OMV.Vector3 touchPosition = kvp.Value.Position;
  234. m_physicsScene.DetailLog("{0},BSCharacter.WalkUpStairs,min={1},max={2},touch={3}",
  235. m_controllingPrim.LocalID, nearFeetHeightMin, nearFeetHeightMax, touchPosition);
  236. if (touchPosition.Z >= nearFeetHeightMin && touchPosition.Z <= nearFeetHeightMax)
  237. {
  238. // This contact is within the 'near the feet' range.
  239. // The normal should be our contact point to the object so it is pointing away
  240. // thus the difference between our facing orientation and the normal should be small.
  241. OMV.Vector3 directionFacing = OMV.Vector3.UnitX * m_controllingPrim.RawOrientation;
  242. OMV.Vector3 touchNormal = OMV.Vector3.Normalize(kvp.Value.SurfaceNormal);
  243. float diff = Math.Abs(OMV.Vector3.Distance(directionFacing, touchNormal));
  244. if (diff < BSParam.AvatarStepApproachFactor)
  245. {
  246. if (highestTouchPosition.Z < touchPosition.Z)
  247. highestTouchPosition = touchPosition;
  248. }
  249. }
  250. }
  251. }
  252. m_walkingUpStairs = 0;
  253. // If there is a good step sensing, move the avatar over the step.
  254. if (highestTouchPosition != OMV.Vector3.Zero)
  255. {
  256. // Remember that we are going up stairs. This is needed because collisions
  257. // will stop when we move up so this smoothes out that effect.
  258. m_walkingUpStairs = BSParam.AvatarStepSmoothingSteps;
  259. m_lastStepUp = highestTouchPosition.Z - nearFeetHeightMin;
  260. ret = ComputeStairCorrection(m_lastStepUp);
  261. m_physicsScene.DetailLog("{0},BSCharacter.WalkUpStairs,touchPos={1},nearFeetMin={2},ret={3}",
  262. m_controllingPrim.LocalID, highestTouchPosition, nearFeetHeightMin, ret);
  263. }
  264. }
  265. else
  266. {
  267. // If we used to be going up stairs but are not now, smooth the case where collision goes away while
  268. // we are bouncing up the stairs.
  269. if (m_walkingUpStairs > 0)
  270. {
  271. m_walkingUpStairs--;
  272. ret = ComputeStairCorrection(m_lastStepUp);
  273. }
  274. }
  275. return ret;
  276. }
  277. private OMV.Vector3 ComputeStairCorrection(float stepUp)
  278. {
  279. OMV.Vector3 ret = OMV.Vector3.Zero;
  280. OMV.Vector3 displacement = OMV.Vector3.Zero;
  281. if (stepUp > 0f)
  282. {
  283. // Found the stairs contact point. Push up a little to raise the character.
  284. if (BSParam.AvatarStepForceFactor > 0f)
  285. {
  286. float upForce = stepUp * m_controllingPrim.Mass * BSParam.AvatarStepForceFactor;
  287. ret = new OMV.Vector3(0f, 0f, upForce);
  288. }
  289. // Also move the avatar up for the new height
  290. if (BSParam.AvatarStepUpCorrectionFactor > 0f)
  291. {
  292. // Move the avatar up related to the height of the collision
  293. displacement = new OMV.Vector3(0f, 0f, stepUp * BSParam.AvatarStepUpCorrectionFactor);
  294. m_controllingPrim.ForcePosition = m_controllingPrim.RawPosition + displacement;
  295. }
  296. else
  297. {
  298. if (BSParam.AvatarStepUpCorrectionFactor < 0f)
  299. {
  300. // Move the avatar up about the specified step height
  301. displacement = new OMV.Vector3(0f, 0f, BSParam.AvatarStepHeight);
  302. m_controllingPrim.ForcePosition = m_controllingPrim.RawPosition + displacement;
  303. }
  304. }
  305. m_physicsScene.DetailLog("{0},BSCharacter.WalkUpStairs.ComputeStairCorrection,disp={1},force={2}",
  306. m_controllingPrim.LocalID, displacement, ret);
  307. }
  308. return ret;
  309. }
  310. }
  311. }