1
0

ODECharacter.cs 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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 PhysicsVector _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 PhysicsVector _velocity;
  73. private PhysicsVector _target_velocity;
  74. private PhysicsVector _acceleration;
  75. private PhysicsVector 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 PhysicsVector m_taintPosition = new PhysicsVector(0, 0, 0);
  98. public uint m_localID = 0;
  99. public bool m_returnCollisions = false;
  100. // taints and their non-tainted counterparts
  101. public bool m_isPhysical = false; // the current physical status
  102. public bool m_tainted_isPhysical = false; // set when the physical status is tainted (false=not existing in physics engine, true=existing)
  103. public float MinimumGroundFlightOffset = 3f;
  104. private float m_tainted_CAPSULE_LENGTH; // set when the capsule length changes.
  105. 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
  106. private float m_buoyancy = 0f;
  107. // private CollisionLocker ode;
  108. private string m_name = String.Empty;
  109. private bool[] m_colliderarr = new bool[11];
  110. private bool[] m_colliderGroundarr = new bool[11];
  111. // Default we're a Character
  112. private CollisionCategories m_collisionCategories = (CollisionCategories.Character);
  113. // Default, Collide with Other Geometries, spaces, bodies and characters.
  114. private CollisionCategories m_collisionFlags = (CollisionCategories.Geom
  115. | CollisionCategories.Space
  116. | CollisionCategories.Body
  117. | CollisionCategories.Character
  118. | CollisionCategories.Land);
  119. public IntPtr Body = IntPtr.Zero;
  120. private OdeScene _parent_scene;
  121. public IntPtr Shell = IntPtr.Zero;
  122. public IntPtr Amotor = IntPtr.Zero;
  123. public d.Mass ShellMass;
  124. public bool collidelock = false;
  125. public int m_eventsubscription = 0;
  126. private CollisionEventUpdate CollisionEventsThisFrame = new CollisionEventUpdate();
  127. public OdeCharacter(String avName, OdeScene parent_scene, PhysicsVector pos, CollisionLocker dode, PhysicsVector size, float pid_d, float pid_p, float capsule_radius, float tensor, float density, float height_fudge_factor, float walk_divisor, float rundivisor)
  128. {
  129. // ode = dode;
  130. _velocity = new PhysicsVector();
  131. _target_velocity = new PhysicsVector();
  132. if (PhysicsVector.isFinite(pos))
  133. {
  134. if (pos.Z > 9999999)
  135. {
  136. pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  137. }
  138. if (pos.Z < -90000)
  139. {
  140. pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  141. }
  142. _position = pos;
  143. m_taintPosition.X = pos.X;
  144. m_taintPosition.Y = pos.Y;
  145. m_taintPosition.Z = pos.Z;
  146. }
  147. else
  148. {
  149. _position = new PhysicsVector(((int)_parent_scene.WorldExtents.X * 0.5f), ((int)_parent_scene.WorldExtents.Y * 0.5f), parent_scene.GetTerrainHeightAtXY(128, 128) + 10);
  150. m_taintPosition.X = _position.X;
  151. m_taintPosition.Y = _position.Y;
  152. m_taintPosition.Z = _position.Z;
  153. m_log.Warn("[PHYSICS]: Got NaN Position on Character Create");
  154. }
  155. _acceleration = new PhysicsVector();
  156. _parent_scene = parent_scene;
  157. PID_D = pid_d;
  158. PID_P = pid_p;
  159. CAPSULE_RADIUS = capsule_radius;
  160. m_tensor = tensor;
  161. m_density = density;
  162. heightFudgeFactor = height_fudge_factor;
  163. walkDivisor = walk_divisor;
  164. runDivisor = rundivisor;
  165. // m_StandUpRotation =
  166. // new d.Matrix3(0.5f, 0.7071068f, 0.5f, -0.7071068f, 0f, 0.7071068f, 0.5f, -0.7071068f,
  167. // 0.5f);
  168. for (int i = 0; i < 11; i++)
  169. {
  170. m_colliderarr[i] = false;
  171. }
  172. CAPSULE_LENGTH = (size.Z * 1.15f) - CAPSULE_RADIUS * 2.0f;
  173. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  174. m_tainted_CAPSULE_LENGTH = CAPSULE_LENGTH;
  175. m_isPhysical = false; // current status: no ODE information exists
  176. m_tainted_isPhysical = true; // new tainted status: need to create ODE information
  177. _parent_scene.AddPhysicsActorTaint(this);
  178. m_name = avName;
  179. }
  180. public override int PhysicsActorType
  181. {
  182. get { return (int) ActorTypes.Agent; }
  183. set { return; }
  184. }
  185. /// <summary>
  186. /// If this is set, the avatar will move faster
  187. /// </summary>
  188. public override bool SetAlwaysRun
  189. {
  190. get { return m_alwaysRun; }
  191. set { m_alwaysRun = value; }
  192. }
  193. public override uint LocalID
  194. {
  195. set { m_localID = value; }
  196. }
  197. public override int GetHashCode()
  198. {
  199. return (int)m_localID;
  200. }
  201. public override bool Grabbed
  202. {
  203. set { return; }
  204. }
  205. public override bool Selected
  206. {
  207. set { return; }
  208. }
  209. public override float Buoyancy
  210. {
  211. get { return m_buoyancy; }
  212. set { m_buoyancy = value; }
  213. }
  214. public override bool FloatOnWater
  215. {
  216. set { return; }
  217. }
  218. public override bool IsPhysical
  219. {
  220. get { return false; }
  221. set { return; }
  222. }
  223. public override bool ThrottleUpdates
  224. {
  225. get { return false; }
  226. set { return; }
  227. }
  228. public override bool Flying
  229. {
  230. get { return flying; }
  231. set { flying = value; }
  232. }
  233. /// <summary>
  234. /// Returns if the avatar is colliding in general.
  235. /// This includes the ground and objects and avatar.
  236. /// </summary>
  237. public override bool IsColliding
  238. {
  239. get { return m_iscolliding; }
  240. set
  241. {
  242. int i;
  243. int truecount = 0;
  244. int falsecount = 0;
  245. if (m_colliderarr.Length >= 10)
  246. {
  247. for (i = 0; i < 10; i++)
  248. {
  249. m_colliderarr[i] = m_colliderarr[i + 1];
  250. }
  251. }
  252. m_colliderarr[10] = value;
  253. for (i = 0; i < 11; i++)
  254. {
  255. if (m_colliderarr[i])
  256. {
  257. truecount++;
  258. }
  259. else
  260. {
  261. falsecount++;
  262. }
  263. }
  264. // Equal truecounts and false counts means we're colliding with something.
  265. if (falsecount > 1.2*truecount)
  266. {
  267. m_iscolliding = false;
  268. }
  269. else
  270. {
  271. m_iscolliding = true;
  272. }
  273. if (m_wascolliding != m_iscolliding)
  274. {
  275. //base.SendCollisionUpdate(new CollisionEventUpdate());
  276. }
  277. m_wascolliding = m_iscolliding;
  278. }
  279. }
  280. /// <summary>
  281. /// Returns if an avatar is colliding with the ground
  282. /// </summary>
  283. public override bool CollidingGround
  284. {
  285. get { return m_iscollidingGround; }
  286. set
  287. {
  288. // Collisions against the ground are not really reliable
  289. // So, to get a consistant value we have to average the current result over time
  290. // Currently we use 1 second = 10 calls to this.
  291. int i;
  292. int truecount = 0;
  293. int falsecount = 0;
  294. if (m_colliderGroundarr.Length >= 10)
  295. {
  296. for (i = 0; i < 10; i++)
  297. {
  298. m_colliderGroundarr[i] = m_colliderGroundarr[i + 1];
  299. }
  300. }
  301. m_colliderGroundarr[10] = value;
  302. for (i = 0; i < 11; i++)
  303. {
  304. if (m_colliderGroundarr[i])
  305. {
  306. truecount++;
  307. }
  308. else
  309. {
  310. falsecount++;
  311. }
  312. }
  313. // Equal truecounts and false counts means we're colliding with something.
  314. if (falsecount > 1.2*truecount)
  315. {
  316. m_iscollidingGround = false;
  317. }
  318. else
  319. {
  320. m_iscollidingGround = true;
  321. }
  322. if (m_wascollidingGround != m_iscollidingGround)
  323. {
  324. //base.SendCollisionUpdate(new CollisionEventUpdate());
  325. }
  326. m_wascollidingGround = m_iscollidingGround;
  327. }
  328. }
  329. /// <summary>
  330. /// Returns if the avatar is colliding with an object
  331. /// </summary>
  332. public override bool CollidingObj
  333. {
  334. get { return m_iscollidingObj; }
  335. set
  336. {
  337. m_iscollidingObj = value;
  338. if (value)
  339. m_pidControllerActive = false;
  340. else
  341. m_pidControllerActive = true;
  342. }
  343. }
  344. /// <summary>
  345. /// turn the PID controller on or off.
  346. /// The PID Controller will turn on all by itself in many situations
  347. /// </summary>
  348. /// <param name="status"></param>
  349. public void SetPidStatus(bool status)
  350. {
  351. m_pidControllerActive = status;
  352. }
  353. public override bool Stopped
  354. {
  355. get { return _zeroFlag; }
  356. }
  357. /// <summary>
  358. /// This 'puts' an avatar somewhere in the physics space.
  359. /// Not really a good choice unless you 'know' it's a good
  360. /// spot otherwise you're likely to orbit the avatar.
  361. /// </summary>
  362. public override PhysicsVector Position
  363. {
  364. get { return _position; }
  365. set
  366. {
  367. if (Body == IntPtr.Zero || Shell == IntPtr.Zero)
  368. {
  369. if (PhysicsVector.isFinite(value))
  370. {
  371. if (value.Z > 9999999)
  372. {
  373. value.Z = _parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  374. }
  375. if (value.Z < -90000)
  376. {
  377. value.Z = _parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  378. }
  379. _position.X = value.X;
  380. _position.Y = value.Y;
  381. _position.Z = value.Z;
  382. m_taintPosition.X = value.X;
  383. m_taintPosition.Y = value.Y;
  384. m_taintPosition.Z = value.Z;
  385. _parent_scene.AddPhysicsActorTaint(this);
  386. }
  387. else
  388. {
  389. m_log.Warn("[PHYSICS]: Got a NaN Position from Scene on a Character");
  390. }
  391. }
  392. }
  393. }
  394. public override PhysicsVector RotationalVelocity
  395. {
  396. get { return m_rotationalVelocity; }
  397. set { m_rotationalVelocity = value; }
  398. }
  399. /// <summary>
  400. /// This property sets the height of the avatar only. We use the height to make sure the avatar stands up straight
  401. /// and use it to offset landings properly
  402. /// </summary>
  403. public override PhysicsVector Size
  404. {
  405. get { return new PhysicsVector(CAPSULE_RADIUS*2, CAPSULE_RADIUS*2, CAPSULE_LENGTH); }
  406. set
  407. {
  408. if (PhysicsVector.isFinite(value))
  409. {
  410. m_pidControllerActive = true;
  411. PhysicsVector SetSize = value;
  412. m_tainted_CAPSULE_LENGTH = (SetSize.Z*1.15f) - CAPSULE_RADIUS*2.0f;
  413. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  414. Velocity = new PhysicsVector(0f, 0f, 0f);
  415. _parent_scene.AddPhysicsActorTaint(this);
  416. }
  417. else
  418. {
  419. m_log.Warn("[PHYSICS]: Got a NaN Size from Scene on a Character");
  420. }
  421. }
  422. }
  423. private void AlignAvatarTiltWithCurrentDirectionOfMovement(PhysicsVector movementVector)
  424. {
  425. movementVector.Z = 0f;
  426. float magnitude = (float)Math.Sqrt((double)(movementVector.X * movementVector.X + movementVector.Y * movementVector.Y));
  427. if (magnitude < 0.1f) return;
  428. // normalize the velocity vector
  429. float invMagnitude = 1.0f / magnitude;
  430. movementVector.X *= invMagnitude;
  431. movementVector.Y *= invMagnitude;
  432. // if we change the capsule heading too often, the capsule can fall down
  433. // therefore we snap movement vector to just 1 of 4 predefined directions (ne, nw, se, sw),
  434. // meaning only 4 possible capsule tilt orientations
  435. if (movementVector.X > 0)
  436. {
  437. // east
  438. if (movementVector.Y > 0)
  439. {
  440. // northeast
  441. movementVector.X = (float)Math.Sqrt(2.0);
  442. movementVector.Y = (float)Math.Sqrt(2.0);
  443. }
  444. else
  445. {
  446. // southeast
  447. movementVector.X = (float)Math.Sqrt(2.0);
  448. movementVector.Y = -(float)Math.Sqrt(2.0);
  449. }
  450. }
  451. else
  452. {
  453. // west
  454. if (movementVector.Y > 0)
  455. {
  456. // northwest
  457. movementVector.X = -(float)Math.Sqrt(2.0);
  458. movementVector.Y = (float)Math.Sqrt(2.0);
  459. }
  460. else
  461. {
  462. // southwest
  463. movementVector.X = -(float)Math.Sqrt(2.0);
  464. movementVector.Y = -(float)Math.Sqrt(2.0);
  465. }
  466. }
  467. // movementVector.Z is zero
  468. // calculate tilt components based on desired amount of tilt and current (snapped) heading.
  469. // the "-" sign is to force the tilt to be OPPOSITE the direction of movement.
  470. float xTiltComponent = -movementVector.X * m_tiltMagnitudeWhenProjectedOnXYPlane;
  471. float yTiltComponent = -movementVector.Y * m_tiltMagnitudeWhenProjectedOnXYPlane;
  472. //m_log.Debug("[PHYSICS] changing avatar tilt");
  473. d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, xTiltComponent);
  474. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, xTiltComponent); // must be same as lowstop, else a different, spurious tilt is introduced
  475. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, yTiltComponent);
  476. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, yTiltComponent); // same as lowstop
  477. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, 0f);
  478. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0f); // same as lowstop
  479. }
  480. /// <summary>
  481. /// This creates the Avatar's physical Surrogate at the position supplied
  482. /// </summary>
  483. /// <param name="npositionX"></param>
  484. /// <param name="npositionY"></param>
  485. /// <param name="npositionZ"></param>
  486. // WARNING: This MUST NOT be called outside of ProcessTaints, else we can have unsynchronized access
  487. // to ODE internals. ProcessTaints is called from within thread-locked Simulate(), so it is the only
  488. // place that is safe to call this routine AvatarGeomAndBodyCreation.
  489. private void AvatarGeomAndBodyCreation(float npositionX, float npositionY, float npositionZ, float tensor)
  490. {
  491. //CAPSULE_LENGTH = -5;
  492. //CAPSULE_RADIUS = -5;
  493. int dAMotorEuler = 1;
  494. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  495. if (CAPSULE_LENGTH <= 0)
  496. {
  497. m_log.Warn("[PHYSICS]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!");
  498. CAPSULE_LENGTH = 0.01f;
  499. }
  500. if (CAPSULE_RADIUS <= 0)
  501. {
  502. m_log.Warn("[PHYSICS]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!");
  503. CAPSULE_RADIUS = 0.01f;
  504. }
  505. Shell = d.CreateCapsule(_parent_scene.space, CAPSULE_RADIUS, CAPSULE_LENGTH);
  506. d.GeomSetCategoryBits(Shell, (int)m_collisionCategories);
  507. d.GeomSetCollideBits(Shell, (int)m_collisionFlags);
  508. d.MassSetCapsuleTotal(out ShellMass, m_mass, 2, CAPSULE_RADIUS, CAPSULE_LENGTH);
  509. Body = d.BodyCreate(_parent_scene.world);
  510. d.BodySetPosition(Body, npositionX, npositionY, npositionZ);
  511. _position.X = npositionX;
  512. _position.Y = npositionY;
  513. _position.Z = npositionZ;
  514. m_taintPosition.X = npositionX;
  515. m_taintPosition.Y = npositionY;
  516. m_taintPosition.Z = npositionZ;
  517. d.BodySetMass(Body, ref ShellMass);
  518. d.Matrix3 m_caprot;
  519. // 90 Stand up on the cap of the capped cyllinder
  520. if (_parent_scene.IsAvCapsuleTilted)
  521. {
  522. d.RFromAxisAndAngle(out m_caprot, 1, 0, 1, (float)(Math.PI / 2));
  523. }
  524. else
  525. {
  526. d.RFromAxisAndAngle(out m_caprot, 0, 0, 1, (float)(Math.PI / 2));
  527. }
  528. d.GeomSetRotation(Shell, ref m_caprot);
  529. d.BodySetRotation(Body, ref m_caprot);
  530. d.GeomSetBody(Shell, Body);
  531. // The purpose of the AMotor here is to keep the avatar's physical
  532. // surrogate from rotating while moving
  533. Amotor = d.JointCreateAMotor(_parent_scene.world, IntPtr.Zero);
  534. d.JointAttach(Amotor, Body, IntPtr.Zero);
  535. d.JointSetAMotorMode(Amotor, dAMotorEuler);
  536. d.JointSetAMotorNumAxes(Amotor, 3);
  537. d.JointSetAMotorAxis(Amotor, 0, 0, 1, 0, 0);
  538. d.JointSetAMotorAxis(Amotor, 1, 0, 0, 1, 0);
  539. d.JointSetAMotorAxis(Amotor, 2, 0, 0, 0, 1);
  540. d.JointSetAMotorAngle(Amotor, 0, 0);
  541. d.JointSetAMotorAngle(Amotor, 1, 0);
  542. d.JointSetAMotorAngle(Amotor, 2, 0);
  543. // These lowstops and high stops are effectively (no wiggle room)
  544. if (_parent_scene.IsAvCapsuleTilted)
  545. {
  546. d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, -0.000000000001f);
  547. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -0.000000000001f);
  548. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, -0.000000000001f);
  549. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 0.000000000001f);
  550. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0.000000000001f);
  551. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 0.000000000001f);
  552. }
  553. else
  554. {
  555. #region Documentation of capsule motor LowStop and HighStop parameters
  556. // Intentionally introduce some tilt into the capsule by setting
  557. // the motor stops to small epsilon values. This small tilt prevents
  558. // the capsule from falling into the terrain; a straight-up capsule
  559. // (with -0..0 motor stops) falls into the terrain for reasons yet
  560. // to be comprehended in their entirety.
  561. #endregion
  562. AlignAvatarTiltWithCurrentDirectionOfMovement(new PhysicsVector(0,0,0));
  563. d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, 0.08f);
  564. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -0f);
  565. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, 0.08f);
  566. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 0.08f); // must be same as lowstop, else a different, spurious tilt is introduced
  567. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0f); // same as lowstop
  568. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 0.08f); // same as lowstop
  569. }
  570. // Fudge factor is 1f by default, we're setting it to 0. We don't want it to Fudge or the
  571. // capped cyllinder will fall over
  572. d.JointSetAMotorParam(Amotor, (int)dParam.FudgeFactor, 0f);
  573. d.JointSetAMotorParam(Amotor, (int)dParam.FMax, tensor);
  574. //d.Matrix3 bodyrotation = d.BodyGetRotation(Body);
  575. //d.QfromR(
  576. //d.Matrix3 checkrotation = new d.Matrix3(0.7071068,0.5, -0.7071068,
  577. //
  578. //m_log.Info("[PHYSICSAV]: Rotation: " + bodyrotation.M00 + " : " + bodyrotation.M01 + " : " + bodyrotation.M02 + " : " + bodyrotation.M10 + " : " + bodyrotation.M11 + " : " + bodyrotation.M12 + " : " + bodyrotation.M20 + " : " + bodyrotation.M21 + " : " + bodyrotation.M22);
  579. //standupStraight();
  580. }
  581. //
  582. /// <summary>
  583. /// Uses the capped cyllinder volume formula to calculate the avatar's mass.
  584. /// This may be used in calculations in the scene/scenepresence
  585. /// </summary>
  586. public override float Mass
  587. {
  588. get
  589. {
  590. float AVvolume = (float) (Math.PI*Math.Pow(CAPSULE_RADIUS, 2)*CAPSULE_LENGTH);
  591. return m_density*AVvolume;
  592. }
  593. }
  594. public override void link(PhysicsActor obj)
  595. {
  596. }
  597. public override void delink()
  598. {
  599. }
  600. public override void LockAngularMotion(PhysicsVector axis)
  601. {
  602. }
  603. // This code is very useful. Written by DanX0r. We're just not using it right now.
  604. // Commented out to prevent a warning.
  605. //
  606. // private void standupStraight()
  607. // {
  608. // // The purpose of this routine here is to quickly stabilize the Body while it's popped up in the air.
  609. // // The amotor needs a few seconds to stabilize so without it, the avatar shoots up sky high when you
  610. // // change appearance and when you enter the simulator
  611. // // After this routine is done, the amotor stabilizes much quicker
  612. // d.Vector3 feet;
  613. // d.Vector3 head;
  614. // d.BodyGetRelPointPos(Body, 0.0f, 0.0f, -1.0f, out feet);
  615. // d.BodyGetRelPointPos(Body, 0.0f, 0.0f, 1.0f, out head);
  616. // float posture = head.Z - feet.Z;
  617. // // restoring force proportional to lack of posture:
  618. // float servo = (2.5f - posture) * POSTURE_SERVO;
  619. // d.BodyAddForceAtRelPos(Body, 0.0f, 0.0f, servo, 0.0f, 0.0f, 1.0f);
  620. // d.BodyAddForceAtRelPos(Body, 0.0f, 0.0f, -servo, 0.0f, 0.0f, -1.0f);
  621. // //d.Matrix3 bodyrotation = d.BodyGetRotation(Body);
  622. // //m_log.Info("[PHYSICSAV]: Rotation: " + bodyrotation.M00 + " : " + bodyrotation.M01 + " : " + bodyrotation.M02 + " : " + bodyrotation.M10 + " : " + bodyrotation.M11 + " : " + bodyrotation.M12 + " : " + bodyrotation.M20 + " : " + bodyrotation.M21 + " : " + bodyrotation.M22);
  623. // }
  624. public override PhysicsVector Force
  625. {
  626. get { return new PhysicsVector(_target_velocity.X, _target_velocity.Y, _target_velocity.Z); }
  627. set { return; }
  628. }
  629. public override int VehicleType
  630. {
  631. get { return 0; }
  632. set { return; }
  633. }
  634. public override void VehicleFloatParam(int param, float value)
  635. {
  636. }
  637. public override void VehicleVectorParam(int param, PhysicsVector value)
  638. {
  639. }
  640. public override void VehicleRotationParam(int param, Quaternion rotation)
  641. {
  642. }
  643. public override void SetVolumeDetect(int param)
  644. {
  645. }
  646. public override PhysicsVector CenterOfMass
  647. {
  648. get { return PhysicsVector.Zero; }
  649. }
  650. public override PhysicsVector GeometricCenter
  651. {
  652. get { return PhysicsVector.Zero; }
  653. }
  654. public override PrimitiveBaseShape Shape
  655. {
  656. set { return; }
  657. }
  658. public override PhysicsVector Velocity
  659. {
  660. get {
  661. // There's a problem with PhysicsVector.Zero! Don't Use it Here!
  662. if (_zeroFlag)
  663. return new PhysicsVector(0f, 0f, 0f);
  664. m_lastUpdateSent = false;
  665. return _velocity;
  666. }
  667. set
  668. {
  669. if (PhysicsVector.isFinite(value))
  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 PhysicsVector Torque
  681. {
  682. get { return PhysicsVector.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 PhysicsVector Acceleration
  705. {
  706. get { return _acceleration; }
  707. }
  708. public void SetAcceleration(PhysicsVector 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(PhysicsVector force, bool pushforce)
  719. {
  720. if (PhysicsVector.isFinite(force))
  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(PhysicsVector 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(PhysicsVector 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(PhysicsVector 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. PhysicsVector localPos = new PhysicsVector(localpos.X, localpos.Y, localpos.Z);
  786. if (!PhysicsVector.isFinite(localPos))
  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. PhysicsVector vec = new PhysicsVector();
  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 (PhysicsVector.isFinite(vec))
  919. {
  920. doForce(vec);
  921. if (!_zeroFlag)
  922. {
  923. AlignAvatarTiltWithCurrentDirectionOfMovement(new PhysicsVector(vec.X, vec.Y, vec.Z));
  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 = d.BodyGetPosition(Body);
  962. // kluge to keep things in bounds. ODE lets dead avatars drift away (they should be removed!)
  963. if (vec.X < 0.0f) vec.X = 0.0f;
  964. if (vec.Y < 0.0f) vec.Y = 0.0f;
  965. if (vec.X > (int)_parent_scene.WorldExtents.X - 0.05f) vec.X = (int)_parent_scene.WorldExtents.X - 0.05f;
  966. if (vec.Y > (int)_parent_scene.WorldExtents.Y - 0.05f) vec.Y = (int)_parent_scene.WorldExtents.Y - 0.05f;
  967. _position.X = vec.X;
  968. _position.Y = vec.Y;
  969. _position.Z = vec.Z;
  970. // Did we move last? = zeroflag
  971. // This helps keep us from sliding all over
  972. if (_zeroFlag)
  973. {
  974. _velocity.X = 0.0f;
  975. _velocity.Y = 0.0f;
  976. _velocity.Z = 0.0f;
  977. // Did we send out the 'stopped' message?
  978. if (!m_lastUpdateSent)
  979. {
  980. m_lastUpdateSent = true;
  981. //base.RequestPhysicsterseUpdate();
  982. }
  983. }
  984. else
  985. {
  986. m_lastUpdateSent = false;
  987. vec = d.BodyGetLinearVel(Body);
  988. _velocity.X = (vec.X);
  989. _velocity.Y = (vec.Y);
  990. _velocity.Z = (vec.Z);
  991. if (_velocity.Z < -6 && !m_hackSentFall)
  992. {
  993. m_hackSentFall = true;
  994. m_pidControllerActive = false;
  995. }
  996. else if (flying && !m_hackSentFly)
  997. {
  998. //m_hackSentFly = true;
  999. //base.SendCollisionUpdate(new CollisionEventUpdate());
  1000. }
  1001. else
  1002. {
  1003. m_hackSentFly = false;
  1004. m_hackSentFall = false;
  1005. }
  1006. }
  1007. }
  1008. /// <summary>
  1009. /// Cleanup the things we use in the scene.
  1010. /// </summary>
  1011. public void Destroy()
  1012. {
  1013. m_tainted_isPhysical = false;
  1014. _parent_scene.AddPhysicsActorTaint(this);
  1015. }
  1016. public override void CrossingFailure()
  1017. {
  1018. }
  1019. public override PhysicsVector PIDTarget { set { return; } }
  1020. public override bool PIDActive { set { return; } }
  1021. public override float PIDTau { set { return; } }
  1022. public override float PIDHoverHeight { set { return; } }
  1023. public override bool PIDHoverActive { set { return; } }
  1024. public override PIDHoverType PIDHoverType { set { return; } }
  1025. public override float PIDHoverTau { set { return; } }
  1026. public override void SubscribeEvents(int ms)
  1027. {
  1028. m_eventsubscription = ms;
  1029. _parent_scene.addCollisionEventReporting(this);
  1030. }
  1031. public override void UnSubscribeEvents()
  1032. {
  1033. _parent_scene.remCollisionEventReporting(this);
  1034. m_eventsubscription = 0;
  1035. }
  1036. public void AddCollisionEvent(uint CollidedWith, float depth)
  1037. {
  1038. if (m_eventsubscription > 0)
  1039. CollisionEventsThisFrame.addCollider(CollidedWith,depth);
  1040. }
  1041. public void SendCollisions()
  1042. {
  1043. if (m_eventsubscription > 0)
  1044. {
  1045. base.SendCollisionUpdate(CollisionEventsThisFrame);
  1046. CollisionEventsThisFrame = new CollisionEventUpdate();
  1047. }
  1048. }
  1049. public override bool SubscribedEvents()
  1050. {
  1051. if (m_eventsubscription > 0)
  1052. return true;
  1053. return false;
  1054. }
  1055. public void ProcessTaints(float timestep)
  1056. {
  1057. if (m_tainted_isPhysical != m_isPhysical)
  1058. {
  1059. if (m_tainted_isPhysical)
  1060. {
  1061. // Create avatar capsule and related ODE data
  1062. if (!(Shell == IntPtr.Zero && Body == IntPtr.Zero && Amotor == IntPtr.Zero))
  1063. {
  1064. m_log.Warn("[PHYSICS]: re-creating the following avatar ODE data, even though it already exists - "
  1065. + (Shell!=IntPtr.Zero ? "Shell ":"")
  1066. + (Body!=IntPtr.Zero ? "Body ":"")
  1067. + (Amotor!=IntPtr.Zero ? "Amotor ":""));
  1068. }
  1069. AvatarGeomAndBodyCreation(_position.X, _position.Y, _position.Z, m_tensor);
  1070. _parent_scene.geom_name_map[Shell] = m_name;
  1071. _parent_scene.actor_name_map[Shell] = (PhysicsActor)this;
  1072. _parent_scene.AddCharacter(this);
  1073. }
  1074. else
  1075. {
  1076. _parent_scene.RemoveCharacter(this);
  1077. // destroy avatar capsule and related ODE data
  1078. if (Amotor != IntPtr.Zero)
  1079. {
  1080. // Kill the Amotor
  1081. d.JointDestroy(Amotor);
  1082. Amotor = IntPtr.Zero;
  1083. }
  1084. //kill the Geometry
  1085. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  1086. if (Body != IntPtr.Zero)
  1087. {
  1088. //kill the body
  1089. d.BodyDestroy(Body);
  1090. Body = IntPtr.Zero;
  1091. }
  1092. if (Shell != IntPtr.Zero)
  1093. {
  1094. d.GeomDestroy(Shell);
  1095. _parent_scene.geom_name_map.Remove(Shell);
  1096. Shell = IntPtr.Zero;
  1097. }
  1098. }
  1099. m_isPhysical = m_tainted_isPhysical;
  1100. }
  1101. if (m_tainted_CAPSULE_LENGTH != CAPSULE_LENGTH)
  1102. {
  1103. if (Shell != IntPtr.Zero && Body != IntPtr.Zero && Amotor != IntPtr.Zero)
  1104. {
  1105. m_pidControllerActive = true;
  1106. // no lock needed on _parent_scene.OdeLock because we are called from within the thread lock in OdePlugin's simulate()
  1107. d.JointDestroy(Amotor);
  1108. float prevCapsule = CAPSULE_LENGTH;
  1109. CAPSULE_LENGTH = m_tainted_CAPSULE_LENGTH;
  1110. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  1111. d.BodyDestroy(Body);
  1112. d.GeomDestroy(Shell);
  1113. AvatarGeomAndBodyCreation(_position.X, _position.Y,
  1114. _position.Z + (Math.Abs(CAPSULE_LENGTH - prevCapsule) * 2), m_tensor);
  1115. Velocity = new PhysicsVector(0f, 0f, 0f);
  1116. _parent_scene.geom_name_map[Shell] = m_name;
  1117. _parent_scene.actor_name_map[Shell] = (PhysicsActor)this;
  1118. }
  1119. else
  1120. {
  1121. m_log.Warn("[PHYSICS]: trying to change capsule size, but the following ODE data is missing - "
  1122. + (Shell==IntPtr.Zero ? "Shell ":"")
  1123. + (Body==IntPtr.Zero ? "Body ":"")
  1124. + (Amotor==IntPtr.Zero ? "Amotor ":""));
  1125. }
  1126. }
  1127. if (!m_taintPosition.IsIdentical(_position, 0.05f))
  1128. {
  1129. if (Body != IntPtr.Zero)
  1130. {
  1131. d.BodySetPosition(Body, m_taintPosition.X, m_taintPosition.Y, m_taintPosition.Z);
  1132. _position.X = m_taintPosition.X;
  1133. _position.Y = m_taintPosition.Y;
  1134. _position.Z = m_taintPosition.Z;
  1135. }
  1136. }
  1137. }
  1138. }
  1139. }