PhysicsScene.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 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.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 delegate void RaycastCallback(bool hitYN, Vector3 collisionPoint, uint localid, float distance, Vector3 normal);
  37. public abstract class PhysicsScene
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. // The only thing that should register for this event is the SceneGraph
  41. // Anything else could cause problems.
  42. public event physicsCrash OnPhysicsCrash;
  43. public static PhysicsScene Null
  44. {
  45. get { return new NullPhysicsScene(); }
  46. }
  47. public virtual void TriggerPhysicsBasedRestart()
  48. {
  49. physicsCrash handler = OnPhysicsCrash;
  50. if (handler != null)
  51. {
  52. OnPhysicsCrash();
  53. }
  54. }
  55. public abstract void Initialise(IMesher meshmerizer, IConfigSource config);
  56. public abstract PhysicsActor AddAvatar(string avName, Vector3 position, Vector3 size, bool isFlying);
  57. public abstract void RemoveAvatar(PhysicsActor actor);
  58. public abstract void RemovePrim(PhysicsActor prim);
  59. public abstract PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  60. Vector3 size, Quaternion rotation); //To be removed
  61. public abstract PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  62. Vector3 size, Quaternion rotation, bool isPhysical);
  63. public virtual float TimeDilation
  64. {
  65. get { return 1.0f; }
  66. }
  67. public virtual bool SupportsNINJAJoints
  68. {
  69. get { return false; }
  70. }
  71. public virtual PhysicsJoint RequestJointCreation(string objectNameInScene, PhysicsJointType jointType, Vector3 position,
  72. Quaternion rotation, string parms, List<string> bodyNames, string trackedBodyName, Quaternion localRotation)
  73. { return null; }
  74. public virtual void RequestJointDeletion(string objectNameInScene)
  75. { return; }
  76. public virtual void RemoveAllJointsConnectedToActorThreadLocked(PhysicsActor actor)
  77. { return; }
  78. public virtual void DumpJointInfo()
  79. { return; }
  80. public event JointMoved OnJointMoved;
  81. protected virtual void DoJointMoved(PhysicsJoint joint)
  82. {
  83. // We need this to allow subclasses (but not other classes) to invoke the event; C# does
  84. // not allow subclasses to invoke the parent class event.
  85. if (OnJointMoved != null)
  86. {
  87. OnJointMoved(joint);
  88. }
  89. }
  90. public event JointDeactivated OnJointDeactivated;
  91. protected virtual void DoJointDeactivated(PhysicsJoint joint)
  92. {
  93. // We need this to allow subclasses (but not other classes) to invoke the event; C# does
  94. // not allow subclasses to invoke the parent class event.
  95. if (OnJointDeactivated != null)
  96. {
  97. OnJointDeactivated(joint);
  98. }
  99. }
  100. public event JointErrorMessage OnJointErrorMessage;
  101. protected virtual void DoJointErrorMessage(PhysicsJoint joint, string message)
  102. {
  103. // We need this to allow subclasses (but not other classes) to invoke the event; C# does
  104. // not allow subclasses to invoke the parent class event.
  105. if (OnJointErrorMessage != null)
  106. {
  107. OnJointErrorMessage(joint, message);
  108. }
  109. }
  110. public virtual Vector3 GetJointAnchor(PhysicsJoint joint)
  111. { return Vector3.Zero; }
  112. public virtual Vector3 GetJointAxis(PhysicsJoint joint)
  113. { return Vector3.Zero; }
  114. public abstract void AddPhysicsActorTaint(PhysicsActor prim);
  115. public abstract float Simulate(float timeStep);
  116. public abstract void GetResults();
  117. public abstract void SetTerrain(float[] heightMap);
  118. public abstract void SetWaterLevel(float baseheight);
  119. public abstract void DeleteTerrain();
  120. public abstract void Dispose();
  121. public abstract Dictionary<uint, float> GetTopColliders();
  122. public abstract bool IsThreaded { get; }
  123. /// <summary>
  124. /// True if the physics plugin supports raycasting against the physics scene
  125. /// </summary>
  126. public virtual bool SupportsRayCast()
  127. {
  128. return false;
  129. }
  130. public virtual bool SupportsCombining()
  131. {
  132. return false;
  133. }
  134. public virtual void Combine(PhysicsScene pScene, Vector3 offset, Vector3 extents)
  135. {
  136. return;
  137. }
  138. public virtual void UnCombine(PhysicsScene pScene)
  139. {
  140. }
  141. /// <summary>
  142. /// Queue a raycast against the physics scene.
  143. /// The provided callback method will be called when the raycast is complete
  144. ///
  145. /// Many physics engines don't support collision testing at the same time as
  146. /// manipulating the physics scene, so we queue the request up and callback
  147. /// a custom method when the raycast is complete.
  148. /// This allows physics engines that give an immediate result to callback immediately
  149. /// and ones that don't, to callback when it gets a result back.
  150. ///
  151. /// ODE for example will not allow you to change the scene while collision testing or
  152. /// it asserts, 'opteration not valid for locked space'. This includes adding a ray to the scene.
  153. ///
  154. /// This is named RayCastWorld to not conflict with modrex's Raycast method.
  155. /// </summary>
  156. /// <param name="position">Origin of the ray</param>
  157. /// <param name="direction">Direction of the ray</param>
  158. /// <param name="length">Length of ray in meters</param>
  159. /// <param name="retMethod">Method to call when the raycast is complete</param>
  160. public virtual void RaycastWorld(Vector3 position, Vector3 direction, float length, RaycastCallback retMethod)
  161. {
  162. if (retMethod != null)
  163. retMethod(false, Vector3.Zero, 0, 999999999999f, Vector3.Zero);
  164. }
  165. private class NullPhysicsScene : PhysicsScene
  166. {
  167. private static int m_workIndicator;
  168. public override void Initialise(IMesher meshmerizer, IConfigSource config)
  169. {
  170. // Does nothing right now
  171. }
  172. public override PhysicsActor AddAvatar(string avName, Vector3 position, Vector3 size, bool isFlying)
  173. {
  174. m_log.InfoFormat("[PHYSICS]: NullPhysicsScene : AddAvatar({0})", position);
  175. return PhysicsActor.Null;
  176. }
  177. public override void RemoveAvatar(PhysicsActor actor)
  178. {
  179. }
  180. public override void RemovePrim(PhysicsActor prim)
  181. {
  182. }
  183. public override void SetWaterLevel(float baseheight)
  184. {
  185. }
  186. /*
  187. public override PhysicsActor AddPrim(Vector3 position, Vector3 size, Quaternion rotation)
  188. {
  189. m_log.InfoFormat("NullPhysicsScene : AddPrim({0},{1})", position, size);
  190. return PhysicsActor.Null;
  191. }
  192. */
  193. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  194. Vector3 size, Quaternion rotation) //To be removed
  195. {
  196. return AddPrimShape(primName, pbs, position, size, rotation, false);
  197. }
  198. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  199. Vector3 size, Quaternion rotation, bool isPhysical)
  200. {
  201. m_log.InfoFormat("[PHYSICS]: NullPhysicsScene : AddPrim({0},{1})", position, size);
  202. return PhysicsActor.Null;
  203. }
  204. public override void AddPhysicsActorTaint(PhysicsActor prim)
  205. {
  206. }
  207. public override float Simulate(float timeStep)
  208. {
  209. m_workIndicator = (m_workIndicator + 1) % 10;
  210. return 0f;
  211. }
  212. public override void GetResults()
  213. {
  214. m_log.Info("[PHYSICS]: NullPhysicsScene : GetResults()");
  215. }
  216. public override void SetTerrain(float[] heightMap)
  217. {
  218. m_log.InfoFormat("[PHYSICS]: NullPhysicsScene : SetTerrain({0} items)", heightMap.Length);
  219. }
  220. public override void DeleteTerrain()
  221. {
  222. }
  223. public override bool IsThreaded
  224. {
  225. get { return false; }
  226. }
  227. public override void Dispose()
  228. {
  229. }
  230. public override Dictionary<uint,float> GetTopColliders()
  231. {
  232. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  233. return returncolliders;
  234. }
  235. }
  236. }
  237. public delegate void JointMoved(PhysicsJoint joint);
  238. public delegate void JointDeactivated(PhysicsJoint joint);
  239. public delegate void JointErrorMessage(PhysicsJoint joint, string message); // this refers to an "error message due to a problem", not "amount of joint constraint violation"
  240. }