PhysicsScene.cs 12 KB

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