PhysicsActor.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using OpenSim.Framework;
  32. using OpenMetaverse;
  33. namespace OpenSim.Region.PhysicsModules.SharedBase
  34. {
  35. public delegate void PositionUpdate(Vector3 position);
  36. public delegate void VelocityUpdate(Vector3 velocity);
  37. public delegate void OrientationUpdate(Quaternion orientation);
  38. public enum ActorTypes : int
  39. {
  40. Unknown = 0,
  41. Agent = 1,
  42. Prim = 2,
  43. Ground = 3
  44. }
  45. public enum PIDHoverType
  46. {
  47. Ground,
  48. GroundAndWater,
  49. Water,
  50. Absolute
  51. }
  52. public struct ContactPoint
  53. {
  54. public Vector3 Position;
  55. public Vector3 SurfaceNormal;
  56. public float PenetrationDepth;
  57. public ContactPoint(Vector3 position, Vector3 surfaceNormal, float penetrationDepth)
  58. {
  59. Position = position;
  60. SurfaceNormal = surfaceNormal;
  61. PenetrationDepth = penetrationDepth;
  62. }
  63. }
  64. /// <summary>
  65. /// Used to pass collision information to OnCollisionUpdate listeners.
  66. /// </summary>
  67. public class CollisionEventUpdate : EventArgs
  68. {
  69. /// <summary>
  70. /// Number of collision events in this update.
  71. /// </summary>
  72. public int Count { get { return m_objCollisionList.Count; } }
  73. public bool CollisionsOnPreviousFrame { get; private set; }
  74. public Dictionary<uint, ContactPoint> m_objCollisionList;
  75. public CollisionEventUpdate(Dictionary<uint, ContactPoint> objCollisionList)
  76. {
  77. m_objCollisionList = objCollisionList;
  78. }
  79. public CollisionEventUpdate()
  80. {
  81. m_objCollisionList = new Dictionary<uint, ContactPoint>();
  82. }
  83. public void AddCollider(uint localID, ContactPoint contact)
  84. {
  85. if (!m_objCollisionList.ContainsKey(localID))
  86. {
  87. m_objCollisionList.Add(localID, contact);
  88. }
  89. else
  90. {
  91. if (m_objCollisionList[localID].PenetrationDepth < contact.PenetrationDepth)
  92. m_objCollisionList[localID] = contact;
  93. }
  94. }
  95. /// <summary>
  96. /// Clear added collision events.
  97. /// </summary>
  98. public void Clear()
  99. {
  100. m_objCollisionList.Clear();
  101. }
  102. }
  103. public abstract class PhysicsActor
  104. {
  105. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  106. public delegate void RequestTerseUpdate();
  107. public delegate void CollisionUpdate(EventArgs e);
  108. public delegate void OutOfBounds(Vector3 pos);
  109. // disable warning: public events
  110. #pragma warning disable 67
  111. public event PositionUpdate OnPositionUpdate;
  112. public event VelocityUpdate OnVelocityUpdate;
  113. public event OrientationUpdate OnOrientationUpdate;
  114. public event RequestTerseUpdate OnRequestTerseUpdate;
  115. /// <summary>
  116. /// Subscribers to this event must synchronously handle the dictionary of collisions received, since the event
  117. /// object is reused in subsequent physics frames.
  118. /// </summary>
  119. public event CollisionUpdate OnCollisionUpdate;
  120. public event OutOfBounds OnOutOfBounds;
  121. #pragma warning restore 67
  122. public static PhysicsActor Null
  123. {
  124. get { return new NullPhysicsActor(); }
  125. }
  126. public abstract bool Stopped { get; }
  127. public abstract Vector3 Size { get; set; }
  128. public virtual byte PhysicsShapeType { get; set; }
  129. public abstract PrimitiveBaseShape Shape { set; }
  130. uint m_baseLocalID;
  131. public virtual uint LocalID
  132. {
  133. set { m_baseLocalID = value; }
  134. get { return m_baseLocalID; }
  135. }
  136. public abstract bool Grabbed { set; }
  137. public abstract bool Selected { set; }
  138. /// <summary>
  139. /// Name of this actor.
  140. /// </summary>
  141. /// <remarks>
  142. /// XXX: Bizarrely, this cannot be "Terrain" or "Water" right now unless it really is simulating terrain or
  143. /// water. This is not a problem due to the formatting of names given by prims and avatars.
  144. /// </remarks>
  145. public string Name { get; protected set; }
  146. /// <summary>
  147. /// This is being used by ODE joint code.
  148. /// </summary>
  149. public string SOPName;
  150. public abstract void CrossingFailure();
  151. public abstract void link(PhysicsActor obj);
  152. public abstract void delink();
  153. public abstract void LockAngularMotion(Vector3 axis);
  154. public virtual void RequestPhysicsterseUpdate()
  155. {
  156. // Make a temporary copy of the event to avoid possibility of
  157. // a race condition if the last subscriber unsubscribes
  158. // immediately after the null check and before the event is raised.
  159. RequestTerseUpdate handler = OnRequestTerseUpdate;
  160. if (handler != null)
  161. {
  162. handler();
  163. }
  164. }
  165. public virtual void RaiseOutOfBounds(Vector3 pos)
  166. {
  167. // Make a temporary copy of the event to avoid possibility of
  168. // a race condition if the last subscriber unsubscribes
  169. // immediately after the null check and before the event is raised.
  170. OutOfBounds handler = OnOutOfBounds;
  171. if (handler != null)
  172. {
  173. handler(pos);
  174. }
  175. }
  176. public virtual void SendCollisionUpdate(EventArgs e)
  177. {
  178. CollisionUpdate handler = OnCollisionUpdate;
  179. // m_log.DebugFormat("[PHYSICS ACTOR]: Sending collision for {0}", LocalID);
  180. if (handler != null)
  181. handler(e);
  182. }
  183. public virtual void SetMaterial (int material) { }
  184. public virtual float Density { get; set; }
  185. public virtual float GravModifier { get; set; }
  186. public virtual float Friction { get; set; }
  187. public virtual float Restitution { get; set; }
  188. /// <summary>
  189. /// Position of this actor.
  190. /// </summary>
  191. /// <remarks>
  192. /// Setting this directly moves the actor to a given position.
  193. /// Getting this retrieves the position calculated by physics scene updates, using factors such as velocity and
  194. /// collisions.
  195. /// </remarks>
  196. public abstract Vector3 Position { get; set; }
  197. public abstract float Mass { get; }
  198. public abstract Vector3 Force { get; set; }
  199. public abstract int VehicleType { get; set; }
  200. public abstract void VehicleFloatParam(int param, float value);
  201. public abstract void VehicleVectorParam(int param, Vector3 value);
  202. public abstract void VehicleRotationParam(int param, Quaternion rotation);
  203. public abstract void VehicleFlags(int param, bool remove);
  204. /// <summary>
  205. /// Allows the detection of collisions with inherently non-physical prims. see llVolumeDetect for more
  206. /// </summary>
  207. public abstract void SetVolumeDetect(int param);
  208. public abstract Vector3 GeometricCenter { get; }
  209. public abstract Vector3 CenterOfMass { get; }
  210. /// <summary>
  211. /// The desired velocity of this actor.
  212. /// </summary>
  213. /// <remarks>
  214. /// Setting this provides a target velocity for physics scene updates.
  215. /// Getting this returns the last set target. Fetch Velocity to get the current velocity.
  216. /// </remarks>
  217. protected Vector3 m_targetVelocity;
  218. public virtual Vector3 TargetVelocity
  219. {
  220. get { return m_targetVelocity; }
  221. set {
  222. m_targetVelocity = value;
  223. Velocity = m_targetVelocity;
  224. }
  225. }
  226. public abstract Vector3 Velocity { get; set; }
  227. public abstract Vector3 Torque { get; set; }
  228. public abstract float CollisionScore { get; set;}
  229. public abstract Vector3 Acceleration { get; set; }
  230. public abstract Quaternion Orientation { get; set; }
  231. public abstract int PhysicsActorType { get; set; }
  232. public abstract bool IsPhysical { get; set; }
  233. public abstract bool Flying { get; set; }
  234. public abstract bool SetAlwaysRun { get; set; }
  235. public abstract bool ThrottleUpdates { get; set; }
  236. public abstract bool IsColliding { get; set; }
  237. public abstract bool CollidingGround { get; set; }
  238. public abstract bool CollidingObj { get; set; }
  239. public abstract bool FloatOnWater { set; }
  240. public abstract Vector3 RotationalVelocity { get; set; }
  241. public abstract bool Kinematic { get; set; }
  242. public abstract float Buoyancy { get; set; }
  243. // Used for MoveTo
  244. public abstract Vector3 PIDTarget { set; }
  245. public abstract bool PIDActive { get; set; }
  246. public abstract float PIDTau { set; }
  247. // Used for llSetHoverHeight and maybe vehicle height
  248. // Hover Height will override MoveTo target's Z
  249. public abstract bool PIDHoverActive { set;}
  250. public abstract float PIDHoverHeight { set;}
  251. public abstract PIDHoverType PIDHoverType { set;}
  252. public abstract float PIDHoverTau { set;}
  253. // For RotLookAt
  254. public abstract Quaternion APIDTarget { set;}
  255. public abstract bool APIDActive { set;}
  256. public abstract float APIDStrength { set;}
  257. public abstract float APIDDamping { set;}
  258. public abstract void AddForce(Vector3 force, bool pushforce);
  259. public abstract void AddAngularForce(Vector3 force, bool pushforce);
  260. public abstract void SetMomentum(Vector3 momentum);
  261. public abstract void SubscribeEvents(int ms);
  262. public abstract void UnSubscribeEvents();
  263. public abstract bool SubscribedEvents();
  264. // Extendable interface for new, physics engine specific operations
  265. public virtual object Extension(string pFunct, params object[] pParams)
  266. {
  267. // A NOP of the physics engine does not implement this feature
  268. return null;
  269. }
  270. }
  271. public class NullPhysicsActor : PhysicsActor
  272. {
  273. public override bool Stopped
  274. {
  275. get{ return false; }
  276. }
  277. public override Vector3 Position
  278. {
  279. get { return Vector3.Zero; }
  280. set { return; }
  281. }
  282. public override bool SetAlwaysRun
  283. {
  284. get { return false; }
  285. set { return; }
  286. }
  287. public override uint LocalID
  288. {
  289. set { return; }
  290. }
  291. public override bool Grabbed
  292. {
  293. set { return; }
  294. }
  295. public override bool Selected
  296. {
  297. set { return; }
  298. }
  299. public override float Buoyancy
  300. {
  301. get { return 0f; }
  302. set { return; }
  303. }
  304. public override bool FloatOnWater
  305. {
  306. set { return; }
  307. }
  308. public override bool CollidingGround
  309. {
  310. get { return false; }
  311. set { return; }
  312. }
  313. public override bool CollidingObj
  314. {
  315. get { return false; }
  316. set { return; }
  317. }
  318. public override Vector3 Size
  319. {
  320. get { return Vector3.Zero; }
  321. set { return; }
  322. }
  323. public override float Mass
  324. {
  325. get { return 0f; }
  326. }
  327. public override Vector3 Force
  328. {
  329. get { return Vector3.Zero; }
  330. set { return; }
  331. }
  332. public override int VehicleType
  333. {
  334. get { return 0; }
  335. set { return; }
  336. }
  337. public override void VehicleFloatParam(int param, float value)
  338. {
  339. }
  340. public override void VehicleVectorParam(int param, Vector3 value)
  341. {
  342. }
  343. public override void VehicleRotationParam(int param, Quaternion rotation)
  344. {
  345. }
  346. public override void VehicleFlags(int param, bool remove)
  347. {
  348. }
  349. public override void SetVolumeDetect(int param)
  350. {
  351. }
  352. public override void SetMaterial(int material)
  353. {
  354. }
  355. public override Vector3 CenterOfMass
  356. {
  357. get { return Vector3.Zero; }
  358. }
  359. public override Vector3 GeometricCenter
  360. {
  361. get { return Vector3.Zero; }
  362. }
  363. public override PrimitiveBaseShape Shape
  364. {
  365. set { return; }
  366. }
  367. public override Vector3 Velocity
  368. {
  369. get { return Vector3.Zero; }
  370. set { return; }
  371. }
  372. public override Vector3 Torque
  373. {
  374. get { return Vector3.Zero; }
  375. set { return; }
  376. }
  377. public override float CollisionScore
  378. {
  379. get { return 0f; }
  380. set { }
  381. }
  382. public override void CrossingFailure()
  383. {
  384. }
  385. public override Quaternion Orientation
  386. {
  387. get { return Quaternion.Identity; }
  388. set { }
  389. }
  390. public override Vector3 Acceleration
  391. {
  392. get { return Vector3.Zero; }
  393. set { }
  394. }
  395. public override bool IsPhysical
  396. {
  397. get { return false; }
  398. set { return; }
  399. }
  400. public override bool Flying
  401. {
  402. get { return false; }
  403. set { return; }
  404. }
  405. public override bool ThrottleUpdates
  406. {
  407. get { return false; }
  408. set { return; }
  409. }
  410. public override bool IsColliding
  411. {
  412. get { return false; }
  413. set { return; }
  414. }
  415. public override int PhysicsActorType
  416. {
  417. get { return (int) ActorTypes.Unknown; }
  418. set { return; }
  419. }
  420. public override bool Kinematic
  421. {
  422. get { return true; }
  423. set { return; }
  424. }
  425. public override void link(PhysicsActor obj)
  426. {
  427. }
  428. public override void delink()
  429. {
  430. }
  431. public override void LockAngularMotion(Vector3 axis)
  432. {
  433. }
  434. public override void AddForce(Vector3 force, bool pushforce)
  435. {
  436. }
  437. public override void AddAngularForce(Vector3 force, bool pushforce)
  438. {
  439. }
  440. public override Vector3 RotationalVelocity
  441. {
  442. get { return Vector3.Zero; }
  443. set { return; }
  444. }
  445. public override Vector3 PIDTarget { set { return; } }
  446. public override bool PIDActive
  447. {
  448. get { return false; }
  449. set { return; }
  450. }
  451. public override float PIDTau { set { return; } }
  452. public override float PIDHoverHeight { set { return; } }
  453. public override bool PIDHoverActive { set { return; } }
  454. public override PIDHoverType PIDHoverType { set { return; } }
  455. public override float PIDHoverTau { set { return; } }
  456. public override Quaternion APIDTarget { set { return; } }
  457. public override bool APIDActive { set { return; } }
  458. public override float APIDStrength { set { return; } }
  459. public override float APIDDamping { set { return; } }
  460. public override void SetMomentum(Vector3 momentum)
  461. {
  462. }
  463. public override void SubscribeEvents(int ms)
  464. {
  465. }
  466. public override void UnSubscribeEvents()
  467. {
  468. }
  469. public override bool SubscribedEvents()
  470. {
  471. return false;
  472. }
  473. }
  474. }