PhysicsActor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 OpenSim 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 Axiom.Math;
  30. using OpenSim.Framework;
  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 class CollisionEventUpdate : EventArgs
  44. {
  45. // Raising the event on the object, so don't need to provide location.. further up the tree knows that info.
  46. public int m_colliderType;
  47. public int m_GenericStartEnd;
  48. //public uint m_LocalID;
  49. public Dictionary<uint,float> m_objCollisionList = new Dictionary<uint,float>();
  50. public CollisionEventUpdate(uint localID, int colliderType, int GenericStartEnd, Dictionary<uint, float> objCollisionList)
  51. {
  52. m_colliderType = colliderType;
  53. m_GenericStartEnd = GenericStartEnd;
  54. m_objCollisionList = objCollisionList;
  55. }
  56. public CollisionEventUpdate()
  57. {
  58. m_colliderType = (int) ActorTypes.Unknown;
  59. m_GenericStartEnd = 1;
  60. // m_objCollisionList = null;
  61. m_objCollisionList = new Dictionary<uint, float>();
  62. }
  63. public int collidertype
  64. {
  65. get { return m_colliderType; }
  66. set { m_colliderType = value; }
  67. }
  68. public int GenericStartEnd
  69. {
  70. get { return m_GenericStartEnd; }
  71. set { m_GenericStartEnd = value; }
  72. }
  73. public void addCollider(uint localID, float depth)
  74. {
  75. if (!m_objCollisionList.ContainsKey(localID))
  76. {
  77. m_objCollisionList.Add(localID, depth);
  78. }
  79. else
  80. {
  81. if (m_objCollisionList[localID] < depth)
  82. m_objCollisionList[localID] = depth;
  83. }
  84. }
  85. }
  86. public abstract class PhysicsActor
  87. {
  88. public delegate void RequestTerseUpdate();
  89. public delegate void CollisionUpdate(EventArgs e);
  90. public delegate void OutOfBounds(PhysicsVector pos);
  91. #pragma warning disable 67
  92. public event PositionUpdate OnPositionUpdate;
  93. public event VelocityUpdate OnVelocityUpdate;
  94. public event OrientationUpdate OnOrientationUpdate;
  95. public event RequestTerseUpdate OnRequestTerseUpdate;
  96. public event CollisionUpdate OnCollisionUpdate;
  97. public event OutOfBounds OnOutOfBounds;
  98. #pragma warning restore 67
  99. public static PhysicsActor Null
  100. {
  101. get { return new NullPhysicsActor(); }
  102. }
  103. public abstract bool Stopped { get; }
  104. public abstract PhysicsVector Size { get; set; }
  105. public abstract PrimitiveBaseShape Shape { set; }
  106. public abstract uint LocalID { set; }
  107. public abstract bool Grabbed { set; }
  108. public abstract bool Selected { set; }
  109. public abstract void CrossingFailure();
  110. public abstract void link(PhysicsActor obj);
  111. public abstract void delink();
  112. public abstract void LockAngularMotion(PhysicsVector axis);
  113. public virtual void RequestPhysicsterseUpdate()
  114. {
  115. // Make a temporary copy of the event to avoid possibility of
  116. // a race condition if the last subscriber unsubscribes
  117. // immediately after the null check and before the event is raised.
  118. RequestTerseUpdate handler = OnRequestTerseUpdate;
  119. if (handler != null)
  120. {
  121. handler();
  122. }
  123. }
  124. public virtual void RaiseOutOfBounds(PhysicsVector pos)
  125. {
  126. // Make a temporary copy of the event to avoid possibility of
  127. // a race condition if the last subscriber unsubscribes
  128. // immediately after the null check and before the event is raised.
  129. OutOfBounds handler = OnOutOfBounds;
  130. if (handler != null)
  131. {
  132. handler(pos);
  133. }
  134. }
  135. public virtual void SendCollisionUpdate(EventArgs e)
  136. {
  137. CollisionUpdate handler = OnCollisionUpdate;
  138. if (handler != null)
  139. {
  140. handler(e);
  141. }
  142. }
  143. public abstract PhysicsVector Position { get; set; }
  144. public abstract float Mass { get; }
  145. public abstract PhysicsVector Force { get; }
  146. public abstract PhysicsVector GeometricCenter { get; }
  147. public abstract PhysicsVector CenterOfMass { get; }
  148. public abstract PhysicsVector Velocity { get; set; }
  149. public abstract float CollisionScore { get; set;}
  150. public abstract PhysicsVector Acceleration { get; }
  151. public abstract Quaternion Orientation { get; set; }
  152. public abstract int PhysicsActorType { get; set; }
  153. public abstract bool IsPhysical { get; set; }
  154. public abstract bool Flying { get; set; }
  155. public abstract bool SetAlwaysRun { get; set; }
  156. public abstract bool ThrottleUpdates { get; set; }
  157. public abstract bool IsColliding { get; set; }
  158. public abstract bool CollidingGround { get; set; }
  159. public abstract bool CollidingObj { get; set; }
  160. public abstract bool FloatOnWater { set; }
  161. public abstract PhysicsVector RotationalVelocity { get; set; }
  162. public abstract bool Kinematic { get; set; }
  163. public abstract float Buoyancy { get; set; }
  164. public abstract PhysicsVector PIDTarget { set;}
  165. public abstract bool PIDActive { set;}
  166. public abstract float PIDTau { set; }
  167. public abstract void AddForce(PhysicsVector force, bool pushforce);
  168. public abstract void SetMomentum(PhysicsVector momentum);
  169. public abstract void SubscribeEvents(int ms);
  170. public abstract void UnSubscribeEvents();
  171. public abstract bool SubscribedEvents();
  172. }
  173. public class NullPhysicsActor : PhysicsActor
  174. {
  175. public override bool Stopped
  176. {
  177. get{ return false; }
  178. }
  179. public override PhysicsVector Position
  180. {
  181. get { return PhysicsVector.Zero; }
  182. set { return; }
  183. }
  184. public override bool SetAlwaysRun
  185. {
  186. get { return false; }
  187. set { return; }
  188. }
  189. public override uint LocalID
  190. {
  191. set { return; }
  192. }
  193. public override bool Grabbed
  194. {
  195. set { return; }
  196. }
  197. public override bool Selected
  198. {
  199. set { return; }
  200. }
  201. public override float Buoyancy
  202. {
  203. get { return 0f; }
  204. set { return; }
  205. }
  206. public override bool FloatOnWater
  207. {
  208. set { return; }
  209. }
  210. public override bool CollidingGround
  211. {
  212. get { return false; }
  213. set { return; }
  214. }
  215. public override bool CollidingObj
  216. {
  217. get { return false; }
  218. set { return; }
  219. }
  220. public override PhysicsVector Size
  221. {
  222. get { return PhysicsVector.Zero; }
  223. set { return; }
  224. }
  225. public override float Mass
  226. {
  227. get { return 0f; }
  228. }
  229. public override PhysicsVector Force
  230. {
  231. get { return PhysicsVector.Zero; }
  232. }
  233. public override PhysicsVector CenterOfMass
  234. {
  235. get { return PhysicsVector.Zero; }
  236. }
  237. public override PhysicsVector GeometricCenter
  238. {
  239. get { return PhysicsVector.Zero; }
  240. }
  241. public override PrimitiveBaseShape Shape
  242. {
  243. set { return; }
  244. }
  245. public override PhysicsVector Velocity
  246. {
  247. get { return PhysicsVector.Zero; }
  248. set { return; }
  249. }
  250. public override float CollisionScore
  251. {
  252. get { return 0f; }
  253. set { }
  254. }
  255. public override void CrossingFailure()
  256. {
  257. }
  258. public override Quaternion Orientation
  259. {
  260. get { return Quaternion.Identity; }
  261. set { }
  262. }
  263. public override PhysicsVector Acceleration
  264. {
  265. get { return PhysicsVector.Zero; }
  266. }
  267. public override bool IsPhysical
  268. {
  269. get { return false; }
  270. set { return; }
  271. }
  272. public override bool Flying
  273. {
  274. get { return false; }
  275. set { return; }
  276. }
  277. public override bool ThrottleUpdates
  278. {
  279. get { return false; }
  280. set { return; }
  281. }
  282. public override bool IsColliding
  283. {
  284. get { return false; }
  285. set { return; }
  286. }
  287. public override int PhysicsActorType
  288. {
  289. get { return (int) ActorTypes.Unknown; }
  290. set { return; }
  291. }
  292. public override bool Kinematic
  293. {
  294. get { return true; }
  295. set { return; }
  296. }
  297. public override void link(PhysicsActor obj)
  298. {
  299. }
  300. public override void delink()
  301. {
  302. }
  303. public override void LockAngularMotion(PhysicsVector axis)
  304. {
  305. }
  306. public override void AddForce(PhysicsVector force, bool pushforce)
  307. {
  308. }
  309. public override PhysicsVector RotationalVelocity
  310. {
  311. get { return PhysicsVector.Zero; }
  312. set { return; }
  313. }
  314. public override PhysicsVector PIDTarget { set { return; } }
  315. public override bool PIDActive { set { return; } }
  316. public override float PIDTau { set { return; } }
  317. public override void SetMomentum(PhysicsVector momentum)
  318. {
  319. }
  320. public override void SubscribeEvents(int ms)
  321. {
  322. }
  323. public override void UnSubscribeEvents()
  324. {
  325. }
  326. public override bool SubscribedEvents()
  327. {
  328. return false;
  329. }
  330. }
  331. }