1
0

BSCharacter.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. }
  221. public override Quaternion Orientation {
  222. get { return _orientation; }
  223. set {
  224. _orientation = value;
  225. // m_log.DebugFormat("{0}: set orientation to {1}", LogHeader, _orientation);
  226. _scene.TaintedObject(delegate()
  227. {
  228. // _position = BulletSimAPI.GetObjectPosition(_scene.WorldID, _localID);
  229. BulletSimAPI.SetObjectTranslation(_scene.WorldID, _localID, _position, _orientation);
  230. });
  231. }
  232. }
  233. public override int PhysicsActorType {
  234. get { return _physicsActorType; }
  235. set { _physicsActorType = value;
  236. }
  237. }
  238. public override bool IsPhysical {
  239. get { return _isPhysical; }
  240. set { _isPhysical = value;
  241. }
  242. }
  243. public override bool Flying {
  244. get { return _flying; }
  245. set {
  246. _flying = value;
  247. _scene.TaintedObject(delegate()
  248. {
  249. // simulate flying by changing the effect of gravity
  250. BulletSimAPI.SetObjectBuoyancy(_scene.WorldID, LocalID, _flying ? 1f : 0f);
  251. });
  252. }
  253. }
  254. public override bool
  255. SetAlwaysRun {
  256. get { return _setAlwaysRun; }
  257. set { _setAlwaysRun = value; }
  258. }
  259. public override bool ThrottleUpdates {
  260. get { return _throttleUpdates; }
  261. set { _throttleUpdates = value; }
  262. }
  263. public override bool IsColliding {
  264. get { return (_collidingStep == _scene.SimulationStep); }
  265. set { _isColliding = value; }
  266. }
  267. public override bool CollidingGround {
  268. get { return (_collidingGroundStep == _scene.SimulationStep); }
  269. set { _collidingGround = value; }
  270. }
  271. public override bool CollidingObj {
  272. get { return _collidingObj; }
  273. set { _collidingObj = value; }
  274. }
  275. public override bool FloatOnWater {
  276. set { _floatOnWater = value; }
  277. }
  278. public override Vector3 RotationalVelocity {
  279. get { return _rotationalVelocity; }
  280. set { _rotationalVelocity = value; }
  281. }
  282. public override bool Kinematic {
  283. get { return _kinematic; }
  284. set { _kinematic = value; }
  285. }
  286. public override float Buoyancy {
  287. get { return _buoyancy; }
  288. set { _buoyancy = value;
  289. _scene.TaintedObject(delegate()
  290. {
  291. BulletSimAPI.SetObjectBuoyancy(_scene.WorldID, LocalID, _buoyancy);
  292. });
  293. }
  294. }
  295. // Used for MoveTo
  296. public override Vector3 PIDTarget {
  297. set { _PIDTarget = value; }
  298. }
  299. public override bool PIDActive {
  300. set { _usePID = value; }
  301. }
  302. public override float PIDTau {
  303. set { _PIDTau = value; }
  304. }
  305. // Used for llSetHoverHeight and maybe vehicle height
  306. // Hover Height will override MoveTo target's Z
  307. public override bool PIDHoverActive {
  308. set { _useHoverPID = value; }
  309. }
  310. public override float PIDHoverHeight {
  311. set { _PIDHoverHeight = value; }
  312. }
  313. public override PIDHoverType PIDHoverType {
  314. set { _PIDHoverType = value; }
  315. }
  316. public override float PIDHoverTau {
  317. set { _PIDHoverTao = value; }
  318. }
  319. // For RotLookAt
  320. public override Quaternion APIDTarget { set { return; } }
  321. public override bool APIDActive { set { return; } }
  322. public override float APIDStrength { set { return; } }
  323. public override float APIDDamping { set { return; } }
  324. public override void AddForce(Vector3 force, bool pushforce) {
  325. if (force.IsFinite())
  326. {
  327. _force.X += force.X;
  328. _force.Y += force.Y;
  329. _force.Z += force.Z;
  330. // m_log.DebugFormat("{0}: AddForce. adding={1}, newForce={2}", LogHeader, force, _force);
  331. _scene.TaintedObject(delegate()
  332. {
  333. BulletSimAPI.SetObjectForce(_scene.WorldID, _localID, _force);
  334. });
  335. }
  336. else
  337. {
  338. m_log.WarnFormat("{0}: Got a NaN force applied to a Character", LogHeader);
  339. }
  340. //m_lastUpdateSent = false;
  341. }
  342. public override void AddAngularForce(Vector3 force, bool pushforce) {
  343. }
  344. public override void SetMomentum(Vector3 momentum) {
  345. }
  346. public override void SubscribeEvents(int ms) {
  347. _subscribedEventsMs = ms;
  348. _lastCollisionTime = Util.EnvironmentTickCount() - _subscribedEventsMs; // make first collision happen
  349. }
  350. public override void UnSubscribeEvents() {
  351. _subscribedEventsMs = 0;
  352. }
  353. public override bool SubscribedEvents() {
  354. return (_subscribedEventsMs > 0);
  355. }
  356. // set _avatarVolume and _mass based on capsule size, _density and _scale
  357. private void ComputeAvatarVolumeAndMass()
  358. {
  359. _avatarVolume = (float)(
  360. Math.PI
  361. * _scene.Params.avatarCapsuleRadius * _scale.X
  362. * _scene.Params.avatarCapsuleRadius * _scale.Y
  363. * _scene.Params.avatarCapsuleHeight * _scale.Z);
  364. _mass = _density * _avatarVolume;
  365. }
  366. // The physics engine says that properties have updated. Update same and inform
  367. // the world that things have changed.
  368. public void UpdateProperties(EntityProperties entprop)
  369. {
  370. bool changed = false;
  371. // we assign to the local variables so the normal set action does not happen
  372. if (_position != entprop.Position)
  373. {
  374. _position = entprop.Position;
  375. changed = true;
  376. }
  377. if (_orientation != entprop.Rotation)
  378. {
  379. _orientation = entprop.Rotation;
  380. changed = true;
  381. }
  382. if (_velocity != entprop.Velocity)
  383. {
  384. _velocity = entprop.Velocity;
  385. changed = true;
  386. }
  387. if (_acceleration != entprop.Acceleration)
  388. {
  389. _acceleration = entprop.Acceleration;
  390. changed = true;
  391. }
  392. if (_rotationalVelocity != entprop.RotationalVelocity)
  393. {
  394. _rotationalVelocity = entprop.RotationalVelocity;
  395. changed = true;
  396. }
  397. if (changed)
  398. {
  399. // m_log.DebugFormat("{0}: UpdateProperties: id={1}, c={2}, pos={3}, rot={4}", LogHeader, LocalID, changed, _position, _orientation);
  400. // Avatar movement is not done by generating this event. There is a system that
  401. // checks for avatar updates each heartbeat loop.
  402. // base.RequestPhysicsterseUpdate();
  403. }
  404. }
  405. public void Collide(uint collidingWith, ActorTypes type, Vector3 contactPoint, Vector3 contactNormal, float pentrationDepth)
  406. {
  407. // m_log.DebugFormat("{0}: Collide: ms={1}, id={2}, with={3}", LogHeader, _subscribedEventsMs, LocalID, collidingWith);
  408. // The following makes IsColliding() and IsCollidingGround() work
  409. _collidingStep = _scene.SimulationStep;
  410. if (collidingWith == BSScene.TERRAIN_ID || collidingWith == BSScene.GROUNDPLANE_ID)
  411. {
  412. _collidingGroundStep = _scene.SimulationStep;
  413. }
  414. // throttle collisions to the rate specified in the subscription
  415. if (_subscribedEventsMs == 0) return; // don't want collisions
  416. int nowTime = _scene.SimulationNowTime;
  417. if (nowTime < (_lastCollisionTime + _subscribedEventsMs)) return;
  418. _lastCollisionTime = nowTime;
  419. Dictionary<uint, ContactPoint> contactPoints = new Dictionary<uint, ContactPoint>();
  420. contactPoints.Add(collidingWith, new ContactPoint(contactPoint, contactNormal, pentrationDepth));
  421. CollisionEventUpdate args = new CollisionEventUpdate(LocalID, (int)type, 1, contactPoints);
  422. base.SendCollisionUpdate(args);
  423. }
  424. }
  425. }