1
0

ODECharacter.cs 72 KB

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