PhysicsActor.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 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 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. // disable warning: public events
  92. #pragma warning disable 67
  93. public event PositionUpdate OnPositionUpdate;
  94. public event VelocityUpdate OnVelocityUpdate;
  95. public event OrientationUpdate OnOrientationUpdate;
  96. public event RequestTerseUpdate OnRequestTerseUpdate;
  97. public event CollisionUpdate OnCollisionUpdate;
  98. public event OutOfBounds OnOutOfBounds;
  99. #pragma warning restore 67
  100. public static PhysicsActor Null
  101. {
  102. get { return new NullPhysicsActor(); }
  103. }
  104. public abstract bool Stopped { get; }
  105. public abstract PhysicsVector Size { get; set; }
  106. public abstract PrimitiveBaseShape Shape { set; }
  107. public abstract uint LocalID { set; }
  108. public abstract bool Grabbed { set; }
  109. public abstract bool Selected { set; }
  110. public string SOPName;
  111. public string SOPDescription;
  112. public abstract void CrossingFailure();
  113. public abstract void link(PhysicsActor obj);
  114. public abstract void delink();
  115. public abstract void LockAngularMotion(PhysicsVector axis);
  116. public virtual void RequestPhysicsterseUpdate()
  117. {
  118. // Make a temporary copy of the event to avoid possibility of
  119. // a race condition if the last subscriber unsubscribes
  120. // immediately after the null check and before the event is raised.
  121. RequestTerseUpdate handler = OnRequestTerseUpdate;
  122. if (handler != null)
  123. {
  124. handler();
  125. }
  126. }
  127. public virtual void RaiseOutOfBounds(PhysicsVector pos)
  128. {
  129. // Make a temporary copy of the event to avoid possibility of
  130. // a race condition if the last subscriber unsubscribes
  131. // immediately after the null check and before the event is raised.
  132. OutOfBounds handler = OnOutOfBounds;
  133. if (handler != null)
  134. {
  135. handler(pos);
  136. }
  137. }
  138. public virtual void SendCollisionUpdate(EventArgs e)
  139. {
  140. CollisionUpdate handler = OnCollisionUpdate;
  141. if (handler != null)
  142. {
  143. handler(e);
  144. }
  145. }
  146. public abstract PhysicsVector Position { get; set; }
  147. public abstract float Mass { get; }
  148. public abstract PhysicsVector Force { get; set; }
  149. public abstract int VehicleType { get; set; }
  150. public abstract void VehicleFloatParam(int param, float value);
  151. public abstract void VehicleVectorParam(int param, PhysicsVector value);
  152. public abstract void VehicleRotationParam(int param, Quaternion rotation);
  153. public abstract void SetVolumeDetect(int param); // Allows the detection of collisions with inherently non-physical prims. see llVolumeDetect for more
  154. public abstract PhysicsVector GeometricCenter { get; }
  155. public abstract PhysicsVector CenterOfMass { get; }
  156. public abstract PhysicsVector Velocity { get; set; }
  157. public abstract PhysicsVector Torque { get; set; }
  158. public abstract float CollisionScore { get; set;}
  159. public abstract PhysicsVector Acceleration { get; }
  160. public abstract Quaternion Orientation { get; set; }
  161. public abstract int PhysicsActorType { get; set; }
  162. public abstract bool IsPhysical { get; set; }
  163. public abstract bool Flying { get; set; }
  164. public abstract bool SetAlwaysRun { get; set; }
  165. public abstract bool ThrottleUpdates { get; set; }
  166. public abstract bool IsColliding { get; set; }
  167. public abstract bool CollidingGround { get; set; }
  168. public abstract bool CollidingObj { get; set; }
  169. public abstract bool FloatOnWater { set; }
  170. public abstract PhysicsVector RotationalVelocity { get; set; }
  171. public abstract bool Kinematic { get; set; }
  172. public abstract float Buoyancy { get; set; }
  173. public abstract PhysicsVector PIDTarget { set;}
  174. public abstract bool PIDActive { set;}
  175. public abstract float PIDTau { set; }
  176. public abstract void AddForce(PhysicsVector force, bool pushforce);
  177. public abstract void AddAngularForce(PhysicsVector force, bool pushforce);
  178. public abstract void SetMomentum(PhysicsVector momentum);
  179. public abstract void SubscribeEvents(int ms);
  180. public abstract void UnSubscribeEvents();
  181. public abstract bool SubscribedEvents();
  182. }
  183. public class NullPhysicsActor : PhysicsActor
  184. {
  185. public override bool Stopped
  186. {
  187. get{ return false; }
  188. }
  189. public override PhysicsVector Position
  190. {
  191. get { return PhysicsVector.Zero; }
  192. set { return; }
  193. }
  194. public override bool SetAlwaysRun
  195. {
  196. get { return false; }
  197. set { return; }
  198. }
  199. public override uint LocalID
  200. {
  201. set { return; }
  202. }
  203. public override bool Grabbed
  204. {
  205. set { return; }
  206. }
  207. public override bool Selected
  208. {
  209. set { return; }
  210. }
  211. public override float Buoyancy
  212. {
  213. get { return 0f; }
  214. set { return; }
  215. }
  216. public override bool FloatOnWater
  217. {
  218. set { return; }
  219. }
  220. public override bool CollidingGround
  221. {
  222. get { return false; }
  223. set { return; }
  224. }
  225. public override bool CollidingObj
  226. {
  227. get { return false; }
  228. set { return; }
  229. }
  230. public override PhysicsVector Size
  231. {
  232. get { return PhysicsVector.Zero; }
  233. set { return; }
  234. }
  235. public override float Mass
  236. {
  237. get { return 0f; }
  238. }
  239. public override PhysicsVector Force
  240. {
  241. get { return PhysicsVector.Zero; }
  242. set { return; }
  243. }
  244. public override int VehicleType
  245. {
  246. get { return 0; }
  247. set { return; }
  248. }
  249. public override void VehicleFloatParam(int param, float value)
  250. {
  251. }
  252. public override void VehicleVectorParam(int param, PhysicsVector value)
  253. {
  254. }
  255. public override void VehicleRotationParam(int param, Quaternion rotation)
  256. {
  257. }
  258. public override void SetVolumeDetect(int param)
  259. {
  260. }
  261. public override PhysicsVector CenterOfMass
  262. {
  263. get { return PhysicsVector.Zero; }
  264. }
  265. public override PhysicsVector GeometricCenter
  266. {
  267. get { return PhysicsVector.Zero; }
  268. }
  269. public override PrimitiveBaseShape Shape
  270. {
  271. set { return; }
  272. }
  273. public override PhysicsVector Velocity
  274. {
  275. get { return PhysicsVector.Zero; }
  276. set { return; }
  277. }
  278. public override PhysicsVector Torque
  279. {
  280. get { return PhysicsVector.Zero; }
  281. set { return; }
  282. }
  283. public override float CollisionScore
  284. {
  285. get { return 0f; }
  286. set { }
  287. }
  288. public override void CrossingFailure()
  289. {
  290. }
  291. public override Quaternion Orientation
  292. {
  293. get { return Quaternion.Identity; }
  294. set { }
  295. }
  296. public override PhysicsVector Acceleration
  297. {
  298. get { return PhysicsVector.Zero; }
  299. }
  300. public override bool IsPhysical
  301. {
  302. get { return false; }
  303. set { return; }
  304. }
  305. public override bool Flying
  306. {
  307. get { return false; }
  308. set { return; }
  309. }
  310. public override bool ThrottleUpdates
  311. {
  312. get { return false; }
  313. set { return; }
  314. }
  315. public override bool IsColliding
  316. {
  317. get { return false; }
  318. set { return; }
  319. }
  320. public override int PhysicsActorType
  321. {
  322. get { return (int) ActorTypes.Unknown; }
  323. set { return; }
  324. }
  325. public override bool Kinematic
  326. {
  327. get { return true; }
  328. set { return; }
  329. }
  330. public override void link(PhysicsActor obj)
  331. {
  332. }
  333. public override void delink()
  334. {
  335. }
  336. public override void LockAngularMotion(PhysicsVector axis)
  337. {
  338. }
  339. public override void AddForce(PhysicsVector force, bool pushforce)
  340. {
  341. }
  342. public override void AddAngularForce(PhysicsVector force, bool pushforce)
  343. {
  344. }
  345. public override PhysicsVector RotationalVelocity
  346. {
  347. get { return PhysicsVector.Zero; }
  348. set { return; }
  349. }
  350. public override PhysicsVector PIDTarget { set { return; } }
  351. public override bool PIDActive { set { return; } }
  352. public override float PIDTau { set { return; } }
  353. public override void SetMomentum(PhysicsVector momentum)
  354. {
  355. }
  356. public override void SubscribeEvents(int ms)
  357. {
  358. }
  359. public override void UnSubscribeEvents()
  360. {
  361. }
  362. public override bool SubscribedEvents()
  363. {
  364. return false;
  365. }
  366. }
  367. }