ODECharacter.cs 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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. // Did we move last? = zeroflag
  983. // This helps keep us from sliding all over
  984. if (_zeroFlag)
  985. {
  986. _velocity.X = 0.0f;
  987. _velocity.Y = 0.0f;
  988. _velocity.Z = 0.0f;
  989. // Did we send out the 'stopped' message?
  990. if (!m_lastUpdateSent)
  991. {
  992. m_lastUpdateSent = true;
  993. //base.RequestPhysicsterseUpdate();
  994. }
  995. }
  996. else
  997. {
  998. m_lastUpdateSent = false;
  999. try
  1000. {
  1001. vec = d.BodyGetLinearVel(Body);
  1002. }
  1003. catch (NullReferenceException)
  1004. {
  1005. vec.X = _velocity.X;
  1006. vec.Y = _velocity.Y;
  1007. vec.Z = _velocity.Z;
  1008. }
  1009. _velocity.X = (vec.X);
  1010. _velocity.Y = (vec.Y);
  1011. _velocity.Z = (vec.Z);
  1012. if (_velocity.Z < -6 && !m_hackSentFall)
  1013. {
  1014. m_hackSentFall = true;
  1015. m_pidControllerActive = false;
  1016. }
  1017. else if (flying && !m_hackSentFly)
  1018. {
  1019. //m_hackSentFly = true;
  1020. //base.SendCollisionUpdate(new CollisionEventUpdate());
  1021. }
  1022. else
  1023. {
  1024. m_hackSentFly = false;
  1025. m_hackSentFall = false;
  1026. }
  1027. }
  1028. }
  1029. /// <summary>
  1030. /// Cleanup the things we use in the scene.
  1031. /// </summary>
  1032. public void Destroy()
  1033. {
  1034. m_tainted_isPhysical = false;
  1035. _parent_scene.AddPhysicsActorTaint(this);
  1036. }
  1037. public override void CrossingFailure()
  1038. {
  1039. }
  1040. public override Vector3 PIDTarget { set { return; } }
  1041. public override bool PIDActive { set { return; } }
  1042. public override float PIDTau { set { return; } }
  1043. public override float PIDHoverHeight { set { return; } }
  1044. public override bool PIDHoverActive { set { return; } }
  1045. public override PIDHoverType PIDHoverType { set { return; } }
  1046. public override float PIDHoverTau { set { return; } }
  1047. public override Quaternion APIDTarget{ set { return; } }
  1048. public override bool APIDActive{ set { return; } }
  1049. public override float APIDStrength{ set { return; } }
  1050. public override float APIDDamping{ set { return; } }
  1051. public override void SubscribeEvents(int ms)
  1052. {
  1053. m_requestedUpdateFrequency = ms;
  1054. m_eventsubscription = ms;
  1055. _parent_scene.addCollisionEventReporting(this);
  1056. }
  1057. public override void UnSubscribeEvents()
  1058. {
  1059. _parent_scene.remCollisionEventReporting(this);
  1060. m_requestedUpdateFrequency = 0;
  1061. m_eventsubscription = 0;
  1062. }
  1063. public void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
  1064. {
  1065. if (m_eventsubscription > 0)
  1066. {
  1067. CollisionEventsThisFrame.addCollider(CollidedWith, contact);
  1068. }
  1069. }
  1070. public void SendCollisions()
  1071. {
  1072. if (m_eventsubscription > m_requestedUpdateFrequency)
  1073. {
  1074. if (CollisionEventsThisFrame != null)
  1075. {
  1076. base.SendCollisionUpdate(CollisionEventsThisFrame);
  1077. }
  1078. CollisionEventsThisFrame = new CollisionEventUpdate();
  1079. m_eventsubscription = 0;
  1080. }
  1081. }
  1082. public override bool SubscribedEvents()
  1083. {
  1084. if (m_eventsubscription > 0)
  1085. return true;
  1086. return false;
  1087. }
  1088. public void ProcessTaints(float timestep)
  1089. {
  1090. if (m_tainted_isPhysical != m_isPhysical)
  1091. {
  1092. if (m_tainted_isPhysical)
  1093. {
  1094. // Create avatar capsule and related ODE data
  1095. if (!(Shell == IntPtr.Zero && Body == IntPtr.Zero && Amotor == IntPtr.Zero))
  1096. {
  1097. m_log.Warn("[PHYSICS]: re-creating the following avatar ODE data, even though it already exists - "
  1098. + (Shell!=IntPtr.Zero ? "Shell ":"")
  1099. + (Body!=IntPtr.Zero ? "Body ":"")
  1100. + (Amotor!=IntPtr.Zero ? "Amotor ":""));
  1101. }
  1102. AvatarGeomAndBodyCreation(_position.X, _position.Y, _position.Z, m_tensor);
  1103. _parent_scene.geom_name_map[Shell] = m_name;
  1104. _parent_scene.actor_name_map[Shell] = (PhysicsActor)this;
  1105. _parent_scene.AddCharacter(this);
  1106. }
  1107. else
  1108. {
  1109. _parent_scene.RemoveCharacter(this);
  1110. // destroy avatar capsule and related ODE data
  1111. if (Amotor != IntPtr.Zero)
  1112. {
  1113. // Kill the Amotor
  1114. d.JointDestroy(Amotor);
  1115. Amotor = IntPtr.Zero;
  1116. }
  1117. //kill the Geometry
  1118. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  1119. if (Body != IntPtr.Zero)
  1120. {
  1121. //kill the body
  1122. d.BodyDestroy(Body);
  1123. Body = IntPtr.Zero;
  1124. }
  1125. if (Shell != IntPtr.Zero)
  1126. {
  1127. d.GeomDestroy(Shell);
  1128. _parent_scene.geom_name_map.Remove(Shell);
  1129. Shell = IntPtr.Zero;
  1130. }
  1131. }
  1132. m_isPhysical = m_tainted_isPhysical;
  1133. }
  1134. if (m_tainted_CAPSULE_LENGTH != CAPSULE_LENGTH)
  1135. {
  1136. if (Shell != IntPtr.Zero && Body != IntPtr.Zero && Amotor != IntPtr.Zero)
  1137. {
  1138. m_pidControllerActive = true;
  1139. // no lock needed on _parent_scene.OdeLock because we are called from within the thread lock in OdePlugin's simulate()
  1140. d.JointDestroy(Amotor);
  1141. float prevCapsule = CAPSULE_LENGTH;
  1142. CAPSULE_LENGTH = m_tainted_CAPSULE_LENGTH;
  1143. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  1144. d.BodyDestroy(Body);
  1145. d.GeomDestroy(Shell);
  1146. AvatarGeomAndBodyCreation(_position.X, _position.Y,
  1147. _position.Z + (Math.Abs(CAPSULE_LENGTH - prevCapsule) * 2), m_tensor);
  1148. Velocity = Vector3.Zero;
  1149. _parent_scene.geom_name_map[Shell] = m_name;
  1150. _parent_scene.actor_name_map[Shell] = (PhysicsActor)this;
  1151. }
  1152. else
  1153. {
  1154. m_log.Warn("[PHYSICS]: trying to change capsule size, but the following ODE data is missing - "
  1155. + (Shell==IntPtr.Zero ? "Shell ":"")
  1156. + (Body==IntPtr.Zero ? "Body ":"")
  1157. + (Amotor==IntPtr.Zero ? "Amotor ":""));
  1158. }
  1159. }
  1160. if (!m_taintPosition.ApproxEquals(_position, 0.05f))
  1161. {
  1162. if (Body != IntPtr.Zero)
  1163. {
  1164. d.BodySetPosition(Body, m_taintPosition.X, m_taintPosition.Y, m_taintPosition.Z);
  1165. _position.X = m_taintPosition.X;
  1166. _position.Y = m_taintPosition.Y;
  1167. _position.Z = m_taintPosition.Z;
  1168. }
  1169. }
  1170. }
  1171. internal void AddCollisionFrameTime(int p)
  1172. {
  1173. // protect it from overflow crashing
  1174. if (m_eventsubscription + p >= int.MaxValue)
  1175. m_eventsubscription = 0;
  1176. m_eventsubscription += p;
  1177. }
  1178. }
  1179. }