ODECharacter.cs 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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.Collections.Generic;
  29. using System.Reflection;
  30. using OpenMetaverse;
  31. using Ode.NET;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Physics.Manager;
  34. using log4net;
  35. namespace OpenSim.Region.Physics.OdePlugin
  36. {
  37. /// <summary>
  38. /// Various properties that ODE uses for AMotors but isn't exposed in ODE.NET so we must define them ourselves.
  39. /// </summary>
  40. public enum dParam : int
  41. {
  42. LowStop = 0,
  43. HiStop = 1,
  44. Vel = 2,
  45. FMax = 3,
  46. FudgeFactor = 4,
  47. Bounce = 5,
  48. CFM = 6,
  49. StopERP = 7,
  50. StopCFM = 8,
  51. LoStop2 = 256,
  52. HiStop2 = 257,
  53. Vel2 = 258,
  54. FMax2 = 259,
  55. StopERP2 = 7 + 256,
  56. StopCFM2 = 8 + 256,
  57. LoStop3 = 512,
  58. HiStop3 = 513,
  59. Vel3 = 514,
  60. FMax3 = 515,
  61. StopERP3 = 7 + 512,
  62. StopCFM3 = 8 + 512
  63. }
  64. public class OdeCharacter : PhysicsActor
  65. {
  66. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  67. private Vector3 _position;
  68. private d.Vector3 _zeroPosition;
  69. // private d.Matrix3 m_StandUpRotation;
  70. private bool _zeroFlag = false;
  71. private bool m_lastUpdateSent = false;
  72. private Vector3 _velocity;
  73. private Vector3 _target_velocity;
  74. private Vector3 _acceleration;
  75. private Vector3 m_rotationalVelocity;
  76. private float m_mass = 80f;
  77. public float m_density = 60f;
  78. private bool m_pidControllerActive = true;
  79. public float PID_D = 800.0f;
  80. public float PID_P = 900.0f;
  81. //private static float POSTURE_SERVO = 10000.0f;
  82. public float CAPSULE_RADIUS = 0.37f;
  83. public float CAPSULE_LENGTH = 2.140599f;
  84. public float m_tensor = 3800000f;
  85. public float heightFudgeFactor = 0.52f;
  86. public float walkDivisor = 1.3f;
  87. public float runDivisor = 0.8f;
  88. private bool flying = false;
  89. private bool m_iscolliding = false;
  90. private bool m_iscollidingGround = false;
  91. private bool m_wascolliding = false;
  92. private bool m_wascollidingGround = false;
  93. private bool m_iscollidingObj = false;
  94. private bool m_alwaysRun = false;
  95. private bool m_hackSentFall = false;
  96. private bool m_hackSentFly = false;
  97. private int m_requestedUpdateFrequency = 0;
  98. private Vector3 m_taintPosition = Vector3.Zero;
  99. public uint m_localID = 0;
  100. public bool m_returnCollisions = false;
  101. // taints and their non-tainted counterparts
  102. public bool m_isPhysical = false; // the current physical status
  103. public bool m_tainted_isPhysical = false; // set when the physical status is tainted (false=not existing in physics engine, true=existing)
  104. public float MinimumGroundFlightOffset = 3f;
  105. private float m_tainted_CAPSULE_LENGTH; // set when the capsule length changes.
  106. private float m_tiltMagnitudeWhenProjectedOnXYPlane = 0.1131371f; // used to introduce a fixed tilt because a straight-up capsule falls through terrain, probably a bug in terrain collider
  107. private float m_buoyancy = 0f;
  108. // private CollisionLocker ode;
  109. private string m_name = String.Empty;
  110. private bool[] m_colliderarr = new bool[11];
  111. private bool[] m_colliderGroundarr = new bool[11];
  112. // Default we're a Character
  113. private CollisionCategories m_collisionCategories = (CollisionCategories.Character);
  114. // Default, Collide with Other Geometries, spaces, bodies and characters.
  115. private CollisionCategories m_collisionFlags = (CollisionCategories.Geom
  116. | CollisionCategories.Space
  117. | CollisionCategories.Body
  118. | CollisionCategories.Character
  119. | CollisionCategories.Land);
  120. public IntPtr Body = IntPtr.Zero;
  121. private OdeScene _parent_scene;
  122. public IntPtr Shell = IntPtr.Zero;
  123. public IntPtr Amotor = IntPtr.Zero;
  124. public d.Mass ShellMass;
  125. public bool collidelock = false;
  126. public int m_eventsubscription = 0;
  127. private CollisionEventUpdate CollisionEventsThisFrame = new CollisionEventUpdate();
  128. // unique UUID of this character object
  129. public UUID m_uuid;
  130. public bool bad = false;
  131. public OdeCharacter(String avName, OdeScene parent_scene, Vector3 pos, CollisionLocker dode, Vector3 size, float pid_d, float pid_p, float capsule_radius, float tensor, float density, float height_fudge_factor, float walk_divisor, float rundivisor)
  132. {
  133. m_uuid = UUID.Random();
  134. if (pos.IsFinite())
  135. {
  136. if (pos.Z > 9999999f)
  137. {
  138. pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  139. }
  140. if (pos.Z < -90000f)
  141. {
  142. pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  143. }
  144. _position = pos;
  145. m_taintPosition.X = pos.X;
  146. m_taintPosition.Y = pos.Y;
  147. m_taintPosition.Z = pos.Z;
  148. }
  149. else
  150. {
  151. _position = new Vector3(((float)_parent_scene.WorldExtents.X * 0.5f), ((float)_parent_scene.WorldExtents.Y * 0.5f), parent_scene.GetTerrainHeightAtXY(128f, 128f) + 10f);
  152. m_taintPosition.X = _position.X;
  153. m_taintPosition.Y = _position.Y;
  154. m_taintPosition.Z = _position.Z;
  155. m_log.Warn("[PHYSICS]: Got NaN Position on Character Create");
  156. }
  157. _parent_scene = parent_scene;
  158. PID_D = pid_d;
  159. PID_P = pid_p;
  160. CAPSULE_RADIUS = capsule_radius;
  161. m_tensor = tensor;
  162. m_density = density;
  163. heightFudgeFactor = height_fudge_factor;
  164. walkDivisor = walk_divisor;
  165. runDivisor = rundivisor;
  166. // m_StandUpRotation =
  167. // new d.Matrix3(0.5f, 0.7071068f, 0.5f, -0.7071068f, 0f, 0.7071068f, 0.5f, -0.7071068f,
  168. // 0.5f);
  169. for (int i = 0; i < 11; i++)
  170. {
  171. m_colliderarr[i] = false;
  172. }
  173. CAPSULE_LENGTH = (size.Z * 1.15f) - CAPSULE_RADIUS * 2.0f;
  174. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  175. m_tainted_CAPSULE_LENGTH = CAPSULE_LENGTH;
  176. m_isPhysical = false; // current status: no ODE information exists
  177. m_tainted_isPhysical = true; // new tainted status: need to create ODE information
  178. _parent_scene.AddPhysicsActorTaint(this);
  179. m_name = avName;
  180. }
  181. public override int PhysicsActorType
  182. {
  183. get { return (int) ActorTypes.Agent; }
  184. set { return; }
  185. }
  186. /// <summary>
  187. /// If this is set, the avatar will move faster
  188. /// </summary>
  189. public override bool SetAlwaysRun
  190. {
  191. get { return m_alwaysRun; }
  192. set { m_alwaysRun = value; }
  193. }
  194. public override uint LocalID
  195. {
  196. set { m_localID = value; }
  197. }
  198. public override bool Grabbed
  199. {
  200. set { return; }
  201. }
  202. public override bool Selected
  203. {
  204. set { return; }
  205. }
  206. public override float Buoyancy
  207. {
  208. get { return m_buoyancy; }
  209. set { m_buoyancy = value; }
  210. }
  211. public override bool FloatOnWater
  212. {
  213. set { return; }
  214. }
  215. public override bool IsPhysical
  216. {
  217. get { return false; }
  218. set { return; }
  219. }
  220. public override bool ThrottleUpdates
  221. {
  222. get { return false; }
  223. set { return; }
  224. }
  225. public override bool Flying
  226. {
  227. get { return flying; }
  228. set { flying = value; }
  229. }
  230. /// <summary>
  231. /// Returns if the avatar is colliding in general.
  232. /// This includes the ground and objects and avatar.
  233. /// </summary>
  234. public override bool IsColliding
  235. {
  236. get { return m_iscolliding; }
  237. set
  238. {
  239. int i;
  240. int truecount = 0;
  241. int falsecount = 0;
  242. if (m_colliderarr.Length >= 10)
  243. {
  244. for (i = 0; i < 10; i++)
  245. {
  246. m_colliderarr[i] = m_colliderarr[i + 1];
  247. }
  248. }
  249. m_colliderarr[10] = value;
  250. for (i = 0; i < 11; i++)
  251. {
  252. if (m_colliderarr[i])
  253. {
  254. truecount++;
  255. }
  256. else
  257. {
  258. falsecount++;
  259. }
  260. }
  261. // Equal truecounts and false counts means we're colliding with something.
  262. if (falsecount > 1.2*truecount)
  263. {
  264. m_iscolliding = false;
  265. }
  266. else
  267. {
  268. m_iscolliding = true;
  269. }
  270. if (m_wascolliding != m_iscolliding)
  271. {
  272. //base.SendCollisionUpdate(new CollisionEventUpdate());
  273. }
  274. m_wascolliding = m_iscolliding;
  275. }
  276. }
  277. /// <summary>
  278. /// Returns if an avatar is colliding with the ground
  279. /// </summary>
  280. public override bool CollidingGround
  281. {
  282. get { return m_iscollidingGround; }
  283. set
  284. {
  285. // Collisions against the ground are not really reliable
  286. // So, to get a consistant value we have to average the current result over time
  287. // Currently we use 1 second = 10 calls to this.
  288. int i;
  289. int truecount = 0;
  290. int falsecount = 0;
  291. if (m_colliderGroundarr.Length >= 10)
  292. {
  293. for (i = 0; i < 10; i++)
  294. {
  295. m_colliderGroundarr[i] = m_colliderGroundarr[i + 1];
  296. }
  297. }
  298. m_colliderGroundarr[10] = value;
  299. for (i = 0; i < 11; i++)
  300. {
  301. if (m_colliderGroundarr[i])
  302. {
  303. truecount++;
  304. }
  305. else
  306. {
  307. falsecount++;
  308. }
  309. }
  310. // Equal truecounts and false counts means we're colliding with something.
  311. if (falsecount > 1.2*truecount)
  312. {
  313. m_iscollidingGround = false;
  314. }
  315. else
  316. {
  317. m_iscollidingGround = true;
  318. }
  319. if (m_wascollidingGround != m_iscollidingGround)
  320. {
  321. //base.SendCollisionUpdate(new CollisionEventUpdate());
  322. }
  323. m_wascollidingGround = m_iscollidingGround;
  324. }
  325. }
  326. /// <summary>
  327. /// Returns if the avatar is colliding with an object
  328. /// </summary>
  329. public override bool CollidingObj
  330. {
  331. get { return m_iscollidingObj; }
  332. set
  333. {
  334. m_iscollidingObj = value;
  335. if (value)
  336. m_pidControllerActive = false;
  337. else
  338. m_pidControllerActive = true;
  339. }
  340. }
  341. /// <summary>
  342. /// turn the PID controller on or off.
  343. /// The PID Controller will turn on all by itself in many situations
  344. /// </summary>
  345. /// <param name="status"></param>
  346. public void SetPidStatus(bool status)
  347. {
  348. m_pidControllerActive = status;
  349. }
  350. public override bool Stopped
  351. {
  352. get { return _zeroFlag; }
  353. }
  354. /// <summary>
  355. /// This 'puts' an avatar somewhere in the physics space.
  356. /// Not really a good choice unless you 'know' it's a good
  357. /// spot otherwise you're likely to orbit the avatar.
  358. /// </summary>
  359. public override Vector3 Position
  360. {
  361. get { return _position; }
  362. set
  363. {
  364. if (Body == IntPtr.Zero || Shell == IntPtr.Zero)
  365. {
  366. if (value.IsFinite())
  367. {
  368. if (value.Z > 9999999f)
  369. {
  370. value.Z = _parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  371. }
  372. if (value.Z < -90000f)
  373. {
  374. value.Z = _parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  375. }
  376. _position.X = value.X;
  377. _position.Y = value.Y;
  378. _position.Z = value.Z;
  379. m_taintPosition.X = value.X;
  380. m_taintPosition.Y = value.Y;
  381. m_taintPosition.Z = value.Z;
  382. _parent_scene.AddPhysicsActorTaint(this);
  383. }
  384. else
  385. {
  386. m_log.Warn("[PHYSICS]: Got a NaN Position from Scene on a Character");
  387. }
  388. }
  389. }
  390. }
  391. public override Vector3 RotationalVelocity
  392. {
  393. get { return m_rotationalVelocity; }
  394. set { m_rotationalVelocity = value; }
  395. }
  396. /// <summary>
  397. /// This property sets the height of the avatar only. We use the height to make sure the avatar stands up straight
  398. /// and use it to offset landings properly
  399. /// </summary>
  400. public override Vector3 Size
  401. {
  402. get { return new Vector3(CAPSULE_RADIUS * 2, CAPSULE_RADIUS * 2, CAPSULE_LENGTH); }
  403. set
  404. {
  405. if (value.IsFinite())
  406. {
  407. m_pidControllerActive = true;
  408. Vector3 SetSize = value;
  409. m_tainted_CAPSULE_LENGTH = (SetSize.Z*1.15f) - CAPSULE_RADIUS*2.0f;
  410. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  411. Velocity = Vector3.Zero;
  412. _parent_scene.AddPhysicsActorTaint(this);
  413. }
  414. else
  415. {
  416. m_log.Warn("[PHYSICS]: Got a NaN Size from Scene on a Character");
  417. }
  418. }
  419. }
  420. private void AlignAvatarTiltWithCurrentDirectionOfMovement(Vector3 movementVector)
  421. {
  422. movementVector.Z = 0f;
  423. float magnitude = (float)Math.Sqrt((double)(movementVector.X * movementVector.X + movementVector.Y * movementVector.Y));
  424. if (magnitude < 0.1f) return;
  425. // normalize the velocity vector
  426. float invMagnitude = 1.0f / magnitude;
  427. movementVector.X *= invMagnitude;
  428. movementVector.Y *= invMagnitude;
  429. // if we change the capsule heading too often, the capsule can fall down
  430. // therefore we snap movement vector to just 1 of 4 predefined directions (ne, nw, se, sw),
  431. // meaning only 4 possible capsule tilt orientations
  432. if (movementVector.X > 0)
  433. {
  434. // east
  435. if (movementVector.Y > 0)
  436. {
  437. // northeast
  438. movementVector.X = (float)Math.Sqrt(2.0);
  439. movementVector.Y = (float)Math.Sqrt(2.0);
  440. }
  441. else
  442. {
  443. // southeast
  444. movementVector.X = (float)Math.Sqrt(2.0);
  445. movementVector.Y = -(float)Math.Sqrt(2.0);
  446. }
  447. }
  448. else
  449. {
  450. // west
  451. if (movementVector.Y > 0)
  452. {
  453. // northwest
  454. movementVector.X = -(float)Math.Sqrt(2.0);
  455. movementVector.Y = (float)Math.Sqrt(2.0);
  456. }
  457. else
  458. {
  459. // southwest
  460. movementVector.X = -(float)Math.Sqrt(2.0);
  461. movementVector.Y = -(float)Math.Sqrt(2.0);
  462. }
  463. }
  464. // movementVector.Z is zero
  465. // calculate tilt components based on desired amount of tilt and current (snapped) heading.
  466. // the "-" sign is to force the tilt to be OPPOSITE the direction of movement.
  467. float xTiltComponent = -movementVector.X * m_tiltMagnitudeWhenProjectedOnXYPlane;
  468. float yTiltComponent = -movementVector.Y * m_tiltMagnitudeWhenProjectedOnXYPlane;
  469. //m_log.Debug("[PHYSICS] changing avatar tilt");
  470. d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, xTiltComponent);
  471. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, xTiltComponent); // must be same as lowstop, else a different, spurious tilt is introduced
  472. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, yTiltComponent);
  473. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, yTiltComponent); // same as lowstop
  474. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, 0f);
  475. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0f); // same as lowstop
  476. }
  477. /// <summary>
  478. /// This creates the Avatar's physical Surrogate at the position supplied
  479. /// </summary>
  480. /// <param name="npositionX"></param>
  481. /// <param name="npositionY"></param>
  482. /// <param name="npositionZ"></param>
  483. // WARNING: This MUST NOT be called outside of ProcessTaints, else we can have unsynchronized access
  484. // to ODE internals. ProcessTaints is called from within thread-locked Simulate(), so it is the only
  485. // place that is safe to call this routine AvatarGeomAndBodyCreation.
  486. private void AvatarGeomAndBodyCreation(float npositionX, float npositionY, float npositionZ, float tensor)
  487. {
  488. //CAPSULE_LENGTH = -5;
  489. //CAPSULE_RADIUS = -5;
  490. int dAMotorEuler = 1;
  491. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  492. if (CAPSULE_LENGTH <= 0)
  493. {
  494. m_log.Warn("[PHYSICS]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!");
  495. CAPSULE_LENGTH = 0.01f;
  496. }
  497. if (CAPSULE_RADIUS <= 0)
  498. {
  499. m_log.Warn("[PHYSICS]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!");
  500. CAPSULE_RADIUS = 0.01f;
  501. }
  502. Shell = d.CreateCapsule(_parent_scene.space, CAPSULE_RADIUS, CAPSULE_LENGTH);
  503. d.GeomSetCategoryBits(Shell, (int)m_collisionCategories);
  504. d.GeomSetCollideBits(Shell, (int)m_collisionFlags);
  505. d.MassSetCapsuleTotal(out ShellMass, m_mass, 2, CAPSULE_RADIUS, CAPSULE_LENGTH);
  506. Body = d.BodyCreate(_parent_scene.world);
  507. d.BodySetPosition(Body, npositionX, npositionY, npositionZ);
  508. _position.X = npositionX;
  509. _position.Y = npositionY;
  510. _position.Z = npositionZ;
  511. m_taintPosition.X = npositionX;
  512. m_taintPosition.Y = npositionY;
  513. m_taintPosition.Z = npositionZ;
  514. d.BodySetMass(Body, ref ShellMass);
  515. d.Matrix3 m_caprot;
  516. // 90 Stand up on the cap of the capped cyllinder
  517. if (_parent_scene.IsAvCapsuleTilted)
  518. {
  519. d.RFromAxisAndAngle(out m_caprot, 1, 0, 1, (float)(Math.PI / 2));
  520. }
  521. else
  522. {
  523. d.RFromAxisAndAngle(out m_caprot, 0, 0, 1, (float)(Math.PI / 2));
  524. }
  525. d.GeomSetRotation(Shell, ref m_caprot);
  526. d.BodySetRotation(Body, ref m_caprot);
  527. d.GeomSetBody(Shell, Body);
  528. // The purpose of the AMotor here is to keep the avatar's physical
  529. // surrogate from rotating while moving
  530. Amotor = d.JointCreateAMotor(_parent_scene.world, IntPtr.Zero);
  531. d.JointAttach(Amotor, Body, IntPtr.Zero);
  532. d.JointSetAMotorMode(Amotor, dAMotorEuler);
  533. d.JointSetAMotorNumAxes(Amotor, 3);
  534. d.JointSetAMotorAxis(Amotor, 0, 0, 1, 0, 0);
  535. d.JointSetAMotorAxis(Amotor, 1, 0, 0, 1, 0);
  536. d.JointSetAMotorAxis(Amotor, 2, 0, 0, 0, 1);
  537. d.JointSetAMotorAngle(Amotor, 0, 0);
  538. d.JointSetAMotorAngle(Amotor, 1, 0);
  539. d.JointSetAMotorAngle(Amotor, 2, 0);
  540. // These lowstops and high stops are effectively (no wiggle room)
  541. if (_parent_scene.IsAvCapsuleTilted)
  542. {
  543. d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, -0.000000000001f);
  544. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -0.000000000001f);
  545. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, -0.000000000001f);
  546. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 0.000000000001f);
  547. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0.000000000001f);
  548. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 0.000000000001f);
  549. }
  550. else
  551. {
  552. #region Documentation of capsule motor LowStop and HighStop parameters
  553. // Intentionally introduce some tilt into the capsule by setting
  554. // the motor stops to small epsilon values. This small tilt prevents
  555. // the capsule from falling into the terrain; a straight-up capsule
  556. // (with -0..0 motor stops) falls into the terrain for reasons yet
  557. // to be comprehended in their entirety.
  558. #endregion
  559. AlignAvatarTiltWithCurrentDirectionOfMovement(Vector3.Zero);
  560. d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, 0.08f);
  561. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -0f);
  562. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, 0.08f);
  563. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 0.08f); // must be same as lowstop, else a different, spurious tilt is introduced
  564. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0f); // same as lowstop
  565. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 0.08f); // same as lowstop
  566. }
  567. // Fudge factor is 1f by default, we're setting it to 0. We don't want it to Fudge or the
  568. // capped cyllinder will fall over
  569. d.JointSetAMotorParam(Amotor, (int)dParam.FudgeFactor, 0f);
  570. d.JointSetAMotorParam(Amotor, (int)dParam.FMax, tensor);
  571. //d.Matrix3 bodyrotation = d.BodyGetRotation(Body);
  572. //d.QfromR(
  573. //d.Matrix3 checkrotation = new d.Matrix3(0.7071068,0.5, -0.7071068,
  574. //
  575. //m_log.Info("[PHYSICSAV]: Rotation: " + bodyrotation.M00 + " : " + bodyrotation.M01 + " : " + bodyrotation.M02 + " : " + bodyrotation.M10 + " : " + bodyrotation.M11 + " : " + bodyrotation.M12 + " : " + bodyrotation.M20 + " : " + bodyrotation.M21 + " : " + bodyrotation.M22);
  576. //standupStraight();
  577. }
  578. //
  579. /// <summary>
  580. /// Uses the capped cyllinder volume formula to calculate the avatar's mass.
  581. /// This may be used in calculations in the scene/scenepresence
  582. /// </summary>
  583. public override float Mass
  584. {
  585. get
  586. {
  587. float AVvolume = (float) (Math.PI*Math.Pow(CAPSULE_RADIUS, 2)*CAPSULE_LENGTH);
  588. return m_density*AVvolume;
  589. }
  590. }
  591. public override void link(PhysicsActor obj)
  592. {
  593. }
  594. public override void delink()
  595. {
  596. }
  597. public override void LockAngularMotion(Vector3 axis)
  598. {
  599. }
  600. // This code is very useful. Written by DanX0r. We're just not using it right now.
  601. // Commented out to prevent a warning.
  602. //
  603. // private void standupStraight()
  604. // {
  605. // // The purpose of this routine here is to quickly stabilize the Body while it's popped up in the air.
  606. // // The amotor needs a few seconds to stabilize so without it, the avatar shoots up sky high when you
  607. // // change appearance and when you enter the simulator
  608. // // After this routine is done, the amotor stabilizes much quicker
  609. // d.Vector3 feet;
  610. // d.Vector3 head;
  611. // d.BodyGetRelPointPos(Body, 0.0f, 0.0f, -1.0f, out feet);
  612. // d.BodyGetRelPointPos(Body, 0.0f, 0.0f, 1.0f, out head);
  613. // float posture = head.Z - feet.Z;
  614. // // restoring force proportional to lack of posture:
  615. // float servo = (2.5f - posture) * POSTURE_SERVO;
  616. // d.BodyAddForceAtRelPos(Body, 0.0f, 0.0f, servo, 0.0f, 0.0f, 1.0f);
  617. // d.BodyAddForceAtRelPos(Body, 0.0f, 0.0f, -servo, 0.0f, 0.0f, -1.0f);
  618. // //d.Matrix3 bodyrotation = d.BodyGetRotation(Body);
  619. // //m_log.Info("[PHYSICSAV]: Rotation: " + bodyrotation.M00 + " : " + bodyrotation.M01 + " : " + bodyrotation.M02 + " : " + bodyrotation.M10 + " : " + bodyrotation.M11 + " : " + bodyrotation.M12 + " : " + bodyrotation.M20 + " : " + bodyrotation.M21 + " : " + bodyrotation.M22);
  620. // }
  621. public override Vector3 Force
  622. {
  623. get { return _target_velocity; }
  624. set { return; }
  625. }
  626. public override int VehicleType
  627. {
  628. get { return 0; }
  629. set { return; }
  630. }
  631. public override void VehicleFloatParam(int param, float value)
  632. {
  633. }
  634. public override void VehicleVectorParam(int param, Vector3 value)
  635. {
  636. }
  637. public override void VehicleRotationParam(int param, Quaternion rotation)
  638. {
  639. }
  640. public override void SetVolumeDetect(int param)
  641. {
  642. }
  643. public override Vector3 CenterOfMass
  644. {
  645. get { return Vector3.Zero; }
  646. }
  647. public override Vector3 GeometricCenter
  648. {
  649. get { return Vector3.Zero; }
  650. }
  651. public override PrimitiveBaseShape Shape
  652. {
  653. set { return; }
  654. }
  655. public override Vector3 Velocity
  656. {
  657. get {
  658. // There's a problem with Vector3.Zero! Don't Use it Here!
  659. if (_zeroFlag)
  660. return Vector3.Zero;
  661. m_lastUpdateSent = false;
  662. return _velocity;
  663. }
  664. set
  665. {
  666. if (value.IsFinite())
  667. {
  668. m_pidControllerActive = true;
  669. _target_velocity = value;
  670. }
  671. else
  672. {
  673. m_log.Warn("[PHYSICS]: Got a NaN velocity from Scene in a Character");
  674. }
  675. }
  676. }
  677. public override Vector3 Torque
  678. {
  679. get { return Vector3.Zero; }
  680. set { return; }
  681. }
  682. public override float CollisionScore
  683. {
  684. get { return 0f; }
  685. set { }
  686. }
  687. public override bool Kinematic
  688. {
  689. get { return false; }
  690. set { }
  691. }
  692. public override Quaternion Orientation
  693. {
  694. get { return Quaternion.Identity; }
  695. set {
  696. //Matrix3 or = Orientation.ToRotationMatrix();
  697. //d.Matrix3 ord = new d.Matrix3(or.m00, or.m10, or.m20, or.m01, or.m11, or.m21, or.m02, or.m12, or.m22);
  698. //d.BodySetRotation(Body, ref ord);
  699. }
  700. }
  701. public override Vector3 Acceleration
  702. {
  703. get { return _acceleration; }
  704. }
  705. public void SetAcceleration(Vector3 accel)
  706. {
  707. m_pidControllerActive = true;
  708. _acceleration = accel;
  709. }
  710. /// <summary>
  711. /// Adds the force supplied to the Target Velocity
  712. /// The PID controller takes this target velocity and tries to make it a reality
  713. /// </summary>
  714. /// <param name="force"></param>
  715. public override void AddForce(Vector3 force, bool pushforce)
  716. {
  717. if (force.IsFinite())
  718. {
  719. if (pushforce)
  720. {
  721. m_pidControllerActive = false;
  722. force *= 100f;
  723. doForce(force);
  724. // If uncommented, things get pushed off world
  725. //
  726. // m_log.Debug("Push!");
  727. // _target_velocity.X += force.X;
  728. // _target_velocity.Y += force.Y;
  729. // _target_velocity.Z += force.Z;
  730. }
  731. else
  732. {
  733. m_pidControllerActive = true;
  734. _target_velocity.X += force.X;
  735. _target_velocity.Y += force.Y;
  736. _target_velocity.Z += force.Z;
  737. }
  738. }
  739. else
  740. {
  741. m_log.Warn("[PHYSICS]: Got a NaN force applied to a Character");
  742. }
  743. //m_lastUpdateSent = false;
  744. }
  745. public override void AddAngularForce(Vector3 force, bool pushforce)
  746. {
  747. }
  748. /// <summary>
  749. /// After all of the forces add up with 'add force' we apply them with doForce
  750. /// </summary>
  751. /// <param name="force"></param>
  752. public void doForce(Vector3 force)
  753. {
  754. if (!collidelock)
  755. {
  756. d.BodyAddForce(Body, force.X, force.Y, force.Z);
  757. //d.BodySetRotation(Body, ref m_StandUpRotation);
  758. //standupStraight();
  759. }
  760. }
  761. public override void SetMomentum(Vector3 momentum)
  762. {
  763. }
  764. /// <summary>
  765. /// Called from Simulate
  766. /// This is the avatar's movement control + PID Controller
  767. /// </summary>
  768. /// <param name="timeStep"></param>
  769. public void Move(float timeStep, List<OdeCharacter> defects)
  770. {
  771. // no lock; for now it's only called from within Simulate()
  772. // If the PID Controller isn't active then we set our force
  773. // calculating base velocity to the current position
  774. if (Body == IntPtr.Zero)
  775. return;
  776. if (m_pidControllerActive == false)
  777. {
  778. _zeroPosition = d.BodyGetPosition(Body);
  779. }
  780. //PidStatus = true;
  781. d.Vector3 localpos = d.BodyGetPosition(Body);
  782. Vector3 localPos = new Vector3(localpos.X, localpos.Y, localpos.Z);
  783. if (!localPos.IsFinite())
  784. {
  785. m_log.Warn("[PHYSICS]: Avatar Position is non-finite!");
  786. defects.Add(this);
  787. // _parent_scene.RemoveCharacter(this);
  788. // destroy avatar capsule and related ODE data
  789. if (Amotor != IntPtr.Zero)
  790. {
  791. // Kill the Amotor
  792. d.JointDestroy(Amotor);
  793. Amotor = IntPtr.Zero;
  794. }
  795. //kill the Geometry
  796. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  797. if (Body != IntPtr.Zero)
  798. {
  799. //kill the body
  800. d.BodyDestroy(Body);
  801. Body = IntPtr.Zero;
  802. }
  803. if (Shell != IntPtr.Zero)
  804. {
  805. d.GeomDestroy(Shell);
  806. _parent_scene.geom_name_map.Remove(Shell);
  807. Shell = IntPtr.Zero;
  808. }
  809. return;
  810. }
  811. Vector3 vec = Vector3.Zero;
  812. d.Vector3 vel = d.BodyGetLinearVel(Body);
  813. float movementdivisor = 1f;
  814. if (!m_alwaysRun)
  815. {
  816. movementdivisor = walkDivisor;
  817. }
  818. else
  819. {
  820. movementdivisor = runDivisor;
  821. }
  822. // if velocity is zero, use position control; otherwise, velocity control
  823. if (_target_velocity.X == 0.0f && _target_velocity.Y == 0.0f && _target_velocity.Z == 0.0f && m_iscolliding)
  824. {
  825. // keep track of where we stopped. No more slippin' & slidin'
  826. if (!_zeroFlag)
  827. {
  828. _zeroFlag = true;
  829. _zeroPosition = d.BodyGetPosition(Body);
  830. }
  831. if (m_pidControllerActive)
  832. {
  833. // We only want to deactivate the PID Controller if we think we want to have our surrogate
  834. // react to the physics scene by moving it's position.
  835. // Avatar to Avatar collisions
  836. // Prim to avatar collisions
  837. d.Vector3 pos = d.BodyGetPosition(Body);
  838. vec.X = (_target_velocity.X - vel.X) * (PID_D) + (_zeroPosition.X - pos.X) * (PID_P * 2);
  839. vec.Y = (_target_velocity.Y - vel.Y)*(PID_D) + (_zeroPosition.Y - pos.Y)* (PID_P * 2);
  840. if (flying)
  841. {
  842. vec.Z = (_target_velocity.Z - vel.Z) * (PID_D) + (_zeroPosition.Z - pos.Z) * PID_P;
  843. }
  844. }
  845. //PidStatus = true;
  846. }
  847. else
  848. {
  849. m_pidControllerActive = true;
  850. _zeroFlag = false;
  851. if (m_iscolliding && !flying)
  852. {
  853. // We're standing on something
  854. vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D);
  855. vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D);
  856. }
  857. else if (m_iscolliding && flying)
  858. {
  859. // We're flying and colliding with something
  860. vec.X = ((_target_velocity.X/movementdivisor) - vel.X)*(PID_D / 16);
  861. vec.Y = ((_target_velocity.Y/movementdivisor) - vel.Y)*(PID_D / 16);
  862. }
  863. else if (!m_iscolliding && flying)
  864. {
  865. // we're in mid air suspended
  866. vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D/6);
  867. vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D/6);
  868. }
  869. if (m_iscolliding && !flying && _target_velocity.Z > 0.0f)
  870. {
  871. // We're colliding with something and we're not flying but we're moving
  872. // This means we're walking or running.
  873. d.Vector3 pos = d.BodyGetPosition(Body);
  874. vec.Z = (_target_velocity.Z - vel.Z)*PID_D + (_zeroPosition.Z - pos.Z)*PID_P;
  875. if (_target_velocity.X > 0)
  876. {
  877. vec.X = ((_target_velocity.X - vel.X)/1.2f)*PID_D;
  878. }
  879. if (_target_velocity.Y > 0)
  880. {
  881. vec.Y = ((_target_velocity.Y - vel.Y)/1.2f)*PID_D;
  882. }
  883. }
  884. else if (!m_iscolliding && !flying)
  885. {
  886. // we're not colliding and we're not flying so that means we're falling!
  887. // m_iscolliding includes collisions with the ground.
  888. // d.Vector3 pos = d.BodyGetPosition(Body);
  889. if (_target_velocity.X > 0)
  890. {
  891. vec.X = ((_target_velocity.X - vel.X)/1.2f)*PID_D;
  892. }
  893. if (_target_velocity.Y > 0)
  894. {
  895. vec.Y = ((_target_velocity.Y - vel.Y)/1.2f)*PID_D;
  896. }
  897. }
  898. if (flying)
  899. {
  900. vec.Z = (_target_velocity.Z - vel.Z) * (PID_D);
  901. }
  902. }
  903. if (flying)
  904. {
  905. vec.Z += ((-1 * _parent_scene.gravityz)*m_mass);
  906. //Added for auto fly height. Kitto Flora
  907. //d.Vector3 pos = d.BodyGetPosition(Body);
  908. float target_altitude = _parent_scene.GetTerrainHeightAtXY(_position.X, _position.Y) + MinimumGroundFlightOffset;
  909. if (_position.Z < target_altitude)
  910. {
  911. vec.Z += (target_altitude - _position.Z) * PID_P * 5.0f;
  912. }
  913. // end add Kitto Flora
  914. }
  915. if (vec.IsFinite())
  916. {
  917. doForce(vec);
  918. if (!_zeroFlag)
  919. {
  920. AlignAvatarTiltWithCurrentDirectionOfMovement(vec);
  921. }
  922. }
  923. else
  924. {
  925. m_log.Warn("[PHYSICS]: Got a NaN force vector in Move()");
  926. m_log.Warn("[PHYSICS]: Avatar Position is non-finite!");
  927. defects.Add(this);
  928. // _parent_scene.RemoveCharacter(this);
  929. // destroy avatar capsule and related ODE data
  930. if (Amotor != IntPtr.Zero)
  931. {
  932. // Kill the Amotor
  933. d.JointDestroy(Amotor);
  934. Amotor = IntPtr.Zero;
  935. }
  936. //kill the Geometry
  937. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  938. if (Body != IntPtr.Zero)
  939. {
  940. //kill the body
  941. d.BodyDestroy(Body);
  942. Body = IntPtr.Zero;
  943. }
  944. if (Shell != IntPtr.Zero)
  945. {
  946. d.GeomDestroy(Shell);
  947. _parent_scene.geom_name_map.Remove(Shell);
  948. Shell = IntPtr.Zero;
  949. }
  950. }
  951. }
  952. /// <summary>
  953. /// Updates the reported position and velocity. This essentially sends the data up to ScenePresence.
  954. /// </summary>
  955. public void UpdatePositionAndVelocity()
  956. {
  957. // no lock; called from Simulate() -- if you call this from elsewhere, gotta lock or do Monitor.Enter/Exit!
  958. d.Vector3 vec;
  959. try
  960. {
  961. vec = d.BodyGetPosition(Body);
  962. }
  963. catch (NullReferenceException)
  964. {
  965. bad = true;
  966. _parent_scene.BadCharacter(this);
  967. vec = new d.Vector3(_position.X, _position.Y, _position.Z);
  968. base.RaiseOutOfBounds(_position); // Tells ScenePresence that there's a problem!
  969. m_log.WarnFormat("[ODEPLUGIN]: Avatar Null reference for Avatar {0}, physical actor {1}", m_name, m_uuid);
  970. }
  971. // kluge to keep things in bounds. ODE lets dead avatars drift away (they should be removed!)
  972. if (vec.X < 0.0f) vec.X = 0.0f;
  973. if (vec.Y < 0.0f) vec.Y = 0.0f;
  974. if (vec.X > (int)_parent_scene.WorldExtents.X - 0.05f) vec.X = (int)_parent_scene.WorldExtents.X - 0.05f;
  975. if (vec.Y > (int)_parent_scene.WorldExtents.Y - 0.05f) vec.Y = (int)_parent_scene.WorldExtents.Y - 0.05f;
  976. _position.X = vec.X;
  977. _position.Y = vec.Y;
  978. _position.Z = vec.Z;
  979. // Did we move last? = zeroflag
  980. // This helps keep us from sliding all over
  981. if (_zeroFlag)
  982. {
  983. _velocity.X = 0.0f;
  984. _velocity.Y = 0.0f;
  985. _velocity.Z = 0.0f;
  986. // Did we send out the 'stopped' message?
  987. if (!m_lastUpdateSent)
  988. {
  989. m_lastUpdateSent = true;
  990. //base.RequestPhysicsterseUpdate();
  991. }
  992. }
  993. else
  994. {
  995. m_lastUpdateSent = false;
  996. try
  997. {
  998. vec = d.BodyGetLinearVel(Body);
  999. }
  1000. catch (NullReferenceException)
  1001. {
  1002. vec.X = _velocity.X;
  1003. vec.Y = _velocity.Y;
  1004. vec.Z = _velocity.Z;
  1005. }
  1006. _velocity.X = (vec.X);
  1007. _velocity.Y = (vec.Y);
  1008. _velocity.Z = (vec.Z);
  1009. if (_velocity.Z < -6 && !m_hackSentFall)
  1010. {
  1011. m_hackSentFall = true;
  1012. m_pidControllerActive = false;
  1013. }
  1014. else if (flying && !m_hackSentFly)
  1015. {
  1016. //m_hackSentFly = true;
  1017. //base.SendCollisionUpdate(new CollisionEventUpdate());
  1018. }
  1019. else
  1020. {
  1021. m_hackSentFly = false;
  1022. m_hackSentFall = false;
  1023. }
  1024. }
  1025. }
  1026. /// <summary>
  1027. /// Cleanup the things we use in the scene.
  1028. /// </summary>
  1029. public void Destroy()
  1030. {
  1031. m_tainted_isPhysical = false;
  1032. _parent_scene.AddPhysicsActorTaint(this);
  1033. }
  1034. public override void CrossingFailure()
  1035. {
  1036. }
  1037. public override Vector3 PIDTarget { set { return; } }
  1038. public override bool PIDActive { set { return; } }
  1039. public override float PIDTau { set { return; } }
  1040. public override float PIDHoverHeight { set { return; } }
  1041. public override bool PIDHoverActive { set { return; } }
  1042. public override PIDHoverType PIDHoverType { set { return; } }
  1043. public override float PIDHoverTau { set { return; } }
  1044. public override Quaternion APIDTarget{ set { return; } }
  1045. public override bool APIDActive{ set { return; } }
  1046. public override float APIDStrength{ set { return; } }
  1047. public override float APIDDamping{ set { return; } }
  1048. public override void SubscribeEvents(int ms)
  1049. {
  1050. m_requestedUpdateFrequency = ms;
  1051. m_eventsubscription = ms;
  1052. _parent_scene.addCollisionEventReporting(this);
  1053. }
  1054. public override void UnSubscribeEvents()
  1055. {
  1056. _parent_scene.remCollisionEventReporting(this);
  1057. m_requestedUpdateFrequency = 0;
  1058. m_eventsubscription = 0;
  1059. }
  1060. public void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
  1061. {
  1062. if (m_eventsubscription > 0)
  1063. {
  1064. CollisionEventsThisFrame.addCollider(CollidedWith, contact);
  1065. }
  1066. }
  1067. public void SendCollisions()
  1068. {
  1069. if (m_eventsubscription > m_requestedUpdateFrequency)
  1070. {
  1071. if (CollisionEventsThisFrame != null)
  1072. {
  1073. base.SendCollisionUpdate(CollisionEventsThisFrame);
  1074. }
  1075. CollisionEventsThisFrame = new CollisionEventUpdate();
  1076. m_eventsubscription = 0;
  1077. }
  1078. }
  1079. public override bool SubscribedEvents()
  1080. {
  1081. if (m_eventsubscription > 0)
  1082. return true;
  1083. return false;
  1084. }
  1085. public void ProcessTaints(float timestep)
  1086. {
  1087. if (m_tainted_isPhysical != m_isPhysical)
  1088. {
  1089. if (m_tainted_isPhysical)
  1090. {
  1091. // Create avatar capsule and related ODE data
  1092. if (!(Shell == IntPtr.Zero && Body == IntPtr.Zero && Amotor == IntPtr.Zero))
  1093. {
  1094. m_log.Warn("[PHYSICS]: re-creating the following avatar ODE data, even though it already exists - "
  1095. + (Shell!=IntPtr.Zero ? "Shell ":"")
  1096. + (Body!=IntPtr.Zero ? "Body ":"")
  1097. + (Amotor!=IntPtr.Zero ? "Amotor ":""));
  1098. }
  1099. AvatarGeomAndBodyCreation(_position.X, _position.Y, _position.Z, m_tensor);
  1100. _parent_scene.geom_name_map[Shell] = m_name;
  1101. _parent_scene.actor_name_map[Shell] = (PhysicsActor)this;
  1102. _parent_scene.AddCharacter(this);
  1103. }
  1104. else
  1105. {
  1106. _parent_scene.RemoveCharacter(this);
  1107. // destroy avatar capsule and related ODE data
  1108. if (Amotor != IntPtr.Zero)
  1109. {
  1110. // Kill the Amotor
  1111. d.JointDestroy(Amotor);
  1112. Amotor = IntPtr.Zero;
  1113. }
  1114. //kill the Geometry
  1115. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  1116. if (Body != IntPtr.Zero)
  1117. {
  1118. //kill the body
  1119. d.BodyDestroy(Body);
  1120. Body = IntPtr.Zero;
  1121. }
  1122. if (Shell != IntPtr.Zero)
  1123. {
  1124. d.GeomDestroy(Shell);
  1125. _parent_scene.geom_name_map.Remove(Shell);
  1126. Shell = IntPtr.Zero;
  1127. }
  1128. }
  1129. m_isPhysical = m_tainted_isPhysical;
  1130. }
  1131. if (m_tainted_CAPSULE_LENGTH != CAPSULE_LENGTH)
  1132. {
  1133. if (Shell != IntPtr.Zero && Body != IntPtr.Zero && Amotor != IntPtr.Zero)
  1134. {
  1135. m_pidControllerActive = true;
  1136. // no lock needed on _parent_scene.OdeLock because we are called from within the thread lock in OdePlugin's simulate()
  1137. d.JointDestroy(Amotor);
  1138. float prevCapsule = CAPSULE_LENGTH;
  1139. CAPSULE_LENGTH = m_tainted_CAPSULE_LENGTH;
  1140. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  1141. d.BodyDestroy(Body);
  1142. d.GeomDestroy(Shell);
  1143. AvatarGeomAndBodyCreation(_position.X, _position.Y,
  1144. _position.Z + (Math.Abs(CAPSULE_LENGTH - prevCapsule) * 2), m_tensor);
  1145. Velocity = Vector3.Zero;
  1146. _parent_scene.geom_name_map[Shell] = m_name;
  1147. _parent_scene.actor_name_map[Shell] = (PhysicsActor)this;
  1148. }
  1149. else
  1150. {
  1151. m_log.Warn("[PHYSICS]: trying to change capsule size, but the following ODE data is missing - "
  1152. + (Shell==IntPtr.Zero ? "Shell ":"")
  1153. + (Body==IntPtr.Zero ? "Body ":"")
  1154. + (Amotor==IntPtr.Zero ? "Amotor ":""));
  1155. }
  1156. }
  1157. if (!m_taintPosition.ApproxEquals(_position, 0.05f))
  1158. {
  1159. if (Body != IntPtr.Zero)
  1160. {
  1161. d.BodySetPosition(Body, m_taintPosition.X, m_taintPosition.Y, m_taintPosition.Z);
  1162. _position.X = m_taintPosition.X;
  1163. _position.Y = m_taintPosition.Y;
  1164. _position.Z = m_taintPosition.Z;
  1165. }
  1166. }
  1167. }
  1168. internal void AddCollisionFrameTime(int p)
  1169. {
  1170. // protect it from overflow crashing
  1171. if (m_eventsubscription + p >= int.MaxValue)
  1172. m_eventsubscription = 0;
  1173. m_eventsubscription += p;
  1174. }
  1175. }
  1176. }