PhysicsScene.cs 16 KB

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