ODECharacter.cs 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  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. // Revision by Ubit 2011/12
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using OpenMetaverse;
  32. using OdeAPI;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.PhysicsModules.SharedBase;
  35. using log4net;
  36. namespace OpenSim.Region.PhysicsModule.ubOde
  37. {
  38. /// <summary>
  39. /// Various properties that ODE uses for AMotors but isn't exposed in ODE.NET so we must define them ourselves.
  40. /// </summary>
  41. public enum dParam : int
  42. {
  43. LowStop = 0,
  44. HiStop = 1,
  45. Vel = 2,
  46. FMax = 3,
  47. FudgeFactor = 4,
  48. Bounce = 5,
  49. CFM = 6,
  50. StopERP = 7,
  51. StopCFM = 8,
  52. LoStop2 = 256,
  53. HiStop2 = 257,
  54. Vel2 = 258,
  55. FMax2 = 259,
  56. StopERP2 = 7 + 256,
  57. StopCFM2 = 8 + 256,
  58. LoStop3 = 512,
  59. HiStop3 = 513,
  60. Vel3 = 514,
  61. FMax3 = 515,
  62. StopERP3 = 7 + 512,
  63. StopCFM3 = 8 + 512
  64. }
  65. public class OdeCharacter : PhysicsActor
  66. {
  67. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  68. private Vector3 _position;
  69. private Vector3 _zeroPosition;
  70. private Vector3 _velocity;
  71. private Vector3 _target_velocity;
  72. private Vector3 _acceleration;
  73. private Vector3 m_rotationalVelocity;
  74. private Vector3 m_size;
  75. private Vector3 m_collideNormal;
  76. private Vector3 m_lastFallVel;
  77. private Quaternion m_orientation;
  78. private Quaternion m_orientation2D;
  79. private float m_mass = 80f;
  80. public float m_density = 60f;
  81. private bool m_pidControllerActive = true;
  82. const float basePID_D = 0.55f; // scaled for unit mass unit time (2200 /(50*80))
  83. const float basePID_P = 0.225f; // scaled for unit mass unit time (900 /(50*80))
  84. public float PID_D;
  85. public float PID_P;
  86. private float timeStep;
  87. private float invtimeStep;
  88. private float m_feetOffset = 0;
  89. private float feetOff = 0;
  90. private float boneOff = 0;
  91. private float AvaAvaSizeXsq = 0.3f;
  92. private float AvaAvaSizeYsq = 0.2f;
  93. public float walkDivisor = 1.3f;
  94. public float runDivisor = 0.8f;
  95. private bool m_flying = false;
  96. private bool m_iscolliding = false;
  97. private bool m_iscollidingGround = false;
  98. private bool m_iscollidingObj = false;
  99. private bool m_alwaysRun = false;
  100. private bool _zeroFlag = false;
  101. private bool m_haveLastFallVel = false;
  102. private uint m_localID = 0;
  103. public bool m_returnCollisions = false;
  104. // taints and their non-tainted counterparts
  105. public bool m_isPhysical = false; // the current physical status
  106. public float MinimumGroundFlightOffset = 3f;
  107. private float m_buoyancy = 0f;
  108. private bool m_freemove = false;
  109. // private string m_name = String.Empty;
  110. // other filter control
  111. int m_colliderfilter = 0;
  112. int m_colliderGroundfilter = 0;
  113. int m_colliderObjectfilter = 0;
  114. // Default we're a Character
  115. private CollisionCategories m_collisionCategories = (CollisionCategories.Character);
  116. // Default, Collide with Other Geometries, spaces, bodies and characters.
  117. private CollisionCategories m_collisionFlags = (CollisionCategories.Character
  118. | CollisionCategories.Geom
  119. | CollisionCategories.VolumeDtc
  120. );
  121. // we do land collisions not ode | CollisionCategories.Land);
  122. public IntPtr Body = IntPtr.Zero;
  123. private ODEScene m_parent_scene;
  124. private IntPtr capsule = IntPtr.Zero;
  125. public IntPtr collider = IntPtr.Zero;
  126. public IntPtr Amotor = IntPtr.Zero;
  127. public d.Mass ShellMass;
  128. public int m_eventsubscription = 0;
  129. private int m_cureventsubscription = 0;
  130. private CollisionEventUpdate CollisionEventsThisFrame = new CollisionEventUpdate();
  131. private bool SentEmptyCollisionsEvent;
  132. // unique UUID of this character object
  133. public UUID m_uuid;
  134. public bool bad = false;
  135. float mu;
  136. // HoverHeight control
  137. private float m_PIDHoverHeight;
  138. private float m_PIDHoverTau;
  139. private bool m_useHoverPID;
  140. private PIDHoverType m_PIDHoverType;
  141. private float m_targetHoverHeight;
  142. public OdeCharacter(uint localID, String avName, ODEScene parent_scene, Vector3 pos, Vector3 pSize, float pfeetOffset, float density, float walk_divisor, float rundivisor)
  143. {
  144. m_uuid = UUID.Random();
  145. m_localID = localID;
  146. m_parent_scene = parent_scene;
  147. timeStep = parent_scene.ODE_STEPSIZE;
  148. invtimeStep = 1 / timeStep;
  149. if (pos.IsFinite())
  150. {
  151. if (pos.Z > 99999f)
  152. {
  153. pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  154. }
  155. if (pos.Z < -100f) // shouldn't this be 0 ?
  156. {
  157. pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  158. }
  159. _position = pos;
  160. }
  161. else
  162. {
  163. _position = new Vector3(((float)m_parent_scene.WorldExtents.X * 0.5f), ((float)m_parent_scene.WorldExtents.Y * 0.5f), parent_scene.GetTerrainHeightAtXY(128f, 128f) + 10f);
  164. m_log.Warn("[PHYSICS]: Got NaN Position on Character Create");
  165. }
  166. m_size.X = pSize.X;
  167. m_size.Y = pSize.Y;
  168. m_size.Z = pSize.Z;
  169. if(m_size.X <0.01f)
  170. m_size.X = 0.01f;
  171. if(m_size.Y <0.01f)
  172. m_size.Y = 0.01f;
  173. if(m_size.Z <0.01f)
  174. m_size.Z = 0.01f;
  175. m_feetOffset = pfeetOffset;
  176. m_orientation = Quaternion.Identity;
  177. m_orientation2D = Quaternion.Identity;
  178. m_density = density;
  179. // force lower density for testing
  180. m_density = 3.0f;
  181. mu = m_parent_scene.AvatarFriction;
  182. walkDivisor = walk_divisor;
  183. runDivisor = rundivisor;
  184. m_mass = m_density * m_size.X * m_size.Y * m_size.Z; ; // sure we have a default
  185. PID_D = basePID_D * m_mass * invtimeStep;
  186. PID_P = basePID_P * m_mass * invtimeStep;
  187. m_isPhysical = false; // current status: no ODE information exists
  188. Name = avName;
  189. AddChange(changes.Add, null);
  190. }
  191. public override int PhysicsActorType
  192. {
  193. get { return (int)ActorTypes.Agent; }
  194. set { return; }
  195. }
  196. public override void getContactData(ref ContactData cdata)
  197. {
  198. cdata.mu = mu;
  199. cdata.bounce = 0;
  200. cdata.softcolide = false;
  201. }
  202. public override bool Building { get; set; }
  203. /// <summary>
  204. /// If this is set, the avatar will move faster
  205. /// </summary>
  206. public override bool SetAlwaysRun
  207. {
  208. get { return m_alwaysRun; }
  209. set { m_alwaysRun = value; }
  210. }
  211. public override uint LocalID
  212. {
  213. get { return m_localID; }
  214. set { m_localID = value; }
  215. }
  216. public override PhysicsActor ParentActor
  217. {
  218. get { return (PhysicsActor)this; }
  219. }
  220. public override bool Grabbed
  221. {
  222. set { return; }
  223. }
  224. public override bool Selected
  225. {
  226. set { return; }
  227. }
  228. public override float Buoyancy
  229. {
  230. get { return m_buoyancy; }
  231. set { m_buoyancy = value; }
  232. }
  233. public override bool FloatOnWater
  234. {
  235. set { return; }
  236. }
  237. public override bool IsPhysical
  238. {
  239. get { return m_isPhysical; }
  240. set { return; }
  241. }
  242. public override bool ThrottleUpdates
  243. {
  244. get { return false; }
  245. set { return; }
  246. }
  247. public override bool Flying
  248. {
  249. get { return m_flying; }
  250. set
  251. {
  252. m_flying = value;
  253. // m_log.DebugFormat("[PHYSICS]: Set OdeCharacter Flying to {0}", flying);
  254. }
  255. }
  256. /// <summary>
  257. /// Returns if the avatar is colliding in general.
  258. /// This includes the ground and objects and avatar.
  259. /// </summary>
  260. public override bool IsColliding
  261. {
  262. get { return (m_iscolliding || m_iscollidingGround); }
  263. set
  264. {
  265. if (value)
  266. {
  267. m_colliderfilter += 3;
  268. if (m_colliderfilter > 3)
  269. m_colliderfilter = 3;
  270. }
  271. else
  272. {
  273. m_colliderfilter--;
  274. if (m_colliderfilter < 0)
  275. m_colliderfilter = 0;
  276. }
  277. if (m_colliderfilter == 0)
  278. m_iscolliding = false;
  279. else
  280. {
  281. m_pidControllerActive = true;
  282. m_iscolliding = true;
  283. m_freemove = false;
  284. }
  285. }
  286. }
  287. /// <summary>
  288. /// Returns if an avatar is colliding with the ground
  289. /// </summary>
  290. public override bool CollidingGround
  291. {
  292. get { return m_iscollidingGround; }
  293. set
  294. {
  295. /* we now control this
  296. if (value)
  297. {
  298. m_colliderGroundfilter += 2;
  299. if (m_colliderGroundfilter > 2)
  300. m_colliderGroundfilter = 2;
  301. }
  302. else
  303. {
  304. m_colliderGroundfilter--;
  305. if (m_colliderGroundfilter < 0)
  306. m_colliderGroundfilter = 0;
  307. }
  308. if (m_colliderGroundfilter == 0)
  309. m_iscollidingGround = false;
  310. else
  311. m_iscollidingGround = true;
  312. */
  313. }
  314. }
  315. /// <summary>
  316. /// Returns if the avatar is colliding with an object
  317. /// </summary>
  318. public override bool CollidingObj
  319. {
  320. get { return m_iscollidingObj; }
  321. set
  322. {
  323. // Ubit filter this also
  324. if (value)
  325. {
  326. m_colliderObjectfilter += 2;
  327. if (m_colliderObjectfilter > 2)
  328. m_colliderObjectfilter = 2;
  329. }
  330. else
  331. {
  332. m_colliderObjectfilter--;
  333. if (m_colliderObjectfilter < 0)
  334. m_colliderObjectfilter = 0;
  335. }
  336. if (m_colliderObjectfilter == 0)
  337. m_iscollidingObj = false;
  338. else
  339. m_iscollidingObj = true;
  340. // m_iscollidingObj = value;
  341. if (m_iscollidingObj)
  342. m_pidControllerActive = false;
  343. else
  344. m_pidControllerActive = true;
  345. }
  346. }
  347. /// <summary>
  348. /// turn the PID controller on or off.
  349. /// The PID Controller will turn on all by itself in many situations
  350. /// </summary>
  351. /// <param name="status"></param>
  352. public void SetPidStatus(bool status)
  353. {
  354. m_pidControllerActive = status;
  355. }
  356. public override bool Stopped
  357. {
  358. get { return _zeroFlag; }
  359. }
  360. /// <summary>
  361. /// This 'puts' an avatar somewhere in the physics space.
  362. /// Not really a good choice unless you 'know' it's a good
  363. /// spot otherwise you're likely to orbit the avatar.
  364. /// </summary>
  365. public override Vector3 Position
  366. {
  367. get { return _position; }
  368. set
  369. {
  370. if (value.IsFinite())
  371. {
  372. if (value.Z > 9999999f)
  373. {
  374. value.Z = m_parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  375. }
  376. if (value.Z < -100f)
  377. {
  378. value.Z = m_parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  379. }
  380. AddChange(changes.Position, value);
  381. }
  382. else
  383. {
  384. m_log.Warn("[PHYSICS]: Got a NaN Position from Scene on a Character");
  385. }
  386. }
  387. }
  388. public override Vector3 RotationalVelocity
  389. {
  390. get { return m_rotationalVelocity; }
  391. set { m_rotationalVelocity = value; }
  392. }
  393. /// <summary>
  394. /// This property sets the height of the avatar only. We use the height to make sure the avatar stands up straight
  395. /// and use it to offset landings properly
  396. /// </summary>
  397. public override Vector3 Size
  398. {
  399. get
  400. {
  401. return m_size;
  402. }
  403. set
  404. {
  405. if (value.IsFinite())
  406. {
  407. if(value.X <0.01f)
  408. value.X = 0.01f;
  409. if(value.Y <0.01f)
  410. value.Y = 0.01f;
  411. if(value.Z <0.01f)
  412. value.Z = 0.01f;
  413. AddChange(changes.Size, value);
  414. }
  415. else
  416. {
  417. m_log.Warn("[PHYSICS]: Got a NaN Size from Scene on a Character");
  418. }
  419. }
  420. }
  421. public override void setAvatarSize(Vector3 size, float feetOffset)
  422. {
  423. if (size.IsFinite())
  424. {
  425. if (size.X < 0.01f)
  426. size.X = 0.01f;
  427. if (size.Y < 0.01f)
  428. size.Y = 0.01f;
  429. if (size.Z < 0.01f)
  430. size.Z = 0.01f;
  431. strAvatarSize st = new strAvatarSize();
  432. st.size = size;
  433. st.offset = feetOffset;
  434. AddChange(changes.AvatarSize, st);
  435. }
  436. else
  437. {
  438. m_log.Warn("[PHYSICS]: Got a NaN AvatarSize from Scene on a Character");
  439. }
  440. }
  441. /// <summary>
  442. /// This creates the Avatar's physical Surrogate at the position supplied
  443. /// </summary>
  444. /// <param name="npositionX"></param>
  445. /// <param name="npositionY"></param>
  446. /// <param name="npositionZ"></param>
  447. //
  448. /// <summary>
  449. /// Uses the capped cyllinder volume formula to calculate the avatar's mass.
  450. /// This may be used in calculations in the scene/scenepresence
  451. /// </summary>
  452. public override float Mass
  453. {
  454. get
  455. {
  456. return m_mass;
  457. }
  458. }
  459. public override void link(PhysicsActor obj)
  460. {
  461. }
  462. public override void delink()
  463. {
  464. }
  465. public override void LockAngularMotion(byte axislocks)
  466. {
  467. }
  468. public override Vector3 Force
  469. {
  470. get { return _target_velocity; }
  471. set { return; }
  472. }
  473. public override int VehicleType
  474. {
  475. get { return 0; }
  476. set { return; }
  477. }
  478. public override void VehicleFloatParam(int param, float value)
  479. {
  480. }
  481. public override void VehicleVectorParam(int param, Vector3 value)
  482. {
  483. }
  484. public override void VehicleRotationParam(int param, Quaternion rotation)
  485. {
  486. }
  487. public override void VehicleFlags(int param, bool remove)
  488. {
  489. }
  490. public override void SetVolumeDetect(int param)
  491. {
  492. }
  493. public override Vector3 CenterOfMass
  494. {
  495. get
  496. {
  497. Vector3 pos = _position;
  498. return pos;
  499. }
  500. }
  501. public override Vector3 GeometricCenter
  502. {
  503. get
  504. {
  505. Vector3 pos = _position;
  506. return pos;
  507. }
  508. }
  509. public override PrimitiveBaseShape Shape
  510. {
  511. set { return; }
  512. }
  513. public override Vector3 rootVelocity
  514. {
  515. get
  516. {
  517. return _velocity;
  518. }
  519. }
  520. public override Vector3 Velocity
  521. {
  522. get
  523. {
  524. return _velocity;
  525. }
  526. set
  527. {
  528. if (value.IsFinite())
  529. {
  530. AddChange(changes.Velocity, value);
  531. }
  532. else
  533. {
  534. m_log.Warn("[PHYSICS]: Got a NaN velocity from Scene in a Character");
  535. }
  536. }
  537. }
  538. public override Vector3 TargetVelocity
  539. {
  540. get
  541. {
  542. return m_targetVelocity;
  543. }
  544. set
  545. {
  546. if (value.IsFinite())
  547. {
  548. AddChange(changes.TargetVelocity, value);
  549. }
  550. else
  551. {
  552. m_log.Warn("[PHYSICS]: Got a NaN velocity from Scene in a Character");
  553. }
  554. }
  555. }
  556. public override Vector3 Torque
  557. {
  558. get { return Vector3.Zero; }
  559. set { return; }
  560. }
  561. public override float CollisionScore
  562. {
  563. get { return 0f; }
  564. set { }
  565. }
  566. public override bool Kinematic
  567. {
  568. get { return false; }
  569. set { }
  570. }
  571. public override Quaternion Orientation
  572. {
  573. get { return m_orientation; }
  574. set
  575. {
  576. // fakeori = value;
  577. // givefakeori++;
  578. value.Normalize();
  579. AddChange(changes.Orientation, value);
  580. }
  581. }
  582. public override Vector3 Acceleration
  583. {
  584. get { return _acceleration; }
  585. set { }
  586. }
  587. public void SetAcceleration(Vector3 accel)
  588. {
  589. m_pidControllerActive = true;
  590. _acceleration = accel;
  591. }
  592. /// <summary>
  593. /// Adds the force supplied to the Target Velocity
  594. /// The PID controller takes this target velocity and tries to make it a reality
  595. /// </summary>
  596. /// <param name="force"></param>
  597. public override void AddForce(Vector3 force, bool pushforce)
  598. {
  599. if (force.IsFinite())
  600. {
  601. if (pushforce)
  602. {
  603. AddChange(changes.Force, force * m_density / (m_parent_scene.ODE_STEPSIZE * 28f));
  604. }
  605. else
  606. {
  607. AddChange(changes.TargetVelocity, force);
  608. }
  609. }
  610. else
  611. {
  612. m_log.Warn("[PHYSICS]: Got a NaN force applied to a Character");
  613. }
  614. //m_lastUpdateSent = false;
  615. }
  616. public override void AddAngularForce(Vector3 force, bool pushforce)
  617. {
  618. }
  619. public override void SetMomentum(Vector3 momentum)
  620. {
  621. if (momentum.IsFinite())
  622. AddChange(changes.Momentum, momentum);
  623. }
  624. private void AvatarGeomAndBodyCreation(float npositionX, float npositionY, float npositionZ)
  625. {
  626. // sizes one day should came from visual parameters
  627. float sx = m_size.X;
  628. float sy = m_size.Y;
  629. float sz = m_size.Z;
  630. float bot = -sz * 0.5f + m_feetOffset;
  631. boneOff = bot + 0.3f;
  632. float feetsz = sz * 0.45f;
  633. if (feetsz > 0.6f)
  634. feetsz = 0.6f;
  635. feetOff = bot + feetsz;
  636. AvaAvaSizeXsq = 0.4f * sx;
  637. AvaAvaSizeXsq *= AvaAvaSizeXsq;
  638. AvaAvaSizeYsq = 0.5f * sy;
  639. AvaAvaSizeYsq *= AvaAvaSizeYsq;
  640. m_parent_scene.waitForSpaceUnlock(m_parent_scene.CharsSpace);
  641. collider = d.HashSpaceCreate(m_parent_scene.CharsSpace);
  642. d.HashSpaceSetLevels(collider, -4, 3);
  643. d.SpaceSetSublevel(collider, 3);
  644. d.SpaceSetCleanup(collider, false);
  645. d.GeomSetCategoryBits(collider, (uint)m_collisionCategories);
  646. d.GeomSetCollideBits(collider, (uint)m_collisionFlags);
  647. float r = m_size.X;
  648. if (m_size.Y > r)
  649. r = m_size.Y;
  650. float l = m_size.Z - r;
  651. r *= 0.5f;
  652. capsule = d.CreateCapsule(collider, r, l);
  653. m_mass = m_density * m_size.X * m_size.Y * m_size.Z; // update mass
  654. d.MassSetBoxTotal(out ShellMass, m_mass, m_size.X, m_size.Y, m_size.Z);
  655. PID_D = basePID_D * m_mass / m_parent_scene.ODE_STEPSIZE;
  656. PID_P = basePID_P * m_mass / m_parent_scene.ODE_STEPSIZE;
  657. Body = d.BodyCreate(m_parent_scene.world);
  658. _zeroFlag = false;
  659. m_pidControllerActive = true;
  660. m_freemove = false;
  661. _velocity = Vector3.Zero;
  662. d.BodySetAutoDisableFlag(Body, false);
  663. d.BodySetPosition(Body, npositionX, npositionY, npositionZ);
  664. _position.X = npositionX;
  665. _position.Y = npositionY;
  666. _position.Z = npositionZ;
  667. d.BodySetMass(Body, ref ShellMass);
  668. d.GeomSetBody(capsule, Body);
  669. // The purpose of the AMotor here is to keep the avatar's physical
  670. // surrogate from rotating while moving
  671. Amotor = d.JointCreateAMotor(m_parent_scene.world, IntPtr.Zero);
  672. d.JointAttach(Amotor, Body, IntPtr.Zero);
  673. d.JointSetAMotorMode(Amotor, 0);
  674. d.JointSetAMotorNumAxes(Amotor, 3);
  675. d.JointSetAMotorAxis(Amotor, 0, 0, 1, 0, 0);
  676. d.JointSetAMotorAxis(Amotor, 1, 0, 0, 1, 0);
  677. d.JointSetAMotorAxis(Amotor, 2, 0, 0, 0, 1);
  678. d.JointSetAMotorAngle(Amotor, 0, 0);
  679. d.JointSetAMotorAngle(Amotor, 1, 0);
  680. d.JointSetAMotorAngle(Amotor, 2, 0);
  681. d.JointSetAMotorParam(Amotor, (int)dParam.StopCFM, 0f); // make it HARD
  682. d.JointSetAMotorParam(Amotor, (int)dParam.StopCFM2, 0f);
  683. d.JointSetAMotorParam(Amotor, (int)dParam.StopCFM3, 0f);
  684. d.JointSetAMotorParam(Amotor, (int)dParam.StopERP, 0.8f);
  685. d.JointSetAMotorParam(Amotor, (int)dParam.StopERP2, 0.8f);
  686. d.JointSetAMotorParam(Amotor, (int)dParam.StopERP3, 0.8f);
  687. // These lowstops and high stops are effectively (no wiggle room)
  688. d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, -1e-5f);
  689. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 1e-5f);
  690. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, -1e-5f);
  691. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 1e-5f);
  692. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -1e-5f);
  693. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 1e-5f);
  694. d.JointSetAMotorParam(Amotor, (int)d.JointParam.Vel, 0);
  695. d.JointSetAMotorParam(Amotor, (int)d.JointParam.Vel2, 0);
  696. d.JointSetAMotorParam(Amotor, (int)d.JointParam.Vel3, 0);
  697. d.JointSetAMotorParam(Amotor, (int)dParam.FMax, 5e8f);
  698. d.JointSetAMotorParam(Amotor, (int)dParam.FMax2, 5e8f);
  699. d.JointSetAMotorParam(Amotor, (int)dParam.FMax3, 5e8f);
  700. }
  701. /// <summary>
  702. /// Destroys the avatar body and geom
  703. private void AvatarGeomAndBodyDestroy()
  704. {
  705. // Kill the Amotor
  706. if (Amotor != IntPtr.Zero)
  707. {
  708. d.JointDestroy(Amotor);
  709. Amotor = IntPtr.Zero;
  710. }
  711. if (Body != IntPtr.Zero)
  712. {
  713. //kill the body
  714. d.BodyDestroy(Body);
  715. Body = IntPtr.Zero;
  716. }
  717. //kill the Geoms
  718. if (capsule != IntPtr.Zero)
  719. {
  720. m_parent_scene.actor_name_map.Remove(capsule);
  721. m_parent_scene.waitForSpaceUnlock(collider);
  722. d.GeomDestroy(capsule);
  723. capsule = IntPtr.Zero;
  724. }
  725. if (collider != IntPtr.Zero)
  726. {
  727. d.SpaceDestroy(collider);
  728. collider = IntPtr.Zero;
  729. }
  730. }
  731. //in place 2D rotation around Z assuming rot is normalised and is a rotation around Z
  732. public void RotateXYonZ(ref float x, ref float y, ref Quaternion rot)
  733. {
  734. float sin = 2.0f * rot.Z * rot.W;
  735. float cos = rot.W * rot.W - rot.Z * rot.Z;
  736. float tx = x;
  737. x = tx * cos - y * sin;
  738. y = tx * sin + y * cos;
  739. }
  740. public void RotateXYonZ(ref float x, ref float y, ref float sin, ref float cos)
  741. {
  742. float tx = x;
  743. x = tx * cos - y * sin;
  744. y = tx * sin + y * cos;
  745. }
  746. public void invRotateXYonZ(ref float x, ref float y, ref float sin, ref float cos)
  747. {
  748. float tx = x;
  749. x = tx * cos + y * sin;
  750. y = -tx * sin + y * cos;
  751. }
  752. public void invRotateXYonZ(ref float x, ref float y, ref Quaternion rot)
  753. {
  754. float sin = - 2.0f * rot.Z * rot.W;
  755. float cos = rot.W * rot.W - rot.Z * rot.Z;
  756. float tx = x;
  757. x = tx * cos - y * sin;
  758. y = tx * sin + y * cos;
  759. }
  760. public bool Collide(IntPtr me, IntPtr other, bool reverse, ref d.ContactGeom contact,
  761. ref d.ContactGeom altContact , ref bool useAltcontact, ref bool feetcollision)
  762. {
  763. feetcollision = false;
  764. useAltcontact = false;
  765. if (me == capsule)
  766. {
  767. Vector3 offset;
  768. float h = contact.pos.Z - _position.Z;
  769. offset.Z = h - feetOff;
  770. offset.X = contact.pos.X - _position.X;
  771. offset.Y = contact.pos.Y - _position.Y;
  772. d.GeomClassID gtype = d.GeomGetClass(other);
  773. if (gtype == d.GeomClassID.CapsuleClass)
  774. {
  775. Vector3 roff = offset * Quaternion.Inverse(m_orientation2D);
  776. float r = roff.X *roff.X / AvaAvaSizeXsq;
  777. r += (roff.Y * roff.Y) / AvaAvaSizeYsq;
  778. if (r > 1.0f)
  779. return false;
  780. float dp = 1.0f -(float)Math.Sqrt((double)r);
  781. if (dp > 0.05f)
  782. dp = 0.05f;
  783. contact.depth = dp;
  784. if (offset.Z < 0)
  785. {
  786. feetcollision = true;
  787. if (h < boneOff)
  788. {
  789. m_collideNormal.X = contact.normal.X;
  790. m_collideNormal.Y = contact.normal.Y;
  791. m_collideNormal.Z = contact.normal.Z;
  792. IsColliding = true;
  793. }
  794. }
  795. return true;
  796. }
  797. if (gtype == d.GeomClassID.SphereClass && d.GeomGetBody(other) != IntPtr.Zero)
  798. {
  799. if(d.GeomSphereGetRadius(other) < 0.5)
  800. return true;
  801. }
  802. if (offset.Z > 0 || contact.normal.Z > 0.35f)
  803. {
  804. if (offset.Z <= 0)
  805. {
  806. feetcollision = true;
  807. if (h < boneOff)
  808. {
  809. m_collideNormal.X = contact.normal.X;
  810. m_collideNormal.Y = contact.normal.Y;
  811. m_collideNormal.Z = contact.normal.Z;
  812. IsColliding = true;
  813. }
  814. }
  815. return true;
  816. }
  817. if(m_flying)
  818. return true;
  819. feetcollision = true;
  820. if (h < boneOff)
  821. {
  822. m_collideNormal.X = contact.normal.X;
  823. m_collideNormal.Y = contact.normal.Y;
  824. m_collideNormal.Z = contact.normal.Z;
  825. IsColliding = true;
  826. }
  827. altContact = contact;
  828. useAltcontact = true;
  829. offset.Z -= 0.2f;
  830. offset.Normalize();
  831. float tdp = contact.depth;
  832. float t = offset.X;
  833. t = Math.Abs(t);
  834. if(t > 1e-6)
  835. {
  836. tdp /= t;
  837. tdp *= contact.normal.X;
  838. }
  839. else
  840. tdp *= 10;
  841. if (tdp > 0.25f)
  842. tdp = 0.25f;
  843. altContact.depth = tdp;
  844. if (reverse)
  845. {
  846. altContact.normal.X = offset.X;
  847. altContact.normal.Y = offset.Y;
  848. altContact.normal.Z = offset.Z;
  849. }
  850. else
  851. {
  852. altContact.normal.X = -offset.X;
  853. altContact.normal.Y = -offset.Y;
  854. altContact.normal.Z = -offset.Z;
  855. }
  856. return true;
  857. }
  858. return false;
  859. }
  860. /// <summary>
  861. /// Called from Simulate
  862. /// This is the avatar's movement control + PID Controller
  863. /// </summary>
  864. /// <param name="timeStep"></param>
  865. public void Move(List<OdeCharacter> defects)
  866. {
  867. if (Body == IntPtr.Zero)
  868. return;
  869. d.Vector3 dtmp = d.BodyGetPosition(Body);
  870. Vector3 localpos = new Vector3(dtmp.X, dtmp.Y, dtmp.Z);
  871. // the Amotor still lets avatar rotation to drift during colisions
  872. // so force it back to identity
  873. d.Quaternion qtmp;
  874. qtmp.W = m_orientation2D.W;
  875. qtmp.X = m_orientation2D.X;
  876. qtmp.Y = m_orientation2D.Y;
  877. qtmp.Z = m_orientation2D.Z;
  878. d.BodySetQuaternion(Body, ref qtmp);
  879. if (m_pidControllerActive == false)
  880. {
  881. _zeroPosition = localpos;
  882. }
  883. if (!localpos.IsFinite())
  884. {
  885. m_log.Warn("[PHYSICS]: Avatar Position is non-finite!");
  886. defects.Add(this);
  887. // _parent_scene.RemoveCharacter(this);
  888. // destroy avatar capsule and related ODE data
  889. AvatarGeomAndBodyDestroy();
  890. return;
  891. }
  892. // check outbounds forcing to be in world
  893. bool fixbody = false;
  894. if (localpos.X < 0.0f)
  895. {
  896. fixbody = true;
  897. localpos.X = 0.1f;
  898. }
  899. else if (localpos.X > m_parent_scene.WorldExtents.X - 0.1f)
  900. {
  901. fixbody = true;
  902. localpos.X = m_parent_scene.WorldExtents.X - 0.1f;
  903. }
  904. if (localpos.Y < 0.0f)
  905. {
  906. fixbody = true;
  907. localpos.Y = 0.1f;
  908. }
  909. else if (localpos.Y > m_parent_scene.WorldExtents.Y - 0.1)
  910. {
  911. fixbody = true;
  912. localpos.Y = m_parent_scene.WorldExtents.Y - 0.1f;
  913. }
  914. if (fixbody)
  915. {
  916. m_freemove = false;
  917. d.BodySetPosition(Body, localpos.X, localpos.Y, localpos.Z);
  918. }
  919. float breakfactor;
  920. Vector3 vec = Vector3.Zero;
  921. dtmp = d.BodyGetLinearVel(Body);
  922. Vector3 vel = new Vector3(dtmp.X, dtmp.Y, dtmp.Z);
  923. float velLengthSquared = vel.LengthSquared();
  924. Vector3 ctz = _target_velocity;
  925. float movementdivisor = 1f;
  926. //Ubit change divisions into multiplications below
  927. if (!m_alwaysRun)
  928. movementdivisor = 1 / walkDivisor;
  929. else
  930. movementdivisor = 1 / runDivisor;
  931. ctz.X *= movementdivisor;
  932. ctz.Y *= movementdivisor;
  933. //******************************************
  934. // colide with land
  935. d.AABB aabb;
  936. // d.GeomGetAABB(feetbox, out aabb);
  937. d.GeomGetAABB(capsule, out aabb);
  938. float chrminZ = aabb.MinZ; // move up a bit
  939. Vector3 posch = localpos;
  940. float ftmp;
  941. if (m_flying)
  942. {
  943. ftmp = timeStep;
  944. posch.X += vel.X * ftmp;
  945. posch.Y += vel.Y * ftmp;
  946. }
  947. float terrainheight = m_parent_scene.GetTerrainHeightAtXY(posch.X, posch.Y);
  948. if (chrminZ < terrainheight)
  949. {
  950. if (ctz.Z < 0)
  951. ctz.Z = 0;
  952. if(!m_haveLastFallVel)
  953. {
  954. m_lastFallVel = vel;
  955. m_haveLastFallVel = true;
  956. }
  957. Vector3 n = m_parent_scene.GetTerrainNormalAtXY(posch.X, posch.Y);
  958. float depth = terrainheight - chrminZ;
  959. vec.Z = depth * PID_P * 50;
  960. if (!m_flying)
  961. {
  962. vec.Z += -vel.Z * PID_D;
  963. if(n.Z < 0.4f)
  964. {
  965. vec.X = depth * PID_P * 50 - vel.X * PID_D;
  966. vec.X *= n.X;
  967. vec.Y = depth * PID_P * 50 - vel.Y * PID_D;
  968. vec.Y *= n.Y;
  969. vec.Z *= n.Z;
  970. if(n.Z < 0.1f)
  971. {
  972. // cancel the slope pose
  973. n.X = 0f;
  974. n.Y = 0f;
  975. n.Z = 1.0f;
  976. }
  977. }
  978. }
  979. if (depth < 0.2f)
  980. {
  981. m_colliderGroundfilter++;
  982. if (m_colliderGroundfilter > 2)
  983. {
  984. m_iscolliding = true;
  985. m_colliderfilter = 2;
  986. if (m_colliderGroundfilter > 10)
  987. {
  988. m_colliderGroundfilter = 10;
  989. m_freemove = false;
  990. }
  991. m_collideNormal.X = n.X;
  992. m_collideNormal.Y = n.Y;
  993. m_collideNormal.Z = n.Z;
  994. m_iscollidingGround = true;
  995. ContactPoint contact = new ContactPoint();
  996. contact.PenetrationDepth = depth;
  997. contact.Position.X = localpos.X;
  998. contact.Position.Y = localpos.Y;
  999. contact.Position.Z = terrainheight;
  1000. contact.SurfaceNormal.X = -n.X;
  1001. contact.SurfaceNormal.Y = -n.Y;
  1002. contact.SurfaceNormal.Z = -n.Z;
  1003. contact.RelativeSpeed = Vector3.Dot(m_lastFallVel, n);
  1004. contact.CharacterFeet = true;
  1005. AddCollisionEvent(0, contact);
  1006. m_lastFallVel = vel;
  1007. // vec.Z *= 0.5f;
  1008. }
  1009. }
  1010. else
  1011. {
  1012. m_colliderGroundfilter -= 5;
  1013. if (m_colliderGroundfilter <= 0)
  1014. {
  1015. m_colliderGroundfilter = 0;
  1016. m_iscollidingGround = false;
  1017. }
  1018. }
  1019. }
  1020. else
  1021. {
  1022. m_haveLastFallVel = false;
  1023. m_colliderGroundfilter -= 5;
  1024. if (m_colliderGroundfilter <= 0)
  1025. {
  1026. m_colliderGroundfilter = 0;
  1027. m_iscollidingGround = false;
  1028. }
  1029. }
  1030. bool hoverPIDActive = false;
  1031. if (m_useHoverPID && m_PIDHoverTau != 0 && m_PIDHoverHeight != 0)
  1032. {
  1033. hoverPIDActive = true;
  1034. switch (m_PIDHoverType)
  1035. {
  1036. case PIDHoverType.Ground:
  1037. m_targetHoverHeight = terrainheight + m_PIDHoverHeight;
  1038. break;
  1039. case PIDHoverType.GroundAndWater:
  1040. float waterHeight = m_parent_scene.GetWaterLevel();
  1041. if (terrainheight > waterHeight)
  1042. m_targetHoverHeight = terrainheight + m_PIDHoverHeight;
  1043. else
  1044. m_targetHoverHeight = waterHeight + m_PIDHoverHeight;
  1045. break;
  1046. } // end switch (m_PIDHoverType)
  1047. // don't go underground
  1048. if (m_targetHoverHeight > terrainheight + 0.5f * (aabb.MaxZ - aabb.MinZ))
  1049. {
  1050. float fz = (m_targetHoverHeight - localpos.Z);
  1051. // if error is zero, use position control; otherwise, velocity control
  1052. if (Math.Abs(fz) < 0.01f)
  1053. {
  1054. ctz.Z = 0;
  1055. }
  1056. else
  1057. {
  1058. _zeroFlag = false;
  1059. fz /= m_PIDHoverTau;
  1060. float tmp = Math.Abs(fz);
  1061. if (tmp > 50)
  1062. fz = 50 * Math.Sign(fz);
  1063. else if (tmp < 0.1)
  1064. fz = 0.1f * Math.Sign(fz);
  1065. ctz.Z = fz;
  1066. }
  1067. }
  1068. }
  1069. //******************************************
  1070. if (!m_iscolliding)
  1071. m_collideNormal.Z = 0;
  1072. bool tviszero = (ctz.X == 0.0f && ctz.Y == 0.0f && ctz.Z == 0.0f);
  1073. if (!tviszero)
  1074. {
  1075. m_freemove = false;
  1076. // movement relative to surface if moving on it
  1077. // dont disturbe vertical movement, ie jumps
  1078. if (m_iscolliding && !m_flying && ctz.Z == 0 && m_collideNormal.Z > 0.2f && m_collideNormal.Z < 0.94f)
  1079. {
  1080. float p = ctz.X * m_collideNormal.X + ctz.Y * m_collideNormal.Y;
  1081. ctz.X *= (float)Math.Sqrt(1 - m_collideNormal.X * m_collideNormal.X);
  1082. ctz.Y *= (float)Math.Sqrt(1 - m_collideNormal.Y * m_collideNormal.Y);
  1083. ctz.Z -= p;
  1084. if (ctz.Z < 0)
  1085. ctz.Z *= 2;
  1086. }
  1087. }
  1088. if (!m_freemove)
  1089. {
  1090. // if velocity is zero, use position control; otherwise, velocity control
  1091. if (tviszero && m_iscolliding && !m_flying)
  1092. {
  1093. // keep track of where we stopped. No more slippin' & slidin'
  1094. if (!_zeroFlag)
  1095. {
  1096. _zeroFlag = true;
  1097. _zeroPosition = localpos;
  1098. }
  1099. if (m_pidControllerActive)
  1100. {
  1101. // We only want to deactivate the PID Controller if we think we want to have our surrogate
  1102. // react to the physics scene by moving it's position.
  1103. // Avatar to Avatar collisions
  1104. // Prim to avatar collisions
  1105. vec.X = -vel.X * PID_D * 2f + (_zeroPosition.X - localpos.X) * (PID_P * 5);
  1106. vec.Y = -vel.Y * PID_D * 2f + (_zeroPosition.Y - localpos.Y) * (PID_P * 5);
  1107. if(vel.Z > 0)
  1108. vec.Z += -vel.Z * PID_D + (_zeroPosition.Z - localpos.Z) * PID_P;
  1109. else
  1110. vec.Z += (-vel.Z * PID_D + (_zeroPosition.Z - localpos.Z) * PID_P) * 0.2f;
  1111. /*
  1112. if (flying)
  1113. {
  1114. vec.Z += -vel.Z * PID_D + (_zeroPosition.Z - localpos.Z) * PID_P;
  1115. }
  1116. */
  1117. }
  1118. //PidStatus = true;
  1119. }
  1120. else
  1121. {
  1122. m_pidControllerActive = true;
  1123. _zeroFlag = false;
  1124. if (m_iscolliding)
  1125. {
  1126. if (!m_flying)
  1127. {
  1128. // we are on a surface
  1129. if (ctz.Z > 0f)
  1130. {
  1131. // moving up or JUMPING
  1132. vec.Z += (ctz.Z - vel.Z) * PID_D * 2f;
  1133. vec.X += (ctz.X - vel.X) * (PID_D);
  1134. vec.Y += (ctz.Y - vel.Y) * (PID_D);
  1135. }
  1136. else
  1137. {
  1138. // we are moving down on a surface
  1139. if (ctz.Z == 0)
  1140. {
  1141. if (vel.Z > 0)
  1142. vec.Z -= vel.Z * PID_D * 2f;
  1143. vec.X += (ctz.X - vel.X) * (PID_D);
  1144. vec.Y += (ctz.Y - vel.Y) * (PID_D);
  1145. }
  1146. // intencionally going down
  1147. else
  1148. {
  1149. if (ctz.Z < vel.Z)
  1150. vec.Z += (ctz.Z - vel.Z) * PID_D;
  1151. else
  1152. {
  1153. }
  1154. if (Math.Abs(ctz.X) > Math.Abs(vel.X))
  1155. vec.X += (ctz.X - vel.X) * (PID_D);
  1156. if (Math.Abs(ctz.Y) > Math.Abs(vel.Y))
  1157. vec.Y += (ctz.Y - vel.Y) * (PID_D);
  1158. }
  1159. }
  1160. // We're standing on something
  1161. }
  1162. else
  1163. {
  1164. // We're flying and colliding with something
  1165. vec.X += (ctz.X - vel.X) * (PID_D * 0.0625f);
  1166. vec.Y += (ctz.Y - vel.Y) * (PID_D * 0.0625f);
  1167. vec.Z += (ctz.Z - vel.Z) * (PID_D * 0.0625f);
  1168. }
  1169. }
  1170. else // ie not colliding
  1171. {
  1172. if (m_flying || hoverPIDActive) //(!m_iscolliding && flying)
  1173. {
  1174. // we're in mid air suspended
  1175. vec.X += (ctz.X - vel.X) * (PID_D);
  1176. vec.Y += (ctz.Y - vel.Y) * (PID_D);
  1177. vec.Z += (ctz.Z - vel.Z) * (PID_D);
  1178. }
  1179. else
  1180. {
  1181. // we're not colliding and we're not flying so that means we're falling!
  1182. // m_iscolliding includes collisions with the ground.
  1183. // d.Vector3 pos = d.BodyGetPosition(Body);
  1184. vec.X += (ctz.X - vel.X) * PID_D * 0.833f;
  1185. vec.Y += (ctz.Y - vel.Y) * PID_D * 0.833f;
  1186. // hack for breaking on fall
  1187. if (ctz.Z == -9999f)
  1188. vec.Z += -vel.Z * PID_D - m_parent_scene.gravityz * m_mass;
  1189. }
  1190. }
  1191. }
  1192. if (velLengthSquared > 2500.0f) // 50m/s apply breaks
  1193. {
  1194. breakfactor = 0.16f * m_mass;
  1195. vec.X -= breakfactor * vel.X;
  1196. vec.Y -= breakfactor * vel.Y;
  1197. vec.Z -= breakfactor * vel.Z;
  1198. }
  1199. }
  1200. else
  1201. {
  1202. breakfactor = m_mass;
  1203. vec.X -= breakfactor * vel.X;
  1204. vec.Y -= breakfactor * vel.Y;
  1205. if (m_flying)
  1206. vec.Z -= 0.5f * breakfactor * vel.Z;
  1207. else
  1208. vec.Z -= .16f* m_mass * vel.Z;
  1209. }
  1210. if (m_flying || hoverPIDActive)
  1211. {
  1212. vec.Z -= m_parent_scene.gravityz * m_mass;
  1213. if(!hoverPIDActive)
  1214. {
  1215. //Added for auto fly height. Kitto Flora
  1216. float target_altitude = terrainheight + MinimumGroundFlightOffset;
  1217. if (localpos.Z < target_altitude)
  1218. {
  1219. vec.Z += (target_altitude - localpos.Z) * PID_P * 5.0f;
  1220. }
  1221. // end add Kitto Flora
  1222. }
  1223. }
  1224. if (vec.IsFinite())
  1225. {
  1226. if (vec.X != 0 || vec.Y !=0 || vec.Z !=0)
  1227. d.BodyAddForce(Body, vec.X, vec.Y, vec.Z);
  1228. }
  1229. else
  1230. {
  1231. m_log.Warn("[PHYSICS]: Got a NaN force vector in Move()");
  1232. m_log.Warn("[PHYSICS]: Avatar Position is non-finite!");
  1233. defects.Add(this);
  1234. // _parent_scene.RemoveCharacter(this);
  1235. // destroy avatar capsule and related ODE data
  1236. AvatarGeomAndBodyDestroy();
  1237. return;
  1238. }
  1239. // update our local ideia of position velocity and aceleration
  1240. // _position = localpos;
  1241. _position = localpos;
  1242. if (_zeroFlag)
  1243. {
  1244. _velocity = Vector3.Zero;
  1245. _acceleration = Vector3.Zero;
  1246. m_rotationalVelocity = Vector3.Zero;
  1247. }
  1248. else
  1249. {
  1250. Vector3 a =_velocity; // previus velocity
  1251. SetSmooth(ref _velocity, ref vel, 2);
  1252. a = (_velocity - a) * invtimeStep;
  1253. SetSmooth(ref _acceleration, ref a, 2);
  1254. dtmp = d.BodyGetAngularVel(Body);
  1255. m_rotationalVelocity.X = 0f;
  1256. m_rotationalVelocity.Y = 0f;
  1257. m_rotationalVelocity.Z = dtmp.Z;
  1258. Math.Round(m_rotationalVelocity.Z,3);
  1259. }
  1260. }
  1261. public void round(ref Vector3 v, int digits)
  1262. {
  1263. v.X = (float)Math.Round(v.X, digits);
  1264. v.Y = (float)Math.Round(v.Y, digits);
  1265. v.Z = (float)Math.Round(v.Z, digits);
  1266. }
  1267. public void SetSmooth(ref Vector3 dst, ref Vector3 value)
  1268. {
  1269. dst.X = 0.1f * dst.X + 0.9f * value.X;
  1270. dst.Y = 0.1f * dst.Y + 0.9f * value.Y;
  1271. dst.Z = 0.1f * dst.Z + 0.9f * value.Z;
  1272. }
  1273. public void SetSmooth(ref Vector3 dst, ref Vector3 value, int rounddigits)
  1274. {
  1275. dst.X = 0.4f * dst.X + 0.6f * value.X;
  1276. dst.X = (float)Math.Round(dst.X, rounddigits);
  1277. dst.Y = 0.4f * dst.Y + 0.6f * value.Y;
  1278. dst.Y = (float)Math.Round(dst.Y, rounddigits);
  1279. dst.Z = 0.4f * dst.Z + 0.6f * value.Z;
  1280. dst.Z = (float)Math.Round(dst.Z, rounddigits);
  1281. }
  1282. /// <summary>
  1283. /// Updates the reported position and velocity.
  1284. /// Used to copy variables from unmanaged space at heartbeat rate and also trigger scene updates acording
  1285. /// also outbounds checking
  1286. /// copy and outbounds now done in move(..) at ode rate
  1287. ///
  1288. /// </summary>
  1289. public void UpdatePositionAndVelocity()
  1290. {
  1291. return;
  1292. // if (Body == IntPtr.Zero)
  1293. // return;
  1294. }
  1295. /// <summary>
  1296. /// Cleanup the things we use in the scene.
  1297. /// </summary>
  1298. public void Destroy()
  1299. {
  1300. AddChange(changes.Remove, null);
  1301. }
  1302. public override void CrossingFailure()
  1303. {
  1304. }
  1305. public override Vector3 PIDTarget { set { return; } }
  1306. public override bool PIDActive {get {return m_pidControllerActive;} set { return; } }
  1307. public override float PIDTau { set { return; } }
  1308. public override float PIDHoverHeight
  1309. {
  1310. set
  1311. {
  1312. AddChange(changes.PIDHoverHeight,value);
  1313. }
  1314. }
  1315. public override bool PIDHoverActive
  1316. {
  1317. get
  1318. {
  1319. return m_useHoverPID;
  1320. }
  1321. set
  1322. {
  1323. AddChange(changes.PIDHoverActive, value);
  1324. }
  1325. }
  1326. public override PIDHoverType PIDHoverType
  1327. {
  1328. set
  1329. {
  1330. AddChange(changes.PIDHoverType,value);
  1331. }
  1332. }
  1333. public override float PIDHoverTau
  1334. {
  1335. set
  1336. {
  1337. float tmp =0;
  1338. if (value > 0)
  1339. {
  1340. float mint = (0.05f > timeStep ? 0.05f : timeStep);
  1341. if (value < mint)
  1342. tmp = mint;
  1343. else
  1344. tmp = value;
  1345. }
  1346. AddChange(changes.PIDHoverTau, tmp);
  1347. }
  1348. }
  1349. public override Quaternion APIDTarget { set { return; } }
  1350. public override bool APIDActive { set { return; } }
  1351. public override float APIDStrength { set { return; } }
  1352. public override float APIDDamping { set { return; } }
  1353. public override void SubscribeEvents(int ms)
  1354. {
  1355. m_eventsubscription = ms;
  1356. m_cureventsubscription = 0;
  1357. CollisionEventsThisFrame.Clear();
  1358. SentEmptyCollisionsEvent = false;
  1359. }
  1360. public override void UnSubscribeEvents()
  1361. {
  1362. m_eventsubscription = 0;
  1363. m_parent_scene.RemoveCollisionEventReporting(this);
  1364. lock(CollisionEventsThisFrame)
  1365. CollisionEventsThisFrame.Clear();
  1366. }
  1367. public override void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
  1368. {
  1369. lock(CollisionEventsThisFrame)
  1370. CollisionEventsThisFrame.AddCollider(CollidedWith, contact);
  1371. m_parent_scene.AddCollisionEventReporting(this);
  1372. }
  1373. public void SendCollisions(int timestep)
  1374. {
  1375. if (m_cureventsubscription < 50000)
  1376. m_cureventsubscription += timestep;
  1377. if (m_cureventsubscription < m_eventsubscription)
  1378. return;
  1379. lock(CollisionEventsThisFrame)
  1380. {
  1381. int ncolisions = CollisionEventsThisFrame.m_objCollisionList.Count;
  1382. if (!SentEmptyCollisionsEvent || ncolisions > 0)
  1383. {
  1384. base.SendCollisionUpdate(CollisionEventsThisFrame);
  1385. m_cureventsubscription = 0;
  1386. if (ncolisions == 0)
  1387. {
  1388. SentEmptyCollisionsEvent = true;
  1389. // _parent_scene.RemoveCollisionEventReporting(this);
  1390. }
  1391. else
  1392. {
  1393. SentEmptyCollisionsEvent = false;
  1394. CollisionEventsThisFrame.Clear();
  1395. }
  1396. }
  1397. }
  1398. }
  1399. public override bool SubscribedEvents()
  1400. {
  1401. if (m_eventsubscription > 0)
  1402. return true;
  1403. return false;
  1404. }
  1405. private void changePhysicsStatus(bool NewStatus)
  1406. {
  1407. if (NewStatus != m_isPhysical)
  1408. {
  1409. if (NewStatus)
  1410. {
  1411. AvatarGeomAndBodyDestroy();
  1412. AvatarGeomAndBodyCreation(_position.X, _position.Y, _position.Z);
  1413. m_parent_scene.actor_name_map[collider] = (PhysicsActor)this;
  1414. m_parent_scene.actor_name_map[capsule] = (PhysicsActor)this;
  1415. m_parent_scene.AddCharacter(this);
  1416. }
  1417. else
  1418. {
  1419. m_parent_scene.RemoveCollisionEventReporting(this);
  1420. m_parent_scene.RemoveCharacter(this);
  1421. // destroy avatar capsule and related ODE data
  1422. AvatarGeomAndBodyDestroy();
  1423. }
  1424. m_freemove = false;
  1425. m_isPhysical = NewStatus;
  1426. }
  1427. }
  1428. private void changeAdd()
  1429. {
  1430. changePhysicsStatus(true);
  1431. }
  1432. private void changeRemove()
  1433. {
  1434. changePhysicsStatus(false);
  1435. }
  1436. private void changeShape(PrimitiveBaseShape arg)
  1437. {
  1438. }
  1439. private void changeAvatarSize(strAvatarSize st)
  1440. {
  1441. m_feetOffset = st.offset;
  1442. changeSize(st.size);
  1443. }
  1444. private void changeSize(Vector3 pSize)
  1445. {
  1446. if (pSize.IsFinite())
  1447. {
  1448. // for now only look to Z changes since viewers also don't change X and Y
  1449. if (pSize.Z != m_size.Z)
  1450. {
  1451. AvatarGeomAndBodyDestroy();
  1452. float oldsz = m_size.Z;
  1453. m_size = pSize;
  1454. AvatarGeomAndBodyCreation(_position.X, _position.Y,
  1455. _position.Z + (m_size.Z - oldsz) * 0.5f);
  1456. // Velocity = Vector3.Zero;
  1457. m_targetVelocity = Vector3.Zero;
  1458. m_parent_scene.actor_name_map[collider] = (PhysicsActor)this;
  1459. m_parent_scene.actor_name_map[capsule] = (PhysicsActor)this;
  1460. }
  1461. m_freemove = false;
  1462. m_pidControllerActive = true;
  1463. }
  1464. else
  1465. {
  1466. m_log.Warn("[PHYSICS]: Got a NaN Size from Scene on a Character");
  1467. }
  1468. }
  1469. private void changePosition( Vector3 newPos)
  1470. {
  1471. if (Body != IntPtr.Zero)
  1472. d.BodySetPosition(Body, newPos.X, newPos.Y, newPos.Z);
  1473. _position = newPos;
  1474. m_freemove = false;
  1475. m_pidControllerActive = true;
  1476. }
  1477. private void changeOrientation(Quaternion newOri)
  1478. {
  1479. if (m_orientation != newOri)
  1480. {
  1481. m_orientation = newOri; // keep a copy for core use
  1482. // but only use rotations around Z
  1483. m_orientation2D.W = newOri.W;
  1484. m_orientation2D.Z = newOri.Z;
  1485. float t = m_orientation2D.W * m_orientation2D.W + m_orientation2D.Z * m_orientation2D.Z;
  1486. if (t > 0)
  1487. {
  1488. t = 1.0f / (float)Math.Sqrt(t);
  1489. m_orientation2D.W *= t;
  1490. m_orientation2D.Z *= t;
  1491. }
  1492. else
  1493. {
  1494. m_orientation2D.W = 1.0f;
  1495. m_orientation2D.Z = 0f;
  1496. }
  1497. m_orientation2D.Y = 0f;
  1498. m_orientation2D.X = 0f;
  1499. d.Quaternion myrot = new d.Quaternion();
  1500. myrot.X = m_orientation2D.X;
  1501. myrot.Y = m_orientation2D.Y;
  1502. myrot.Z = m_orientation2D.Z;
  1503. myrot.W = m_orientation2D.W;
  1504. d.BodySetQuaternion(Body, ref myrot);
  1505. }
  1506. }
  1507. private void changeVelocity(Vector3 newVel)
  1508. {
  1509. _velocity = newVel;
  1510. setFreeMove();
  1511. if (Body != IntPtr.Zero)
  1512. d.BodySetLinearVel(Body, newVel.X, newVel.Y, newVel.Z);
  1513. }
  1514. private void changeTargetVelocity(Vector3 newVel)
  1515. {
  1516. m_pidControllerActive = true;
  1517. m_freemove = false;
  1518. _target_velocity = newVel;
  1519. }
  1520. private void changeSetTorque(Vector3 newTorque)
  1521. {
  1522. }
  1523. private void changeAddForce(Vector3 newForce)
  1524. {
  1525. }
  1526. private void changeAddAngularForce(Vector3 arg)
  1527. {
  1528. }
  1529. private void changeAngularLock(byte arg)
  1530. {
  1531. }
  1532. private void changeFloatOnWater(bool arg)
  1533. {
  1534. }
  1535. private void changeVolumedetetion(bool arg)
  1536. {
  1537. }
  1538. private void changeSelectedStatus(bool arg)
  1539. {
  1540. }
  1541. private void changeDisable(bool arg)
  1542. {
  1543. }
  1544. private void changeBuilding(bool arg)
  1545. {
  1546. }
  1547. private void setFreeMove()
  1548. {
  1549. m_pidControllerActive = true;
  1550. _zeroFlag = false;
  1551. _target_velocity = Vector3.Zero;
  1552. m_freemove = true;
  1553. m_colliderfilter = -1;
  1554. m_colliderObjectfilter = -1;
  1555. m_colliderGroundfilter = -1;
  1556. m_iscolliding = false;
  1557. m_iscollidingGround = false;
  1558. m_iscollidingObj = false;
  1559. CollisionEventsThisFrame.Clear();
  1560. }
  1561. private void changeForce(Vector3 newForce)
  1562. {
  1563. setFreeMove();
  1564. if (Body != IntPtr.Zero)
  1565. {
  1566. if (newForce.X != 0f || newForce.Y != 0f || newForce.Z != 0)
  1567. d.BodyAddForce(Body, newForce.X, newForce.Y, newForce.Z);
  1568. }
  1569. }
  1570. // for now momentum is actually velocity
  1571. private void changeMomentum(Vector3 newmomentum)
  1572. {
  1573. _velocity = newmomentum;
  1574. setFreeMove();
  1575. if (Body != IntPtr.Zero)
  1576. d.BodySetLinearVel(Body, newmomentum.X, newmomentum.Y, newmomentum.Z);
  1577. }
  1578. private void changePIDHoverHeight(float val)
  1579. {
  1580. m_PIDHoverHeight = val;
  1581. if (val == 0)
  1582. m_useHoverPID = false;
  1583. }
  1584. private void changePIDHoverType(PIDHoverType type)
  1585. {
  1586. m_PIDHoverType = type;
  1587. }
  1588. private void changePIDHoverTau(float tau)
  1589. {
  1590. m_PIDHoverTau = tau;
  1591. }
  1592. private void changePIDHoverActive(bool active)
  1593. {
  1594. m_useHoverPID = active;
  1595. }
  1596. private void donullchange()
  1597. {
  1598. }
  1599. public bool DoAChange(changes what, object arg)
  1600. {
  1601. if (collider == IntPtr.Zero && what != changes.Add && what != changes.Remove)
  1602. {
  1603. return false;
  1604. }
  1605. // nasty switch
  1606. switch (what)
  1607. {
  1608. case changes.Add:
  1609. changeAdd();
  1610. break;
  1611. case changes.Remove:
  1612. changeRemove();
  1613. break;
  1614. case changes.Position:
  1615. changePosition((Vector3)arg);
  1616. break;
  1617. case changes.Orientation:
  1618. changeOrientation((Quaternion)arg);
  1619. break;
  1620. case changes.PosOffset:
  1621. donullchange();
  1622. break;
  1623. case changes.OriOffset:
  1624. donullchange();
  1625. break;
  1626. case changes.Velocity:
  1627. changeVelocity((Vector3)arg);
  1628. break;
  1629. case changes.TargetVelocity:
  1630. changeTargetVelocity((Vector3)arg);
  1631. break;
  1632. // case changes.Acceleration:
  1633. // changeacceleration((Vector3)arg);
  1634. // break;
  1635. // case changes.AngVelocity:
  1636. // changeangvelocity((Vector3)arg);
  1637. // break;
  1638. case changes.Force:
  1639. changeForce((Vector3)arg);
  1640. break;
  1641. case changes.Torque:
  1642. changeSetTorque((Vector3)arg);
  1643. break;
  1644. case changes.AddForce:
  1645. changeAddForce((Vector3)arg);
  1646. break;
  1647. case changes.AddAngForce:
  1648. changeAddAngularForce((Vector3)arg);
  1649. break;
  1650. case changes.AngLock:
  1651. changeAngularLock((byte)arg);
  1652. break;
  1653. case changes.Size:
  1654. changeSize((Vector3)arg);
  1655. break;
  1656. case changes.AvatarSize:
  1657. changeAvatarSize((strAvatarSize)arg);
  1658. break;
  1659. case changes.Momentum:
  1660. changeMomentum((Vector3)arg);
  1661. break;
  1662. case changes.PIDHoverHeight:
  1663. changePIDHoverHeight((float)arg);
  1664. break;
  1665. case changes.PIDHoverType:
  1666. changePIDHoverType((PIDHoverType)arg);
  1667. break;
  1668. case changes.PIDHoverTau:
  1669. changePIDHoverTau((float)arg);
  1670. break;
  1671. case changes.PIDHoverActive:
  1672. changePIDHoverActive((bool)arg);
  1673. break;
  1674. /* not in use for now
  1675. case changes.Shape:
  1676. changeShape((PrimitiveBaseShape)arg);
  1677. break;
  1678. case changes.CollidesWater:
  1679. changeFloatOnWater((bool)arg);
  1680. break;
  1681. case changes.VolumeDtc:
  1682. changeVolumedetetion((bool)arg);
  1683. break;
  1684. case changes.Physical:
  1685. changePhysicsStatus((bool)arg);
  1686. break;
  1687. case changes.Selected:
  1688. changeSelectedStatus((bool)arg);
  1689. break;
  1690. case changes.disabled:
  1691. changeDisable((bool)arg);
  1692. break;
  1693. case changes.building:
  1694. changeBuilding((bool)arg);
  1695. break;
  1696. */
  1697. case changes.Null:
  1698. donullchange();
  1699. break;
  1700. default:
  1701. donullchange();
  1702. break;
  1703. }
  1704. return false;
  1705. }
  1706. public void AddChange(changes what, object arg)
  1707. {
  1708. m_parent_scene.AddChange((PhysicsActor)this, what, arg);
  1709. }
  1710. private struct strAvatarSize
  1711. {
  1712. public Vector3 size;
  1713. public float offset;
  1714. }
  1715. }
  1716. }