ODECharacter.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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 OpenSim 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. */
  28. using System;
  29. using Axiom.Math;
  30. using Ode.NET;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Physics.Manager;
  33. namespace OpenSim.Region.Physics.OdePlugin
  34. {
  35. /// <summary>
  36. /// Various properties that ODE uses for AMotors but isn't exposed in ODE.NET so we must define them ourselves.
  37. /// </summary>
  38. public enum dParam : int
  39. {
  40. LowStop = 0,
  41. HiStop = 1,
  42. Vel = 2,
  43. FMax = 3,
  44. FudgeFactor = 4,
  45. Bounce = 5,
  46. CFM = 6,
  47. ERP = 7,
  48. StopCFM = 8,
  49. LoStop2 = 256,
  50. HiStop2 = 257,
  51. LoStop3 = 512,
  52. HiStop3 = 513
  53. }
  54. public class OdeCharacter : PhysicsActor
  55. {
  56. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  57. private PhysicsVector _position;
  58. private d.Vector3 _zeroPosition;
  59. private d.Matrix3 m_StandUpRotation;
  60. private bool _zeroFlag = false;
  61. private bool m_lastUpdateSent = false;
  62. private PhysicsVector _velocity;
  63. private PhysicsVector _target_velocity;
  64. private PhysicsVector _acceleration;
  65. private PhysicsVector m_rotationalVelocity;
  66. private float m_mass = 80f;
  67. private float m_density = 60f;
  68. private bool m_pidControllerActive = true;
  69. private float PID_D = 800.0f;
  70. private float PID_P = 900.0f;
  71. private static float POSTURE_SERVO = 10000.0f;
  72. public static float CAPSULE_RADIUS = 0.37f;
  73. public float CAPSULE_LENGTH = 2.140599f;
  74. private float m_tensor = 3800000f;
  75. private bool flying = false;
  76. private bool m_iscolliding = false;
  77. private bool m_iscollidingGround = false;
  78. private bool m_wascolliding = false;
  79. private bool m_wascollidingGround = false;
  80. private bool m_iscollidingObj = false;
  81. private bool m_wascollidingObj = false;
  82. private bool m_alwaysRun = false;
  83. private bool m_hackSentFall = false;
  84. private bool m_hackSentFly = false;
  85. private bool m_foundDebian = false;
  86. private CollisionLocker ode;
  87. private string m_name = String.Empty;
  88. private bool[] m_colliderarr = new bool[11];
  89. private bool[] m_colliderGroundarr = new bool[11];
  90. private bool jumping = false;
  91. //private float gravityAccel;
  92. public IntPtr Body;
  93. private OdeScene _parent_scene;
  94. public IntPtr Shell;
  95. public IntPtr Amotor;
  96. public d.Mass ShellMass;
  97. public bool collidelock = false;
  98. public OdeCharacter(String avName, OdeScene parent_scene, PhysicsVector pos, CollisionLocker dode, PhysicsVector size)
  99. {
  100. ode = dode;
  101. _velocity = new PhysicsVector();
  102. _target_velocity = new PhysicsVector();
  103. _position = pos;
  104. _acceleration = new PhysicsVector();
  105. _parent_scene = parent_scene;
  106. if (System.Environment.OSVersion.Platform == PlatformID.Unix)
  107. {
  108. m_foundDebian = true;
  109. m_tensor = 2000000f;
  110. }
  111. else
  112. {
  113. m_tensor = 1300000f;
  114. }
  115. m_StandUpRotation =
  116. new d.Matrix3(0.5f, 0.7071068f, 0.5f, -0.7071068f, 0f, 0.7071068f, 0.5f, -0.7071068f,
  117. 0.5f);
  118. for (int i = 0; i < 11; i++)
  119. {
  120. m_colliderarr[i] = false;
  121. }
  122. CAPSULE_LENGTH = (size.Z - ((size.Z * 0.52f)));
  123. lock (OdeScene.OdeLock)
  124. {
  125. AvatarGeomAndBodyCreation(pos.X, pos.Y, pos.Z, m_tensor);
  126. }
  127. m_name = avName;
  128. parent_scene.geom_name_map[Shell] = avName;
  129. parent_scene.actor_name_map[Shell] = (PhysicsActor) this;
  130. }
  131. public override int PhysicsActorType
  132. {
  133. get { return (int) ActorTypes.Agent; }
  134. set { return; }
  135. }
  136. /// <summary>
  137. /// If this is set, the avatar will move faster
  138. /// </summary>
  139. public override bool SetAlwaysRun
  140. {
  141. get { return m_alwaysRun; }
  142. set { m_alwaysRun = value; }
  143. }
  144. public override bool Grabbed
  145. {
  146. set { return; }
  147. }
  148. public override bool Selected
  149. {
  150. set { return; }
  151. }
  152. public override bool IsPhysical
  153. {
  154. get { return false; }
  155. set { return; }
  156. }
  157. public override bool ThrottleUpdates
  158. {
  159. get { return false; }
  160. set { return; }
  161. }
  162. public override bool Flying
  163. {
  164. get { return flying; }
  165. set { flying = value; }
  166. }
  167. /// <summary>
  168. /// Returns if the avatar is colliding in general.
  169. /// This includes the ground and objects and avatar.
  170. /// </summary>
  171. public override bool IsColliding
  172. {
  173. get { return m_iscolliding; }
  174. set
  175. {
  176. int i;
  177. int truecount = 0;
  178. int falsecount = 0;
  179. if (m_colliderarr.Length >= 10)
  180. {
  181. for (i = 0; i < 10; i++)
  182. {
  183. m_colliderarr[i] = m_colliderarr[i + 1];
  184. }
  185. }
  186. m_colliderarr[10] = value;
  187. for (i = 0; i < 11; i++)
  188. {
  189. if (m_colliderarr[i])
  190. {
  191. truecount++;
  192. }
  193. else
  194. {
  195. falsecount++;
  196. }
  197. }
  198. // Equal truecounts and false counts means we're colliding with something.
  199. if (falsecount > 1.2*truecount)
  200. {
  201. m_iscolliding = false;
  202. }
  203. else
  204. {
  205. m_iscolliding = true;
  206. }
  207. if (m_wascolliding != m_iscolliding)
  208. {
  209. //base.SendCollisionUpdate(new CollisionEventUpdate());
  210. }
  211. m_wascolliding = m_iscolliding;
  212. }
  213. }
  214. /// <summary>
  215. /// Returns if an avatar is colliding with the ground
  216. /// </summary>
  217. public override bool CollidingGround
  218. {
  219. get { return m_iscollidingGround; }
  220. set
  221. {
  222. // Collisions against the ground are not really reliable
  223. // So, to get a consistant value we have to average the current result over time
  224. // Currently we use 1 second = 10 calls to this.
  225. int i;
  226. int truecount = 0;
  227. int falsecount = 0;
  228. if (m_colliderGroundarr.Length >= 10)
  229. {
  230. for (i = 0; i < 10; i++)
  231. {
  232. m_colliderGroundarr[i] = m_colliderGroundarr[i + 1];
  233. }
  234. }
  235. m_colliderGroundarr[10] = value;
  236. for (i = 0; i < 11; i++)
  237. {
  238. if (m_colliderGroundarr[i])
  239. {
  240. truecount++;
  241. }
  242. else
  243. {
  244. falsecount++;
  245. }
  246. }
  247. // Equal truecounts and false counts means we're colliding with something.
  248. if (falsecount > 1.2*truecount)
  249. {
  250. m_iscollidingGround = false;
  251. }
  252. else
  253. {
  254. m_iscollidingGround = true;
  255. }
  256. if (m_wascollidingGround != m_iscollidingGround)
  257. {
  258. //base.SendCollisionUpdate(new CollisionEventUpdate());
  259. }
  260. m_wascollidingGround = m_iscollidingGround;
  261. }
  262. }
  263. /// <summary>
  264. /// Returns if the avatar is colliding with an object
  265. /// </summary>
  266. public override bool CollidingObj
  267. {
  268. get { return m_iscollidingObj; }
  269. set
  270. {
  271. m_iscollidingObj = value;
  272. if (value)
  273. m_pidControllerActive = false;
  274. else
  275. m_pidControllerActive = true;
  276. }
  277. }
  278. /// <summary>
  279. /// turn the PID controller on or off.
  280. /// The PID Controller will turn on all by itself in many situations
  281. /// </summary>
  282. /// <param name="status"></param>
  283. public void SetPidStatus(bool status)
  284. {
  285. m_pidControllerActive = status;
  286. }
  287. public override bool Stopped
  288. {
  289. get { return _zeroFlag; }
  290. }
  291. /// <summary>
  292. /// This 'puts' an avatar somewhere in the physics space.
  293. /// Not really a good choice unless you 'know' it's a good
  294. /// spot otherwise you're likely to orbit the avatar.
  295. /// </summary>
  296. public override PhysicsVector Position
  297. {
  298. get { return _position; }
  299. set
  300. {
  301. lock (OdeScene.OdeLock)
  302. {
  303. d.BodySetPosition(Body, value.X, value.Y, value.Z);
  304. _position = value;
  305. }
  306. }
  307. }
  308. public override PhysicsVector RotationalVelocity
  309. {
  310. get { return m_rotationalVelocity; }
  311. set { m_rotationalVelocity = value; }
  312. }
  313. /// <summary>
  314. /// This property sets the height of the avatar only. We use the height to make sure the avatar stands up straight
  315. /// and use it to offset landings properly
  316. /// </summary>
  317. public override PhysicsVector Size
  318. {
  319. get { return new PhysicsVector(CAPSULE_RADIUS*2, CAPSULE_RADIUS*2, CAPSULE_LENGTH); }
  320. set
  321. {
  322. m_pidControllerActive = true;
  323. lock (OdeScene.OdeLock)
  324. {
  325. d.JointDestroy(Amotor);
  326. PhysicsVector SetSize = value;
  327. float prevCapsule = CAPSULE_LENGTH;
  328. float capsuleradius = CAPSULE_RADIUS;
  329. //capsuleradius = 0.2f;
  330. CAPSULE_LENGTH = (SetSize.Z - ((SetSize.Z*0.52f))); // subtract 43% of the size
  331. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  332. d.BodyDestroy(Body);
  333. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  334. d.GeomDestroy(Shell);
  335. AvatarGeomAndBodyCreation(_position.X, _position.Y,
  336. _position.Z + (Math.Abs(CAPSULE_LENGTH - prevCapsule) * 2), m_tensor);
  337. Velocity = new PhysicsVector(0f, 0f, 0f);
  338. }
  339. _parent_scene.geom_name_map[Shell] = m_name;
  340. _parent_scene.actor_name_map[Shell] = (PhysicsActor) this;
  341. }
  342. }
  343. /// <summary>
  344. /// This creates the Avatar's physical Surrogate at the position supplied
  345. /// </summary>
  346. /// <param name="npositionX"></param>
  347. /// <param name="npositionY"></param>
  348. /// <param name="npositionZ"></param>
  349. private void AvatarGeomAndBodyCreation(float npositionX, float npositionY, float npositionZ, float tensor)
  350. {
  351. if (System.Environment.OSVersion.Platform == PlatformID.Unix)
  352. {
  353. m_foundDebian = true;
  354. m_tensor = 2000000f;
  355. }
  356. else
  357. {
  358. m_tensor = 550000f;
  359. }
  360. int dAMotorEuler = 1;
  361. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  362. Shell = d.CreateCapsule(_parent_scene.space, CAPSULE_RADIUS, CAPSULE_LENGTH);
  363. d.MassSetCapsuleTotal(out ShellMass, m_mass, 2, CAPSULE_RADIUS, CAPSULE_LENGTH);
  364. Body = d.BodyCreate(_parent_scene.world);
  365. d.BodySetPosition(Body, npositionX, npositionY, npositionZ);
  366. d.BodySetMass(Body, ref ShellMass);
  367. d.Matrix3 m_caprot;
  368. // 90 Stand up on the cap of the capped cyllinder
  369. d.RFromAxisAndAngle(out m_caprot, 1, 0, 1, (float)(Math.PI / 2));
  370. d.GeomSetRotation(Shell, ref m_caprot);
  371. d.BodySetRotation(Body, ref m_caprot);
  372. d.GeomSetBody(Shell, Body);
  373. // The purpose of the AMotor here is to keep the avatar's physical
  374. // surrogate from rotating while moving
  375. Amotor = d.JointCreateAMotor(_parent_scene.world, IntPtr.Zero);
  376. d.JointAttach(Amotor, Body, IntPtr.Zero);
  377. d.JointSetAMotorMode(Amotor, dAMotorEuler);
  378. d.JointSetAMotorNumAxes(Amotor, 3);
  379. d.JointSetAMotorAxis(Amotor, 0, 0, 1, 0, 0);
  380. d.JointSetAMotorAxis(Amotor, 1, 0, 0, 1, 0);
  381. d.JointSetAMotorAxis(Amotor, 2, 0, 0, 0, 1);
  382. d.JointSetAMotorAngle(Amotor, 0, 0);
  383. d.JointSetAMotorAngle(Amotor, 1, 0);
  384. d.JointSetAMotorAngle(Amotor, 2, 0);
  385. // These lowstops and high stops are effectively (no wiggle room)
  386. d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, -0.000000000001f);
  387. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -0.000000000001f);
  388. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, -0.000000000001f);
  389. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 0.000000000001f);
  390. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0.000000000001f);
  391. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 0.000000000001f);
  392. // Fudge factor is 1f by default, we're setting it to 0. We don't want it to Fudge or the
  393. // capped cyllinder will fall over
  394. d.JointSetAMotorParam(Amotor, (int)dParam.FudgeFactor, 0f);
  395. d.JointSetAMotorParam(Amotor, (int)dParam.FMax, tensor);
  396. //d.Matrix3 bodyrotation = d.BodyGetRotation(Body);
  397. //d.QfromR(
  398. //d.Matrix3 checkrotation = new d.Matrix3(0.7071068,0.5, -0.7071068,
  399. //
  400. //m_log.Info("[PHYSICSAV]: Rotation: " + bodyrotation.M00 + " : " + bodyrotation.M01 + " : " + bodyrotation.M02 + " : " + bodyrotation.M10 + " : " + bodyrotation.M11 + " : " + bodyrotation.M12 + " : " + bodyrotation.M20 + " : " + bodyrotation.M21 + " : " + bodyrotation.M22);
  401. //standupStraight();
  402. }
  403. //
  404. /// <summary>
  405. /// Uses the capped cyllinder volume formula to calculate the avatar's mass.
  406. /// This may be used in calculations in the scene/scenepresence
  407. /// </summary>
  408. public override float Mass
  409. {
  410. get
  411. {
  412. float AVvolume = (float) (Math.PI*Math.Pow(CAPSULE_RADIUS, 2)*CAPSULE_LENGTH);
  413. return m_density*AVvolume;
  414. }
  415. }
  416. private void standupStraight()
  417. {
  418. // The purpose of this routine here is to quickly stabilize the Body while it's popped up in the air.
  419. // The amotor needs a few seconds to stabilize so without it, the avatar shoots up sky high when you
  420. // change appearance and when you enter the simulator
  421. // After this routine is done, the amotor stabilizes much quicker
  422. d.Vector3 feet;
  423. d.Vector3 head;
  424. d.BodyGetRelPointPos(Body, 0.0f, 0.0f, -1.0f, out feet);
  425. d.BodyGetRelPointPos(Body, 0.0f, 0.0f, 1.0f, out head);
  426. float posture = head.Z - feet.Z;
  427. // restoring force proportional to lack of posture:
  428. float servo = (2.5f - posture) * POSTURE_SERVO;
  429. d.BodyAddForceAtRelPos(Body, 0.0f, 0.0f, servo, 0.0f, 0.0f, 1.0f);
  430. d.BodyAddForceAtRelPos(Body, 0.0f, 0.0f, -servo, 0.0f, 0.0f, -1.0f);
  431. //d.Matrix3 bodyrotation = d.BodyGetRotation(Body);
  432. //m_log.Info("[PHYSICSAV]: Rotation: " + bodyrotation.M00 + " : " + bodyrotation.M01 + " : " + bodyrotation.M02 + " : " + bodyrotation.M10 + " : " + bodyrotation.M11 + " : " + bodyrotation.M12 + " : " + bodyrotation.M20 + " : " + bodyrotation.M21 + " : " + bodyrotation.M22);
  433. }
  434. public override PhysicsVector Force
  435. {
  436. get { return new PhysicsVector(_target_velocity.X, _target_velocity.Y, _target_velocity.Z); }
  437. }
  438. public override PhysicsVector CenterOfMass
  439. {
  440. get { return PhysicsVector.Zero; }
  441. }
  442. public override PhysicsVector GeometricCenter
  443. {
  444. get { return PhysicsVector.Zero; }
  445. }
  446. public override PrimitiveBaseShape Shape
  447. {
  448. set { return; }
  449. }
  450. public override PhysicsVector Velocity
  451. {
  452. get {
  453. // There's a problem with PhysicsVector.Zero! Don't Use it Here!
  454. if (_zeroFlag)
  455. return new PhysicsVector(0f, 0f, 0f);
  456. m_lastUpdateSent = false;
  457. return _velocity;
  458. }
  459. set
  460. {
  461. m_pidControllerActive = true;
  462. _target_velocity = value;
  463. }
  464. }
  465. public override float CollisionScore
  466. {
  467. get { return 0f; }
  468. }
  469. public override bool Kinematic
  470. {
  471. get { return false; }
  472. set { }
  473. }
  474. public override Quaternion Orientation
  475. {
  476. get { return Quaternion.Identity; }
  477. set {
  478. //Matrix3 or = Orientation.ToRotationMatrix();
  479. //d.Matrix3 ord = new d.Matrix3(or.m00, or.m10, or.m20, or.m01, or.m11, or.m21, or.m02, or.m12, or.m22);
  480. //d.BodySetRotation(Body, ref ord);
  481. }
  482. }
  483. public override PhysicsVector Acceleration
  484. {
  485. get { return _acceleration; }
  486. }
  487. public void SetAcceleration(PhysicsVector accel)
  488. {
  489. m_pidControllerActive = true;
  490. _acceleration = accel;
  491. }
  492. /// <summary>
  493. /// Adds the force supplied to the Target Velocity
  494. /// The PID controller takes this target velocity and tries to make it a reality
  495. /// </summary>
  496. /// <param name="force"></param>
  497. public override void AddForce(PhysicsVector force)
  498. {
  499. m_pidControllerActive = true;
  500. _target_velocity.X += force.X;
  501. _target_velocity.Y += force.Y;
  502. _target_velocity.Z += force.Z;
  503. //m_lastUpdateSent = false;
  504. }
  505. /// <summary>
  506. /// After all of the forces add up with 'add force' we apply them with doForce
  507. /// </summary>
  508. /// <param name="force"></param>
  509. public void doForce(PhysicsVector force)
  510. {
  511. if (!collidelock)
  512. {
  513. d.BodyAddForce(Body, force.X, force.Y, force.Z);
  514. //d.BodySetRotation(Body, ref m_StandUpRotation);
  515. //standupStraight();
  516. }
  517. }
  518. public override void SetMomentum(PhysicsVector momentum)
  519. {
  520. }
  521. /// <summary>
  522. /// Called from Simulate
  523. /// This is the avatar's movement control + PID Controller
  524. /// </summary>
  525. /// <param name="timeStep"></param>
  526. public void Move(float timeStep)
  527. {
  528. // no lock; for now it's only called from within Simulate()
  529. // If the PID Controller isn't active then we set our force
  530. // calculating base velocity to the current position
  531. if (System.Environment.OSVersion.Platform == PlatformID.Unix)
  532. {
  533. PID_D = 3200.0f;
  534. PID_P = 1400.0f;
  535. }
  536. else
  537. {
  538. PID_D = 2200.0f;
  539. PID_P = 900.0f;
  540. }
  541. if (m_pidControllerActive == false)
  542. {
  543. _zeroPosition = d.BodyGetPosition(Body);
  544. }
  545. //PidStatus = true;
  546. PhysicsVector vec = new PhysicsVector();
  547. d.Vector3 vel = d.BodyGetLinearVel(Body);
  548. float movementdivisor = 1f;
  549. if (!m_alwaysRun)
  550. {
  551. movementdivisor = 1.3f;
  552. }
  553. else
  554. {
  555. movementdivisor = 0.8f;
  556. }
  557. // if velocity is zero, use position control; otherwise, velocity control
  558. if (_target_velocity.X == 0.0f && _target_velocity.Y == 0.0f && _target_velocity.Z == 0.0f && m_iscolliding)
  559. {
  560. // keep track of where we stopped. No more slippin' & slidin'
  561. if (!_zeroFlag)
  562. {
  563. _zeroFlag = true;
  564. _zeroPosition = d.BodyGetPosition(Body);
  565. }
  566. if (m_pidControllerActive)
  567. {
  568. // We only want to deactivate the PID Controller if we think we want to have our surrogate
  569. // react to the physics scene by moving it's position.
  570. // Avatar to Avatar collisions
  571. // Prim to avatar collisions
  572. d.Vector3 pos = d.BodyGetPosition(Body);
  573. vec.X = (_target_velocity.X - vel.X) * (PID_D) + (_zeroPosition.X - pos.X) * (PID_P * 2);
  574. vec.Y = (_target_velocity.Y - vel.Y)*(PID_D) + (_zeroPosition.Y - pos.Y)* (PID_P * 2);
  575. if (flying)
  576. {
  577. vec.Z = (_target_velocity.Z - vel.Z) * (PID_D) + (_zeroPosition.Z - pos.Z) * PID_P;
  578. }
  579. }
  580. //PidStatus = true;
  581. }
  582. else
  583. {
  584. m_pidControllerActive = true;
  585. _zeroFlag = false;
  586. if (m_iscolliding && !flying)
  587. {
  588. // We're flying and colliding with something
  589. vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D);
  590. vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D);
  591. }
  592. else if (m_iscolliding && flying)
  593. {
  594. // We're flying and colliding with something
  595. vec.X = ((_target_velocity.X/movementdivisor) - vel.X)*(PID_D / 16);
  596. vec.Y = ((_target_velocity.Y/movementdivisor) - vel.Y)*(PID_D / 16);
  597. }
  598. else if (!m_iscolliding && flying)
  599. {
  600. // We're flying and colliding with something
  601. vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D/6);
  602. vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D/6);
  603. }
  604. if (m_iscolliding && !flying && _target_velocity.Z > 0.0f)
  605. {
  606. // We're colliding with something and we're not flying but we're moving
  607. // This means we're walking or running.
  608. d.Vector3 pos = d.BodyGetPosition(Body);
  609. vec.Z = (_target_velocity.Z - vel.Z)*PID_D + (_zeroPosition.Z - pos.Z)*PID_P;
  610. if (_target_velocity.X > 0)
  611. {
  612. vec.X = ((_target_velocity.X - vel.X)/1.2f)*PID_D;
  613. }
  614. if (_target_velocity.Y > 0)
  615. {
  616. vec.Y = ((_target_velocity.Y - vel.Y)/1.2f)*PID_D;
  617. }
  618. }
  619. else if (!m_iscolliding && !flying)
  620. {
  621. // we're not colliding and we're not flying so that means we're falling!
  622. // m_iscolliding includes collisions with the ground.
  623. d.Vector3 pos = d.BodyGetPosition(Body);
  624. if (_target_velocity.X > 0)
  625. {
  626. vec.X = ((_target_velocity.X - vel.X)/1.2f)*PID_D;
  627. }
  628. if (_target_velocity.Y > 0)
  629. {
  630. vec.Y = ((_target_velocity.Y - vel.Y)/1.2f)*PID_D;
  631. }
  632. }
  633. if (flying)
  634. {
  635. vec.Z = (_target_velocity.Z - vel.Z) * (PID_D);
  636. }
  637. }
  638. if (flying)
  639. {
  640. vec.Z += (9.8f*m_mass);
  641. }
  642. doForce(vec);
  643. }
  644. /// <summary>
  645. /// Updates the reported position and velocity. This essentially sends the data up to ScenePresence.
  646. /// </summary>
  647. public void UpdatePositionAndVelocity()
  648. {
  649. // no lock; called from Simulate() -- if you call this from elsewhere, gotta lock or do Monitor.Enter/Exit!
  650. d.Vector3 vec = d.BodyGetPosition(Body);
  651. // kluge to keep things in bounds. ODE lets dead avatars drift away (they should be removed!)
  652. if (vec.X < 0.0f) vec.X = 0.0f;
  653. if (vec.Y < 0.0f) vec.Y = 0.0f;
  654. if (vec.X > 255.95f) vec.X = 255.95f;
  655. if (vec.Y > 255.95f) vec.Y = 255.95f;
  656. _position.X = vec.X;
  657. _position.Y = vec.Y;
  658. _position.Z = vec.Z;
  659. // Did we move last? = zeroflag
  660. // This helps keep us from sliding all over
  661. if (_zeroFlag)
  662. {
  663. _velocity.X = 0.0f;
  664. _velocity.Y = 0.0f;
  665. _velocity.Z = 0.0f;
  666. // Did we send out the 'stopped' message?
  667. if (!m_lastUpdateSent)
  668. {
  669. m_lastUpdateSent = true;
  670. //base.RequestPhysicsterseUpdate();
  671. }
  672. }
  673. else
  674. {
  675. m_lastUpdateSent = false;
  676. vec = d.BodyGetLinearVel(Body);
  677. _velocity.X = (vec.X);
  678. _velocity.Y = (vec.Y);
  679. _velocity.Z = (vec.Z);
  680. if (_velocity.Z < -6 && !m_hackSentFall)
  681. {
  682. // Collisionupdates will be used in the future, right now the're not being used.
  683. m_hackSentFall = true;
  684. //base.SendCollisionUpdate(new CollisionEventUpdate());
  685. m_pidControllerActive = false;
  686. }
  687. else if (flying && !m_hackSentFly)
  688. {
  689. //m_hackSentFly = true;
  690. //base.SendCollisionUpdate(new CollisionEventUpdate());
  691. }
  692. else
  693. {
  694. m_hackSentFly = false;
  695. m_hackSentFall = false;
  696. }
  697. }
  698. }
  699. /// <summary>
  700. /// Cleanup the things we use in the scene.
  701. /// </summary>
  702. public void Destroy()
  703. {
  704. lock (OdeScene.OdeLock)
  705. {
  706. // Kill the Amotor
  707. d.JointDestroy(Amotor);
  708. //kill the Geometry
  709. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  710. d.GeomDestroy(Shell);
  711. _parent_scene.geom_name_map.Remove(Shell);
  712. //kill the body
  713. d.BodyDestroy(Body);
  714. }
  715. }
  716. public override void CrossingFailure()
  717. {
  718. }
  719. }
  720. }