PhysicsScene.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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;
  28. using System.Collections.Generic;
  29. using System.Runtime.CompilerServices;
  30. using OpenSim.Framework;
  31. using OpenMetaverse;
  32. namespace OpenSim.Region.PhysicsModules.SharedBase
  33. {
  34. public delegate void physicsCrash();
  35. public delegate void RaycastCallback(bool hitYN, Vector3 collisionPoint, uint localid, float distance, Vector3 normal);
  36. public delegate void RayCallback(List<ContactResult> list);
  37. public delegate void SitAvatarCallback(int status, uint partID, Vector3 offset, Quaternion Orientation);
  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. public enum RayFilterFlags : ushort
  42. {
  43. // the flags
  44. water = 0x01,
  45. land = 0x02,
  46. agent = 0x04,
  47. nonphysical = 0x08,
  48. physical = 0x10,
  49. phantom = 0x20,
  50. volumedtc = 0x40,
  51. // ray cast colision control (may only work for meshs)
  52. ContactsUnImportant = 0x2000,
  53. BackFaceCull = 0x4000,
  54. ClosestHit = 0x8000,
  55. // some combinations
  56. LSLPhantom = phantom | volumedtc,
  57. PrimsNonPhantom = nonphysical | physical,
  58. PrimsNonPhantomAgents = nonphysical | physical | agent,
  59. AllPrims = nonphysical | phantom | volumedtc | physical,
  60. AllButLand = agent | nonphysical | physical | phantom | volumedtc,
  61. ClosestAndBackCull = ClosestHit | BackFaceCull,
  62. All = 0x3f
  63. }
  64. public delegate void RequestAssetDelegate(UUID assetID, AssetReceivedDelegate callback);
  65. public delegate void AssetReceivedDelegate(AssetBase asset);
  66. /// <summary>
  67. /// Contact result from a raycast.
  68. /// </summary>
  69. public struct ContactResult
  70. {
  71. public Vector3 Pos;
  72. public Vector3 Normal;
  73. public float Depth;
  74. public uint ConsumerID;
  75. }
  76. public abstract class PhysicsScene
  77. {
  78. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  79. /// <summary>
  80. /// A unique identifying string for this instance of the physics engine.
  81. /// Useful in debug messages to distinguish one OdeScene instance from another.
  82. /// Usually set to include the region name that the physics engine is acting for.
  83. /// </summary>
  84. public string PhysicsSceneName { get; protected set; }
  85. /// <summary>
  86. /// A string identifying the family of this physics engine. Most common values returned
  87. /// are "OpenDynamicsEngine" and "BulletSim" but others are possible.
  88. /// </summary>
  89. public string EngineType { get; protected set; }
  90. public string EngineName { get; protected set; }
  91. // The only thing that should register for this event is the SceneGraph
  92. // Anything else could cause problems.
  93. public event physicsCrash OnPhysicsCrash;
  94. public static PhysicsScene Null
  95. {
  96. get { return new NullPhysicsScene(); }
  97. }
  98. public RequestAssetDelegate RequestAssetMethod { get; set; }
  99. protected void Initialise(RequestAssetDelegate m, float[] terrain, float waterHeight)
  100. {
  101. RequestAssetMethod = m;
  102. SetTerrain(terrain);
  103. SetWaterLevel(waterHeight);
  104. }
  105. public virtual void TriggerPhysicsBasedRestart()
  106. {
  107. physicsCrash handler = OnPhysicsCrash;
  108. if (handler != null)
  109. {
  110. OnPhysicsCrash();
  111. }
  112. }
  113. /// <summary>
  114. /// Add an avatar
  115. /// </summary>
  116. /// <param name="avName"></param>
  117. /// <param name="position"></param>
  118. /// <param name="velocity"></param>
  119. /// <param name="size"></param>
  120. /// <param name="isFlying"></param>
  121. /// <returns></returns>
  122. public abstract PhysicsActor AddAvatar(
  123. string avName, Vector3 position, Vector3 velocity, Vector3 size, bool isFlying);
  124. /// <summary>
  125. /// Add an avatar
  126. /// </summary>
  127. /// <param name="localID"></param>
  128. /// <param name="avName"></param>
  129. /// <param name="position"></param>
  130. /// <param name="velocity"></param>
  131. /// <param name="size"></param>
  132. /// <param name="isFlying"></param>
  133. /// <returns></returns>
  134. public virtual PhysicsActor AddAvatar(
  135. uint localID, string avName, Vector3 position, Vector3 velocity, Vector3 size, bool isFlying)
  136. {
  137. PhysicsActor ret = AddAvatar(avName, position, velocity, size, isFlying);
  138. if (ret is not null)
  139. ret.LocalID = localID;
  140. return ret;
  141. }
  142. public virtual PhysicsActor AddAvatar(
  143. uint localID, string avName, Vector3 position, Vector3 size, bool isFlying)
  144. {
  145. PhysicsActor ret = AddAvatar(localID, avName, position, Vector3.Zero, size, isFlying);
  146. return ret;
  147. }
  148. public virtual PhysicsActor AddAvatar(
  149. uint localID, string avName, Vector3 position, Vector3 size, float feetOffset, bool isFlying)
  150. {
  151. PhysicsActor ret = AddAvatar(localID, avName, position, Vector3.Zero, size, isFlying);
  152. return ret;
  153. }
  154. /// <summary>
  155. /// Remove an avatar.
  156. /// </summary>
  157. /// <param name="actor"></param>
  158. public abstract void RemoveAvatar(PhysicsActor actor);
  159. /// <summary>
  160. /// Remove a prim.
  161. /// </summary>
  162. /// <param name="prim"></param>
  163. public abstract void RemovePrim(PhysicsActor prim);
  164. public abstract PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  165. Vector3 size, Quaternion rotation, bool isPhysical, uint localid);
  166. public virtual PhysicsActor AddPrimShape(string primName, PhysicsActor parent, PrimitiveBaseShape pbs, Vector3 position,
  167. uint localid, byte[] sdata)
  168. {
  169. return null;
  170. }
  171. public virtual PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  172. Vector3 size, Quaternion rotation, bool isPhysical, bool isPhantom, uint localid)
  173. {
  174. return AddPrimShape(primName, pbs, position, size, rotation, isPhysical, localid);
  175. }
  176. public virtual PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  177. Vector3 size, Quaternion rotation, bool isPhysical, bool isPhantom, byte shapetype, uint localid)
  178. {
  179. return AddPrimShape(primName, pbs, position, size, rotation, isPhysical, localid);
  180. }
  181. public virtual float TimeDilation
  182. {
  183. get { return 1.0f; }
  184. }
  185. //legacy for any modules that may still call it
  186. public virtual void AddPhysicsActorTaint(PhysicsActor prim) { }
  187. public virtual void ProcessPreSimulation() { }
  188. /// <summary>
  189. /// Perform a simulation of the current physics scene over the given timestep.
  190. /// </summary>
  191. /// <param name="timeStep"></param>
  192. /// <returns>The number of frames simulated over that period.</returns>
  193. public abstract float Simulate(float timeStep);
  194. /// <summary>
  195. /// Get statistics about this scene.
  196. /// </summary>
  197. /// <remarks>This facility is currently experimental and subject to change.</remarks>
  198. /// <returns>
  199. /// A dictionary where the key is the statistic name. If no statistics are supplied then returns null.
  200. /// </returns>
  201. public virtual Dictionary<string, float> GetStats() { return null; }
  202. public abstract void SetTerrain(float[] heightMap);
  203. public abstract void SetWaterLevel(float baseheight);
  204. public abstract void DeleteTerrain();
  205. public abstract void Dispose();
  206. public abstract Dictionary<uint, float> GetTopColliders();
  207. /// <summary>
  208. /// True if the physics plugin supports raycasting against the physics scene
  209. /// </summary>
  210. public virtual bool SupportsRayCast()
  211. {
  212. return false;
  213. }
  214. /// <summary>
  215. /// Queue a raycast against the physics scene.
  216. /// The provided callback method will be called when the raycast is complete
  217. ///
  218. /// Many physics engines don't support collision testing at the same time as
  219. /// manipulating the physics scene, so we queue the request up and callback
  220. /// a custom method when the raycast is complete.
  221. /// This allows physics engines that give an immediate result to callback immediately
  222. /// and ones that don't, to callback when it gets a result back.
  223. ///
  224. /// ODE for example will not allow you to change the scene while collision testing or
  225. /// it asserts, 'opteration not valid for locked space'. This includes adding a ray to the scene.
  226. ///
  227. /// This is named RayCastWorld to not conflict with modrex's Raycast method.
  228. /// </summary>
  229. /// <param name="position">Origin of the ray</param>
  230. /// <param name="direction">Direction of the ray</param>
  231. /// <param name="length">Length of ray in meters</param>
  232. /// <param name="retMethod">Method to call when the raycast is complete</param>
  233. public virtual void RaycastWorld(Vector3 position, Vector3 direction, float length, RaycastCallback retMethod)
  234. {
  235. retMethod?.Invoke(false, Vector3.Zero, 0, 999999999999f, Vector3.Zero);
  236. }
  237. public virtual void RaycastWorld(Vector3 position, Vector3 direction, float length, int Count, RayCallback retMethod)
  238. {
  239. retMethod?.Invoke(new List<ContactResult>());
  240. }
  241. public virtual List<ContactResult> RaycastWorld(Vector3 position, Vector3 direction, float length, int Count)
  242. {
  243. return new List<ContactResult>();
  244. }
  245. public virtual object RaycastWorld(Vector3 position, Vector3 direction, float length, int Count, RayFilterFlags filter)
  246. {
  247. return null;
  248. }
  249. public virtual bool SupportsRaycastWorldFiltered()
  250. {
  251. return false;
  252. }
  253. public virtual List<ContactResult> RaycastActor(PhysicsActor actor, Vector3 position, Vector3 direction, float length, int Count, RayFilterFlags flags)
  254. {
  255. return new List<ContactResult>();
  256. }
  257. public virtual List<ContactResult> BoxProbe(Vector3 position, Vector3 size, Quaternion orientation, int Count, RayFilterFlags flags)
  258. {
  259. return new List<ContactResult>();
  260. }
  261. public virtual List<ContactResult> SphereProbe(Vector3 position, float radius, int Count, RayFilterFlags flags)
  262. {
  263. return new List<ContactResult>();
  264. }
  265. public virtual List<ContactResult> PlaneProbe(PhysicsActor actor, Vector4 plane, int Count, RayFilterFlags flags)
  266. {
  267. return new List<ContactResult>();
  268. }
  269. public virtual int SitAvatar(PhysicsActor actor, Vector3 AbsolutePosition, Vector3 CameraPosition, Vector3 offset, Vector3 AvatarSize, SitAvatarCallback PhysicsSitResponse)
  270. {
  271. return 0;
  272. }
  273. // Extendable interface for new, physics engine specific operations
  274. public virtual object Extension(string pFunct, params object[] pParams)
  275. {
  276. // A NOP if the extension thing is not implemented by the physics engine
  277. return null;
  278. }
  279. public virtual void GetResults() { }
  280. public virtual bool IsThreaded { get {return false;} }
  281. }
  282. }