ODECharacter.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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 VehicleFlags(int param, bool remove)
  641. {
  642. }
  643. public override void SetVolumeDetect(int param)
  644. {
  645. }
  646. public override Vector3 CenterOfMass
  647. {
  648. get { return Vector3.Zero; }
  649. }
  650. public override Vector3 GeometricCenter
  651. {
  652. get { return Vector3.Zero; }
  653. }
  654. public override PrimitiveBaseShape Shape
  655. {
  656. set { return; }
  657. }
  658. public override Vector3 Velocity
  659. {
  660. get {
  661. // There's a problem with Vector3.Zero! Don't Use it Here!
  662. if (_zeroFlag)
  663. return Vector3.Zero;
  664. m_lastUpdateSent = false;
  665. return _velocity;
  666. }
  667. set
  668. {
  669. if (value.IsFinite())
  670. {
  671. m_pidControllerActive = true;
  672. _target_velocity = value;
  673. }
  674. else
  675. {
  676. m_log.Warn("[PHYSICS]: Got a NaN velocity from Scene in a Character");
  677. }
  678. }
  679. }
  680. public override Vector3 Torque
  681. {
  682. get { return Vector3.Zero; }
  683. set { return; }
  684. }
  685. public override float CollisionScore
  686. {
  687. get { return 0f; }
  688. set { }
  689. }
  690. public override bool Kinematic
  691. {
  692. get { return false; }
  693. set { }
  694. }
  695. public override Quaternion Orientation
  696. {
  697. get { return Quaternion.Identity; }
  698. set {
  699. //Matrix3 or = Orientation.ToRotationMatrix();
  700. //d.Matrix3 ord = new d.Matrix3(or.m00, or.m10, or.m20, or.m01, or.m11, or.m21, or.m02, or.m12, or.m22);
  701. //d.BodySetRotation(Body, ref ord);
  702. }
  703. }
  704. public override Vector3 Acceleration
  705. {
  706. get { return _acceleration; }
  707. }
  708. public void SetAcceleration(Vector3 accel)
  709. {
  710. m_pidControllerActive = true;
  711. _acceleration = accel;
  712. }
  713. /// <summary>
  714. /// Adds the force supplied to the Target Velocity
  715. /// The PID controller takes this target velocity and tries to make it a reality
  716. /// </summary>
  717. /// <param name="force"></param>
  718. public override void AddForce(Vector3 force, bool pushforce)
  719. {
  720. if (force.IsFinite())
  721. {
  722. if (pushforce)
  723. {
  724. m_pidControllerActive = false;
  725. force *= 100f;
  726. doForce(force);
  727. // If uncommented, things get pushed off world
  728. //
  729. // m_log.Debug("Push!");
  730. // _target_velocity.X += force.X;
  731. // _target_velocity.Y += force.Y;
  732. // _target_velocity.Z += force.Z;
  733. }
  734. else
  735. {
  736. m_pidControllerActive = true;
  737. _target_velocity.X += force.X;
  738. _target_velocity.Y += force.Y;
  739. _target_velocity.Z += force.Z;
  740. }
  741. }
  742. else
  743. {
  744. m_log.Warn("[PHYSICS]: Got a NaN force applied to a Character");
  745. }
  746. //m_lastUpdateSent = false;
  747. }
  748. public override void AddAngularForce(Vector3 force, bool pushforce)
  749. {
  750. }
  751. /// <summary>
  752. /// After all of the forces add up with 'add force' we apply them with doForce
  753. /// </summary>
  754. /// <param name="force"></param>
  755. public void doForce(Vector3 force)
  756. {
  757. if (!collidelock)
  758. {
  759. d.BodyAddForce(Body, force.X, force.Y, force.Z);
  760. //d.BodySetRotation(Body, ref m_StandUpRotation);
  761. //standupStraight();
  762. }
  763. }
  764. public override void SetMomentum(Vector3 momentum)
  765. {
  766. }
  767. /// <summary>
  768. /// Called from Simulate
  769. /// This is the avatar's movement control + PID Controller
  770. /// </summary>
  771. /// <param name="timeStep"></param>
  772. public void Move(float timeStep, List<OdeCharacter> defects)
  773. {
  774. // no lock; for now it's only called from within Simulate()
  775. // If the PID Controller isn't active then we set our force
  776. // calculating base velocity to the current position
  777. if (Body == IntPtr.Zero)
  778. return;
  779. if (m_pidControllerActive == false)
  780. {
  781. _zeroPosition = d.BodyGetPosition(Body);
  782. }
  783. //PidStatus = true;
  784. d.Vector3 localpos = d.BodyGetPosition(Body);
  785. Vector3 localPos = new Vector3(localpos.X, localpos.Y, localpos.Z);
  786. if (!localPos.IsFinite())
  787. {
  788. m_log.Warn("[PHYSICS]: Avatar Position is non-finite!");
  789. defects.Add(this);
  790. // _parent_scene.RemoveCharacter(this);
  791. // destroy avatar capsule and related ODE data
  792. if (Amotor != IntPtr.Zero)
  793. {
  794. // Kill the Amotor
  795. d.JointDestroy(Amotor);
  796. Amotor = IntPtr.Zero;
  797. }
  798. //kill the Geometry
  799. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  800. if (Body != IntPtr.Zero)
  801. {
  802. //kill the body
  803. d.BodyDestroy(Body);
  804. Body = IntPtr.Zero;
  805. }
  806. if (Shell != IntPtr.Zero)
  807. {
  808. d.GeomDestroy(Shell);
  809. _parent_scene.geom_name_map.Remove(Shell);
  810. Shell = IntPtr.Zero;
  811. }
  812. return;
  813. }
  814. Vector3 vec = Vector3.Zero;
  815. d.Vector3 vel = d.BodyGetLinearVel(Body);
  816. float movementdivisor = 1f;
  817. if (!m_alwaysRun)
  818. {
  819. movementdivisor = walkDivisor;
  820. }
  821. else
  822. {
  823. movementdivisor = runDivisor;
  824. }
  825. // if velocity is zero, use position control; otherwise, velocity control
  826. if (_target_velocity.X == 0.0f && _target_velocity.Y == 0.0f && _target_velocity.Z == 0.0f && m_iscolliding)
  827. {
  828. // keep track of where we stopped. No more slippin' & slidin'
  829. if (!_zeroFlag)
  830. {
  831. _zeroFlag = true;
  832. _zeroPosition = d.BodyGetPosition(Body);
  833. }
  834. if (m_pidControllerActive)
  835. {
  836. // We only want to deactivate the PID Controller if we think we want to have our surrogate
  837. // react to the physics scene by moving it's position.
  838. // Avatar to Avatar collisions
  839. // Prim to avatar collisions
  840. d.Vector3 pos = d.BodyGetPosition(Body);
  841. vec.X = (_target_velocity.X - vel.X) * (PID_D) + (_zeroPosition.X - pos.X) * (PID_P * 2);
  842. vec.Y = (_target_velocity.Y - vel.Y)*(PID_D) + (_zeroPosition.Y - pos.Y)* (PID_P * 2);
  843. if (flying)
  844. {
  845. vec.Z = (_target_velocity.Z - vel.Z) * (PID_D) + (_zeroPosition.Z - pos.Z) * PID_P;
  846. }
  847. }
  848. //PidStatus = true;
  849. }
  850. else
  851. {
  852. m_pidControllerActive = true;
  853. _zeroFlag = false;
  854. if (m_iscolliding && !flying)
  855. {
  856. // We're standing on something
  857. vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D);
  858. vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D);
  859. }
  860. else if (m_iscolliding && flying)
  861. {
  862. // We're flying and colliding with something
  863. vec.X = ((_target_velocity.X/movementdivisor) - vel.X)*(PID_D / 16);
  864. vec.Y = ((_target_velocity.Y/movementdivisor) - vel.Y)*(PID_D / 16);
  865. }
  866. else if (!m_iscolliding && flying)
  867. {
  868. // we're in mid air suspended
  869. vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D/6);
  870. vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D/6);
  871. }
  872. if (m_iscolliding && !flying && _target_velocity.Z > 0.0f)
  873. {
  874. // We're colliding with something and we're not flying but we're moving
  875. // This means we're walking or running.
  876. d.Vector3 pos = d.BodyGetPosition(Body);
  877. vec.Z = (_target_velocity.Z - vel.Z)*PID_D + (_zeroPosition.Z - pos.Z)*PID_P;
  878. if (_target_velocity.X > 0)
  879. {
  880. vec.X = ((_target_velocity.X - vel.X)/1.2f)*PID_D;
  881. }
  882. if (_target_velocity.Y > 0)
  883. {
  884. vec.Y = ((_target_velocity.Y - vel.Y)/1.2f)*PID_D;
  885. }
  886. }
  887. else if (!m_iscolliding && !flying)
  888. {
  889. // we're not colliding and we're not flying so that means we're falling!
  890. // m_iscolliding includes collisions with the ground.
  891. // d.Vector3 pos = d.BodyGetPosition(Body);
  892. if (_target_velocity.X > 0)
  893. {
  894. vec.X = ((_target_velocity.X - vel.X)/1.2f)*PID_D;
  895. }
  896. if (_target_velocity.Y > 0)
  897. {
  898. vec.Y = ((_target_velocity.Y - vel.Y)/1.2f)*PID_D;
  899. }
  900. }
  901. if (flying)
  902. {
  903. vec.Z = (_target_velocity.Z - vel.Z) * (PID_D);
  904. }
  905. }
  906. if (flying)
  907. {
  908. vec.Z += ((-1 * _parent_scene.gravityz)*m_mass);
  909. //Added for auto fly height. Kitto Flora
  910. //d.Vector3 pos = d.BodyGetPosition(Body);
  911. float target_altitude = _parent_scene.GetTerrainHeightAtXY(_position.X, _position.Y) + MinimumGroundFlightOffset;
  912. if (_position.Z < target_altitude)
  913. {
  914. vec.Z += (target_altitude - _position.Z) * PID_P * 5.0f;
  915. }
  916. // end add Kitto Flora
  917. }
  918. if (vec.IsFinite())
  919. {
  920. doForce(vec);
  921. if (!_zeroFlag)
  922. {
  923. AlignAvatarTiltWithCurrentDirectionOfMovement(vec);
  924. }
  925. }
  926. else
  927. {
  928. m_log.Warn("[PHYSICS]: Got a NaN force vector in Move()");
  929. m_log.Warn("[PHYSICS]: Avatar Position is non-finite!");
  930. defects.Add(this);
  931. // _parent_scene.RemoveCharacter(this);
  932. // destroy avatar capsule and related ODE data
  933. if (Amotor != IntPtr.Zero)
  934. {
  935. // Kill the Amotor
  936. d.JointDestroy(Amotor);
  937. Amotor = IntPtr.Zero;
  938. }
  939. //kill the Geometry
  940. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  941. if (Body != IntPtr.Zero)
  942. {
  943. //kill the body
  944. d.BodyDestroy(Body);
  945. Body = IntPtr.Zero;
  946. }
  947. if (Shell != IntPtr.Zero)
  948. {
  949. d.GeomDestroy(Shell);
  950. _parent_scene.geom_name_map.Remove(Shell);
  951. Shell = IntPtr.Zero;
  952. }
  953. }
  954. }
  955. /// <summary>
  956. /// Updates the reported position and velocity. This essentially sends the data up to ScenePresence.
  957. /// </summary>
  958. public void UpdatePositionAndVelocity()
  959. {
  960. // no lock; called from Simulate() -- if you call this from elsewhere, gotta lock or do Monitor.Enter/Exit!
  961. d.Vector3 vec;
  962. try
  963. {
  964. vec = d.BodyGetPosition(Body);
  965. }
  966. catch (NullReferenceException)
  967. {
  968. bad = true;
  969. _parent_scene.BadCharacter(this);
  970. vec = new d.Vector3(_position.X, _position.Y, _position.Z);
  971. base.RaiseOutOfBounds(_position); // Tells ScenePresence that there's a problem!
  972. m_log.WarnFormat("[ODEPLUGIN]: Avatar Null reference for Avatar {0}, physical actor {1}", m_name, m_uuid);
  973. }
  974. // kluge to keep things in bounds. ODE lets dead avatars drift away (they should be removed!)
  975. if (vec.X < 0.0f) vec.X = 0.0f;
  976. if (vec.Y < 0.0f) vec.Y = 0.0f;
  977. if (vec.X > (int)_parent_scene.WorldExtents.X - 0.05f) vec.X = (int)_parent_scene.WorldExtents.X - 0.05f;
  978. if (vec.Y > (int)_parent_scene.WorldExtents.Y - 0.05f) vec.Y = (int)_parent_scene.WorldExtents.Y - 0.05f;
  979. _position.X = vec.X;
  980. _position.Y = vec.Y;
  981. _position.Z = vec.Z;
  982. // I think we need to update the taintPosition too -- Diva 12/24/10
  983. m_taintPosition.X = vec.X;
  984. m_taintPosition.Y = vec.Y;
  985. m_taintPosition.Z = vec.Z;
  986. // Did we move last? = zeroflag
  987. // This helps keep us from sliding all over
  988. if (_zeroFlag)
  989. {
  990. _velocity.X = 0.0f;
  991. _velocity.Y = 0.0f;
  992. _velocity.Z = 0.0f;
  993. // Did we send out the 'stopped' message?
  994. if (!m_lastUpdateSent)
  995. {
  996. m_lastUpdateSent = true;
  997. //base.RequestPhysicsterseUpdate();
  998. }
  999. }
  1000. else
  1001. {
  1002. m_lastUpdateSent = false;
  1003. try
  1004. {
  1005. vec = d.BodyGetLinearVel(Body);
  1006. }
  1007. catch (NullReferenceException)
  1008. {
  1009. vec.X = _velocity.X;
  1010. vec.Y = _velocity.Y;
  1011. vec.Z = _velocity.Z;
  1012. }
  1013. _velocity.X = (vec.X);
  1014. _velocity.Y = (vec.Y);
  1015. _velocity.Z = (vec.Z);
  1016. if (_velocity.Z < -6 && !m_hackSentFall)
  1017. {
  1018. m_hackSentFall = true;
  1019. m_pidControllerActive = false;
  1020. }
  1021. else if (flying && !m_hackSentFly)
  1022. {
  1023. //m_hackSentFly = true;
  1024. //base.SendCollisionUpdate(new CollisionEventUpdate());
  1025. }
  1026. else
  1027. {
  1028. m_hackSentFly = false;
  1029. m_hackSentFall = false;
  1030. }
  1031. }
  1032. }
  1033. /// <summary>
  1034. /// Cleanup the things we use in the scene.
  1035. /// </summary>
  1036. public void Destroy()
  1037. {
  1038. m_tainted_isPhysical = false;
  1039. _parent_scene.AddPhysicsActorTaint(this);
  1040. }
  1041. public override void CrossingFailure()
  1042. {
  1043. }
  1044. public override Vector3 PIDTarget { set { return; } }
  1045. public override bool PIDActive { set { return; } }
  1046. public override float PIDTau { set { return; } }
  1047. public override float PIDHoverHeight { set { return; } }
  1048. public override bool PIDHoverActive { set { return; } }
  1049. public override PIDHoverType PIDHoverType { set { return; } }
  1050. public override float PIDHoverTau { set { return; } }
  1051. public override Quaternion APIDTarget{ set { return; } }
  1052. public override bool APIDActive{ set { return; } }
  1053. public override float APIDStrength{ set { return; } }
  1054. public override float APIDDamping{ set { return; } }
  1055. public override void SubscribeEvents(int ms)
  1056. {
  1057. m_requestedUpdateFrequency = ms;
  1058. m_eventsubscription = ms;
  1059. _parent_scene.addCollisionEventReporting(this);
  1060. }
  1061. public override void UnSubscribeEvents()
  1062. {
  1063. _parent_scene.remCollisionEventReporting(this);
  1064. m_requestedUpdateFrequency = 0;
  1065. m_eventsubscription = 0;
  1066. }
  1067. public void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
  1068. {
  1069. if (m_eventsubscription > 0)
  1070. {
  1071. CollisionEventsThisFrame.addCollider(CollidedWith, contact);
  1072. }
  1073. }
  1074. public void SendCollisions()
  1075. {
  1076. if (m_eventsubscription > m_requestedUpdateFrequency)
  1077. {
  1078. if (CollisionEventsThisFrame != null)
  1079. {
  1080. base.SendCollisionUpdate(CollisionEventsThisFrame);
  1081. }
  1082. CollisionEventsThisFrame = new CollisionEventUpdate();
  1083. m_eventsubscription = 0;
  1084. }
  1085. }
  1086. public override bool SubscribedEvents()
  1087. {
  1088. if (m_eventsubscription > 0)
  1089. return true;
  1090. return false;
  1091. }
  1092. public void ProcessTaints(float timestep)
  1093. {
  1094. if (m_tainted_isPhysical != m_isPhysical)
  1095. {
  1096. if (m_tainted_isPhysical)
  1097. {
  1098. // Create avatar capsule and related ODE data
  1099. if (!(Shell == IntPtr.Zero && Body == IntPtr.Zero && Amotor == IntPtr.Zero))
  1100. {
  1101. m_log.Warn("[PHYSICS]: re-creating the following avatar ODE data, even though it already exists - "
  1102. + (Shell!=IntPtr.Zero ? "Shell ":"")
  1103. + (Body!=IntPtr.Zero ? "Body ":"")
  1104. + (Amotor!=IntPtr.Zero ? "Amotor ":""));
  1105. }
  1106. AvatarGeomAndBodyCreation(_position.X, _position.Y, _position.Z, m_tensor);
  1107. _parent_scene.geom_name_map[Shell] = m_name;
  1108. _parent_scene.actor_name_map[Shell] = (PhysicsActor)this;
  1109. _parent_scene.AddCharacter(this);
  1110. }
  1111. else
  1112. {
  1113. _parent_scene.RemoveCharacter(this);
  1114. // destroy avatar capsule and related ODE data
  1115. if (Amotor != IntPtr.Zero)
  1116. {
  1117. // Kill the Amotor
  1118. d.JointDestroy(Amotor);
  1119. Amotor = IntPtr.Zero;
  1120. }
  1121. //kill the Geometry
  1122. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  1123. if (Body != IntPtr.Zero)
  1124. {
  1125. //kill the body
  1126. d.BodyDestroy(Body);
  1127. Body = IntPtr.Zero;
  1128. }
  1129. if (Shell != IntPtr.Zero)
  1130. {
  1131. d.GeomDestroy(Shell);
  1132. _parent_scene.geom_name_map.Remove(Shell);
  1133. Shell = IntPtr.Zero;
  1134. }
  1135. }
  1136. m_isPhysical = m_tainted_isPhysical;
  1137. }
  1138. if (m_tainted_CAPSULE_LENGTH != CAPSULE_LENGTH)
  1139. {
  1140. if (Shell != IntPtr.Zero && Body != IntPtr.Zero && Amotor != IntPtr.Zero)
  1141. {
  1142. m_pidControllerActive = true;
  1143. // no lock needed on _parent_scene.OdeLock because we are called from within the thread lock in OdePlugin's simulate()
  1144. d.JointDestroy(Amotor);
  1145. float prevCapsule = CAPSULE_LENGTH;
  1146. CAPSULE_LENGTH = m_tainted_CAPSULE_LENGTH;
  1147. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  1148. d.BodyDestroy(Body);
  1149. d.GeomDestroy(Shell);
  1150. AvatarGeomAndBodyCreation(_position.X, _position.Y,
  1151. _position.Z + (Math.Abs(CAPSULE_LENGTH - prevCapsule) * 2), m_tensor);
  1152. Velocity = Vector3.Zero;
  1153. _parent_scene.geom_name_map[Shell] = m_name;
  1154. _parent_scene.actor_name_map[Shell] = (PhysicsActor)this;
  1155. }
  1156. else
  1157. {
  1158. m_log.Warn("[PHYSICS]: trying to change capsule size, but the following ODE data is missing - "
  1159. + (Shell==IntPtr.Zero ? "Shell ":"")
  1160. + (Body==IntPtr.Zero ? "Body ":"")
  1161. + (Amotor==IntPtr.Zero ? "Amotor ":""));
  1162. }
  1163. }
  1164. if (!m_taintPosition.ApproxEquals(_position, 0.05f))
  1165. {
  1166. if (Body != IntPtr.Zero)
  1167. {
  1168. d.BodySetPosition(Body, m_taintPosition.X, m_taintPosition.Y, m_taintPosition.Z);
  1169. _position.X = m_taintPosition.X;
  1170. _position.Y = m_taintPosition.Y;
  1171. _position.Z = m_taintPosition.Z;
  1172. }
  1173. }
  1174. }
  1175. internal void AddCollisionFrameTime(int p)
  1176. {
  1177. // protect it from overflow crashing
  1178. if (m_eventsubscription + p >= int.MaxValue)
  1179. m_eventsubscription = 0;
  1180. m_eventsubscription += p;
  1181. }
  1182. }
  1183. }