1
0

ODECharacter.cs 53 KB

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