BSCharacter.cs 14 KB

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