PhysicsScene.cs 12 KB

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