BSCharacter.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 copyrightD
  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 System.Reflection;
  30. using log4net;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Physics.Manager;
  34. namespace OpenSim.Region.Physics.BulletSPlugin
  35. {
  36. public class BSCharacter : PhysicsActor
  37. {
  38. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  39. private static readonly string LogHeader = "[BULLETS CHAR]";
  40. private BSScene _scene;
  41. private String _avName;
  42. private bool _stopped;
  43. private Vector3 _size;
  44. private Vector3 _scale;
  45. private PrimitiveBaseShape _pbs;
  46. private uint _localID = 0;
  47. private bool _grabbed;
  48. private bool _selected;
  49. private Vector3 _position;
  50. private float _mass;
  51. public float _density;
  52. public float _avatarVolume;
  53. private Vector3 _force;
  54. private Vector3 _velocity;
  55. private Vector3 _torque;
  56. private float _collisionScore;
  57. private Vector3 _acceleration;
  58. private Quaternion _orientation;
  59. private int _physicsActorType;
  60. private bool _isPhysical;
  61. private bool _flying;
  62. private bool _setAlwaysRun;
  63. private bool _throttleUpdates;
  64. private bool _isColliding;
  65. private long _collidingStep;
  66. private bool _collidingGround;
  67. private long _collidingGroundStep;
  68. private bool _collidingObj;
  69. private bool _floatOnWater;
  70. private Vector3 _rotationalVelocity;
  71. private bool _kinematic;
  72. private float _buoyancy;
  73. private int _subscribedEventsMs = 0;
  74. private int _lastCollisionTime = 0;
  75. private Vector3 _PIDTarget;
  76. private bool _usePID;
  77. private float _PIDTau;
  78. private bool _useHoverPID;
  79. private float _PIDHoverHeight;
  80. private PIDHoverType _PIDHoverType;
  81. private float _PIDHoverTao;
  82. public BSCharacter(uint localID, String avName, BSScene parent_scene, Vector3 pos, Vector3 size, bool isFlying)
  83. {
  84. _localID = localID;
  85. _avName = avName;
  86. _scene = parent_scene;
  87. _position = pos;
  88. _size = size;
  89. _flying = isFlying;
  90. _orientation = Quaternion.Identity;
  91. _velocity = Vector3.Zero;
  92. _buoyancy = isFlying ? 1f : 0f;
  93. _scale = new Vector3(1f, 1f, 1f);
  94. _density = _scene.Params.avatarDensity;
  95. ComputeAvatarVolumeAndMass(); // set _avatarVolume and _mass based on capsule size, _density and _scale
  96. ShapeData shapeData = new ShapeData();
  97. shapeData.ID = _localID;
  98. shapeData.Type = ShapeData.PhysicsShapeType.SHAPE_AVATAR;
  99. shapeData.Position = _position;
  100. shapeData.Rotation = _orientation;
  101. shapeData.Velocity = _velocity;
  102. shapeData.Scale = _scale;
  103. shapeData.Mass = _mass;
  104. shapeData.Buoyancy = _buoyancy;
  105. shapeData.Static = ShapeData.numericFalse;
  106. shapeData.Friction = _scene.Params.avatarFriction;
  107. shapeData.Restitution = _scene.Params.defaultRestitution;
  108. // do actual create at taint time
  109. _scene.TaintedObject(delegate()
  110. {
  111. BulletSimAPI.CreateObject(parent_scene.WorldID, shapeData);
  112. });
  113. return;
  114. }
  115. // called when this character is being destroyed and the resources should be released
  116. public void Destroy()
  117. {
  118. _scene.TaintedObject(delegate()
  119. {
  120. BulletSimAPI.DestroyObject(_scene.WorldID, _localID);
  121. });
  122. }
  123. public override void RequestPhysicsterseUpdate()
  124. {
  125. base.RequestPhysicsterseUpdate();
  126. }
  127. public override bool Stopped {
  128. get { return _stopped; }
  129. }
  130. public override Vector3 Size {
  131. get { return _size; }
  132. set { _size = value;
  133. }
  134. }
  135. public override PrimitiveBaseShape Shape {
  136. set { _pbs = value;
  137. }
  138. }
  139. public override uint LocalID {
  140. set { _localID = value;
  141. }
  142. get { return _localID; }
  143. }
  144. public override bool Grabbed {
  145. set { _grabbed = value;
  146. }
  147. }
  148. public override bool Selected {
  149. set { _selected = value;
  150. }
  151. }
  152. public override void CrossingFailure() { return; }
  153. public override void link(PhysicsActor obj) { return; }
  154. public override void delink() { return; }
  155. public override void LockAngularMotion(Vector3 axis) { return; }
  156. public override Vector3 Position {
  157. get {
  158. // _position = BulletSimAPI.GetObjectPosition(_scene.WorldID, _localID);
  159. return _position;
  160. }
  161. set {
  162. _position = value;
  163. _scene.TaintedObject(delegate()
  164. {
  165. BulletSimAPI.SetObjectTranslation(_scene.WorldID, _localID, _position, _orientation);
  166. });
  167. }
  168. }
  169. public override float Mass {
  170. get {
  171. return _mass;
  172. }
  173. }
  174. public override Vector3 Force {
  175. get { return _force; }
  176. set {
  177. _force = value;
  178. // m_log.DebugFormat("{0}: Force = {1}", LogHeader, _force);
  179. _scene.TaintedObject(delegate()
  180. {
  181. BulletSimAPI.SetObjectForce(_scene.WorldID, _localID, _force);
  182. });
  183. }
  184. }
  185. public override int VehicleType {
  186. get { return 0; }
  187. set { return; }
  188. }
  189. public override void VehicleFloatParam(int param, float value) { }
  190. public override void VehicleVectorParam(int param, Vector3 value) {}
  191. public override void VehicleRotationParam(int param, Quaternion rotation) { }
  192. public override void VehicleFlags(int param, bool remove) { }
  193. // Allows the detection of collisions with inherently non-physical prims. see llVolumeDetect for more
  194. public override void SetVolumeDetect(int param) { return; }
  195. public override Vector3 GeometricCenter { get { return Vector3.Zero; } }
  196. public override Vector3 CenterOfMass { get { return Vector3.Zero; } }
  197. public override Vector3 Velocity {
  198. get { return _velocity; }
  199. set {
  200. _velocity = value;
  201. // m_log.DebugFormat("{0}: set velocity = {1}", LogHeader, _velocity);
  202. _scene.TaintedObject(delegate()
  203. {
  204. BulletSimAPI.SetObjectVelocity(_scene.WorldID, _localID, _velocity);
  205. });
  206. }
  207. }
  208. public override Vector3 Torque {
  209. get { return _torque; }
  210. set { _torque = value;
  211. }
  212. }
  213. public override float CollisionScore {
  214. get { return _collisionScore; }
  215. set { _collisionScore = value;
  216. }
  217. }
  218. public override Vector3 Acceleration {
  219. get { return _acceleration; }
  220. set { _acceleration = value; }
  221. }
  222. public override Quaternion Orientation {
  223. get { return _orientation; }
  224. set {
  225. _orientation = value;
  226. // m_log.DebugFormat("{0}: set orientation to {1}", LogHeader, _orientation);
  227. _scene.TaintedObject(delegate()
  228. {
  229. // _position = BulletSimAPI.GetObjectPosition(_scene.WorldID, _localID);
  230. BulletSimAPI.SetObjectTranslation(_scene.WorldID, _localID, _position, _orientation);
  231. });
  232. }
  233. }
  234. public override int PhysicsActorType {
  235. get { return _physicsActorType; }
  236. set { _physicsActorType = value;
  237. }
  238. }
  239. public override bool IsPhysical {
  240. get { return _isPhysical; }
  241. set { _isPhysical = value;
  242. }
  243. }
  244. public override bool Flying {
  245. get { return _flying; }
  246. set {
  247. _flying = value;
  248. _scene.TaintedObject(delegate()
  249. {
  250. // simulate flying by changing the effect of gravity
  251. BulletSimAPI.SetObjectBuoyancy(_scene.WorldID, LocalID, _flying ? 1f : 0f);
  252. });
  253. }
  254. }
  255. public override bool
  256. SetAlwaysRun {
  257. get { return _setAlwaysRun; }
  258. set { _setAlwaysRun = value; }
  259. }
  260. public override bool ThrottleUpdates {
  261. get { return _throttleUpdates; }
  262. set { _throttleUpdates = value; }
  263. }
  264. public override bool IsColliding {
  265. get { return (_collidingStep == _scene.SimulationStep); }
  266. set { _isColliding = value; }
  267. }
  268. public override bool CollidingGround {
  269. get { return (_collidingGroundStep == _scene.SimulationStep); }
  270. set { _collidingGround = value; }
  271. }
  272. public override bool CollidingObj {
  273. get { return _collidingObj; }
  274. set { _collidingObj = value; }
  275. }
  276. public override bool FloatOnWater {
  277. set { _floatOnWater = value; }
  278. }
  279. public override Vector3 RotationalVelocity {
  280. get { return _rotationalVelocity; }
  281. set { _rotationalVelocity = value; }
  282. }
  283. public override bool Kinematic {
  284. get { return _kinematic; }
  285. set { _kinematic = value; }
  286. }
  287. public override float Buoyancy {
  288. get { return _buoyancy; }
  289. set { _buoyancy = value;
  290. _scene.TaintedObject(delegate()
  291. {
  292. BulletSimAPI.SetObjectBuoyancy(_scene.WorldID, LocalID, _buoyancy);
  293. });
  294. }
  295. }
  296. // Used for MoveTo
  297. public override Vector3 PIDTarget {
  298. set { _PIDTarget = value; }
  299. }
  300. public override bool PIDActive {
  301. set { _usePID = value; }
  302. }
  303. public override float PIDTau {
  304. set { _PIDTau = value; }
  305. }
  306. // Used for llSetHoverHeight and maybe vehicle height
  307. // Hover Height will override MoveTo target's Z
  308. public override bool PIDHoverActive {
  309. set { _useHoverPID = value; }
  310. }
  311. public override float PIDHoverHeight {
  312. set { _PIDHoverHeight = value; }
  313. }
  314. public override PIDHoverType PIDHoverType {
  315. set { _PIDHoverType = value; }
  316. }
  317. public override float PIDHoverTau {
  318. set { _PIDHoverTao = value; }
  319. }
  320. // For RotLookAt
  321. public override Quaternion APIDTarget { set { return; } }
  322. public override bool APIDActive { set { return; } }
  323. public override float APIDStrength { set { return; } }
  324. public override float APIDDamping { set { return; } }
  325. public override void AddForce(Vector3 force, bool pushforce) {
  326. if (force.IsFinite())
  327. {
  328. _force.X += force.X;
  329. _force.Y += force.Y;
  330. _force.Z += force.Z;
  331. // m_log.DebugFormat("{0}: AddForce. adding={1}, newForce={2}", LogHeader, force, _force);
  332. _scene.TaintedObject(delegate()
  333. {
  334. BulletSimAPI.SetObjectForce(_scene.WorldID, _localID, _force);
  335. });
  336. }
  337. else
  338. {
  339. m_log.WarnFormat("{0}: Got a NaN force applied to a Character", LogHeader);
  340. }
  341. //m_lastUpdateSent = false;
  342. }
  343. public override void AddAngularForce(Vector3 force, bool pushforce) {
  344. }
  345. public override void SetMomentum(Vector3 momentum) {
  346. }
  347. public override void SubscribeEvents(int ms) {
  348. _subscribedEventsMs = ms;
  349. _lastCollisionTime = Util.EnvironmentTickCount() - _subscribedEventsMs; // make first collision happen
  350. }
  351. public override void UnSubscribeEvents() {
  352. _subscribedEventsMs = 0;
  353. }
  354. public override bool SubscribedEvents() {
  355. return (_subscribedEventsMs > 0);
  356. }
  357. // set _avatarVolume and _mass based on capsule size, _density and _scale
  358. private void ComputeAvatarVolumeAndMass()
  359. {
  360. _avatarVolume = (float)(
  361. Math.PI
  362. * _scene.Params.avatarCapsuleRadius * _scale.X
  363. * _scene.Params.avatarCapsuleRadius * _scale.Y
  364. * _scene.Params.avatarCapsuleHeight * _scale.Z);
  365. _mass = _density * _avatarVolume;
  366. }
  367. // The physics engine says that properties have updated. Update same and inform
  368. // the world that things have changed.
  369. public void UpdateProperties(EntityProperties entprop)
  370. {
  371. bool changed = false;
  372. // we assign to the local variables so the normal set action does not happen
  373. if (_position != entprop.Position)
  374. {
  375. _position = entprop.Position;
  376. changed = true;
  377. }
  378. if (_orientation != entprop.Rotation)
  379. {
  380. _orientation = entprop.Rotation;
  381. changed = true;
  382. }
  383. if (_velocity != entprop.Velocity)
  384. {
  385. _velocity = entprop.Velocity;
  386. changed = true;
  387. }
  388. if (_acceleration != entprop.Acceleration)
  389. {
  390. _acceleration = entprop.Acceleration;
  391. changed = true;
  392. }
  393. if (_rotationalVelocity != entprop.RotationalVelocity)
  394. {
  395. _rotationalVelocity = entprop.RotationalVelocity;
  396. changed = true;
  397. }
  398. if (changed)
  399. {
  400. // m_log.DebugFormat("{0}: UpdateProperties: id={1}, c={2}, pos={3}, rot={4}", LogHeader, LocalID, changed, _position, _orientation);
  401. // Avatar movement is not done by generating this event. There is a system that
  402. // checks for avatar updates each heartbeat loop.
  403. // base.RequestPhysicsterseUpdate();
  404. }
  405. }
  406. public void Collide(uint collidingWith, ActorTypes type, Vector3 contactPoint, Vector3 contactNormal, float pentrationDepth)
  407. {
  408. // m_log.DebugFormat("{0}: Collide: ms={1}, id={2}, with={3}", LogHeader, _subscribedEventsMs, LocalID, collidingWith);
  409. // The following makes IsColliding() and IsCollidingGround() work
  410. _collidingStep = _scene.SimulationStep;
  411. if (collidingWith == BSScene.TERRAIN_ID || collidingWith == BSScene.GROUNDPLANE_ID)
  412. {
  413. _collidingGroundStep = _scene.SimulationStep;
  414. }
  415. // throttle collisions to the rate specified in the subscription
  416. if (_subscribedEventsMs == 0) return; // don't want collisions
  417. int nowTime = _scene.SimulationNowTime;
  418. if (nowTime < (_lastCollisionTime + _subscribedEventsMs)) return;
  419. _lastCollisionTime = nowTime;
  420. Dictionary<uint, ContactPoint> contactPoints = new Dictionary<uint, ContactPoint>();
  421. contactPoints.Add(collidingWith, new ContactPoint(contactPoint, contactNormal, pentrationDepth));
  422. CollisionEventUpdate args = new CollisionEventUpdate(contactPoints);
  423. base.SendCollisionUpdate(args);
  424. }
  425. }
  426. }