PhysicsScene.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 copyright
  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 OpenSim 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.Collections.Generic;
  28. using System.Reflection;
  29. using log4net;
  30. using Nini.Config;
  31. using OpenSim.Framework;
  32. using OpenMetaverse;
  33. namespace OpenSim.Region.Physics.Manager
  34. {
  35. public delegate void physicsCrash();
  36. public abstract class PhysicsScene
  37. {
  38. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  39. // The only thing that should register for this event is the SceneGraph
  40. // Anything else could cause problems.
  41. public event physicsCrash OnPhysicsCrash;
  42. public static PhysicsScene Null
  43. {
  44. get { return new NullPhysicsScene(); }
  45. }
  46. public virtual void TriggerPhysicsBasedRestart()
  47. {
  48. physicsCrash handler = OnPhysicsCrash;
  49. if (handler != null)
  50. {
  51. OnPhysicsCrash();
  52. }
  53. }
  54. public abstract void Initialise(IMesher meshmerizer, IConfigSource config);
  55. public abstract PhysicsActor AddAvatar(string avName, PhysicsVector position, PhysicsVector size, bool isFlying);
  56. public abstract void RemoveAvatar(PhysicsActor actor);
  57. public abstract void RemovePrim(PhysicsActor prim);
  58. public abstract PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  59. PhysicsVector size, Quaternion rotation); //To be removed
  60. public abstract PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  61. PhysicsVector size, Quaternion rotation, bool isPhysical);
  62. public virtual bool SupportsNINJAJoints
  63. {
  64. get { return false; }
  65. }
  66. public virtual PhysicsJoint RequestJointCreation(string objectNameInScene, PhysicsJointType jointType, PhysicsVector position,
  67. Quaternion rotation, string parms, List<string> bodyNames, string trackedBodyName, Quaternion localRotation)
  68. { return null; }
  69. public virtual void RequestJointDeletion(string objectNameInScene)
  70. { return; }
  71. public virtual void RemoveAllJointsConnectedToActorThreadLocked(PhysicsActor actor)
  72. { return; }
  73. public virtual void DumpJointInfo()
  74. { return; }
  75. public event JointMoved OnJointMoved;
  76. protected virtual void DoJointMoved(PhysicsJoint joint)
  77. {
  78. // We need this to allow subclasses (but not other classes) to invoke the event; C# does
  79. // not allow subclasses to invoke the parent class event.
  80. if (OnJointMoved != null)
  81. {
  82. OnJointMoved(joint);
  83. }
  84. }
  85. public event JointDeactivated OnJointDeactivated;
  86. protected virtual void DoJointDeactivated(PhysicsJoint joint)
  87. {
  88. // We need this to allow subclasses (but not other classes) to invoke the event; C# does
  89. // not allow subclasses to invoke the parent class event.
  90. if (OnJointDeactivated != null)
  91. {
  92. OnJointDeactivated(joint);
  93. }
  94. }
  95. public event JointErrorMessage OnJointErrorMessage;
  96. protected virtual void DoJointErrorMessage(PhysicsJoint joint, string message)
  97. {
  98. // We need this to allow subclasses (but not other classes) to invoke the event; C# does
  99. // not allow subclasses to invoke the parent class event.
  100. if (OnJointErrorMessage != null)
  101. {
  102. OnJointErrorMessage(joint, message);
  103. }
  104. }
  105. public virtual PhysicsVector GetJointAnchor(PhysicsJoint joint)
  106. { return null; }
  107. public virtual PhysicsVector GetJointAxis(PhysicsJoint joint)
  108. { return null; }
  109. public abstract void AddPhysicsActorTaint(PhysicsActor prim);
  110. public abstract float Simulate(float timeStep);
  111. public abstract void GetResults();
  112. public abstract void SetTerrain(float[] heightMap);
  113. public abstract void SetWaterLevel(float baseheight);
  114. public abstract void DeleteTerrain();
  115. public abstract void Dispose();
  116. public abstract Dictionary<uint, float> GetTopColliders();
  117. public abstract bool IsThreaded { get; }
  118. private class NullPhysicsScene : PhysicsScene
  119. {
  120. private static int m_workIndicator;
  121. public override void Initialise(IMesher meshmerizer, IConfigSource config)
  122. {
  123. // Does nothing right now
  124. }
  125. public override PhysicsActor AddAvatar(string avName, PhysicsVector position, PhysicsVector size, bool isFlying)
  126. {
  127. m_log.InfoFormat("[PHYSICS]: NullPhysicsScene : AddAvatar({0})", position);
  128. return PhysicsActor.Null;
  129. }
  130. public override void RemoveAvatar(PhysicsActor actor)
  131. {
  132. }
  133. public override void RemovePrim(PhysicsActor prim)
  134. {
  135. }
  136. public override void SetWaterLevel(float baseheight)
  137. {
  138. }
  139. /*
  140. public override PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size, Quaternion rotation)
  141. {
  142. m_log.InfoFormat("NullPhysicsScene : AddPrim({0},{1})", position, size);
  143. return PhysicsActor.Null;
  144. }
  145. */
  146. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  147. PhysicsVector size, Quaternion rotation) //To be removed
  148. {
  149. return AddPrimShape(primName, pbs, position, size, rotation, false);
  150. }
  151. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  152. PhysicsVector size, Quaternion rotation, bool isPhysical)
  153. {
  154. m_log.InfoFormat("[PHYSICS]: NullPhysicsScene : AddPrim({0},{1})", position, size);
  155. return PhysicsActor.Null;
  156. }
  157. public override void AddPhysicsActorTaint(PhysicsActor prim)
  158. {
  159. }
  160. public override float Simulate(float timeStep)
  161. {
  162. m_workIndicator = (m_workIndicator + 1) % 10;
  163. return 0f;
  164. }
  165. public override void GetResults()
  166. {
  167. m_log.Info("[PHYSICS]: NullPhysicsScene : GetResults()");
  168. }
  169. public override void SetTerrain(float[] heightMap)
  170. {
  171. m_log.InfoFormat("[PHYSICS]: NullPhysicsScene : SetTerrain({0} items)", heightMap.Length);
  172. }
  173. public override void DeleteTerrain()
  174. {
  175. }
  176. public override bool IsThreaded
  177. {
  178. get { return false; }
  179. }
  180. public override void Dispose()
  181. {
  182. }
  183. public override Dictionary<uint,float> GetTopColliders()
  184. {
  185. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  186. return returncolliders;
  187. }
  188. }
  189. }
  190. public delegate void JointMoved(PhysicsJoint joint);
  191. public delegate void JointDeactivated(PhysicsJoint joint);
  192. public delegate void JointErrorMessage(PhysicsJoint joint, string message); // this refers to an "error message due to a problem", not "amount of joint constraint violation"
  193. }