PhysicsActor.cs 14 KB

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