1
0

ODECharacter.cs 52 KB

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