1
0

BSActorSetTorque.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 BSActorSetTorque : BSActor
  36. {
  37. BSFMotor m_torqueMotor;
  38. public BSActorSetTorque(BSScene physicsScene, BSPhysObject pObj, string actorName)
  39. : base(physicsScene, pObj, actorName)
  40. {
  41. m_torqueMotor = null;
  42. m_physicsScene.DetailLog("{0},BSActorSetTorque,constructor", m_controllingPrim.LocalID);
  43. }
  44. // BSActor.isActive
  45. public override bool isActive
  46. {
  47. get { return Enabled && m_controllingPrim.IsPhysicallyActive; }
  48. }
  49. // Release any connections and resources used by the actor.
  50. // BSActor.Dispose()
  51. public override void Dispose()
  52. {
  53. Enabled = false;
  54. }
  55. // Called when physical parameters (properties set in Bullet) need to be re-applied.
  56. // Called at taint-time.
  57. // BSActor.Refresh()
  58. public override void Refresh()
  59. {
  60. m_physicsScene.DetailLog("{0},BSActorSetTorque,refresh,torque={1}", m_controllingPrim.LocalID, m_controllingPrim.RawTorque);
  61. // If not active any more, get rid of me (shouldn't ever happen, but just to be safe)
  62. if (m_controllingPrim.RawTorque == OMV.Vector3.Zero)
  63. {
  64. m_physicsScene.DetailLog("{0},BSActorSetTorque,refresh,notSetTorque,disabling={1}", m_controllingPrim.LocalID, ActorName);
  65. Enabled = false;
  66. return;
  67. }
  68. // If the object is physically active, add the hoverer prestep action
  69. if (isActive)
  70. {
  71. ActivateSetTorque();
  72. }
  73. else
  74. {
  75. DeactivateSetTorque();
  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 hoverer 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 ActivateSetTorque()
  88. {
  89. if (m_torqueMotor == null)
  90. {
  91. // A fake motor that might be used someday
  92. m_torqueMotor = new BSFMotor("setTorque", 1f, 1f, 1f);
  93. m_physicsScene.BeforeStep += Mover;
  94. }
  95. }
  96. private void DeactivateSetTorque()
  97. {
  98. if (m_torqueMotor != null)
  99. {
  100. m_physicsScene.BeforeStep -= Mover;
  101. m_torqueMotor = null;
  102. }
  103. }
  104. // Called just before the simulation step. Update the vertical position for hoverness.
  105. private void Mover(float timeStep)
  106. {
  107. // Don't do force while the object is selected.
  108. if (!isActive)
  109. return;
  110. m_physicsScene.DetailLog("{0},BSActorSetTorque,preStep,force={1}", m_controllingPrim.LocalID, m_controllingPrim.RawTorque);
  111. if (m_controllingPrim.PhysBody.HasPhysicalBody)
  112. {
  113. m_controllingPrim.AddAngularForce(m_controllingPrim.RawTorque, false, true);
  114. m_controllingPrim.ActivateIfPhysical(false);
  115. }
  116. // TODO:
  117. }
  118. }
  119. }