PhysicsScene.cs 11 KB

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