1
0

PhysicsScene.cs 14 KB

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