PhysicsActor.cs 20 KB

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