ODECharacter.cs 49 KB

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