PhysicsActor.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 OpenSim.Framework;
  30. using OpenMetaverse;
  31. namespace OpenSim.Region.Physics.Manager
  32. {
  33. public delegate void PositionUpdate(Vector3 position);
  34. public delegate void VelocityUpdate(Vector3 velocity);
  35. public delegate void OrientationUpdate(Quaternion orientation);
  36. public enum ActorTypes : int
  37. {
  38. Unknown = 0,
  39. Agent = 1,
  40. Prim = 2,
  41. Ground = 3
  42. }
  43. public enum PIDHoverType
  44. {
  45. Ground
  46. , GroundAndWater
  47. , Water
  48. , Absolute
  49. }
  50. public struct ContactPoint
  51. {
  52. public Vector3 Position;
  53. public Vector3 SurfaceNormal;
  54. public float PenetrationDepth;
  55. public ContactPoint(Vector3 position, Vector3 surfaceNormal, float penetrationDepth)
  56. {
  57. Position = position;
  58. SurfaceNormal = surfaceNormal;
  59. PenetrationDepth = penetrationDepth;
  60. }
  61. }
  62. public class CollisionEventUpdate : EventArgs
  63. {
  64. // Raising the event on the object, so don't need to provide location.. further up the tree knows that info.
  65. public int m_colliderType;
  66. public int m_GenericStartEnd;
  67. //public uint m_LocalID;
  68. public Dictionary<uint, ContactPoint> m_objCollisionList = new Dictionary<uint, ContactPoint>();
  69. public CollisionEventUpdate(uint localID, int colliderType, int GenericStartEnd, Dictionary<uint, ContactPoint> objCollisionList)
  70. {
  71. m_colliderType = colliderType;
  72. m_GenericStartEnd = GenericStartEnd;
  73. m_objCollisionList = objCollisionList;
  74. }
  75. public CollisionEventUpdate()
  76. {
  77. m_colliderType = (int) ActorTypes.Unknown;
  78. m_GenericStartEnd = 1;
  79. m_objCollisionList = new Dictionary<uint, ContactPoint>();
  80. }
  81. public int collidertype
  82. {
  83. get { return m_colliderType; }
  84. set { m_colliderType = value; }
  85. }
  86. public int GenericStartEnd
  87. {
  88. get { return m_GenericStartEnd; }
  89. set { m_GenericStartEnd = value; }
  90. }
  91. public void addCollider(uint localID, ContactPoint contact)
  92. {
  93. if (!m_objCollisionList.ContainsKey(localID))
  94. {
  95. m_objCollisionList.Add(localID, contact);
  96. }
  97. else
  98. {
  99. if (m_objCollisionList[localID].PenetrationDepth < contact.PenetrationDepth)
  100. m_objCollisionList[localID] = contact;
  101. }
  102. }
  103. }
  104. public abstract class PhysicsActor
  105. {
  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. public event CollisionUpdate OnCollisionUpdate;
  116. public event OutOfBounds OnOutOfBounds;
  117. #pragma warning restore 67
  118. public static PhysicsActor Null
  119. {
  120. get { return new NullPhysicsActor(); }
  121. }
  122. public abstract bool Stopped { get; }
  123. public abstract Vector3 Size { get; set; }
  124. public abstract PrimitiveBaseShape Shape { set; }
  125. uint m_baseLocalID;
  126. public virtual uint LocalID
  127. {
  128. set { m_baseLocalID = value; }
  129. get { return m_baseLocalID; }
  130. }
  131. public abstract bool Grabbed { set; }
  132. public abstract bool Selected { set; }
  133. public string SOPName;
  134. public string SOPDescription;
  135. public abstract void CrossingFailure();
  136. public abstract void link(PhysicsActor obj);
  137. public abstract void delink();
  138. public abstract void LockAngularMotion(Vector3 axis);
  139. public virtual void RequestPhysicsterseUpdate()
  140. {
  141. // Make a temporary copy of the event to avoid possibility of
  142. // a race condition if the last subscriber unsubscribes
  143. // immediately after the null check and before the event is raised.
  144. RequestTerseUpdate handler = OnRequestTerseUpdate;
  145. if (handler != null)
  146. {
  147. handler();
  148. }
  149. }
  150. public virtual void RaiseOutOfBounds(Vector3 pos)
  151. {
  152. // Make a temporary copy of the event to avoid possibility of
  153. // a race condition if the last subscriber unsubscribes
  154. // immediately after the null check and before the event is raised.
  155. OutOfBounds handler = OnOutOfBounds;
  156. if (handler != null)
  157. {
  158. handler(pos);
  159. }
  160. }
  161. public virtual void SendCollisionUpdate(EventArgs e)
  162. {
  163. CollisionUpdate handler = OnCollisionUpdate;
  164. if (handler != null)
  165. {
  166. handler(e);
  167. }
  168. }
  169. public virtual void SetMaterial (int material)
  170. {
  171. }
  172. public abstract Vector3 Position { get; set; }
  173. public abstract float Mass { get; }
  174. public abstract Vector3 Force { get; set; }
  175. public abstract int VehicleType { get; set; }
  176. public abstract void VehicleFloatParam(int param, float value);
  177. public abstract void VehicleVectorParam(int param, Vector3 value);
  178. public abstract void VehicleRotationParam(int param, Quaternion rotation);
  179. public abstract void VehicleFlags(int param, bool remove);
  180. public abstract void SetVolumeDetect(int param); // Allows the detection of collisions with inherently non-physical prims. see llVolumeDetect for more
  181. public abstract Vector3 GeometricCenter { get; }
  182. public abstract Vector3 CenterOfMass { get; }
  183. public abstract Vector3 Velocity { get; set; }
  184. public abstract Vector3 Torque { get; set; }
  185. public abstract float CollisionScore { get; set;}
  186. public abstract Vector3 Acceleration { get; }
  187. public abstract Quaternion Orientation { get; set; }
  188. public abstract int PhysicsActorType { get; set; }
  189. public abstract bool IsPhysical { get; set; }
  190. public abstract bool Flying { get; set; }
  191. public abstract bool SetAlwaysRun { get; set; }
  192. public abstract bool ThrottleUpdates { get; set; }
  193. public abstract bool IsColliding { get; set; }
  194. public abstract bool CollidingGround { get; set; }
  195. public abstract bool CollidingObj { get; set; }
  196. public abstract bool FloatOnWater { set; }
  197. public abstract Vector3 RotationalVelocity { get; set; }
  198. public abstract bool Kinematic { get; set; }
  199. public abstract float Buoyancy { get; set; }
  200. // Used for MoveTo
  201. public abstract Vector3 PIDTarget { set; }
  202. public abstract bool PIDActive { set;}
  203. public abstract float PIDTau { set; }
  204. // Used for llSetHoverHeight and maybe vehicle height
  205. // Hover Height will override MoveTo target's Z
  206. public abstract bool PIDHoverActive { set;}
  207. public abstract float PIDHoverHeight { set;}
  208. public abstract PIDHoverType PIDHoverType { set;}
  209. public abstract float PIDHoverTau { set;}
  210. // For RotLookAt
  211. public abstract Quaternion APIDTarget { set;}
  212. public abstract bool APIDActive { set;}
  213. public abstract float APIDStrength { set;}
  214. public abstract float APIDDamping { set;}
  215. public abstract void AddForce(Vector3 force, bool pushforce);
  216. public abstract void AddAngularForce(Vector3 force, bool pushforce);
  217. public abstract void SetMomentum(Vector3 momentum);
  218. public abstract void SubscribeEvents(int ms);
  219. public abstract void UnSubscribeEvents();
  220. public abstract bool SubscribedEvents();
  221. }
  222. public class NullPhysicsActor : PhysicsActor
  223. {
  224. public override bool Stopped
  225. {
  226. get{ return false; }
  227. }
  228. public override Vector3 Position
  229. {
  230. get { return Vector3.Zero; }
  231. set { return; }
  232. }
  233. public override bool SetAlwaysRun
  234. {
  235. get { return false; }
  236. set { return; }
  237. }
  238. public override uint LocalID
  239. {
  240. set { return; }
  241. }
  242. public override bool Grabbed
  243. {
  244. set { return; }
  245. }
  246. public override bool Selected
  247. {
  248. set { return; }
  249. }
  250. public override float Buoyancy
  251. {
  252. get { return 0f; }
  253. set { return; }
  254. }
  255. public override bool FloatOnWater
  256. {
  257. set { return; }
  258. }
  259. public override bool CollidingGround
  260. {
  261. get { return false; }
  262. set { return; }
  263. }
  264. public override bool CollidingObj
  265. {
  266. get { return false; }
  267. set { return; }
  268. }
  269. public override Vector3 Size
  270. {
  271. get { return Vector3.Zero; }
  272. set { return; }
  273. }
  274. public override float Mass
  275. {
  276. get { return 0f; }
  277. }
  278. public override Vector3 Force
  279. {
  280. get { return Vector3.Zero; }
  281. set { return; }
  282. }
  283. public override int VehicleType
  284. {
  285. get { return 0; }
  286. set { return; }
  287. }
  288. public override void VehicleFloatParam(int param, float value)
  289. {
  290. }
  291. public override void VehicleVectorParam(int param, Vector3 value)
  292. {
  293. }
  294. public override void VehicleRotationParam(int param, Quaternion rotation)
  295. {
  296. }
  297. public override void VehicleFlags(int param, bool remove)
  298. {
  299. }
  300. public override void SetVolumeDetect(int param)
  301. {
  302. }
  303. public override void SetMaterial(int material)
  304. {
  305. }
  306. public override Vector3 CenterOfMass
  307. {
  308. get { return Vector3.Zero; }
  309. }
  310. public override Vector3 GeometricCenter
  311. {
  312. get { return Vector3.Zero; }
  313. }
  314. public override PrimitiveBaseShape Shape
  315. {
  316. set { return; }
  317. }
  318. public override Vector3 Velocity
  319. {
  320. get { return Vector3.Zero; }
  321. set { return; }
  322. }
  323. public override Vector3 Torque
  324. {
  325. get { return Vector3.Zero; }
  326. set { return; }
  327. }
  328. public override float CollisionScore
  329. {
  330. get { return 0f; }
  331. set { }
  332. }
  333. public override void CrossingFailure()
  334. {
  335. }
  336. public override Quaternion Orientation
  337. {
  338. get { return Quaternion.Identity; }
  339. set { }
  340. }
  341. public override Vector3 Acceleration
  342. {
  343. get { return Vector3.Zero; }
  344. }
  345. public override bool IsPhysical
  346. {
  347. get { return false; }
  348. set { return; }
  349. }
  350. public override bool Flying
  351. {
  352. get { return false; }
  353. set { return; }
  354. }
  355. public override bool ThrottleUpdates
  356. {
  357. get { return false; }
  358. set { return; }
  359. }
  360. public override bool IsColliding
  361. {
  362. get { return false; }
  363. set { return; }
  364. }
  365. public override int PhysicsActorType
  366. {
  367. get { return (int) ActorTypes.Unknown; }
  368. set { return; }
  369. }
  370. public override bool Kinematic
  371. {
  372. get { return true; }
  373. set { return; }
  374. }
  375. public override void link(PhysicsActor obj)
  376. {
  377. }
  378. public override void delink()
  379. {
  380. }
  381. public override void LockAngularMotion(Vector3 axis)
  382. {
  383. }
  384. public override void AddForce(Vector3 force, bool pushforce)
  385. {
  386. }
  387. public override void AddAngularForce(Vector3 force, bool pushforce)
  388. {
  389. }
  390. public override Vector3 RotationalVelocity
  391. {
  392. get { return Vector3.Zero; }
  393. set { return; }
  394. }
  395. public override Vector3 PIDTarget { set { return; } }
  396. public override bool PIDActive { set { return; } }
  397. public override float PIDTau { set { return; } }
  398. public override float PIDHoverHeight { set { return; } }
  399. public override bool PIDHoverActive { set { return; } }
  400. public override PIDHoverType PIDHoverType { set { return; } }
  401. public override float PIDHoverTau { set { return; } }
  402. public override Quaternion APIDTarget { set { return; } }
  403. public override bool APIDActive { set { return; } }
  404. public override float APIDStrength { set { return; } }
  405. public override float APIDDamping { set { return; } }
  406. public override void SetMomentum(Vector3 momentum)
  407. {
  408. }
  409. public override void SubscribeEvents(int ms)
  410. {
  411. }
  412. public override void UnSubscribeEvents()
  413. {
  414. }
  415. public override bool SubscribedEvents()
  416. {
  417. return false;
  418. }
  419. }
  420. }