ODECharacter.cs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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 OpenSim 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.Reflection;
  29. using OpenMetaverse;
  30. using Ode.NET;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Physics.Manager;
  33. using log4net;
  34. namespace OpenSim.Region.Physics.OdePlugin
  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. ERP = 7,
  49. StopCFM = 8,
  50. LoStop2 = 256,
  51. HiStop2 = 257,
  52. LoStop3 = 512,
  53. HiStop3 = 513
  54. }
  55. public class OdeCharacter : PhysicsActor
  56. {
  57. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  58. private PhysicsVector _position;
  59. private d.Vector3 _zeroPosition;
  60. // private d.Matrix3 m_StandUpRotation;
  61. private bool _zeroFlag = false;
  62. private bool m_lastUpdateSent = false;
  63. private PhysicsVector _velocity;
  64. private PhysicsVector _target_velocity;
  65. private PhysicsVector _acceleration;
  66. private PhysicsVector m_rotationalVelocity;
  67. private float m_mass = 80f;
  68. public float m_density = 60f;
  69. private bool m_pidControllerActive = true;
  70. public float PID_D = 800.0f;
  71. public float PID_P = 900.0f;
  72. //private static float POSTURE_SERVO = 10000.0f;
  73. public float CAPSULE_RADIUS = 0.37f;
  74. public float CAPSULE_LENGTH = 2.140599f;
  75. public float m_tensor = 3800000f;
  76. public float heightFudgeFactor = 0.52f;
  77. public float walkDivisor = 1.3f;
  78. public float runDivisor = 0.8f;
  79. private bool flying = false;
  80. private bool m_iscolliding = false;
  81. private bool m_iscollidingGround = false;
  82. private bool m_wascolliding = false;
  83. private bool m_wascollidingGround = false;
  84. private bool m_iscollidingObj = false;
  85. private bool m_alwaysRun = false;
  86. private bool m_hackSentFall = false;
  87. private bool m_hackSentFly = false;
  88. public uint m_localID = 0;
  89. public bool m_returnCollisions = false;
  90. // taints and their non-tainted counterparts
  91. public bool m_isPhysical = false; // the current physical status
  92. public bool m_tainted_isPhysical = false; // set when the physical status is tainted (false=not existing in physics engine, true=existing)
  93. private float m_tainted_CAPSULE_LENGTH; // set when the capsule length changes.
  94. private float m_buoyancy = 0f;
  95. // private CollisionLocker ode;
  96. private string m_name = String.Empty;
  97. private bool[] m_colliderarr = new bool[11];
  98. private bool[] m_colliderGroundarr = new bool[11];
  99. // Default we're a Character
  100. private CollisionCategories m_collisionCategories = (CollisionCategories.Character);
  101. // Default, Collide with Other Geometries, spaces, bodies and characters.
  102. private CollisionCategories m_collisionFlags = (CollisionCategories.Geom
  103. | CollisionCategories.Space
  104. | CollisionCategories.Body
  105. | CollisionCategories.Character
  106. | CollisionCategories.Land);
  107. public IntPtr Body = IntPtr.Zero;
  108. private OdeScene _parent_scene;
  109. public IntPtr Shell = IntPtr.Zero;
  110. public IntPtr Amotor = IntPtr.Zero;
  111. public d.Mass ShellMass;
  112. public bool collidelock = false;
  113. public int m_eventsubscription = 0;
  114. private CollisionEventUpdate CollisionEventsThisFrame = new CollisionEventUpdate();
  115. public OdeCharacter(String avName, OdeScene parent_scene, PhysicsVector pos, CollisionLocker dode, PhysicsVector size, float pid_d, float pid_p, float capsule_radius, float tensor, float density, float height_fudge_factor, float walk_divisor, float rundivisor)
  116. {
  117. // ode = dode;
  118. _velocity = new PhysicsVector();
  119. _target_velocity = new PhysicsVector();
  120. _position = pos;
  121. _acceleration = new PhysicsVector();
  122. _parent_scene = parent_scene;
  123. PID_D = pid_d;
  124. PID_P = pid_p;
  125. CAPSULE_RADIUS = capsule_radius;
  126. m_tensor = tensor;
  127. m_density = density;
  128. heightFudgeFactor = height_fudge_factor;
  129. walkDivisor = walk_divisor;
  130. runDivisor = rundivisor;
  131. // m_StandUpRotation =
  132. // new d.Matrix3(0.5f, 0.7071068f, 0.5f, -0.7071068f, 0f, 0.7071068f, 0.5f, -0.7071068f,
  133. // 0.5f);
  134. for (int i = 0; i < 11; i++)
  135. {
  136. m_colliderarr[i] = false;
  137. }
  138. CAPSULE_LENGTH = (size.Z * 1.15f) - CAPSULE_RADIUS * 2.0f;
  139. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  140. m_tainted_CAPSULE_LENGTH = CAPSULE_LENGTH;
  141. m_isPhysical = false; // current status: no ODE information exists
  142. m_tainted_isPhysical = true; // new tainted status: need to create ODE information
  143. lock (_parent_scene.OdeLock)
  144. {
  145. _parent_scene.AddPhysicsActorTaint(this);
  146. }
  147. m_name = avName;
  148. }
  149. public override int PhysicsActorType
  150. {
  151. get { return (int) ActorTypes.Agent; }
  152. set { return; }
  153. }
  154. /// <summary>
  155. /// If this is set, the avatar will move faster
  156. /// </summary>
  157. public override bool SetAlwaysRun
  158. {
  159. get { return m_alwaysRun; }
  160. set { m_alwaysRun = value; }
  161. }
  162. public override uint LocalID
  163. {
  164. set { m_localID = value; }
  165. }
  166. public override bool Grabbed
  167. {
  168. set { return; }
  169. }
  170. public override bool Selected
  171. {
  172. set { return; }
  173. }
  174. public override float Buoyancy
  175. {
  176. get { return m_buoyancy; }
  177. set { m_buoyancy = value; }
  178. }
  179. public override bool FloatOnWater
  180. {
  181. set { return; }
  182. }
  183. public override bool IsPhysical
  184. {
  185. get { return false; }
  186. set { return; }
  187. }
  188. public override bool ThrottleUpdates
  189. {
  190. get { return false; }
  191. set { return; }
  192. }
  193. public override bool Flying
  194. {
  195. get { return flying; }
  196. set { flying = value; }
  197. }
  198. /// <summary>
  199. /// Returns if the avatar is colliding in general.
  200. /// This includes the ground and objects and avatar.
  201. /// </summary>
  202. public override bool IsColliding
  203. {
  204. get { return m_iscolliding; }
  205. set
  206. {
  207. int i;
  208. int truecount = 0;
  209. int falsecount = 0;
  210. if (m_colliderarr.Length >= 10)
  211. {
  212. for (i = 0; i < 10; i++)
  213. {
  214. m_colliderarr[i] = m_colliderarr[i + 1];
  215. }
  216. }
  217. m_colliderarr[10] = value;
  218. for (i = 0; i < 11; i++)
  219. {
  220. if (m_colliderarr[i])
  221. {
  222. truecount++;
  223. }
  224. else
  225. {
  226. falsecount++;
  227. }
  228. }
  229. // Equal truecounts and false counts means we're colliding with something.
  230. if (falsecount > 1.2*truecount)
  231. {
  232. m_iscolliding = false;
  233. }
  234. else
  235. {
  236. m_iscolliding = true;
  237. }
  238. if (m_wascolliding != m_iscolliding)
  239. {
  240. //base.SendCollisionUpdate(new CollisionEventUpdate());
  241. }
  242. m_wascolliding = m_iscolliding;
  243. }
  244. }
  245. /// <summary>
  246. /// Returns if an avatar is colliding with the ground
  247. /// </summary>
  248. public override bool CollidingGround
  249. {
  250. get { return m_iscollidingGround; }
  251. set
  252. {
  253. // Collisions against the ground are not really reliable
  254. // So, to get a consistant value we have to average the current result over time
  255. // Currently we use 1 second = 10 calls to this.
  256. int i;
  257. int truecount = 0;
  258. int falsecount = 0;
  259. if (m_colliderGroundarr.Length >= 10)
  260. {
  261. for (i = 0; i < 10; i++)
  262. {
  263. m_colliderGroundarr[i] = m_colliderGroundarr[i + 1];
  264. }
  265. }
  266. m_colliderGroundarr[10] = value;
  267. for (i = 0; i < 11; i++)
  268. {
  269. if (m_colliderGroundarr[i])
  270. {
  271. truecount++;
  272. }
  273. else
  274. {
  275. falsecount++;
  276. }
  277. }
  278. // Equal truecounts and false counts means we're colliding with something.
  279. if (falsecount > 1.2*truecount)
  280. {
  281. m_iscollidingGround = false;
  282. }
  283. else
  284. {
  285. m_iscollidingGround = true;
  286. }
  287. if (m_wascollidingGround != m_iscollidingGround)
  288. {
  289. //base.SendCollisionUpdate(new CollisionEventUpdate());
  290. }
  291. m_wascollidingGround = m_iscollidingGround;
  292. }
  293. }
  294. /// <summary>
  295. /// Returns if the avatar is colliding with an object
  296. /// </summary>
  297. public override bool CollidingObj
  298. {
  299. get { return m_iscollidingObj; }
  300. set
  301. {
  302. m_iscollidingObj = value;
  303. if (value)
  304. m_pidControllerActive = false;
  305. else
  306. m_pidControllerActive = true;
  307. }
  308. }
  309. /// <summary>
  310. /// turn the PID controller on or off.
  311. /// The PID Controller will turn on all by itself in many situations
  312. /// </summary>
  313. /// <param name="status"></param>
  314. public void SetPidStatus(bool status)
  315. {
  316. m_pidControllerActive = status;
  317. }
  318. public override bool Stopped
  319. {
  320. get { return _zeroFlag; }
  321. }
  322. /// <summary>
  323. /// This 'puts' an avatar somewhere in the physics space.
  324. /// Not really a good choice unless you 'know' it's a good
  325. /// spot otherwise you're likely to orbit the avatar.
  326. /// </summary>
  327. public override PhysicsVector Position
  328. {
  329. get { return _position; }
  330. set
  331. {
  332. lock (_parent_scene.OdeLock)
  333. {
  334. d.BodySetPosition(Body, value.X, value.Y, value.Z);
  335. _position = value;
  336. }
  337. }
  338. }
  339. public override PhysicsVector RotationalVelocity
  340. {
  341. get { return m_rotationalVelocity; }
  342. set { m_rotationalVelocity = value; }
  343. }
  344. /// <summary>
  345. /// This property sets the height of the avatar only. We use the height to make sure the avatar stands up straight
  346. /// and use it to offset landings properly
  347. /// </summary>
  348. public override PhysicsVector Size
  349. {
  350. get { return new PhysicsVector(CAPSULE_RADIUS*2, CAPSULE_RADIUS*2, CAPSULE_LENGTH); }
  351. set
  352. {
  353. m_pidControllerActive = true;
  354. lock (_parent_scene.OdeLock)
  355. {
  356. PhysicsVector SetSize = value;
  357. m_tainted_CAPSULE_LENGTH = (SetSize.Z * 1.15f) - CAPSULE_RADIUS * 2.0f;
  358. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  359. Velocity = new PhysicsVector(0f, 0f, 0f);
  360. }
  361. _parent_scene.AddPhysicsActorTaint(this);
  362. }
  363. }
  364. /// <summary>
  365. /// This creates the Avatar's physical Surrogate at the position supplied
  366. /// </summary>
  367. /// <param name="npositionX"></param>
  368. /// <param name="npositionY"></param>
  369. /// <param name="npositionZ"></param>
  370. // WARNING: This MUST NOT be called outside of ProcessTaints, else we can have unsynchronized access
  371. // to ODE internals. ProcessTaints is called from within thread-locked Simulate(), so it is the only
  372. // place that is safe to call this routine AvatarGeomAndBodyCreation.
  373. private void AvatarGeomAndBodyCreation(float npositionX, float npositionY, float npositionZ, float tensor)
  374. {
  375. //CAPSULE_LENGTH = -5;
  376. //CAPSULE_RADIUS = -5;
  377. int dAMotorEuler = 1;
  378. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  379. if (CAPSULE_LENGTH <= 0)
  380. {
  381. m_log.Warn("[PHYSICS]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!");
  382. CAPSULE_LENGTH = 0.01f;
  383. }
  384. if (CAPSULE_RADIUS <= 0)
  385. {
  386. m_log.Warn("[PHYSICS]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!");
  387. CAPSULE_RADIUS = 0.01f;
  388. }
  389. Shell = d.CreateCapsule(_parent_scene.space, CAPSULE_RADIUS, CAPSULE_LENGTH);
  390. d.GeomSetCategoryBits(Shell, (int)m_collisionCategories);
  391. d.GeomSetCollideBits(Shell, (int)m_collisionFlags);
  392. d.MassSetCapsuleTotal(out ShellMass, m_mass, 2, CAPSULE_RADIUS, CAPSULE_LENGTH);
  393. Body = d.BodyCreate(_parent_scene.world);
  394. d.BodySetPosition(Body, npositionX, npositionY, npositionZ);
  395. d.BodySetMass(Body, ref ShellMass);
  396. d.Matrix3 m_caprot;
  397. // 90 Stand up on the cap of the capped cyllinder
  398. d.RFromAxisAndAngle(out m_caprot, 1, 0, 1, (float)(Math.PI / 2));
  399. d.GeomSetRotation(Shell, ref m_caprot);
  400. d.BodySetRotation(Body, ref m_caprot);
  401. d.GeomSetBody(Shell, Body);
  402. // The purpose of the AMotor here is to keep the avatar's physical
  403. // surrogate from rotating while moving
  404. Amotor = d.JointCreateAMotor(_parent_scene.world, IntPtr.Zero);
  405. d.JointAttach(Amotor, Body, IntPtr.Zero);
  406. d.JointSetAMotorMode(Amotor, dAMotorEuler);
  407. d.JointSetAMotorNumAxes(Amotor, 3);
  408. d.JointSetAMotorAxis(Amotor, 0, 0, 1, 0, 0);
  409. d.JointSetAMotorAxis(Amotor, 1, 0, 0, 1, 0);
  410. d.JointSetAMotorAxis(Amotor, 2, 0, 0, 0, 1);
  411. d.JointSetAMotorAngle(Amotor, 0, 0);
  412. d.JointSetAMotorAngle(Amotor, 1, 0);
  413. d.JointSetAMotorAngle(Amotor, 2, 0);
  414. // These lowstops and high stops are effectively (no wiggle room)
  415. d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, -0.000000000001f);
  416. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -0.000000000001f);
  417. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, -0.000000000001f);
  418. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 0.000000000001f);
  419. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0.000000000001f);
  420. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 0.000000000001f);
  421. // Fudge factor is 1f by default, we're setting it to 0. We don't want it to Fudge or the
  422. // capped cyllinder will fall over
  423. d.JointSetAMotorParam(Amotor, (int)dParam.FudgeFactor, 0f);
  424. d.JointSetAMotorParam(Amotor, (int)dParam.FMax, tensor);
  425. //d.Matrix3 bodyrotation = d.BodyGetRotation(Body);
  426. //d.QfromR(
  427. //d.Matrix3 checkrotation = new d.Matrix3(0.7071068,0.5, -0.7071068,
  428. //
  429. //m_log.Info("[PHYSICSAV]: Rotation: " + bodyrotation.M00 + " : " + bodyrotation.M01 + " : " + bodyrotation.M02 + " : " + bodyrotation.M10 + " : " + bodyrotation.M11 + " : " + bodyrotation.M12 + " : " + bodyrotation.M20 + " : " + bodyrotation.M21 + " : " + bodyrotation.M22);
  430. //standupStraight();
  431. }
  432. //
  433. /// <summary>
  434. /// Uses the capped cyllinder volume formula to calculate the avatar's mass.
  435. /// This may be used in calculations in the scene/scenepresence
  436. /// </summary>
  437. public override float Mass
  438. {
  439. get
  440. {
  441. float AVvolume = (float) (Math.PI*Math.Pow(CAPSULE_RADIUS, 2)*CAPSULE_LENGTH);
  442. return m_density*AVvolume;
  443. }
  444. }
  445. public override void link(PhysicsActor obj)
  446. {
  447. }
  448. public override void delink()
  449. {
  450. }
  451. public override void LockAngularMotion(PhysicsVector axis)
  452. {
  453. }
  454. // This code is very useful. Written by DanX0r. We're just not using it right now.
  455. // Commented out to prevent a warning.
  456. //
  457. // private void standupStraight()
  458. // {
  459. // // The purpose of this routine here is to quickly stabilize the Body while it's popped up in the air.
  460. // // The amotor needs a few seconds to stabilize so without it, the avatar shoots up sky high when you
  461. // // change appearance and when you enter the simulator
  462. // // After this routine is done, the amotor stabilizes much quicker
  463. // d.Vector3 feet;
  464. // d.Vector3 head;
  465. // d.BodyGetRelPointPos(Body, 0.0f, 0.0f, -1.0f, out feet);
  466. // d.BodyGetRelPointPos(Body, 0.0f, 0.0f, 1.0f, out head);
  467. // float posture = head.Z - feet.Z;
  468. // // restoring force proportional to lack of posture:
  469. // float servo = (2.5f - posture) * POSTURE_SERVO;
  470. // d.BodyAddForceAtRelPos(Body, 0.0f, 0.0f, servo, 0.0f, 0.0f, 1.0f);
  471. // d.BodyAddForceAtRelPos(Body, 0.0f, 0.0f, -servo, 0.0f, 0.0f, -1.0f);
  472. // //d.Matrix3 bodyrotation = d.BodyGetRotation(Body);
  473. // //m_log.Info("[PHYSICSAV]: Rotation: " + bodyrotation.M00 + " : " + bodyrotation.M01 + " : " + bodyrotation.M02 + " : " + bodyrotation.M10 + " : " + bodyrotation.M11 + " : " + bodyrotation.M12 + " : " + bodyrotation.M20 + " : " + bodyrotation.M21 + " : " + bodyrotation.M22);
  474. // }
  475. public override PhysicsVector Force
  476. {
  477. get { return new PhysicsVector(_target_velocity.X, _target_velocity.Y, _target_velocity.Z); }
  478. set { return; }
  479. }
  480. public override int VehicleType
  481. {
  482. get { return 0; }
  483. set { return; }
  484. }
  485. public override void VehicleFloatParam(int param, float value)
  486. {
  487. }
  488. public override void VehicleVectorParam(int param, PhysicsVector value)
  489. {
  490. }
  491. public override void VehicleRotationParam(int param, Quaternion rotation)
  492. {
  493. }
  494. public override void SetVolumeDetect(int param)
  495. {
  496. }
  497. public override PhysicsVector CenterOfMass
  498. {
  499. get { return PhysicsVector.Zero; }
  500. }
  501. public override PhysicsVector GeometricCenter
  502. {
  503. get { return PhysicsVector.Zero; }
  504. }
  505. public override PrimitiveBaseShape Shape
  506. {
  507. set { return; }
  508. }
  509. public override PhysicsVector Velocity
  510. {
  511. get {
  512. // There's a problem with PhysicsVector.Zero! Don't Use it Here!
  513. if (_zeroFlag)
  514. return new PhysicsVector(0f, 0f, 0f);
  515. m_lastUpdateSent = false;
  516. return _velocity;
  517. }
  518. set
  519. {
  520. m_pidControllerActive = true;
  521. _target_velocity = value;
  522. }
  523. }
  524. public override PhysicsVector Torque
  525. {
  526. get { return PhysicsVector.Zero; }
  527. set { return; }
  528. }
  529. public override float CollisionScore
  530. {
  531. get { return 0f; }
  532. set { }
  533. }
  534. public override bool Kinematic
  535. {
  536. get { return false; }
  537. set { }
  538. }
  539. public override Quaternion Orientation
  540. {
  541. get { return Quaternion.Identity; }
  542. set {
  543. //Matrix3 or = Orientation.ToRotationMatrix();
  544. //d.Matrix3 ord = new d.Matrix3(or.m00, or.m10, or.m20, or.m01, or.m11, or.m21, or.m02, or.m12, or.m22);
  545. //d.BodySetRotation(Body, ref ord);
  546. }
  547. }
  548. public override PhysicsVector Acceleration
  549. {
  550. get { return _acceleration; }
  551. }
  552. public void SetAcceleration(PhysicsVector accel)
  553. {
  554. m_pidControllerActive = true;
  555. _acceleration = accel;
  556. }
  557. /// <summary>
  558. /// Adds the force supplied to the Target Velocity
  559. /// The PID controller takes this target velocity and tries to make it a reality
  560. /// </summary>
  561. /// <param name="force"></param>
  562. public override void AddForce(PhysicsVector force, bool pushforce)
  563. {
  564. if (pushforce)
  565. {
  566. m_pidControllerActive = false;
  567. force *= 100f;
  568. doForce(force);
  569. //System.Console.WriteLine("Push!");
  570. //_target_velocity.X += force.X;
  571. // _target_velocity.Y += force.Y;
  572. //_target_velocity.Z += force.Z;
  573. }
  574. else
  575. {
  576. m_pidControllerActive = true;
  577. _target_velocity.X += force.X;
  578. _target_velocity.Y += force.Y;
  579. _target_velocity.Z += force.Z;
  580. }
  581. //m_lastUpdateSent = false;
  582. }
  583. public override void AddAngularForce(PhysicsVector force, bool pushforce)
  584. {
  585. }
  586. /// <summary>
  587. /// After all of the forces add up with 'add force' we apply them with doForce
  588. /// </summary>
  589. /// <param name="force"></param>
  590. public void doForce(PhysicsVector force)
  591. {
  592. if (!collidelock)
  593. {
  594. d.BodyAddForce(Body, force.X, force.Y, force.Z);
  595. //d.BodySetRotation(Body, ref m_StandUpRotation);
  596. //standupStraight();
  597. }
  598. }
  599. public override void SetMomentum(PhysicsVector momentum)
  600. {
  601. }
  602. /// <summary>
  603. /// Called from Simulate
  604. /// This is the avatar's movement control + PID Controller
  605. /// </summary>
  606. /// <param name="timeStep"></param>
  607. public void Move(float timeStep)
  608. {
  609. // no lock; for now it's only called from within Simulate()
  610. // If the PID Controller isn't active then we set our force
  611. // calculating base velocity to the current position
  612. if (m_pidControllerActive == false)
  613. {
  614. _zeroPosition = d.BodyGetPosition(Body);
  615. }
  616. //PidStatus = true;
  617. PhysicsVector vec = new PhysicsVector();
  618. d.Vector3 vel = d.BodyGetLinearVel(Body);
  619. float movementdivisor = 1f;
  620. if (!m_alwaysRun)
  621. {
  622. movementdivisor = walkDivisor;
  623. }
  624. else
  625. {
  626. movementdivisor = runDivisor;
  627. }
  628. // if velocity is zero, use position control; otherwise, velocity control
  629. if (_target_velocity.X == 0.0f && _target_velocity.Y == 0.0f && _target_velocity.Z == 0.0f && m_iscolliding)
  630. {
  631. // keep track of where we stopped. No more slippin' & slidin'
  632. if (!_zeroFlag)
  633. {
  634. _zeroFlag = true;
  635. _zeroPosition = d.BodyGetPosition(Body);
  636. }
  637. if (m_pidControllerActive)
  638. {
  639. // We only want to deactivate the PID Controller if we think we want to have our surrogate
  640. // react to the physics scene by moving it's position.
  641. // Avatar to Avatar collisions
  642. // Prim to avatar collisions
  643. d.Vector3 pos = d.BodyGetPosition(Body);
  644. vec.X = (_target_velocity.X - vel.X) * (PID_D) + (_zeroPosition.X - pos.X) * (PID_P * 2);
  645. vec.Y = (_target_velocity.Y - vel.Y)*(PID_D) + (_zeroPosition.Y - pos.Y)* (PID_P * 2);
  646. if (flying)
  647. {
  648. vec.Z = (_target_velocity.Z - vel.Z) * (PID_D) + (_zeroPosition.Z - pos.Z) * PID_P;
  649. }
  650. }
  651. //PidStatus = true;
  652. }
  653. else
  654. {
  655. m_pidControllerActive = true;
  656. _zeroFlag = false;
  657. if (m_iscolliding && !flying)
  658. {
  659. // We're standing on something
  660. vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D);
  661. vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D);
  662. }
  663. else if (m_iscolliding && flying)
  664. {
  665. // We're flying and colliding with something
  666. vec.X = ((_target_velocity.X/movementdivisor) - vel.X)*(PID_D / 16);
  667. vec.Y = ((_target_velocity.Y/movementdivisor) - vel.Y)*(PID_D / 16);
  668. }
  669. else if (!m_iscolliding && flying)
  670. {
  671. // we're in mid air suspended
  672. vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D/6);
  673. vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D/6);
  674. }
  675. if (m_iscolliding && !flying && _target_velocity.Z > 0.0f)
  676. {
  677. // We're colliding with something and we're not flying but we're moving
  678. // This means we're walking or running.
  679. d.Vector3 pos = d.BodyGetPosition(Body);
  680. vec.Z = (_target_velocity.Z - vel.Z)*PID_D + (_zeroPosition.Z - pos.Z)*PID_P;
  681. if (_target_velocity.X > 0)
  682. {
  683. vec.X = ((_target_velocity.X - vel.X)/1.2f)*PID_D;
  684. }
  685. if (_target_velocity.Y > 0)
  686. {
  687. vec.Y = ((_target_velocity.Y - vel.Y)/1.2f)*PID_D;
  688. }
  689. }
  690. else if (!m_iscolliding && !flying)
  691. {
  692. // we're not colliding and we're not flying so that means we're falling!
  693. // m_iscolliding includes collisions with the ground.
  694. // d.Vector3 pos = d.BodyGetPosition(Body);
  695. if (_target_velocity.X > 0)
  696. {
  697. vec.X = ((_target_velocity.X - vel.X)/1.2f)*PID_D;
  698. }
  699. if (_target_velocity.Y > 0)
  700. {
  701. vec.Y = ((_target_velocity.Y - vel.Y)/1.2f)*PID_D;
  702. }
  703. }
  704. if (flying)
  705. {
  706. vec.Z = (_target_velocity.Z - vel.Z) * (PID_D);
  707. }
  708. }
  709. if (flying)
  710. {
  711. vec.Z += ((-1 * _parent_scene.gravityz)*m_mass);
  712. }
  713. doForce(vec);
  714. }
  715. /// <summary>
  716. /// Updates the reported position and velocity. This essentially sends the data up to ScenePresence.
  717. /// </summary>
  718. public void UpdatePositionAndVelocity()
  719. {
  720. // no lock; called from Simulate() -- if you call this from elsewhere, gotta lock or do Monitor.Enter/Exit!
  721. d.Vector3 vec = d.BodyGetPosition(Body);
  722. // kluge to keep things in bounds. ODE lets dead avatars drift away (they should be removed!)
  723. if (vec.X < 0.0f) vec.X = 0.0f;
  724. if (vec.Y < 0.0f) vec.Y = 0.0f;
  725. if (vec.X > 255.95f) vec.X = 255.95f;
  726. if (vec.Y > 255.95f) vec.Y = 255.95f;
  727. _position.X = vec.X;
  728. _position.Y = vec.Y;
  729. _position.Z = vec.Z;
  730. // Did we move last? = zeroflag
  731. // This helps keep us from sliding all over
  732. if (_zeroFlag)
  733. {
  734. _velocity.X = 0.0f;
  735. _velocity.Y = 0.0f;
  736. _velocity.Z = 0.0f;
  737. // Did we send out the 'stopped' message?
  738. if (!m_lastUpdateSent)
  739. {
  740. m_lastUpdateSent = true;
  741. //base.RequestPhysicsterseUpdate();
  742. }
  743. }
  744. else
  745. {
  746. m_lastUpdateSent = false;
  747. vec = d.BodyGetLinearVel(Body);
  748. _velocity.X = (vec.X);
  749. _velocity.Y = (vec.Y);
  750. _velocity.Z = (vec.Z);
  751. if (_velocity.Z < -6 && !m_hackSentFall)
  752. {
  753. m_hackSentFall = true;
  754. m_pidControllerActive = false;
  755. }
  756. else if (flying && !m_hackSentFly)
  757. {
  758. //m_hackSentFly = true;
  759. //base.SendCollisionUpdate(new CollisionEventUpdate());
  760. }
  761. else
  762. {
  763. m_hackSentFly = false;
  764. m_hackSentFall = false;
  765. }
  766. }
  767. }
  768. /// <summary>
  769. /// Cleanup the things we use in the scene.
  770. /// </summary>
  771. public void Destroy()
  772. {
  773. lock (_parent_scene.OdeLock)
  774. {
  775. m_tainted_isPhysical = false;
  776. _parent_scene.AddPhysicsActorTaint(this);
  777. }
  778. }
  779. public override void CrossingFailure()
  780. {
  781. }
  782. public override PhysicsVector PIDTarget { set { return; } }
  783. public override bool PIDActive { set { return; } }
  784. public override float PIDTau { set { return; } }
  785. public override void SubscribeEvents(int ms)
  786. {
  787. m_eventsubscription = ms;
  788. _parent_scene.addCollisionEventReporting(this);
  789. }
  790. public override void UnSubscribeEvents()
  791. {
  792. _parent_scene.remCollisionEventReporting(this);
  793. m_eventsubscription = 0;
  794. }
  795. public void AddCollisionEvent(uint CollidedWith, float depth)
  796. {
  797. if (m_eventsubscription > 0)
  798. CollisionEventsThisFrame.addCollider(CollidedWith,depth);
  799. }
  800. public void SendCollisions()
  801. {
  802. if (m_eventsubscription > 0)
  803. {
  804. base.SendCollisionUpdate(CollisionEventsThisFrame);
  805. CollisionEventsThisFrame = new CollisionEventUpdate();
  806. }
  807. }
  808. public override bool SubscribedEvents()
  809. {
  810. if (m_eventsubscription > 0)
  811. return true;
  812. return false;
  813. }
  814. public void ProcessTaints(float timestep)
  815. {
  816. if (m_tainted_isPhysical != m_isPhysical)
  817. {
  818. if (m_tainted_isPhysical)
  819. {
  820. // Create avatar capsule and related ODE data
  821. if (!(Shell == IntPtr.Zero && Body == IntPtr.Zero && Amotor == IntPtr.Zero))
  822. {
  823. m_log.Warn("[PHYSICS]: re-creating the following avatar ODE data, even though it already exists - "
  824. + (Shell!=IntPtr.Zero ? "Shell ":"")
  825. + (Body!=IntPtr.Zero ? "Body ":"")
  826. + (Amotor!=IntPtr.Zero ? "Amotor ":"") );
  827. }
  828. AvatarGeomAndBodyCreation(_position.X, _position.Y, _position.Z, m_tensor);
  829. _parent_scene.geom_name_map[Shell] = m_name;
  830. _parent_scene.actor_name_map[Shell] = (PhysicsActor)this;
  831. }
  832. else
  833. {
  834. // destroy avatar capsule and related ODE data
  835. // Kill the Amotor
  836. d.JointDestroy(Amotor);
  837. Amotor = IntPtr.Zero;
  838. //kill the Geometry
  839. _parent_scene.waitForSpaceUnlock(_parent_scene.space);
  840. d.GeomDestroy(Shell);
  841. _parent_scene.geom_name_map.Remove(Shell);
  842. Shell = IntPtr.Zero;
  843. //kill the body
  844. d.BodyDestroy(Body);
  845. Body=IntPtr.Zero;
  846. }
  847. m_isPhysical = m_tainted_isPhysical;
  848. }
  849. if (m_tainted_CAPSULE_LENGTH != CAPSULE_LENGTH)
  850. {
  851. if (Shell != IntPtr.Zero && Body != IntPtr.Zero && Amotor != IntPtr.Zero)
  852. {
  853. m_pidControllerActive = true;
  854. // no lock needed on _parent_scene.OdeLock because we are called from within the thread lock in OdePlugin's simulate()
  855. d.JointDestroy(Amotor);
  856. float prevCapsule = CAPSULE_LENGTH;
  857. CAPSULE_LENGTH = m_tainted_CAPSULE_LENGTH;
  858. //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
  859. d.BodyDestroy(Body);
  860. d.GeomDestroy(Shell);
  861. AvatarGeomAndBodyCreation(_position.X, _position.Y,
  862. _position.Z + (Math.Abs(CAPSULE_LENGTH - prevCapsule) * 2), m_tensor);
  863. Velocity = new PhysicsVector(0f, 0f, 0f);
  864. _parent_scene.geom_name_map[Shell] = m_name;
  865. _parent_scene.actor_name_map[Shell] = (PhysicsActor)this;
  866. }
  867. else
  868. {
  869. m_log.Warn("[PHYSICS]: trying to change capsule size, but the following ODE data is missing - "
  870. + (Shell==IntPtr.Zero ? "Shell ":"")
  871. + (Body==IntPtr.Zero ? "Body ":"")
  872. + (Amotor==IntPtr.Zero ? "Amotor ":"") );
  873. }
  874. }
  875. }
  876. }
  877. }