ODECharacter.cs 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  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 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 flying; }
  250. set
  251. {
  252. 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. /*
  798. d.AABB aabb;
  799. d.GeomGetAABB(other,out aabb);
  800. float othertop = aabb.MaxZ - _position.Z;
  801. */
  802. // if (offset.Z > 0 || othertop > -feetOff || contact.normal.Z > 0.35f)
  803. if (offset.Z > 0 || contact.normal.Z > 0.35f)
  804. {
  805. if (offset.Z <= 0)
  806. {
  807. feetcollision = true;
  808. if (h < boneOff)
  809. {
  810. m_collideNormal.X = contact.normal.X;
  811. m_collideNormal.Y = contact.normal.Y;
  812. m_collideNormal.Z = contact.normal.Z;
  813. IsColliding = true;
  814. }
  815. }
  816. return true;
  817. }
  818. altContact = contact;
  819. useAltcontact = true;
  820. offset.Z -= 0.2f;
  821. offset.Normalize();
  822. if (contact.depth > 0.1f)
  823. contact.depth = 0.1f;
  824. if (reverse)
  825. {
  826. altContact.normal.X = offset.X;
  827. altContact.normal.Y = offset.Y;
  828. altContact.normal.Z = offset.Z;
  829. }
  830. else
  831. {
  832. altContact.normal.X = -offset.X;
  833. altContact.normal.Y = -offset.Y;
  834. altContact.normal.Z = -offset.Z;
  835. }
  836. feetcollision = true;
  837. if (h < boneOff)
  838. {
  839. m_collideNormal.X = contact.normal.X;
  840. m_collideNormal.Y = contact.normal.Y;
  841. m_collideNormal.Z = contact.normal.Z;
  842. IsColliding = true;
  843. }
  844. return true;
  845. }
  846. return false;
  847. }
  848. /// <summary>
  849. /// Called from Simulate
  850. /// This is the avatar's movement control + PID Controller
  851. /// </summary>
  852. /// <param name="timeStep"></param>
  853. public void Move(List<OdeCharacter> defects)
  854. {
  855. if (Body == IntPtr.Zero)
  856. return;
  857. d.Vector3 dtmp = d.BodyGetPosition(Body);
  858. Vector3 localpos = new Vector3(dtmp.X, dtmp.Y, dtmp.Z);
  859. // the Amotor still lets avatar rotation to drift during colisions
  860. // so force it back to identity
  861. d.Quaternion qtmp;
  862. qtmp.W = m_orientation2D.W;
  863. qtmp.X = m_orientation2D.X;
  864. qtmp.Y = m_orientation2D.Y;
  865. qtmp.Z = m_orientation2D.Z;
  866. d.BodySetQuaternion(Body, ref qtmp);
  867. if (m_pidControllerActive == false)
  868. {
  869. _zeroPosition = localpos;
  870. }
  871. if (!localpos.IsFinite())
  872. {
  873. m_log.Warn("[PHYSICS]: Avatar Position is non-finite!");
  874. defects.Add(this);
  875. // _parent_scene.RemoveCharacter(this);
  876. // destroy avatar capsule and related ODE data
  877. AvatarGeomAndBodyDestroy();
  878. return;
  879. }
  880. // check outbounds forcing to be in world
  881. bool fixbody = false;
  882. if (localpos.X < 0.0f)
  883. {
  884. fixbody = true;
  885. localpos.X = 0.1f;
  886. }
  887. else if (localpos.X > m_parent_scene.WorldExtents.X - 0.1f)
  888. {
  889. fixbody = true;
  890. localpos.X = m_parent_scene.WorldExtents.X - 0.1f;
  891. }
  892. if (localpos.Y < 0.0f)
  893. {
  894. fixbody = true;
  895. localpos.Y = 0.1f;
  896. }
  897. else if (localpos.Y > m_parent_scene.WorldExtents.Y - 0.1)
  898. {
  899. fixbody = true;
  900. localpos.Y = m_parent_scene.WorldExtents.Y - 0.1f;
  901. }
  902. if (fixbody)
  903. {
  904. m_freemove = false;
  905. d.BodySetPosition(Body, localpos.X, localpos.Y, localpos.Z);
  906. }
  907. float breakfactor;
  908. Vector3 vec = Vector3.Zero;
  909. dtmp = d.BodyGetLinearVel(Body);
  910. Vector3 vel = new Vector3(dtmp.X, dtmp.Y, dtmp.Z);
  911. float velLengthSquared = vel.LengthSquared();
  912. Vector3 ctz = _target_velocity;
  913. float movementdivisor = 1f;
  914. //Ubit change divisions into multiplications below
  915. if (!m_alwaysRun)
  916. movementdivisor = 1 / walkDivisor;
  917. else
  918. movementdivisor = 1 / runDivisor;
  919. ctz.X *= movementdivisor;
  920. ctz.Y *= movementdivisor;
  921. //******************************************
  922. // colide with land
  923. d.AABB aabb;
  924. // d.GeomGetAABB(feetbox, out aabb);
  925. d.GeomGetAABB(capsule, out aabb);
  926. float chrminZ = aabb.MinZ; // move up a bit
  927. Vector3 posch = localpos;
  928. float ftmp;
  929. if (flying)
  930. {
  931. ftmp = timeStep;
  932. posch.X += vel.X * ftmp;
  933. posch.Y += vel.Y * ftmp;
  934. }
  935. float terrainheight = m_parent_scene.GetTerrainHeightAtXY(posch.X, posch.Y);
  936. if (chrminZ < terrainheight)
  937. {
  938. if (ctz.Z < 0)
  939. ctz.Z = 0;
  940. if(!m_haveLastFallVel)
  941. {
  942. m_lastFallVel = vel;
  943. m_haveLastFallVel = true;
  944. }
  945. Vector3 n = m_parent_scene.GetTerrainNormalAtXY(posch.X, posch.Y);
  946. float depth = terrainheight - chrminZ;
  947. vec.Z = depth * PID_P * 50;
  948. if (!flying)
  949. {
  950. vec.Z += -vel.Z * PID_D;
  951. if(n.Z < 0.4f)
  952. {
  953. vec.X = depth * PID_P * 50 - vel.X * PID_D;
  954. vec.X *= n.X;
  955. vec.Y = depth * PID_P * 50 - vel.Y * PID_D;
  956. vec.Y *= n.Y;
  957. vec.Z *= n.Z;
  958. if(n.Z < 0.1f)
  959. {
  960. // cancel the slope pose
  961. n.X = 0f;
  962. n.Y = 0f;
  963. n.Z = 1.0f;
  964. }
  965. }
  966. }
  967. if (depth < 0.2f)
  968. {
  969. m_colliderGroundfilter++;
  970. if (m_colliderGroundfilter > 2)
  971. {
  972. m_iscolliding = true;
  973. m_colliderfilter = 2;
  974. if (m_colliderGroundfilter > 10)
  975. {
  976. m_colliderGroundfilter = 10;
  977. m_freemove = false;
  978. }
  979. m_collideNormal.X = n.X;
  980. m_collideNormal.Y = n.Y;
  981. m_collideNormal.Z = n.Z;
  982. m_iscollidingGround = true;
  983. ContactPoint contact = new ContactPoint();
  984. contact.PenetrationDepth = depth;
  985. contact.Position.X = localpos.X;
  986. contact.Position.Y = localpos.Y;
  987. contact.Position.Z = terrainheight;
  988. contact.SurfaceNormal.X = -n.X;
  989. contact.SurfaceNormal.Y = -n.Y;
  990. contact.SurfaceNormal.Z = -n.Z;
  991. contact.RelativeSpeed = Vector3.Dot(m_lastFallVel, n);
  992. contact.CharacterFeet = true;
  993. AddCollisionEvent(0, contact);
  994. m_lastFallVel = vel;
  995. // vec.Z *= 0.5f;
  996. }
  997. }
  998. else
  999. {
  1000. m_colliderGroundfilter -= 5;
  1001. if (m_colliderGroundfilter <= 0)
  1002. {
  1003. m_colliderGroundfilter = 0;
  1004. m_iscollidingGround = false;
  1005. }
  1006. }
  1007. }
  1008. else
  1009. {
  1010. m_haveLastFallVel = false;
  1011. m_colliderGroundfilter -= 5;
  1012. if (m_colliderGroundfilter <= 0)
  1013. {
  1014. m_colliderGroundfilter = 0;
  1015. m_iscollidingGround = false;
  1016. }
  1017. }
  1018. bool hoverPIDActive = false;
  1019. if (m_useHoverPID && m_PIDHoverTau != 0 && m_PIDHoverHeight != 0)
  1020. {
  1021. hoverPIDActive = true;
  1022. switch (m_PIDHoverType)
  1023. {
  1024. case PIDHoverType.Ground:
  1025. m_targetHoverHeight = terrainheight + m_PIDHoverHeight;
  1026. break;
  1027. case PIDHoverType.GroundAndWater:
  1028. float waterHeight = m_parent_scene.GetWaterLevel();
  1029. if (terrainheight > waterHeight)
  1030. m_targetHoverHeight = terrainheight + m_PIDHoverHeight;
  1031. else
  1032. m_targetHoverHeight = waterHeight + m_PIDHoverHeight;
  1033. break;
  1034. } // end switch (m_PIDHoverType)
  1035. // don't go underground
  1036. if (m_targetHoverHeight > terrainheight + 0.5f * (aabb.MaxZ - aabb.MinZ))
  1037. {
  1038. float fz = (m_targetHoverHeight - localpos.Z);
  1039. // if error is zero, use position control; otherwise, velocity control
  1040. if (Math.Abs(fz) < 0.01f)
  1041. {
  1042. ctz.Z = 0;
  1043. }
  1044. else
  1045. {
  1046. _zeroFlag = false;
  1047. fz /= m_PIDHoverTau;
  1048. float tmp = Math.Abs(fz);
  1049. if (tmp > 50)
  1050. fz = 50 * Math.Sign(fz);
  1051. else if (tmp < 0.1)
  1052. fz = 0.1f * Math.Sign(fz);
  1053. ctz.Z = fz;
  1054. }
  1055. }
  1056. }
  1057. //******************************************
  1058. if (!m_iscolliding)
  1059. m_collideNormal.Z = 0;
  1060. bool tviszero = (ctz.X == 0.0f && ctz.Y == 0.0f && ctz.Z == 0.0f);
  1061. if (!tviszero)
  1062. {
  1063. m_freemove = false;
  1064. // movement relative to surface if moving on it
  1065. // dont disturbe vertical movement, ie jumps
  1066. if (m_iscolliding && !flying && ctz.Z == 0 && m_collideNormal.Z > 0.2f && m_collideNormal.Z < 0.94f)
  1067. {
  1068. float p = ctz.X * m_collideNormal.X + ctz.Y * m_collideNormal.Y;
  1069. ctz.X *= (float)Math.Sqrt(1 - m_collideNormal.X * m_collideNormal.X);
  1070. ctz.Y *= (float)Math.Sqrt(1 - m_collideNormal.Y * m_collideNormal.Y);
  1071. ctz.Z -= p;
  1072. if (ctz.Z < 0)
  1073. ctz.Z *= 2;
  1074. }
  1075. }
  1076. if (!m_freemove)
  1077. {
  1078. // if velocity is zero, use position control; otherwise, velocity control
  1079. if (tviszero && m_iscolliding && !flying)
  1080. {
  1081. // keep track of where we stopped. No more slippin' & slidin'
  1082. if (!_zeroFlag)
  1083. {
  1084. _zeroFlag = true;
  1085. _zeroPosition = localpos;
  1086. }
  1087. if (m_pidControllerActive)
  1088. {
  1089. // We only want to deactivate the PID Controller if we think we want to have our surrogate
  1090. // react to the physics scene by moving it's position.
  1091. // Avatar to Avatar collisions
  1092. // Prim to avatar collisions
  1093. vec.X = -vel.X * PID_D * 2f + (_zeroPosition.X - localpos.X) * (PID_P * 5);
  1094. vec.Y = -vel.Y * PID_D * 2f + (_zeroPosition.Y - localpos.Y) * (PID_P * 5);
  1095. if(vel.Z > 0)
  1096. vec.Z += -vel.Z * PID_D + (_zeroPosition.Z - localpos.Z) * PID_P;
  1097. else
  1098. vec.Z += (-vel.Z * PID_D + (_zeroPosition.Z - localpos.Z) * PID_P) * 0.2f;
  1099. /*
  1100. if (flying)
  1101. {
  1102. vec.Z += -vel.Z * PID_D + (_zeroPosition.Z - localpos.Z) * PID_P;
  1103. }
  1104. */
  1105. }
  1106. //PidStatus = true;
  1107. }
  1108. else
  1109. {
  1110. m_pidControllerActive = true;
  1111. _zeroFlag = false;
  1112. if (m_iscolliding)
  1113. {
  1114. if (!flying)
  1115. {
  1116. // we are on a surface
  1117. if (ctz.Z > 0f)
  1118. {
  1119. // moving up or JUMPING
  1120. vec.Z += (ctz.Z - vel.Z) * PID_D * 2f;
  1121. vec.X += (ctz.X - vel.X) * (PID_D);
  1122. vec.Y += (ctz.Y - vel.Y) * (PID_D);
  1123. }
  1124. else
  1125. {
  1126. // we are moving down on a surface
  1127. if (ctz.Z == 0)
  1128. {
  1129. if (vel.Z > 0)
  1130. vec.Z -= vel.Z * PID_D * 2f;
  1131. vec.X += (ctz.X - vel.X) * (PID_D);
  1132. vec.Y += (ctz.Y - vel.Y) * (PID_D);
  1133. }
  1134. // intencionally going down
  1135. else
  1136. {
  1137. if (ctz.Z < vel.Z)
  1138. vec.Z += (ctz.Z - vel.Z) * PID_D;
  1139. else
  1140. {
  1141. }
  1142. if (Math.Abs(ctz.X) > Math.Abs(vel.X))
  1143. vec.X += (ctz.X - vel.X) * (PID_D);
  1144. if (Math.Abs(ctz.Y) > Math.Abs(vel.Y))
  1145. vec.Y += (ctz.Y - vel.Y) * (PID_D);
  1146. }
  1147. }
  1148. // We're standing on something
  1149. }
  1150. else
  1151. {
  1152. // We're flying and colliding with something
  1153. vec.X += (ctz.X - vel.X) * (PID_D * 0.0625f);
  1154. vec.Y += (ctz.Y - vel.Y) * (PID_D * 0.0625f);
  1155. vec.Z += (ctz.Z - vel.Z) * (PID_D * 0.0625f);
  1156. }
  1157. }
  1158. else // ie not colliding
  1159. {
  1160. if (flying || hoverPIDActive) //(!m_iscolliding && flying)
  1161. {
  1162. // we're in mid air suspended
  1163. vec.X += (ctz.X - vel.X) * (PID_D);
  1164. vec.Y += (ctz.Y - vel.Y) * (PID_D);
  1165. vec.Z += (ctz.Z - vel.Z) * (PID_D);
  1166. }
  1167. else
  1168. {
  1169. // we're not colliding and we're not flying so that means we're falling!
  1170. // m_iscolliding includes collisions with the ground.
  1171. // d.Vector3 pos = d.BodyGetPosition(Body);
  1172. vec.X += (ctz.X - vel.X) * PID_D * 0.833f;
  1173. vec.Y += (ctz.Y - vel.Y) * PID_D * 0.833f;
  1174. // hack for breaking on fall
  1175. if (ctz.Z == -9999f)
  1176. vec.Z += -vel.Z * PID_D - m_parent_scene.gravityz * m_mass;
  1177. }
  1178. }
  1179. }
  1180. if (velLengthSquared > 2500.0f) // 50m/s apply breaks
  1181. {
  1182. breakfactor = 0.16f * m_mass;
  1183. vec.X -= breakfactor * vel.X;
  1184. vec.Y -= breakfactor * vel.Y;
  1185. vec.Z -= breakfactor * vel.Z;
  1186. }
  1187. }
  1188. else
  1189. {
  1190. breakfactor = m_mass;
  1191. vec.X -= breakfactor * vel.X;
  1192. vec.Y -= breakfactor * vel.Y;
  1193. if (flying)
  1194. vec.Z -= 0.5f * breakfactor * vel.Z;
  1195. else
  1196. vec.Z -= .16f* m_mass * vel.Z;
  1197. }
  1198. if (flying || hoverPIDActive)
  1199. {
  1200. vec.Z -= m_parent_scene.gravityz * m_mass;
  1201. if(!hoverPIDActive)
  1202. {
  1203. //Added for auto fly height. Kitto Flora
  1204. float target_altitude = terrainheight + MinimumGroundFlightOffset;
  1205. if (localpos.Z < target_altitude)
  1206. {
  1207. vec.Z += (target_altitude - localpos.Z) * PID_P * 5.0f;
  1208. }
  1209. // end add Kitto Flora
  1210. }
  1211. }
  1212. if (vec.IsFinite())
  1213. {
  1214. if (vec.X != 0 || vec.Y !=0 || vec.Z !=0)
  1215. d.BodyAddForce(Body, vec.X, vec.Y, vec.Z);
  1216. }
  1217. else
  1218. {
  1219. m_log.Warn("[PHYSICS]: Got a NaN force vector in Move()");
  1220. m_log.Warn("[PHYSICS]: Avatar Position is non-finite!");
  1221. defects.Add(this);
  1222. // _parent_scene.RemoveCharacter(this);
  1223. // destroy avatar capsule and related ODE data
  1224. AvatarGeomAndBodyDestroy();
  1225. return;
  1226. }
  1227. // update our local ideia of position velocity and aceleration
  1228. // _position = localpos;
  1229. _position = localpos;
  1230. if (_zeroFlag)
  1231. {
  1232. _velocity = Vector3.Zero;
  1233. _acceleration = Vector3.Zero;
  1234. m_rotationalVelocity = Vector3.Zero;
  1235. }
  1236. else
  1237. {
  1238. Vector3 a =_velocity; // previus velocity
  1239. SetSmooth(ref _velocity, ref vel, 2);
  1240. a = (_velocity - a) * invtimeStep;
  1241. SetSmooth(ref _acceleration, ref a, 2);
  1242. dtmp = d.BodyGetAngularVel(Body);
  1243. m_rotationalVelocity.X = 0f;
  1244. m_rotationalVelocity.Y = 0f;
  1245. m_rotationalVelocity.Z = dtmp.Z;
  1246. Math.Round(m_rotationalVelocity.Z,3);
  1247. }
  1248. }
  1249. public void round(ref Vector3 v, int digits)
  1250. {
  1251. v.X = (float)Math.Round(v.X, digits);
  1252. v.Y = (float)Math.Round(v.Y, digits);
  1253. v.Z = (float)Math.Round(v.Z, digits);
  1254. }
  1255. public void SetSmooth(ref Vector3 dst, ref Vector3 value)
  1256. {
  1257. dst.X = 0.1f * dst.X + 0.9f * value.X;
  1258. dst.Y = 0.1f * dst.Y + 0.9f * value.Y;
  1259. dst.Z = 0.1f * dst.Z + 0.9f * value.Z;
  1260. }
  1261. public void SetSmooth(ref Vector3 dst, ref Vector3 value, int rounddigits)
  1262. {
  1263. dst.X = 0.4f * dst.X + 0.6f * value.X;
  1264. dst.X = (float)Math.Round(dst.X, rounddigits);
  1265. dst.Y = 0.4f * dst.Y + 0.6f * value.Y;
  1266. dst.Y = (float)Math.Round(dst.Y, rounddigits);
  1267. dst.Z = 0.4f * dst.Z + 0.6f * value.Z;
  1268. dst.Z = (float)Math.Round(dst.Z, rounddigits);
  1269. }
  1270. /// <summary>
  1271. /// Updates the reported position and velocity.
  1272. /// Used to copy variables from unmanaged space at heartbeat rate and also trigger scene updates acording
  1273. /// also outbounds checking
  1274. /// copy and outbounds now done in move(..) at ode rate
  1275. ///
  1276. /// </summary>
  1277. public void UpdatePositionAndVelocity()
  1278. {
  1279. return;
  1280. // if (Body == IntPtr.Zero)
  1281. // return;
  1282. }
  1283. /// <summary>
  1284. /// Cleanup the things we use in the scene.
  1285. /// </summary>
  1286. public void Destroy()
  1287. {
  1288. AddChange(changes.Remove, null);
  1289. }
  1290. public override void CrossingFailure()
  1291. {
  1292. }
  1293. public override Vector3 PIDTarget { set { return; } }
  1294. public override bool PIDActive {get {return m_pidControllerActive;} set { return; } }
  1295. public override float PIDTau { set { return; } }
  1296. public override float PIDHoverHeight
  1297. {
  1298. set
  1299. {
  1300. AddChange(changes.PIDHoverHeight,value);
  1301. }
  1302. }
  1303. public override bool PIDHoverActive
  1304. {
  1305. get
  1306. {
  1307. return m_useHoverPID;
  1308. }
  1309. set
  1310. {
  1311. AddChange(changes.PIDHoverActive, value);
  1312. }
  1313. }
  1314. public override PIDHoverType PIDHoverType
  1315. {
  1316. set
  1317. {
  1318. AddChange(changes.PIDHoverType,value);
  1319. }
  1320. }
  1321. public override float PIDHoverTau
  1322. {
  1323. set
  1324. {
  1325. float tmp =0;
  1326. if (value > 0)
  1327. {
  1328. float mint = (0.05f > timeStep ? 0.05f : timeStep);
  1329. if (value < mint)
  1330. tmp = mint;
  1331. else
  1332. tmp = value;
  1333. }
  1334. AddChange(changes.PIDHoverTau, tmp);
  1335. }
  1336. }
  1337. public override Quaternion APIDTarget { set { return; } }
  1338. public override bool APIDActive { set { return; } }
  1339. public override float APIDStrength { set { return; } }
  1340. public override float APIDDamping { set { return; } }
  1341. public override void SubscribeEvents(int ms)
  1342. {
  1343. m_eventsubscription = ms;
  1344. m_cureventsubscription = 0;
  1345. CollisionEventsThisFrame.Clear();
  1346. SentEmptyCollisionsEvent = false;
  1347. }
  1348. public override void UnSubscribeEvents()
  1349. {
  1350. m_eventsubscription = 0;
  1351. m_parent_scene.RemoveCollisionEventReporting(this);
  1352. lock(CollisionEventsThisFrame)
  1353. CollisionEventsThisFrame.Clear();
  1354. }
  1355. public override void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
  1356. {
  1357. lock(CollisionEventsThisFrame)
  1358. CollisionEventsThisFrame.AddCollider(CollidedWith, contact);
  1359. m_parent_scene.AddCollisionEventReporting(this);
  1360. }
  1361. public void SendCollisions(int timestep)
  1362. {
  1363. if (m_cureventsubscription < 50000)
  1364. m_cureventsubscription += timestep;
  1365. if (m_cureventsubscription < m_eventsubscription)
  1366. return;
  1367. lock(CollisionEventsThisFrame)
  1368. {
  1369. int ncolisions = CollisionEventsThisFrame.m_objCollisionList.Count;
  1370. if (!SentEmptyCollisionsEvent || ncolisions > 0)
  1371. {
  1372. base.SendCollisionUpdate(CollisionEventsThisFrame);
  1373. m_cureventsubscription = 0;
  1374. if (ncolisions == 0)
  1375. {
  1376. SentEmptyCollisionsEvent = true;
  1377. // _parent_scene.RemoveCollisionEventReporting(this);
  1378. }
  1379. else
  1380. {
  1381. SentEmptyCollisionsEvent = false;
  1382. CollisionEventsThisFrame.Clear();
  1383. }
  1384. }
  1385. }
  1386. }
  1387. public override bool SubscribedEvents()
  1388. {
  1389. if (m_eventsubscription > 0)
  1390. return true;
  1391. return false;
  1392. }
  1393. private void changePhysicsStatus(bool NewStatus)
  1394. {
  1395. if (NewStatus != m_isPhysical)
  1396. {
  1397. if (NewStatus)
  1398. {
  1399. AvatarGeomAndBodyDestroy();
  1400. AvatarGeomAndBodyCreation(_position.X, _position.Y, _position.Z);
  1401. m_parent_scene.actor_name_map[collider] = (PhysicsActor)this;
  1402. m_parent_scene.actor_name_map[capsule] = (PhysicsActor)this;
  1403. m_parent_scene.AddCharacter(this);
  1404. }
  1405. else
  1406. {
  1407. m_parent_scene.RemoveCollisionEventReporting(this);
  1408. m_parent_scene.RemoveCharacter(this);
  1409. // destroy avatar capsule and related ODE data
  1410. AvatarGeomAndBodyDestroy();
  1411. }
  1412. m_freemove = false;
  1413. m_isPhysical = NewStatus;
  1414. }
  1415. }
  1416. private void changeAdd()
  1417. {
  1418. changePhysicsStatus(true);
  1419. }
  1420. private void changeRemove()
  1421. {
  1422. changePhysicsStatus(false);
  1423. }
  1424. private void changeShape(PrimitiveBaseShape arg)
  1425. {
  1426. }
  1427. private void changeAvatarSize(strAvatarSize st)
  1428. {
  1429. m_feetOffset = st.offset;
  1430. changeSize(st.size);
  1431. }
  1432. private void changeSize(Vector3 pSize)
  1433. {
  1434. if (pSize.IsFinite())
  1435. {
  1436. // for now only look to Z changes since viewers also don't change X and Y
  1437. if (pSize.Z != m_size.Z)
  1438. {
  1439. AvatarGeomAndBodyDestroy();
  1440. float oldsz = m_size.Z;
  1441. m_size = pSize;
  1442. AvatarGeomAndBodyCreation(_position.X, _position.Y,
  1443. _position.Z + (m_size.Z - oldsz) * 0.5f);
  1444. // Velocity = Vector3.Zero;
  1445. m_targetVelocity = Vector3.Zero;
  1446. m_parent_scene.actor_name_map[collider] = (PhysicsActor)this;
  1447. m_parent_scene.actor_name_map[capsule] = (PhysicsActor)this;
  1448. }
  1449. m_freemove = false;
  1450. m_pidControllerActive = true;
  1451. }
  1452. else
  1453. {
  1454. m_log.Warn("[PHYSICS]: Got a NaN Size from Scene on a Character");
  1455. }
  1456. }
  1457. private void changePosition( Vector3 newPos)
  1458. {
  1459. if (Body != IntPtr.Zero)
  1460. d.BodySetPosition(Body, newPos.X, newPos.Y, newPos.Z);
  1461. _position = newPos;
  1462. m_freemove = false;
  1463. m_pidControllerActive = true;
  1464. }
  1465. private void changeOrientation(Quaternion newOri)
  1466. {
  1467. if (m_orientation != newOri)
  1468. {
  1469. m_orientation = newOri; // keep a copy for core use
  1470. // but only use rotations around Z
  1471. m_orientation2D.W = newOri.W;
  1472. m_orientation2D.Z = newOri.Z;
  1473. float t = m_orientation2D.W * m_orientation2D.W + m_orientation2D.Z * m_orientation2D.Z;
  1474. if (t > 0)
  1475. {
  1476. t = 1.0f / (float)Math.Sqrt(t);
  1477. m_orientation2D.W *= t;
  1478. m_orientation2D.Z *= t;
  1479. }
  1480. else
  1481. {
  1482. m_orientation2D.W = 1.0f;
  1483. m_orientation2D.Z = 0f;
  1484. }
  1485. m_orientation2D.Y = 0f;
  1486. m_orientation2D.X = 0f;
  1487. d.Quaternion myrot = new d.Quaternion();
  1488. myrot.X = m_orientation2D.X;
  1489. myrot.Y = m_orientation2D.Y;
  1490. myrot.Z = m_orientation2D.Z;
  1491. myrot.W = m_orientation2D.W;
  1492. d.BodySetQuaternion(Body, ref myrot);
  1493. }
  1494. }
  1495. private void changeVelocity(Vector3 newVel)
  1496. {
  1497. _velocity = newVel;
  1498. setFreeMove();
  1499. if (Body != IntPtr.Zero)
  1500. d.BodySetLinearVel(Body, newVel.X, newVel.Y, newVel.Z);
  1501. }
  1502. private void changeTargetVelocity(Vector3 newVel)
  1503. {
  1504. m_pidControllerActive = true;
  1505. m_freemove = false;
  1506. _target_velocity = newVel;
  1507. }
  1508. private void changeSetTorque(Vector3 newTorque)
  1509. {
  1510. }
  1511. private void changeAddForce(Vector3 newForce)
  1512. {
  1513. }
  1514. private void changeAddAngularForce(Vector3 arg)
  1515. {
  1516. }
  1517. private void changeAngularLock(byte arg)
  1518. {
  1519. }
  1520. private void changeFloatOnWater(bool arg)
  1521. {
  1522. }
  1523. private void changeVolumedetetion(bool arg)
  1524. {
  1525. }
  1526. private void changeSelectedStatus(bool arg)
  1527. {
  1528. }
  1529. private void changeDisable(bool arg)
  1530. {
  1531. }
  1532. private void changeBuilding(bool arg)
  1533. {
  1534. }
  1535. private void setFreeMove()
  1536. {
  1537. m_pidControllerActive = true;
  1538. _zeroFlag = false;
  1539. _target_velocity = Vector3.Zero;
  1540. m_freemove = true;
  1541. m_colliderfilter = -1;
  1542. m_colliderObjectfilter = -1;
  1543. m_colliderGroundfilter = -1;
  1544. m_iscolliding = false;
  1545. m_iscollidingGround = false;
  1546. m_iscollidingObj = false;
  1547. CollisionEventsThisFrame.Clear();
  1548. }
  1549. private void changeForce(Vector3 newForce)
  1550. {
  1551. setFreeMove();
  1552. if (Body != IntPtr.Zero)
  1553. {
  1554. if (newForce.X != 0f || newForce.Y != 0f || newForce.Z != 0)
  1555. d.BodyAddForce(Body, newForce.X, newForce.Y, newForce.Z);
  1556. }
  1557. }
  1558. // for now momentum is actually velocity
  1559. private void changeMomentum(Vector3 newmomentum)
  1560. {
  1561. _velocity = newmomentum;
  1562. setFreeMove();
  1563. if (Body != IntPtr.Zero)
  1564. d.BodySetLinearVel(Body, newmomentum.X, newmomentum.Y, newmomentum.Z);
  1565. }
  1566. private void changePIDHoverHeight(float val)
  1567. {
  1568. m_PIDHoverHeight = val;
  1569. if (val == 0)
  1570. m_useHoverPID = false;
  1571. }
  1572. private void changePIDHoverType(PIDHoverType type)
  1573. {
  1574. m_PIDHoverType = type;
  1575. }
  1576. private void changePIDHoverTau(float tau)
  1577. {
  1578. m_PIDHoverTau = tau;
  1579. }
  1580. private void changePIDHoverActive(bool active)
  1581. {
  1582. m_useHoverPID = active;
  1583. }
  1584. private void donullchange()
  1585. {
  1586. }
  1587. public bool DoAChange(changes what, object arg)
  1588. {
  1589. if (collider == IntPtr.Zero && what != changes.Add && what != changes.Remove)
  1590. {
  1591. return false;
  1592. }
  1593. // nasty switch
  1594. switch (what)
  1595. {
  1596. case changes.Add:
  1597. changeAdd();
  1598. break;
  1599. case changes.Remove:
  1600. changeRemove();
  1601. break;
  1602. case changes.Position:
  1603. changePosition((Vector3)arg);
  1604. break;
  1605. case changes.Orientation:
  1606. changeOrientation((Quaternion)arg);
  1607. break;
  1608. case changes.PosOffset:
  1609. donullchange();
  1610. break;
  1611. case changes.OriOffset:
  1612. donullchange();
  1613. break;
  1614. case changes.Velocity:
  1615. changeVelocity((Vector3)arg);
  1616. break;
  1617. case changes.TargetVelocity:
  1618. changeTargetVelocity((Vector3)arg);
  1619. break;
  1620. // case changes.Acceleration:
  1621. // changeacceleration((Vector3)arg);
  1622. // break;
  1623. // case changes.AngVelocity:
  1624. // changeangvelocity((Vector3)arg);
  1625. // break;
  1626. case changes.Force:
  1627. changeForce((Vector3)arg);
  1628. break;
  1629. case changes.Torque:
  1630. changeSetTorque((Vector3)arg);
  1631. break;
  1632. case changes.AddForce:
  1633. changeAddForce((Vector3)arg);
  1634. break;
  1635. case changes.AddAngForce:
  1636. changeAddAngularForce((Vector3)arg);
  1637. break;
  1638. case changes.AngLock:
  1639. changeAngularLock((byte)arg);
  1640. break;
  1641. case changes.Size:
  1642. changeSize((Vector3)arg);
  1643. break;
  1644. case changes.AvatarSize:
  1645. changeAvatarSize((strAvatarSize)arg);
  1646. break;
  1647. case changes.Momentum:
  1648. changeMomentum((Vector3)arg);
  1649. break;
  1650. case changes.PIDHoverHeight:
  1651. changePIDHoverHeight((float)arg);
  1652. break;
  1653. case changes.PIDHoverType:
  1654. changePIDHoverType((PIDHoverType)arg);
  1655. break;
  1656. case changes.PIDHoverTau:
  1657. changePIDHoverTau((float)arg);
  1658. break;
  1659. case changes.PIDHoverActive:
  1660. changePIDHoverActive((bool)arg);
  1661. break;
  1662. /* not in use for now
  1663. case changes.Shape:
  1664. changeShape((PrimitiveBaseShape)arg);
  1665. break;
  1666. case changes.CollidesWater:
  1667. changeFloatOnWater((bool)arg);
  1668. break;
  1669. case changes.VolumeDtc:
  1670. changeVolumedetetion((bool)arg);
  1671. break;
  1672. case changes.Physical:
  1673. changePhysicsStatus((bool)arg);
  1674. break;
  1675. case changes.Selected:
  1676. changeSelectedStatus((bool)arg);
  1677. break;
  1678. case changes.disabled:
  1679. changeDisable((bool)arg);
  1680. break;
  1681. case changes.building:
  1682. changeBuilding((bool)arg);
  1683. break;
  1684. */
  1685. case changes.Null:
  1686. donullchange();
  1687. break;
  1688. default:
  1689. donullchange();
  1690. break;
  1691. }
  1692. return false;
  1693. }
  1694. public void AddChange(changes what, object arg)
  1695. {
  1696. m_parent_scene.AddChange((PhysicsActor)this, what, arg);
  1697. }
  1698. private struct strAvatarSize
  1699. {
  1700. public Vector3 size;
  1701. public float offset;
  1702. }
  1703. }
  1704. }