BulletDotNETCharacter.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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 System;
  28. using System.Reflection;
  29. using BulletDotNET;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Physics.Manager;
  33. using log4net;
  34. namespace OpenSim.Region.Physics.BulletDotNETPlugin
  35. {
  36. public class BulletDotNETCharacter : PhysicsActor
  37. {
  38. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  39. public btRigidBody Body;
  40. public btCollisionShape Shell;
  41. public btVector3 tempVector1;
  42. public btVector3 tempVector2;
  43. public btVector3 tempVector3;
  44. public btVector3 tempVector4;
  45. public btVector3 tempVector5RayCast;
  46. public btVector3 tempVector6RayCast;
  47. public btVector3 tempVector7RayCast;
  48. public btQuaternion tempQuat1;
  49. public btTransform tempTrans1;
  50. public ClosestNotMeRayResultCallback ClosestCastResult;
  51. private btTransform m_bodyTransform;
  52. private btVector3 m_bodyPosition;
  53. private btVector3 m_CapsuleOrientationAxis;
  54. private btQuaternion m_bodyOrientation;
  55. private btDefaultMotionState m_bodyMotionState;
  56. private btGeneric6DofConstraint m_aMotor;
  57. // private Vector3 m_movementComparision;
  58. private Vector3 m_position;
  59. private Vector3 m_zeroPosition;
  60. private bool m_zeroFlag = false;
  61. private bool m_lastUpdateSent = false;
  62. private Vector3 m_velocity;
  63. private Vector3 m_target_velocity;
  64. private Vector3 m_acceleration;
  65. private Vector3 m_rotationalVelocity;
  66. private bool m_pidControllerActive = true;
  67. public float PID_D = 80.0f;
  68. public float PID_P = 90.0f;
  69. public float CAPSULE_RADIUS = 0.37f;
  70. public float CAPSULE_LENGTH = 2.140599f;
  71. public float heightFudgeFactor = 0.52f;
  72. public float walkDivisor = 1.3f;
  73. public float runDivisor = 0.8f;
  74. private float m_mass = 80f;
  75. public float m_density = 60f;
  76. private bool m_flying = false;
  77. private bool m_iscolliding = false;
  78. private bool m_iscollidingGround = false;
  79. private bool m_wascolliding = false;
  80. private bool m_wascollidingGround = false;
  81. private bool m_iscollidingObj = false;
  82. private bool m_alwaysRun = false;
  83. private bool m_hackSentFall = false;
  84. private bool m_hackSentFly = false;
  85. public uint m_localID = 0;
  86. public bool m_returnCollisions = false;
  87. // taints and their non-tainted counterparts
  88. public bool m_isPhysical = false; // the current physical status
  89. public bool m_tainted_isPhysical = false; // set when the physical status is tainted (false=not existing in physics engine, true=existing)
  90. private float m_tainted_CAPSULE_LENGTH; // set when the capsule length changes.
  91. private bool m_taintRemove = false;
  92. // private bool m_taintedPosition = false;
  93. // private Vector3 m_taintedPosition_value;
  94. private Vector3 m_taintedForce;
  95. private float m_buoyancy = 0f;
  96. // private CollisionLocker ode;
  97. // private string m_name = String.Empty;
  98. private bool[] m_colliderarr = new bool[11];
  99. private bool[] m_colliderGroundarr = new bool[11];
  100. private BulletDotNETScene m_parent_scene;
  101. public int m_eventsubscription = 0;
  102. // private CollisionEventUpdate CollisionEventsThisFrame = new CollisionEventUpdate();
  103. public BulletDotNETCharacter(string avName, BulletDotNETScene parent_scene, Vector3 pos, Vector3 size, float pid_d, float pid_p, float capsule_radius, float tensor, float density, float height_fudge_factor, float walk_divisor, float rundivisor)
  104. {
  105. m_position = pos;
  106. m_zeroPosition = pos;
  107. m_parent_scene = parent_scene;
  108. PID_D = pid_d;
  109. PID_P = pid_p;
  110. CAPSULE_RADIUS = capsule_radius;
  111. m_density = density;
  112. heightFudgeFactor = height_fudge_factor;
  113. walkDivisor = walk_divisor;
  114. runDivisor = rundivisor;
  115. for (int i = 0; i < 11; i++)
  116. {
  117. m_colliderarr[i] = false;
  118. }
  119. for (int i = 0; i < 11; i++)
  120. {
  121. m_colliderGroundarr[i] = false;
  122. }
  123. CAPSULE_LENGTH = (size.Z * 1.15f) - CAPSULE_RADIUS * 2.0f;
  124. m_tainted_CAPSULE_LENGTH = CAPSULE_LENGTH;
  125. m_isPhysical = false; // current status: no ODE information exists
  126. m_tainted_isPhysical = true; // new tainted status: need to create ODE information
  127. m_parent_scene.AddPhysicsActorTaint(this);
  128. // m_name = avName;
  129. tempVector1 = new btVector3(0, 0, 0);
  130. tempVector2 = new btVector3(0, 0, 0);
  131. tempVector3 = new btVector3(0, 0, 0);
  132. tempVector4 = new btVector3(0, 0, 0);
  133. tempVector5RayCast = new btVector3(0, 0, 0);
  134. tempVector6RayCast = new btVector3(0, 0, 0);
  135. tempVector7RayCast = new btVector3(0, 0, 0);
  136. tempQuat1 = new btQuaternion(0, 0, 0, 1);
  137. tempTrans1 = new btTransform(tempQuat1, tempVector1);
  138. // m_movementComparision = new PhysicsVector(0, 0, 0);
  139. m_CapsuleOrientationAxis = new btVector3(1, 0, 1);
  140. }
  141. /// <summary>
  142. /// This creates the Avatar's physical Surrogate at the position supplied
  143. /// </summary>
  144. /// <param name="npositionX"></param>
  145. /// <param name="npositionY"></param>
  146. /// <param name="npositionZ"></param>
  147. // WARNING: This MUST NOT be called outside of ProcessTaints, else we can have unsynchronized access
  148. // to ODE internals. ProcessTaints is called from within thread-locked Simulate(), so it is the only
  149. // place that is safe to call this routine AvatarGeomAndBodyCreation.
  150. private void AvatarGeomAndBodyCreation(float npositionX, float npositionY, float npositionZ)
  151. {
  152. if (CAPSULE_LENGTH <= 0)
  153. {
  154. m_log.Warn("[PHYSICS]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!");
  155. CAPSULE_LENGTH = 0.01f;
  156. }
  157. if (CAPSULE_RADIUS <= 0)
  158. {
  159. m_log.Warn("[PHYSICS]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!");
  160. CAPSULE_RADIUS = 0.01f;
  161. }
  162. Shell = new btCapsuleShape(CAPSULE_RADIUS, CAPSULE_LENGTH);
  163. if (m_bodyPosition == null)
  164. m_bodyPosition = new btVector3(npositionX, npositionY, npositionZ);
  165. m_bodyPosition.setValue(npositionX, npositionY, npositionZ);
  166. if (m_bodyOrientation == null)
  167. m_bodyOrientation = new btQuaternion(m_CapsuleOrientationAxis, (Utils.DEG_TO_RAD * 90));
  168. if (m_bodyTransform == null)
  169. m_bodyTransform = new btTransform(m_bodyOrientation, m_bodyPosition);
  170. else
  171. {
  172. m_bodyTransform.Dispose();
  173. m_bodyTransform = new btTransform(m_bodyOrientation, m_bodyPosition);
  174. }
  175. if (m_bodyMotionState == null)
  176. m_bodyMotionState = new btDefaultMotionState(m_bodyTransform);
  177. else
  178. m_bodyMotionState.setWorldTransform(m_bodyTransform);
  179. m_mass = Mass;
  180. Body = new btRigidBody(m_mass, m_bodyMotionState, Shell);
  181. Body.setUserPointer(new IntPtr((int)Body.Handle));
  182. if (ClosestCastResult != null)
  183. ClosestCastResult.Dispose();
  184. ClosestCastResult = new ClosestNotMeRayResultCallback(Body);
  185. m_parent_scene.AddRigidBody(Body);
  186. Body.setActivationState(4);
  187. if (m_aMotor != null)
  188. {
  189. if (m_aMotor.Handle != IntPtr.Zero)
  190. {
  191. m_parent_scene.getBulletWorld().removeConstraint(m_aMotor);
  192. m_aMotor.Dispose();
  193. }
  194. m_aMotor = null;
  195. }
  196. m_aMotor = new btGeneric6DofConstraint(Body, m_parent_scene.TerrainBody,
  197. m_parent_scene.TransZero,
  198. m_parent_scene.TransZero, false);
  199. m_aMotor.setAngularLowerLimit(m_parent_scene.VectorZero);
  200. m_aMotor.setAngularUpperLimit(m_parent_scene.VectorZero);
  201. }
  202. public void Remove()
  203. {
  204. m_taintRemove = true;
  205. }
  206. public override bool Stopped
  207. {
  208. get { return m_zeroFlag; }
  209. }
  210. public override Vector3 Size
  211. {
  212. get { return new Vector3(CAPSULE_RADIUS * 2, CAPSULE_RADIUS * 2, CAPSULE_LENGTH); }
  213. set
  214. {
  215. m_pidControllerActive = true;
  216. Vector3 SetSize = value;
  217. m_tainted_CAPSULE_LENGTH = (SetSize.Z * 1.15f) - CAPSULE_RADIUS * 2.0f;
  218. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  219. Velocity = Vector3.Zero;
  220. m_parent_scene.AddPhysicsActorTaint(this);
  221. }
  222. }
  223. /// <summary>
  224. /// turn the PID controller on or off.
  225. /// The PID Controller will turn on all by itself in many situations
  226. /// </summary>
  227. /// <param name="status"></param>
  228. public void SetPidStatus(bool status)
  229. {
  230. m_pidControllerActive = status;
  231. }
  232. public override PrimitiveBaseShape Shape
  233. {
  234. set { return; }
  235. }
  236. public override uint LocalID
  237. {
  238. set { m_localID = value; }
  239. }
  240. public override bool Grabbed
  241. {
  242. set { return; }
  243. }
  244. public override bool Selected
  245. {
  246. set { return; }
  247. }
  248. public override void CrossingFailure()
  249. {
  250. }
  251. public override void link(PhysicsActor obj)
  252. {
  253. }
  254. public override void delink()
  255. {
  256. }
  257. public override void LockAngularMotion(Vector3 axis)
  258. {
  259. }
  260. public override Vector3 Position
  261. {
  262. get { return m_position; }
  263. set
  264. {
  265. // m_taintedPosition_value = value;
  266. m_position = value;
  267. // m_taintedPosition = true;
  268. }
  269. }
  270. public override float Mass
  271. {
  272. get
  273. {
  274. float AVvolume = (float)(Math.PI * Math.Pow(CAPSULE_RADIUS, 2) * CAPSULE_LENGTH);
  275. return m_density * AVvolume;
  276. }
  277. }
  278. public override Vector3 Force
  279. {
  280. get { return m_target_velocity; }
  281. set { return; }
  282. }
  283. public override int VehicleType
  284. {
  285. get { return 0; }
  286. set { return; }
  287. }
  288. public override void VehicleFloatParam(int param, float value)
  289. {
  290. }
  291. public override void VehicleVectorParam(int param, Vector3 value)
  292. {
  293. }
  294. public override void VehicleRotationParam(int param, Quaternion rotation)
  295. {
  296. }
  297. public override void SetVolumeDetect(int param)
  298. {
  299. }
  300. public override Vector3 GeometricCenter
  301. {
  302. get { return Vector3.Zero; }
  303. }
  304. public override Vector3 CenterOfMass
  305. {
  306. get { return Vector3.Zero; }
  307. }
  308. public override Vector3 Velocity
  309. {
  310. get
  311. {
  312. if (m_zeroFlag)
  313. return Vector3.Zero;
  314. m_lastUpdateSent = false;
  315. return m_velocity;
  316. }
  317. set
  318. {
  319. m_pidControllerActive = true;
  320. m_target_velocity = value;
  321. }
  322. }
  323. public override Vector3 Torque
  324. {
  325. get { return Vector3.Zero; }
  326. set { return; }
  327. }
  328. public override float CollisionScore
  329. {
  330. get { return 0f; }
  331. set { }
  332. }
  333. public override Vector3 Acceleration
  334. {
  335. get { return m_acceleration; }
  336. }
  337. public override Quaternion Orientation
  338. {
  339. get { return Quaternion.Identity; }
  340. set
  341. {
  342. }
  343. }
  344. public override int PhysicsActorType
  345. {
  346. get { return (int)ActorTypes.Agent; }
  347. set { return; }
  348. }
  349. public override bool IsPhysical
  350. {
  351. get { return false; }
  352. set { return; }
  353. }
  354. public override bool Flying
  355. {
  356. get { return m_flying; }
  357. set { m_flying = value; }
  358. }
  359. public override bool SetAlwaysRun
  360. {
  361. get { return m_alwaysRun; }
  362. set { m_alwaysRun = value; }
  363. }
  364. public override bool ThrottleUpdates
  365. {
  366. get { return false; }
  367. set { return; }
  368. }
  369. /// <summary>
  370. /// Returns if the avatar is colliding in general.
  371. /// This includes the ground and objects and avatar.
  372. /// </summary>
  373. public override bool IsColliding
  374. {
  375. get { return m_iscolliding; }
  376. set
  377. {
  378. int i;
  379. int truecount = 0;
  380. int falsecount = 0;
  381. if (m_colliderarr.Length >= 10)
  382. {
  383. for (i = 0; i < 10; i++)
  384. {
  385. m_colliderarr[i] = m_colliderarr[i + 1];
  386. }
  387. }
  388. m_colliderarr[10] = value;
  389. for (i = 0; i < 11; i++)
  390. {
  391. if (m_colliderarr[i])
  392. {
  393. truecount++;
  394. }
  395. else
  396. {
  397. falsecount++;
  398. }
  399. }
  400. // Equal truecounts and false counts means we're colliding with something.
  401. m_log.DebugFormat("[PHYSICS]: TrueCount:{0}, FalseCount:{1}",truecount,falsecount);
  402. if (falsecount > 1.2 * truecount)
  403. {
  404. m_iscolliding = false;
  405. }
  406. else
  407. {
  408. m_iscolliding = true;
  409. }
  410. if (m_wascolliding != m_iscolliding)
  411. {
  412. //base.SendCollisionUpdate(new CollisionEventUpdate());
  413. }
  414. m_wascolliding = m_iscolliding;
  415. }
  416. }
  417. /// <summary>
  418. /// Returns if an avatar is colliding with the ground
  419. /// </summary>
  420. public override bool CollidingGround
  421. {
  422. get { return m_iscollidingGround; }
  423. set
  424. {
  425. // Collisions against the ground are not really reliable
  426. // So, to get a consistant value we have to average the current result over time
  427. // Currently we use 1 second = 10 calls to this.
  428. int i;
  429. int truecount = 0;
  430. int falsecount = 0;
  431. if (m_colliderGroundarr.Length >= 10)
  432. {
  433. for (i = 0; i < 10; i++)
  434. {
  435. m_colliderGroundarr[i] = m_colliderGroundarr[i + 1];
  436. }
  437. }
  438. m_colliderGroundarr[10] = value;
  439. for (i = 0; i < 11; i++)
  440. {
  441. if (m_colliderGroundarr[i])
  442. {
  443. truecount++;
  444. }
  445. else
  446. {
  447. falsecount++;
  448. }
  449. }
  450. // Equal truecounts and false counts means we're colliding with something.
  451. if (falsecount > 1.2 * truecount)
  452. {
  453. m_iscollidingGround = false;
  454. }
  455. else
  456. {
  457. m_iscollidingGround = true;
  458. }
  459. if (m_wascollidingGround != m_iscollidingGround)
  460. {
  461. //base.SendCollisionUpdate(new CollisionEventUpdate());
  462. }
  463. m_wascollidingGround = m_iscollidingGround;
  464. }
  465. }
  466. /// <summary>
  467. /// Returns if the avatar is colliding with an object
  468. /// </summary>
  469. public override bool CollidingObj
  470. {
  471. get { return m_iscollidingObj; }
  472. set
  473. {
  474. m_iscollidingObj = value;
  475. if (value)
  476. m_pidControllerActive = false;
  477. else
  478. m_pidControllerActive = true;
  479. }
  480. }
  481. public override bool FloatOnWater
  482. {
  483. set { return; }
  484. }
  485. public override Vector3 RotationalVelocity
  486. {
  487. get { return m_rotationalVelocity; }
  488. set { m_rotationalVelocity = value; }
  489. }
  490. public override bool Kinematic
  491. {
  492. get { return false; }
  493. set { }
  494. }
  495. public override float Buoyancy
  496. {
  497. get { return m_buoyancy; }
  498. set { m_buoyancy = value; }
  499. }
  500. public override Vector3 PIDTarget { set { return; } }
  501. public override bool PIDActive { set { return; } }
  502. public override float PIDTau { set { return; } }
  503. public override bool PIDHoverActive
  504. {
  505. set { return; }
  506. }
  507. public override float PIDHoverHeight
  508. {
  509. set { return; }
  510. }
  511. public override PIDHoverType PIDHoverType
  512. {
  513. set { return; }
  514. }
  515. public override float PIDHoverTau
  516. {
  517. set { return; }
  518. }
  519. public override Quaternion APIDTarget
  520. {
  521. set { return; }
  522. }
  523. public override bool APIDActive
  524. {
  525. set { return; }
  526. }
  527. public override float APIDStrength
  528. {
  529. set { return; }
  530. }
  531. public override float APIDDamping
  532. {
  533. set { return; }
  534. }
  535. /// <summary>
  536. /// Adds the force supplied to the Target Velocity
  537. /// The PID controller takes this target velocity and tries to make it a reality
  538. /// </summary>
  539. /// <param name="force"></param>
  540. /// <param name="pushforce">Is this a push by a script?</param>
  541. public override void AddForce(Vector3 force, bool pushforce)
  542. {
  543. if (pushforce)
  544. {
  545. m_pidControllerActive = false;
  546. force *= 100f;
  547. doForce(force, false);
  548. //System.Console.WriteLine("Push!");
  549. //_target_velocity.X += force.X;
  550. // _target_velocity.Y += force.Y;
  551. //_target_velocity.Z += force.Z;
  552. }
  553. else
  554. {
  555. m_pidControllerActive = true;
  556. m_target_velocity.X += force.X;
  557. m_target_velocity.Y += force.Y;
  558. m_target_velocity.Z += force.Z;
  559. }
  560. //m_lastUpdateSent = false;
  561. }
  562. public void doForce(Vector3 force, bool now)
  563. {
  564. tempVector3.setValue(force.X, force.Y, force.Z);
  565. if (now)
  566. {
  567. Body.applyCentralForce(tempVector3);
  568. }
  569. else
  570. {
  571. m_taintedForce += force;
  572. m_parent_scene.AddPhysicsActorTaint(this);
  573. }
  574. }
  575. public void doImpulse(Vector3 force, bool now)
  576. {
  577. tempVector3.setValue(force.X, force.Y, force.Z);
  578. if (now)
  579. {
  580. Body.applyCentralImpulse(tempVector3);
  581. }
  582. else
  583. {
  584. m_taintedForce += force;
  585. m_parent_scene.AddPhysicsActorTaint(this);
  586. }
  587. }
  588. public override void AddAngularForce(Vector3 force, bool pushforce)
  589. {
  590. }
  591. public override void SetMomentum(Vector3 momentum)
  592. {
  593. }
  594. public override void SubscribeEvents(int ms)
  595. {
  596. m_eventsubscription = ms;
  597. m_parent_scene.addCollisionEventReporting(this);
  598. }
  599. public override void UnSubscribeEvents()
  600. {
  601. m_parent_scene.remCollisionEventReporting(this);
  602. m_eventsubscription = 0;
  603. }
  604. public override bool SubscribedEvents()
  605. {
  606. if (m_eventsubscription > 0)
  607. return true;
  608. return false;
  609. }
  610. internal void Dispose()
  611. {
  612. if (Body.isInWorld())
  613. m_parent_scene.removeFromWorld(Body);
  614. if (m_aMotor.Handle != IntPtr.Zero)
  615. m_parent_scene.getBulletWorld().removeConstraint(m_aMotor);
  616. m_aMotor.Dispose(); m_aMotor = null;
  617. ClosestCastResult.Dispose(); ClosestCastResult = null;
  618. Body.Dispose(); Body = null;
  619. Shell.Dispose(); Shell = null;
  620. tempQuat1.Dispose();
  621. tempTrans1.Dispose();
  622. tempVector1.Dispose();
  623. tempVector2.Dispose();
  624. tempVector3.Dispose();
  625. tempVector4.Dispose();
  626. tempVector5RayCast.Dispose();
  627. tempVector6RayCast.Dispose();
  628. }
  629. public void ProcessTaints(float timestep)
  630. {
  631. if (m_tainted_isPhysical != m_isPhysical)
  632. {
  633. if (m_tainted_isPhysical)
  634. {
  635. // Create avatar capsule and related ODE data
  636. if (!(Shell == null && Body == null))
  637. {
  638. m_log.Warn("[PHYSICS]: re-creating the following avatar ODE data, even though it already exists - "
  639. + (Shell != null ? "Shell " : "")
  640. + (Body != null ? "Body " : ""));
  641. }
  642. AvatarGeomAndBodyCreation(m_position.X, m_position.Y, m_position.Z);
  643. }
  644. else
  645. {
  646. // destroy avatar capsule and related ODE data
  647. Dispose();
  648. tempVector1 = new btVector3(0, 0, 0);
  649. tempVector2 = new btVector3(0, 0, 0);
  650. tempVector3 = new btVector3(0, 0, 0);
  651. tempVector4 = new btVector3(0, 0, 0);
  652. tempVector5RayCast = new btVector3(0, 0, 0);
  653. tempVector6RayCast = new btVector3(0, 0, 0);
  654. tempVector7RayCast = new btVector3(0, 0, 0);
  655. tempQuat1 = new btQuaternion(0, 0, 0, 1);
  656. tempTrans1 = new btTransform(tempQuat1, tempVector1);
  657. // m_movementComparision = new PhysicsVector(0, 0, 0);
  658. m_CapsuleOrientationAxis = new btVector3(1, 0, 1);
  659. }
  660. m_isPhysical = m_tainted_isPhysical;
  661. }
  662. if (m_tainted_CAPSULE_LENGTH != CAPSULE_LENGTH)
  663. {
  664. if (Body != null)
  665. {
  666. m_pidControllerActive = true;
  667. // no lock needed on _parent_scene.OdeLock because we are called from within the thread lock in OdePlugin's simulate()
  668. //d.JointDestroy(Amotor);
  669. float prevCapsule = CAPSULE_LENGTH;
  670. CAPSULE_LENGTH = m_tainted_CAPSULE_LENGTH;
  671. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  672. Dispose();
  673. tempVector1 = new btVector3(0, 0, 0);
  674. tempVector2 = new btVector3(0, 0, 0);
  675. tempVector3 = new btVector3(0, 0, 0);
  676. tempVector4 = new btVector3(0, 0, 0);
  677. tempVector5RayCast = new btVector3(0, 0, 0);
  678. tempVector6RayCast = new btVector3(0, 0, 0);
  679. tempVector7RayCast = new btVector3(0, 0, 0);
  680. tempQuat1 = new btQuaternion(0, 0, 0, 1);
  681. tempTrans1 = new btTransform(tempQuat1, tempVector1);
  682. // m_movementComparision = new PhysicsVector(0, 0, 0);
  683. m_CapsuleOrientationAxis = new btVector3(1, 0, 1);
  684. AvatarGeomAndBodyCreation(m_position.X, m_position.Y,
  685. m_position.Z + (Math.Abs(CAPSULE_LENGTH - prevCapsule) * 2));
  686. Velocity = Vector3.Zero;
  687. }
  688. else
  689. {
  690. m_log.Warn("[PHYSICS]: trying to change capsule size, but the following ODE data is missing - "
  691. + (Shell == null ? "Shell " : "")
  692. + (Body == null ? "Body " : ""));
  693. }
  694. }
  695. if (m_taintRemove)
  696. {
  697. Dispose();
  698. }
  699. }
  700. /// <summary>
  701. /// Called from Simulate
  702. /// This is the avatar's movement control + PID Controller
  703. /// </summary>
  704. /// <param name="timeStep"></param>
  705. public void Move(float timeStep)
  706. {
  707. // no lock; for now it's only called from within Simulate()
  708. // If the PID Controller isn't active then we set our force
  709. // calculating base velocity to the current position
  710. if (Body == null)
  711. return;
  712. tempTrans1.Dispose();
  713. tempTrans1 = Body.getInterpolationWorldTransform();
  714. tempVector1.Dispose();
  715. tempVector1 = tempTrans1.getOrigin();
  716. tempVector2.Dispose();
  717. tempVector2 = Body.getInterpolationLinearVelocity();
  718. if (m_pidControllerActive == false)
  719. {
  720. m_zeroPosition.X = tempVector1.getX();
  721. m_zeroPosition.Y = tempVector1.getY();
  722. m_zeroPosition.Z = tempVector1.getZ();
  723. }
  724. //PidStatus = true;
  725. Vector3 vec = Vector3.Zero;
  726. Vector3 vel = new Vector3(tempVector2.getX(), tempVector2.getY(), tempVector2.getZ());
  727. float movementdivisor = 1f;
  728. if (!m_alwaysRun)
  729. {
  730. movementdivisor = walkDivisor;
  731. }
  732. else
  733. {
  734. movementdivisor = runDivisor;
  735. }
  736. // if velocity is zero, use position control; otherwise, velocity control
  737. if (m_target_velocity.X == 0.0f && m_target_velocity.Y == 0.0f && m_target_velocity.Z == 0.0f && m_iscolliding)
  738. {
  739. // keep track of where we stopped. No more slippin' & slidin'
  740. if (!m_zeroFlag)
  741. {
  742. m_zeroFlag = true;
  743. m_zeroPosition.X = tempVector1.getX();
  744. m_zeroPosition.Y = tempVector1.getY();
  745. m_zeroPosition.Z = tempVector1.getZ();
  746. }
  747. if (m_pidControllerActive)
  748. {
  749. // We only want to deactivate the PID Controller if we think we want to have our surrogate
  750. // react to the physics scene by moving it's position.
  751. // Avatar to Avatar collisions
  752. // Prim to avatar collisions
  753. Vector3 pos = new Vector3(tempVector1.getX(), tempVector1.getY(), tempVector1.getZ());
  754. vec.X = (m_target_velocity.X - vel.X) * (PID_D) + (m_zeroPosition.X - pos.X) * (PID_P * 2);
  755. vec.Y = (m_target_velocity.Y - vel.Y) * (PID_D) + (m_zeroPosition.Y - pos.Y) * (PID_P * 2);
  756. if (m_flying)
  757. {
  758. vec.Z = (m_target_velocity.Z - vel.Z) * (PID_D) + (m_zeroPosition.Z - pos.Z) * PID_P;
  759. }
  760. }
  761. //PidStatus = true;
  762. }
  763. else
  764. {
  765. m_pidControllerActive = true;
  766. m_zeroFlag = false;
  767. if (m_iscolliding && !m_flying)
  768. {
  769. // We're standing on something
  770. vec.X = ((m_target_velocity.X / movementdivisor) - vel.X) * (PID_D);
  771. vec.Y = ((m_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D);
  772. }
  773. else if (m_iscolliding && m_flying)
  774. {
  775. // We're flying and colliding with something
  776. vec.X = ((m_target_velocity.X / movementdivisor) - vel.X) * (PID_D / 16);
  777. vec.Y = ((m_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D / 16);
  778. }
  779. else if (!m_iscolliding && m_flying)
  780. {
  781. // we're in mid air suspended
  782. vec.X = ((m_target_velocity.X / movementdivisor) - vel.X) * (PID_D / 6);
  783. vec.Y = ((m_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D / 6);
  784. // We don't want linear velocity to cause our avatar to bounce, so we check target Z and actual velocity X, Y
  785. // rebound preventing
  786. if (m_target_velocity.Z < 0.025f && m_velocity.X < 0.25f && m_velocity.Y < 0.25f)
  787. m_zeroFlag = true;
  788. }
  789. if (m_iscolliding && !m_flying && m_target_velocity.Z > 0.0f)
  790. {
  791. // We're colliding with something and we're not flying but we're moving
  792. // This means we're walking or running.
  793. Vector3 pos = new Vector3(tempVector1.getX(), tempVector1.getY(), tempVector1.getZ());
  794. vec.Z = (m_target_velocity.Z - vel.Z) * PID_D + (m_zeroPosition.Z - pos.Z) * PID_P;
  795. if (m_target_velocity.X > 0)
  796. {
  797. vec.X = ((m_target_velocity.X - vel.X) / 1.2f) * PID_D;
  798. }
  799. if (m_target_velocity.Y > 0)
  800. {
  801. vec.Y = ((m_target_velocity.Y - vel.Y) / 1.2f) * PID_D;
  802. }
  803. }
  804. else if (!m_iscolliding && !m_flying)
  805. {
  806. // we're not colliding and we're not flying so that means we're falling!
  807. // m_iscolliding includes collisions with the ground.
  808. // d.Vector3 pos = d.BodyGetPosition(Body);
  809. if (m_target_velocity.X > 0)
  810. {
  811. vec.X = ((m_target_velocity.X - vel.X) / 1.2f) * PID_D;
  812. }
  813. if (m_target_velocity.Y > 0)
  814. {
  815. vec.Y = ((m_target_velocity.Y - vel.Y) / 1.2f) * PID_D;
  816. }
  817. }
  818. if (m_flying)
  819. {
  820. vec.Z = (m_target_velocity.Z - vel.Z) * (PID_D);
  821. }
  822. }
  823. if (m_flying)
  824. {
  825. // Slight PID correction
  826. vec.Z += (((-1 * m_parent_scene.gravityz) * m_mass) * 0.06f);
  827. //auto fly height. Kitto Flora
  828. //d.Vector3 pos = d.BodyGetPosition(Body);
  829. float target_altitude = m_parent_scene.GetTerrainHeightAtXY(m_position.X, m_position.Y) + 5.0f;
  830. if (m_position.Z < target_altitude)
  831. {
  832. vec.Z += (target_altitude - m_position.Z) * PID_P * 5.0f;
  833. }
  834. }
  835. if (Body != null && (((m_target_velocity.X > 0.2f || m_target_velocity.X < -0.2f) || (m_target_velocity.Y > 0.2f || m_target_velocity.Y < -0.2f))))
  836. {
  837. Body.setFriction(0.001f);
  838. //m_log.DebugFormat("[PHYSICS]: Avatar force applied: {0}, Target:{1}", vec.ToString(), m_target_velocity.ToString());
  839. }
  840. if (Body != null)
  841. {
  842. int activationstate = Body.getActivationState();
  843. if (activationstate == 0)
  844. {
  845. Body.forceActivationState(1);
  846. }
  847. }
  848. doImpulse(vec, true);
  849. }
  850. /// <summary>
  851. /// Updates the reported position and velocity. This essentially sends the data up to ScenePresence.
  852. /// </summary>
  853. public void UpdatePositionAndVelocity()
  854. {
  855. if (Body == null)
  856. return;
  857. //int val = Environment.TickCount;
  858. CheckIfStandingOnObject();
  859. //m_log.DebugFormat("time:{0}", Environment.TickCount - val);
  860. //IsColliding = Body.checkCollideWith(m_parent_scene.TerrainBody);
  861. tempTrans1.Dispose();
  862. tempTrans1 = Body.getInterpolationWorldTransform();
  863. tempVector1.Dispose();
  864. tempVector1 = tempTrans1.getOrigin();
  865. tempVector2.Dispose();
  866. tempVector2 = Body.getInterpolationLinearVelocity();
  867. // no lock; called from Simulate() -- if you call this from elsewhere, gotta lock or do Monitor.Enter/Exit!
  868. Vector3 vec = new Vector3(tempVector1.getX(), tempVector1.getY(), tempVector1.getZ());
  869. // kluge to keep things in bounds. ODE lets dead avatars drift away (they should be removed!)
  870. if (vec.X < -10.0f) vec.X = 0.0f;
  871. if (vec.Y < -10.0f) vec.Y = 0.0f;
  872. if (vec.X > (int)Constants.RegionSize + 10.2f) vec.X = (int)Constants.RegionSize + 10.2f;
  873. if (vec.Y > (int)Constants.RegionSize + 10.2f) vec.Y = (int)Constants.RegionSize + 10.2f;
  874. m_position.X = vec.X;
  875. m_position.Y = vec.Y;
  876. m_position.Z = vec.Z;
  877. // Did we move last? = zeroflag
  878. // This helps keep us from sliding all over
  879. if (m_zeroFlag)
  880. {
  881. m_velocity.X = 0.0f;
  882. m_velocity.Y = 0.0f;
  883. m_velocity.Z = 0.0f;
  884. // Did we send out the 'stopped' message?
  885. if (!m_lastUpdateSent)
  886. {
  887. m_lastUpdateSent = true;
  888. //base.RequestPhysicsterseUpdate();
  889. }
  890. }
  891. else
  892. {
  893. m_lastUpdateSent = false;
  894. vec = new Vector3(tempVector2.getX(), tempVector2.getY(), tempVector2.getZ());
  895. m_velocity.X = (vec.X);
  896. m_velocity.Y = (vec.Y);
  897. m_velocity.Z = (vec.Z);
  898. //m_log.Debug(m_target_velocity);
  899. if (m_velocity.Z < -6 && !m_hackSentFall)
  900. {
  901. m_hackSentFall = true;
  902. m_pidControllerActive = false;
  903. }
  904. else if (m_flying && !m_hackSentFly)
  905. {
  906. //m_hackSentFly = true;
  907. //base.SendCollisionUpdate(new CollisionEventUpdate());
  908. }
  909. else
  910. {
  911. m_hackSentFly = false;
  912. m_hackSentFall = false;
  913. }
  914. }
  915. if (Body != null)
  916. {
  917. if (Body.getFriction() < 0.9f)
  918. Body.setFriction(0.9f);
  919. }
  920. //if (Body != null)
  921. // Body.clearForces();
  922. }
  923. public void CheckIfStandingOnObject()
  924. {
  925. float capsuleHalfHeight = ((CAPSULE_LENGTH + 2*CAPSULE_RADIUS)*0.5f);
  926. tempVector5RayCast.setValue(m_position.X, m_position.Y, m_position.Z);
  927. tempVector6RayCast.setValue(m_position.X, m_position.Y, m_position.Z - 1 * capsuleHalfHeight * 1.1f);
  928. ClosestCastResult.Dispose();
  929. ClosestCastResult = new ClosestNotMeRayResultCallback(Body);
  930. try
  931. {
  932. m_parent_scene.getBulletWorld().rayTest(tempVector5RayCast, tempVector6RayCast, ClosestCastResult);
  933. }
  934. catch (AccessViolationException)
  935. {
  936. m_log.Debug("BAD!");
  937. }
  938. if (ClosestCastResult.hasHit())
  939. {
  940. if (tempVector7RayCast != null)
  941. tempVector7RayCast.Dispose();
  942. //tempVector7RayCast = ClosestCastResult.getHitPointWorld();
  943. /*if (tempVector7RayCast == null) // null == no result also
  944. {
  945. CollidingObj = false;
  946. IsColliding = false;
  947. CollidingGround = false;
  948. return;
  949. }
  950. float zVal = tempVector7RayCast.getZ();
  951. if (zVal != 0)
  952. m_log.Debug("[PHYSICS]: HAAAA");
  953. if (zVal < m_position.Z && zVal > ((CAPSULE_LENGTH + 2 * CAPSULE_RADIUS) *0.5f))
  954. {
  955. CollidingObj = true;
  956. IsColliding = true;
  957. }
  958. else
  959. {
  960. CollidingObj = false;
  961. IsColliding = false;
  962. CollidingGround = false;
  963. }*/
  964. //height+2*radius = capsule full length
  965. //CollidingObj = true;
  966. //IsColliding = true;
  967. m_iscolliding = true;
  968. }
  969. else
  970. {
  971. //CollidingObj = false;
  972. //IsColliding = false;
  973. //CollidingGround = false;
  974. m_iscolliding = false;
  975. }
  976. }
  977. }
  978. }