PhysicsScene.cs 11 KB

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