1
0

ODEPrim.cs 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Runtime.CompilerServices;
  31. using log4net;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.PhysicsModules.SharedBase;
  35. namespace OpenSim.Region.PhysicsModule.ubOde
  36. {
  37. public class OdePrim : PhysicsActor
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. private bool m_isphysical;
  41. private bool m_fakeisphysical;
  42. private bool m_isphantom;
  43. private bool m_fakeisphantom;
  44. internal bool m_isVolumeDetect; // If true, this prim only detects collisions but doesn't collide actively
  45. private bool m_fakeisVolumeDetect; // If true, this prim only detects collisions but doesn't collide actively
  46. internal bool m_building;
  47. protected bool m_forcePosOrRotation;
  48. private bool m_iscolliding;
  49. internal bool m_isSelected;
  50. private bool m_delaySelect;
  51. private bool m_lastdoneSelected;
  52. internal bool m_outbounds;
  53. private byte m_angularlocks = 0;
  54. //private Quaternion m_lastorientation;
  55. private Quaternion m_orientation;
  56. private Vector3 m_position;
  57. private Vector3 _velocity;
  58. //private Vector3 m_lastVelocity;
  59. //private Vector3 m_lastposition;
  60. private Vector3 m_rotationalVelocity;
  61. private Vector3 m_size;
  62. private Vector3 m_acceleration;
  63. private IntPtr Amotor;
  64. internal Vector3 m_force;
  65. internal Vector3 m_forceacc;
  66. internal Vector3 m_torque;
  67. internal Vector3 m_angularForceacc;
  68. public readonly ODEScene m_parentScene;
  69. private readonly float m_sceneInverseTimeStep;
  70. private readonly float m_sceneTimeStep;
  71. private Vector3 m_PIDTarget;
  72. private float m_PIDTau;
  73. private bool m_usePID;
  74. private float m_PIDHoverHeight;
  75. private float m_PIDHoverTau;
  76. private bool m_useHoverPID;
  77. private PIDHoverType m_PIDHoverType;
  78. private float m_targetHoverHeight;
  79. private float m_buoyancy; //KF: m_buoyancy should be set by llSetBuoyancy() for non-vehicle.
  80. private readonly int m_body_autodisable_frames;
  81. public int m_bodydisablecontrol = 0;
  82. private float m_gravmod = 1.0f;
  83. // Default we're a Geometry
  84. private CollisionCategories m_collisionCategories = (CollisionCategories.Geom);
  85. // Default colide nonphysical don't try to colide with anything
  86. private const CollisionCategories m_default_collisionFlagsNotPhysical = 0;
  87. private const CollisionCategories m_default_collisionFlagsPhysical = (CollisionCategories.Geom |
  88. CollisionCategories.Character |
  89. CollisionCategories.Land |
  90. CollisionCategories.VolumeDtc);
  91. //private bool m_collidesLand = true;
  92. //private bool m_collidesWater;
  93. //public bool m_returnCollisions;
  94. private bool m_NoColide; // for now only for internal use for bad meshs
  95. // Default, Collide with Other Geometries, spaces and Bodies
  96. private CollisionCategories m_collisionFlags = m_default_collisionFlagsNotPhysical;
  97. public bool m_disabled;
  98. private IMesh m_mesh;
  99. private readonly object m_meshlock = new();
  100. private PrimitiveBaseShape m_pbs;
  101. //private UUID? m_assetID;
  102. private MeshState m_meshState;
  103. /// <summary>
  104. /// The physics space which contains prim geometry
  105. /// </summary>
  106. public IntPtr m_targetSpace;
  107. public IntPtr m_prim_geom;
  108. public IntPtr _triMeshData;
  109. private PhysicsActor _parent;
  110. private readonly List<OdePrim> childrenPrim = new();
  111. public float m_collisionscore;
  112. private int m_colliderfilter = 0;
  113. public IntPtr m_collide_geom; // for objects: geom if single prim space it linkset
  114. private float m_density;
  115. private byte m_shapetype;
  116. private byte m_fakeShapetype;
  117. public bool _zeroFlag;
  118. private bool m_lastUpdateSent;
  119. public IntPtr Body;
  120. private Vector3 _target_velocity;
  121. public Vector3 m_OBBOffset;
  122. public Vector3 m_OBB;
  123. public float primOOBradiusSQ;
  124. //private bool m_hasOBB = true;
  125. private float m_physCost;
  126. private float m_streamCost;
  127. internal UBOdeNative.Mass primdMass; // prim inertia information on it's own referencial
  128. private PhysicsInertiaData m_InertiaOverride;
  129. float primMass; // prim own mass
  130. float primVolume; // prim own volume;
  131. float m_mass; // object mass acording to case
  132. public int m_givefakepos;
  133. private Vector3 fakepos;
  134. public int m_givefakeori;
  135. private Quaternion fakeori;
  136. private PhysicsInertiaData m_fakeInertiaOverride;
  137. private int m_eventsubscription;
  138. private int m_cureventsubscription;
  139. private CollisionEventUpdate CollisionEvents = null;
  140. private CollisionEventUpdate CollisionVDTCEvents = null;
  141. private bool SentEmptyCollisionsEvent;
  142. public volatile bool childPrim;
  143. public ODEDynamics m_vehicle;
  144. internal int m_material = (int)Material.Wood;
  145. private float mu;
  146. private float bounce;
  147. /// <summary>
  148. /// Is this prim subject to physics? Even if not, it's still solid for collision purposes.
  149. /// </summary>
  150. public override bool IsPhysical // this is not reliable for internal use
  151. {
  152. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  153. get { return m_fakeisphysical; }
  154. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  155. set
  156. {
  157. m_fakeisphysical = value; // we show imediatly to outside that we changed physical
  158. // and also to stop imediatly some updates
  159. // but real change will only happen in taintprocessing
  160. //if (!value) // Zero the remembered last velocity
  161. //m_lastVelocity = Vector3.Zero;
  162. AddChange(changes.Physical, value);
  163. }
  164. }
  165. public override bool IsVolumeDtc
  166. {
  167. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  168. get { return m_fakeisVolumeDetect; }
  169. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  170. set
  171. {
  172. m_fakeisVolumeDetect = value;
  173. AddChange(changes.VolumeDtc, value);
  174. }
  175. }
  176. public override bool Phantom // this is not reliable for internal use
  177. {
  178. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  179. get { return m_fakeisphantom; }
  180. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  181. set
  182. {
  183. m_fakeisphantom = value;
  184. AddChange(changes.Phantom, value);
  185. }
  186. }
  187. public override bool Building // this is not reliable for internal use
  188. {
  189. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  190. get { return m_building; }
  191. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  192. set
  193. {
  194. //if (value)
  195. // m_building = true;
  196. AddChange(changes.building, value);
  197. }
  198. }
  199. public override void getContactData(ref ContactData cdata)
  200. {
  201. cdata.mu = mu;
  202. cdata.bounce = bounce;
  203. //cdata.softcolide = m_softcolide;
  204. cdata.softcolide = false;
  205. if (m_isphysical)
  206. {
  207. ODEDynamics veh;
  208. if (_parent != null)
  209. veh = ((OdePrim)_parent).m_vehicle;
  210. else
  211. veh = m_vehicle;
  212. if (veh != null && veh.Type != Vehicle.TYPE_NONE)
  213. cdata.mu *= veh.FrictionFactor;
  214. // cdata.mu *= 0;
  215. }
  216. }
  217. public override float PhysicsCost
  218. {
  219. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  220. get
  221. {
  222. return m_physCost;
  223. }
  224. }
  225. public override float StreamCost
  226. {
  227. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  228. get
  229. {
  230. return m_streamCost;
  231. }
  232. }
  233. public override int PhysicsActorType
  234. {
  235. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  236. get { return (int)ActorTypes.Prim; }
  237. set {}
  238. }
  239. public override bool SetAlwaysRun
  240. {
  241. get { return false; }
  242. set {}
  243. }
  244. public override uint LocalID
  245. {
  246. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  247. get { return m_baseLocalID; }
  248. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  249. set
  250. {
  251. uint oldid = m_baseLocalID;
  252. m_baseLocalID = value;
  253. m_parentScene.changePrimID(this, oldid);
  254. }
  255. }
  256. public override PhysicsActor ParentActor
  257. {
  258. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  259. get
  260. {
  261. return (childPrim) ? _parent : (PhysicsActor)this;
  262. }
  263. }
  264. public override bool Grabbed
  265. {
  266. set {}
  267. }
  268. public override bool Selected
  269. {
  270. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  271. set
  272. {
  273. if (value)
  274. m_isSelected = value; // if true set imediatly to stop moves etc
  275. AddChange(changes.Selected, value);
  276. }
  277. }
  278. public override bool Flying
  279. {
  280. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  281. // no flying prims for you
  282. get { return false; }
  283. set { }
  284. }
  285. public override bool IsColliding
  286. {
  287. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  288. get { return m_iscolliding; }
  289. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  290. set
  291. {
  292. if (value)
  293. {
  294. m_colliderfilter += 2;
  295. if (m_colliderfilter > 2)
  296. m_colliderfilter = 2;
  297. }
  298. else
  299. {
  300. m_colliderfilter--;
  301. if (m_colliderfilter < 0)
  302. m_colliderfilter = 0;
  303. }
  304. if (m_colliderfilter == 0)
  305. m_iscolliding = false;
  306. else
  307. m_iscolliding = true;
  308. }
  309. }
  310. public override bool CollidingGround
  311. {
  312. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  313. get { return false; }
  314. set {}
  315. }
  316. public override bool CollidingObj
  317. {
  318. get { return false; }
  319. set {}
  320. }
  321. public override bool ThrottleUpdates {get;set;}
  322. public override bool Stopped
  323. {
  324. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  325. get { return _zeroFlag; }
  326. }
  327. public override Vector3 Position
  328. {
  329. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  330. get
  331. {
  332. return (m_givefakepos > 0) ? fakepos : m_position;
  333. }
  334. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  335. set
  336. {
  337. fakepos = value;
  338. m_givefakepos++;
  339. AddChange(changes.Position, value);
  340. }
  341. }
  342. public override Vector3 Size
  343. {
  344. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  345. get { return m_size; }
  346. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  347. set
  348. {
  349. if (value.IsFinite())
  350. {
  351. m_parentScene.m_meshWorker.ChangeActorPhysRep(this, m_pbs, value, m_fakeShapetype);
  352. }
  353. else
  354. {
  355. m_log.WarnFormat("[PHYSICS]: Got NaN Size on object {0}", Name);
  356. }
  357. }
  358. }
  359. public override float Mass
  360. {
  361. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  362. get { return primMass; }
  363. }
  364. public override Vector3 Force
  365. {
  366. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  367. get { return m_force; }
  368. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  369. set
  370. {
  371. if (value.IsFinite())
  372. {
  373. AddChange(changes.Force, value);
  374. }
  375. else
  376. {
  377. m_log.WarnFormat("[PHYSICS]: NaN in Force Applied to an Object {0}", Name);
  378. }
  379. }
  380. }
  381. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  382. public override void SetVolumeDetect(int param)
  383. {
  384. m_fakeisVolumeDetect = (param != 0);
  385. AddChange(changes.VolumeDtc, m_fakeisVolumeDetect);
  386. }
  387. public override Vector3 GeometricCenter
  388. {
  389. // this is not real geometric center but a average of positions relative to root prim acording to
  390. // http://wiki.secondlife.com/wiki/llGetGeometricCenter
  391. // ignoring tortured prims details since sl also seems to ignore
  392. // so no real use in doing it on physics
  393. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  394. get
  395. {
  396. return Vector3.Zero;
  397. }
  398. }
  399. public override PhysicsInertiaData GetInertiaData()
  400. {
  401. PhysicsInertiaData inertia;
  402. if(childPrim)
  403. {
  404. if(_parent != null)
  405. return _parent.GetInertiaData();
  406. else
  407. {
  408. inertia = new PhysicsInertiaData
  409. {
  410. TotalMass = -1
  411. };
  412. return inertia;
  413. }
  414. }
  415. inertia = new PhysicsInertiaData();
  416. // double buffering
  417. if(m_fakeInertiaOverride != null)
  418. {
  419. UBOdeNative.Mass objdmass = new();
  420. objdmass.I.M00 = m_fakeInertiaOverride.Inertia.X;
  421. objdmass.I.M11 = m_fakeInertiaOverride.Inertia.Y;
  422. objdmass.I.M22 = m_fakeInertiaOverride.Inertia.Z;
  423. objdmass.mass = m_fakeInertiaOverride.TotalMass;
  424. if(MathF.Abs(m_fakeInertiaOverride.InertiaRotation.W) < 0.999)
  425. {
  426. UBOdeNative.Matrix3 inertiarotmat = new();
  427. UBOdeNative.RfromQ(ref inertiarotmat, ref m_fakeInertiaOverride.InertiaRotation);
  428. UBOdeNative.MassRotate(ref objdmass, ref inertiarotmat);
  429. }
  430. inertia.TotalMass = m_fakeInertiaOverride.TotalMass;
  431. inertia.CenterOfMass = m_fakeInertiaOverride.CenterOfMass;
  432. inertia.Inertia.X = objdmass.I.M00;
  433. inertia.Inertia.Y = objdmass.I.M11;
  434. inertia.Inertia.Z = objdmass.I.M22;
  435. inertia.InertiaRotation.X = objdmass.I.M01;
  436. inertia.InertiaRotation.Y = objdmass.I.M02;
  437. inertia.InertiaRotation.Z = objdmass.I.M12;
  438. return inertia;
  439. }
  440. inertia.TotalMass = m_mass;
  441. if(Body == IntPtr.Zero || m_prim_geom == IntPtr.Zero)
  442. {
  443. inertia.CenterOfMass = Vector3.Zero;
  444. inertia.Inertia = Vector3.Zero;
  445. inertia.InertiaRotation = Vector4.Zero;
  446. return inertia;
  447. }
  448. UBOdeNative.Vector3 dtmp;
  449. UBOdeNative.Mass m = new();
  450. lock(m_parentScene.OdeLock)
  451. {
  452. UBOdeNative.AllocateODEDataForThread(0);
  453. dtmp = UBOdeNative.GeomGetOffsetPosition(m_prim_geom);
  454. UBOdeNative.BodyGetMass(Body, out m);
  455. }
  456. Vector3 cm = new(-dtmp.X, -dtmp.Y, -dtmp.Z);
  457. inertia.CenterOfMass = cm;
  458. inertia.Inertia = new(m.I.M00, m.I.M11, m.I.M22);
  459. inertia.InertiaRotation = new(m.I.M01, m.I.M02 , m.I.M12, 0);
  460. return inertia;
  461. }
  462. public override void SetInertiaData(PhysicsInertiaData inertia)
  463. {
  464. if(childPrim)
  465. {
  466. _parent?.SetInertiaData(inertia);
  467. return;
  468. }
  469. if(inertia.TotalMass > 0)
  470. m_fakeInertiaOverride = new PhysicsInertiaData(inertia);
  471. else
  472. m_fakeInertiaOverride = null;
  473. if (inertia.TotalMass > m_parentScene.maximumMassObject)
  474. inertia.TotalMass = m_parentScene.maximumMassObject;
  475. AddChange(changes.SetInertia,(object)m_fakeInertiaOverride);
  476. }
  477. public override Vector3 CenterOfMass
  478. {
  479. get
  480. {
  481. lock (m_parentScene.OdeLock)
  482. {
  483. UBOdeNative.AllocateODEDataForThread(0);
  484. if (!childPrim && Body != IntPtr.Zero)
  485. {
  486. return UBOdeNative.BodyGetPositionOMV(Body);
  487. }
  488. else if (m_prim_geom != IntPtr.Zero)
  489. {
  490. Vector3 Ptot = UBOdeNative.GeomGetPositionOMV(m_prim_geom);
  491. Quaternion q = UBOdeNative.GeomGetQuaternionOMV(m_prim_geom);
  492. Ptot += m_OBBOffset * q;
  493. return Ptot;
  494. /*
  495. float tmass = _mass;
  496. Ptot *= tmass;
  497. float m;
  498. foreach (OdePrim prm in childrenPrim)
  499. {
  500. m = prm._mass;
  501. Ptot += prm.CenterOfMass * m;
  502. tmass += m;
  503. }
  504. if (tmass == 0)
  505. tmass = 0;
  506. else
  507. tmass = 1.0f / tmass;
  508. Ptot *= tmass;
  509. return Ptot;
  510. */
  511. }
  512. else
  513. return m_position;
  514. }
  515. }
  516. }
  517. public override PrimitiveBaseShape Shape
  518. {
  519. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  520. set
  521. {
  522. //AddChange(changes.Shape, value);
  523. m_parentScene.m_meshWorker.ChangeActorPhysRep(this, value, m_size, m_fakeShapetype);
  524. }
  525. }
  526. public override byte PhysicsShapeType
  527. {
  528. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  529. get
  530. {
  531. return m_fakeShapetype;
  532. }
  533. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  534. set
  535. {
  536. m_fakeShapetype = value;
  537. m_parentScene.m_meshWorker.ChangeActorPhysRep(this, m_pbs, m_size, value);
  538. }
  539. }
  540. public override Vector3 rootVelocity
  541. {
  542. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  543. get
  544. {
  545. return (_parent == null) ? Velocity : ((OdePrim)_parent).Velocity;
  546. }
  547. }
  548. public override Vector3 Velocity
  549. {
  550. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  551. get
  552. {
  553. return (_zeroFlag) ? Vector3.Zero : _velocity;
  554. }
  555. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  556. set
  557. {
  558. if (value.IsFinite())
  559. {
  560. if(m_outbounds)
  561. _velocity = value;
  562. else
  563. AddChange(changes.Velocity, value);
  564. }
  565. else
  566. {
  567. m_log.WarnFormat("[PHYSICS]: Got NaN Velocity in Object {0}", Name);
  568. }
  569. }
  570. }
  571. public override Vector3 Torque
  572. {
  573. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  574. get
  575. {
  576. return (!IsPhysical || Body == IntPtr.Zero) ? Vector3.Zero : m_torque;
  577. }
  578. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  579. set
  580. {
  581. if (value.IsFinite())
  582. {
  583. AddChange(changes.Torque, value);
  584. }
  585. else
  586. {
  587. m_log.WarnFormat("[PHYSICS]: Got NaN Torque in Object {0}", Name);
  588. }
  589. }
  590. }
  591. public override float CollisionScore
  592. {
  593. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  594. get { return m_collisionscore; }
  595. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  596. set { m_collisionscore = value; }
  597. }
  598. public override bool Kinematic
  599. {
  600. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  601. get { return false; }
  602. set { }
  603. }
  604. public override Quaternion Orientation
  605. {
  606. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  607. get
  608. {
  609. return m_givefakeori > 0 ? fakeori : m_orientation;
  610. }
  611. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  612. set
  613. {
  614. if (QuaternionIsFinite(value))
  615. {
  616. fakeori = value;
  617. m_givefakeori++;
  618. value.Normalize();
  619. AddChange(changes.Orientation, value);
  620. }
  621. else
  622. m_log.WarnFormat("[PHYSICS]: Got NaN quaternion Orientation from Scene in Object {0}", Name);
  623. }
  624. }
  625. public override Vector3 Acceleration
  626. {
  627. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  628. get { return m_acceleration; }
  629. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  630. set
  631. {
  632. if(m_outbounds)
  633. m_acceleration = value;
  634. }
  635. }
  636. public override Vector3 RotationalVelocity
  637. {
  638. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  639. get
  640. {
  641. return _zeroFlag || m_rotationalVelocity.ApproxZero(0.001f) ? Vector3.Zero : m_rotationalVelocity;
  642. }
  643. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  644. set
  645. {
  646. if (value.IsFinite())
  647. {
  648. if(m_outbounds)
  649. m_rotationalVelocity = value;
  650. else
  651. AddChange(changes.AngVelocity, value);
  652. }
  653. else
  654. {
  655. m_log.WarnFormat("[PHYSICS]: Got NaN RotationalVelocity in Object {0}", Name);
  656. }
  657. }
  658. }
  659. public override float Buoyancy
  660. {
  661. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  662. get { return m_buoyancy; }
  663. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  664. set
  665. {
  666. AddChange(changes.Buoyancy,value);
  667. }
  668. }
  669. public override Vector3 PIDTarget
  670. {
  671. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  672. set
  673. {
  674. if (value.IsFinite())
  675. {
  676. AddChange(changes.PIDTarget,value);
  677. }
  678. else
  679. m_log.WarnFormat("[PHYSICS]: Got NaN PIDTarget from Scene on Object {0}", Name);
  680. }
  681. }
  682. public override bool PIDActive
  683. {
  684. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  685. get
  686. {
  687. return m_usePID;
  688. }
  689. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  690. set
  691. {
  692. AddChange(changes.PIDActive,value);
  693. }
  694. }
  695. public override float PIDTau
  696. {
  697. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  698. set
  699. {
  700. if (value > 0)
  701. {
  702. float mint = (0.05f > m_sceneTimeStep ? 0.05f : m_sceneTimeStep);
  703. if (value < mint)
  704. AddChange(changes.PIDTau, mint);
  705. else
  706. AddChange(changes.PIDTau, value);
  707. }
  708. else
  709. AddChange(changes.PIDTau, 0);
  710. }
  711. }
  712. public override float PIDHoverHeight
  713. {
  714. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  715. set
  716. {
  717. AddChange(changes.PIDHoverHeight,value);
  718. }
  719. }
  720. public override bool PIDHoverActive
  721. {
  722. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  723. get
  724. {
  725. return m_useHoverPID;
  726. }
  727. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  728. set
  729. {
  730. AddChange(changes.PIDHoverActive, value);
  731. }
  732. }
  733. public override PIDHoverType PIDHoverType
  734. {
  735. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  736. set
  737. {
  738. AddChange(changes.PIDHoverType,value);
  739. }
  740. }
  741. public override float PIDHoverTau
  742. {
  743. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  744. set
  745. {
  746. if (value > 0)
  747. {
  748. float mint = (0.05f > m_sceneTimeStep ? 0.05f : m_sceneTimeStep);
  749. if (value < mint)
  750. AddChange(changes.PIDHoverTau, mint);
  751. else
  752. AddChange(changes.PIDHoverTau, value);
  753. }
  754. else
  755. AddChange(changes.PIDHoverTau, 0);
  756. }
  757. }
  758. public override Quaternion APIDTarget { set { return; } }
  759. public override bool APIDActive { set { return; } }
  760. public override float APIDStrength { set { return; } }
  761. public override float APIDDamping { set { return; } }
  762. public override int VehicleType
  763. {
  764. // we may need to put a fake on this
  765. get
  766. {
  767. if (m_vehicle == null)
  768. return (int)Vehicle.TYPE_NONE;
  769. else
  770. return (int)m_vehicle.Type;
  771. }
  772. set
  773. {
  774. AddChange(changes.VehicleType, value);
  775. }
  776. }
  777. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  778. public override void VehicleFloatParam(int param, float value)
  779. {
  780. strVehicleFloatParam fp = new()
  781. {
  782. param = param,
  783. value = value
  784. };
  785. AddChange(changes.VehicleFloatParam, fp);
  786. }
  787. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  788. public override void VehicleVectorParam(int param, Vector3 value)
  789. {
  790. strVehicleVectorParam fp = new()
  791. {
  792. param = param,
  793. value = value
  794. };
  795. AddChange(changes.VehicleVectorParam, fp);
  796. }
  797. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  798. public override void VehicleRotationParam(int param, Quaternion value)
  799. {
  800. strVehicleQuatParam fp = new()
  801. {
  802. param = param,
  803. value = value
  804. };
  805. AddChange(changes.VehicleRotationParam, fp);
  806. }
  807. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  808. public override void VehicleFlags(int param, bool value)
  809. {
  810. strVehicleBoolParam bp = new()
  811. {
  812. param = param,
  813. value = value
  814. };
  815. AddChange(changes.VehicleFlags, bp);
  816. }
  817. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  818. public override void SetVehicle(object vdata)
  819. {
  820. AddChange(changes.SetVehicle, vdata);
  821. }
  822. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  823. public void SetAcceleration(Vector3 accel)
  824. {
  825. m_acceleration = accel;
  826. }
  827. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  828. public override void AvatarJump(float forceZ) { }
  829. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  830. public override void AddForce(Vector3 force, bool pushforce)
  831. {
  832. if (force.IsFinite())
  833. {
  834. if(pushforce)
  835. AddChange(changes.AddForce, force);
  836. else // a impulse
  837. AddChange(changes.AddForce, force * m_sceneInverseTimeStep);
  838. }
  839. else
  840. {
  841. m_log.WarnFormat("[PHYSICS]: Got Invalid linear force vector from Scene in Object {0}", Name);
  842. }
  843. //m_log.Info("[PHYSICS]: Added Force:" + force.ToString() + " to prim at " + Position.ToString());
  844. }
  845. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  846. public override void AddAngularForce(Vector3 force, bool pushforce)
  847. {
  848. if (force.IsFinite())
  849. {
  850. //if(pushforce) for now applyrotationimpulse seems more happy applied as a force
  851. AddChange(changes.AddAngForce, force);
  852. //else // a impulse
  853. //AddChange(changes.AddAngForce, force * m_invTimeStep);
  854. }
  855. else
  856. {
  857. m_log.WarnFormat("[PHYSICS]: Got Invalid Angular force vector from Scene in Object {0}", Name);
  858. }
  859. }
  860. public override void CrossingFailure()
  861. {
  862. lock(m_parentScene.OdeLock)
  863. {
  864. if (m_outbounds)
  865. {
  866. m_position.X = Utils.Clamp(m_position.X, 0.5f, m_parentScene.WorldExtents.X - 0.5f);
  867. m_position.Y = Utils.Clamp(m_position.Y, 0.5f, m_parentScene.WorldExtents.Y - 0.5f);
  868. m_position.Z = Utils.Clamp(m_position.Z + 0.2f, Constants.MinSimulationHeight, Constants.MaxSimulationHeight);
  869. //m_lastposition = m_position;
  870. _velocity = Vector3.Zero;
  871. UBOdeNative.AllocateODEDataForThread(0);
  872. //m_lastVelocity = _velocity;
  873. if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
  874. m_vehicle.Stop();
  875. if(Body != IntPtr.Zero)
  876. UBOdeNative.BodySetLinearVel(Body, 0, 0, 0); // stop it
  877. if (m_prim_geom != IntPtr.Zero)
  878. UBOdeNative.GeomSetPosition(m_prim_geom, m_position.X, m_position.Y, m_position.Z);
  879. m_outbounds = false;
  880. changeDisable(false);
  881. base.RequestPhysicsterseUpdate();
  882. }
  883. }
  884. }
  885. public override void CrossingStart()
  886. {
  887. lock(m_parentScene.OdeLock)
  888. {
  889. if (m_outbounds || childPrim)
  890. return;
  891. m_outbounds = true;
  892. //m_lastposition = m_position;
  893. //m_lastorientation = m_orientation;
  894. UBOdeNative.AllocateODEDataForThread(0);
  895. if(Body != IntPtr.Zero)
  896. {
  897. m_rotationalVelocity = UBOdeNative.BodyGetAngularVelOMV(Body);
  898. _velocity = UBOdeNative.BodyGetLinearVelOMV(Body);
  899. UBOdeNative.BodySetLinearVel(Body, 0, 0, 0); // stop it
  900. UBOdeNative.BodySetAngularVel(Body, 0, 0, 0);
  901. }
  902. if(m_prim_geom != IntPtr.Zero)
  903. UBOdeNative.GeomSetPosition(m_prim_geom, m_position.X, m_position.Y, m_position.Z);
  904. disableBodySoft(); // stop collisions
  905. UnSubscribeEvents();
  906. }
  907. }
  908. public override void SetMomentum(Vector3 momentum)
  909. {
  910. }
  911. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  912. public override void SetMaterial(int pMaterial)
  913. {
  914. m_material = pMaterial;
  915. mu = m_parentScene.m_materialContactsData[pMaterial].mu;
  916. bounce = m_parentScene.m_materialContactsData[pMaterial].bounce;
  917. }
  918. public override float Density
  919. {
  920. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  921. get
  922. {
  923. return m_density * 100f;
  924. }
  925. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  926. set
  927. {
  928. //float old = m_density;
  929. m_density = value / 100f;
  930. //if(m_density != old)
  931. // UpdatePrimBodyData();
  932. }
  933. }
  934. public override float GravModifier
  935. {
  936. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  937. get
  938. {
  939. return m_gravmod;
  940. }
  941. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  942. set
  943. {
  944. m_gravmod = value;
  945. if (m_vehicle != null)
  946. m_vehicle.GravMod = m_gravmod;
  947. }
  948. }
  949. public override float Friction
  950. {
  951. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  952. get
  953. {
  954. return mu;
  955. }
  956. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  957. set
  958. {
  959. mu = value;
  960. }
  961. }
  962. public override float Restitution
  963. {
  964. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  965. get
  966. {
  967. return bounce;
  968. }
  969. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  970. set
  971. {
  972. bounce = value;
  973. }
  974. }
  975. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  976. public void setPrimForRemoval()
  977. {
  978. AddChange(changes.Remove, null);
  979. }
  980. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  981. public override void link(PhysicsActor obj)
  982. {
  983. AddChange(changes.Link, obj);
  984. }
  985. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  986. public override void delink()
  987. {
  988. AddChange(changes.DeLink, null);
  989. }
  990. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  991. public override void LockAngularMotion(byte axislock)
  992. {
  993. //m_log.DebugFormat("[axislock]: <{0},{1},{2}>", axis.X, axis.Y, axis.Z);
  994. AddChange(changes.AngLock, axislock);
  995. }
  996. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  997. public override void SubscribeEvents(int ms)
  998. {
  999. m_eventsubscription = ms;
  1000. m_cureventsubscription = 0;
  1001. CollisionEvents ??= new CollisionEventUpdate();
  1002. CollisionVDTCEvents ??= new CollisionEventUpdate();
  1003. SentEmptyCollisionsEvent = false;
  1004. }
  1005. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1006. public override void UnSubscribeEvents()
  1007. {
  1008. if (CollisionVDTCEvents != null)
  1009. {
  1010. CollisionVDTCEvents.Clear();
  1011. CollisionVDTCEvents = null;
  1012. }
  1013. if (CollisionEvents != null)
  1014. {
  1015. CollisionEvents.Clear();
  1016. CollisionEvents = null;
  1017. }
  1018. m_eventsubscription = 0;
  1019. m_parentScene.RemoveCollisionEventReporting(this);
  1020. }
  1021. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1022. public override void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
  1023. {
  1024. CollisionEvents ??= new CollisionEventUpdate();
  1025. CollisionEvents.AddCollider(CollidedWith, contact);
  1026. m_parentScene.AddCollisionEventReporting(this);
  1027. }
  1028. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1029. public override void AddVDTCCollisionEvent(uint CollidedWith, ContactPoint contact)
  1030. {
  1031. CollisionVDTCEvents ??= new CollisionEventUpdate();
  1032. CollisionVDTCEvents.AddCollider(CollidedWith, contact);
  1033. m_parentScene.AddCollisionEventReporting(this);
  1034. }
  1035. internal void SleeperAddCollisionEvents()
  1036. {
  1037. if(CollisionEvents != null && CollisionEvents.m_objCollisionList.Count != 0)
  1038. {
  1039. foreach(KeyValuePair<uint,ContactPoint> kvp in CollisionEvents.m_objCollisionList)
  1040. {
  1041. if(kvp.Key == 0)
  1042. continue;
  1043. OdePrim other = m_parentScene.getPrim(kvp.Key);
  1044. if(other == null)
  1045. continue;
  1046. ContactPoint cp = kvp.Value;
  1047. cp.SurfaceNormal = - cp.SurfaceNormal;
  1048. cp.RelativeSpeed = -cp.RelativeSpeed;
  1049. other.AddCollisionEvent(ParentActor.m_baseLocalID, cp);
  1050. }
  1051. }
  1052. if(CollisionVDTCEvents != null && CollisionVDTCEvents.m_objCollisionList.Count != 0)
  1053. {
  1054. foreach(KeyValuePair<uint,ContactPoint> kvp in CollisionVDTCEvents.m_objCollisionList)
  1055. {
  1056. OdePrim other = m_parentScene.getPrim(kvp.Key);
  1057. if(other == null)
  1058. continue;
  1059. ContactPoint cp = kvp.Value;
  1060. cp.SurfaceNormal = - cp.SurfaceNormal;
  1061. cp.RelativeSpeed = -cp.RelativeSpeed;
  1062. other.AddCollisionEvent(ParentActor.m_baseLocalID, cp);
  1063. }
  1064. }
  1065. }
  1066. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1067. internal void clearSleeperCollisions()
  1068. {
  1069. if(CollisionVDTCEvents != null && CollisionVDTCEvents.Count >0 )
  1070. CollisionVDTCEvents.Clear();
  1071. }
  1072. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1073. public void SendCollisions(int timestep)
  1074. {
  1075. if (m_cureventsubscription < 50000)
  1076. m_cureventsubscription += timestep;
  1077. if (m_cureventsubscription < m_eventsubscription)
  1078. return;
  1079. if (CollisionEvents == null)
  1080. return;
  1081. int ncolisions = CollisionEvents.m_objCollisionList.Count;
  1082. if (!SentEmptyCollisionsEvent || ncolisions > 0)
  1083. {
  1084. base.SendCollisionUpdate(CollisionEvents);
  1085. m_cureventsubscription = 0;
  1086. if (ncolisions == 0)
  1087. {
  1088. SentEmptyCollisionsEvent = true;
  1089. //_parent_scene.RemoveCollisionEventReporting(this);
  1090. }
  1091. else if(Body == IntPtr.Zero || (UBOdeNative.BodyIsEnabled(Body) && m_bodydisablecontrol >= 0 ))
  1092. {
  1093. SentEmptyCollisionsEvent = false;
  1094. CollisionEvents.Clear();
  1095. }
  1096. }
  1097. }
  1098. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1099. public override bool SubscribedEvents()
  1100. {
  1101. if (m_eventsubscription > 0)
  1102. return true;
  1103. return false;
  1104. }
  1105. public OdePrim(String primName, ODEScene parent_scene, Vector3 pos, Vector3 size,
  1106. Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical,bool pisPhantom,byte _shapeType,uint plocalID)
  1107. {
  1108. m_parentScene = parent_scene;
  1109. Name = primName;
  1110. m_baseLocalID = plocalID;
  1111. m_vehicle = null;
  1112. if (!pos.IsFinite())
  1113. {
  1114. pos = new Vector3(((float)Constants.RegionSize * 0.5f), ((float)Constants.RegionSize * 0.5f),
  1115. parent_scene.GetTerrainHeightAtXY(((float)Constants.RegionSize * 0.5f), ((float)Constants.RegionSize * 0.5f)) + 0.5f);
  1116. m_log.WarnFormat("[PHYSICS]: Got nonFinite Object create Position for {0}", Name);
  1117. }
  1118. m_position = pos;
  1119. m_givefakepos = 0;
  1120. m_sceneTimeStep = parent_scene.ODE_STEPSIZE;
  1121. m_sceneInverseTimeStep = 1f / m_sceneTimeStep;
  1122. m_density = parent_scene.geomDefaultDensity;
  1123. m_body_autodisable_frames = parent_scene.bodyFramesAutoDisable;
  1124. m_prim_geom = IntPtr.Zero;
  1125. m_collide_geom = IntPtr.Zero;
  1126. Body = IntPtr.Zero;
  1127. if (!size.IsFinite())
  1128. {
  1129. size = new Vector3(0.5f, 0.5f, 0.5f);
  1130. m_log.WarnFormat("[PHYSICS]: Got nonFinite Object create Size for {0}", Name);
  1131. }
  1132. m_size.X = (size.X <= 0) ? 0.01f : size.X;
  1133. m_size.Y = (size.Y <= 0) ? 0.01f : size.Y;
  1134. m_size.Z = (size.Z <= 0) ? 0.01f : size.Z;
  1135. if (!QuaternionIsFinite(rotation))
  1136. {
  1137. rotation = Quaternion.Identity;
  1138. m_log.WarnFormat("[PHYSICS]: Got nonFinite Object create Rotation for {0}", Name);
  1139. }
  1140. m_orientation = rotation;
  1141. m_givefakeori = 0;
  1142. m_pbs = pbs;
  1143. m_targetSpace = IntPtr.Zero;
  1144. m_isphysical = pos.Z >= Constants.MinSimulationHeight && pos.Z <= Constants.MaxSimulationHeight && pisPhysical;
  1145. m_fakeisphysical = m_isphysical;
  1146. m_isVolumeDetect = false;
  1147. m_fakeisVolumeDetect = false;
  1148. m_force = Vector3.Zero;
  1149. m_iscolliding = false;
  1150. m_colliderfilter = 0;
  1151. m_NoColide = false;
  1152. _triMeshData = IntPtr.Zero;
  1153. m_fakeShapetype = _shapeType;
  1154. m_lastdoneSelected = false;
  1155. m_isSelected = false;
  1156. m_delaySelect = false;
  1157. m_isphantom = pisPhantom;
  1158. m_fakeisphantom = pisPhantom;
  1159. mu = parent_scene.m_materialContactsData[(int)Material.Wood].mu;
  1160. bounce = parent_scene.m_materialContactsData[(int)Material.Wood].bounce;
  1161. m_building = true; // control must set this to false when done
  1162. AddChange(changes.Add, null);
  1163. // get basic mass parameters
  1164. ODEPhysRepData repData = m_parentScene.m_meshWorker.NewActorPhysRep(this, m_pbs, m_size, _shapeType);
  1165. primVolume = repData.volume;
  1166. m_OBB = repData.OBB;
  1167. m_OBBOffset = repData.OBBOffset;
  1168. UpdatePrimBodyData();
  1169. }
  1170. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1171. private void resetCollisionAccounting()
  1172. {
  1173. m_collisionscore = 0;
  1174. }
  1175. private void UpdateCollisionCatFlags()
  1176. {
  1177. if(m_isphysical && m_disabled)
  1178. {
  1179. m_collisionCategories = 0;
  1180. m_collisionFlags = 0;
  1181. }
  1182. else if (m_isSelected)
  1183. {
  1184. m_collisionCategories = CollisionCategories.Selected;
  1185. m_collisionFlags = 0;
  1186. }
  1187. else if (m_isVolumeDetect)
  1188. {
  1189. m_collisionCategories = CollisionCategories.VolumeDtc;
  1190. if (m_isphysical)
  1191. m_collisionFlags = CollisionCategories.Geom | CollisionCategories.Character;
  1192. else
  1193. m_collisionFlags = 0;
  1194. }
  1195. else if (m_isphantom)
  1196. {
  1197. m_collisionCategories = CollisionCategories.Phantom;
  1198. if (m_isphysical)
  1199. m_collisionFlags = CollisionCategories.Land;
  1200. else
  1201. m_collisionFlags = 0;
  1202. }
  1203. else
  1204. {
  1205. m_collisionCategories = CollisionCategories.Geom;
  1206. if (m_isphysical)
  1207. m_collisionFlags = m_default_collisionFlagsPhysical;
  1208. else
  1209. m_collisionFlags = m_default_collisionFlagsNotPhysical;
  1210. }
  1211. }
  1212. private void ApplyCollisionCatFlags()
  1213. {
  1214. if (m_prim_geom != IntPtr.Zero)
  1215. {
  1216. if (!childPrim && childrenPrim.Count > 0)
  1217. {
  1218. foreach (OdePrim prm in childrenPrim)
  1219. {
  1220. if (m_isphysical && m_disabled)
  1221. {
  1222. prm.m_collisionCategories = 0;
  1223. prm.m_collisionFlags = 0;
  1224. }
  1225. else
  1226. {
  1227. // preserve some
  1228. if (prm.m_isSelected)
  1229. {
  1230. prm.m_collisionCategories = CollisionCategories.Selected;
  1231. prm.m_collisionFlags = 0;
  1232. }
  1233. else if (prm.m_isVolumeDetect)
  1234. {
  1235. prm.m_collisionCategories = CollisionCategories.VolumeDtc;
  1236. if (m_isphysical)
  1237. prm.m_collisionFlags = CollisionCategories.Geom | CollisionCategories.Character;
  1238. else
  1239. prm.m_collisionFlags = 0;
  1240. }
  1241. else if (prm.m_isphantom)
  1242. {
  1243. prm.m_collisionCategories = CollisionCategories.Phantom;
  1244. if (m_isphysical)
  1245. prm.m_collisionFlags = CollisionCategories.Land;
  1246. else
  1247. prm.m_collisionFlags = 0;
  1248. }
  1249. else
  1250. {
  1251. prm.m_collisionCategories = m_collisionCategories;
  1252. prm.m_collisionFlags = m_collisionFlags;
  1253. }
  1254. }
  1255. if (prm.m_prim_geom != IntPtr.Zero)
  1256. {
  1257. if (prm.m_NoColide)
  1258. {
  1259. UBOdeNative.GeomSetCategoryBits(prm.m_prim_geom, 0);
  1260. if (m_isphysical)
  1261. UBOdeNative.GeomSetCollideBits(prm.m_prim_geom, (int)CollisionCategories.Land);
  1262. else
  1263. UBOdeNative.GeomSetCollideBits(prm.m_prim_geom, 0);
  1264. }
  1265. else
  1266. {
  1267. UBOdeNative.GeomSetCategoryBits(prm.m_prim_geom, (uint)prm.m_collisionCategories);
  1268. UBOdeNative.GeomSetCollideBits(prm.m_prim_geom, (uint)prm.m_collisionFlags);
  1269. }
  1270. }
  1271. }
  1272. }
  1273. if (m_NoColide)
  1274. {
  1275. UBOdeNative.GeomSetCategoryBits(m_prim_geom, 0);
  1276. UBOdeNative.GeomSetCollideBits(m_prim_geom, (uint)CollisionCategories.Land);
  1277. if (m_collide_geom != m_prim_geom && m_collide_geom != IntPtr.Zero)
  1278. {
  1279. UBOdeNative.GeomSetCategoryBits(m_collide_geom, 0);
  1280. UBOdeNative.GeomSetCollideBits(m_collide_geom, (uint)CollisionCategories.Land);
  1281. }
  1282. }
  1283. else
  1284. {
  1285. UBOdeNative.GeomSetCategoryBits(m_prim_geom, (uint)m_collisionCategories);
  1286. UBOdeNative.GeomSetCollideBits(m_prim_geom, (uint)m_collisionFlags);
  1287. if (m_collide_geom != m_prim_geom && m_collide_geom != IntPtr.Zero)
  1288. {
  1289. UBOdeNative.GeomSetCategoryBits(m_collide_geom, (uint)m_collisionCategories);
  1290. UBOdeNative.GeomSetCollideBits(m_collide_geom, (uint)m_collisionFlags);
  1291. }
  1292. }
  1293. }
  1294. }
  1295. private void createAMotor(byte axislock)
  1296. {
  1297. if (Body == IntPtr.Zero)
  1298. return;
  1299. if (Amotor != IntPtr.Zero)
  1300. {
  1301. UBOdeNative.JointDestroy(Amotor);
  1302. Amotor = IntPtr.Zero;
  1303. }
  1304. int axisnum = 0;
  1305. bool axisX = false;
  1306. bool axisY = false;
  1307. bool axisZ = false;
  1308. if((axislock & 0x02) != 0)
  1309. {
  1310. axisnum++;
  1311. axisX = true;
  1312. }
  1313. if((axislock & 0x04) != 0)
  1314. {
  1315. axisnum++;
  1316. axisY = true;
  1317. }
  1318. if((axislock & 0x08) != 0)
  1319. {
  1320. axisnum++;
  1321. axisZ = true;
  1322. }
  1323. if(axisnum == 0)
  1324. return;
  1325. // stop it
  1326. UBOdeNative.BodySetTorque(Body, 0, 0, 0);
  1327. UBOdeNative.BodySetAngularVel(Body, 0, 0, 0);
  1328. Amotor = UBOdeNative.JointCreateAMotor(m_parentScene.world, IntPtr.Zero);
  1329. UBOdeNative.JointAttach(Amotor, Body, IntPtr.Zero);
  1330. UBOdeNative.JointSetAMotorMode(Amotor, 0);
  1331. UBOdeNative.JointSetAMotorNumAxes(Amotor, axisnum);
  1332. // get current orientation to lock
  1333. Quaternion curr= UBOdeNative.BodyGetQuaternionOMV(Body);
  1334. Vector3 ax;
  1335. int i = 0;
  1336. int j = 0;
  1337. if (axisX)
  1338. {
  1339. ax = (new Vector3(1, 0, 0)) * curr; // rotate world X to current local X
  1340. UBOdeNative.JointSetAMotorAxis(Amotor, 0, 0, ax.X, ax.Y, ax.Z);
  1341. UBOdeNative.JointSetAMotorAngle(Amotor, 0, 0);
  1342. UBOdeNative.JointSetAMotorParam(Amotor, (int)UBOdeNative.JointParam.LoStop, 0f);
  1343. UBOdeNative.JointSetAMotorParam(Amotor, (int)UBOdeNative.JointParam.HiStop, 0f);
  1344. UBOdeNative.JointSetAMotorParam(Amotor, (int)UBOdeNative.JointParam.Vel, 0);
  1345. UBOdeNative.JointSetAMotorParam(Amotor, (int)UBOdeNative.JointParam.FudgeFactor, 0.0001f);
  1346. UBOdeNative.JointSetAMotorParam(Amotor, (int)UBOdeNative.JointParam.Bounce, 0f);
  1347. UBOdeNative.JointSetAMotorParam(Amotor, (int)UBOdeNative.JointParam.CFM, 0f);
  1348. UBOdeNative.JointSetAMotorParam(Amotor, (int)UBOdeNative.JointParam.FMax, 5e8f);
  1349. UBOdeNative.JointSetAMotorParam(Amotor, (int)UBOdeNative.JointParam.StopCFM, 0f);
  1350. UBOdeNative.JointSetAMotorParam(Amotor, (int)UBOdeNative.JointParam.StopERP, 0.8f);
  1351. i++;
  1352. j = 256; // move to next axis set
  1353. }
  1354. if (axisY)
  1355. {
  1356. ax = (new Vector3(0, 1, 0)) * curr;
  1357. UBOdeNative.JointSetAMotorAxis(Amotor, i, 0, ax.X, ax.Y, ax.Z);
  1358. UBOdeNative.JointSetAMotorAngle(Amotor, i, 0);
  1359. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.LoStop, 0f);
  1360. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.HiStop, 0f);
  1361. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.Vel, 0);
  1362. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.FudgeFactor, 0.0001f);
  1363. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.Bounce, 0f);
  1364. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.CFM, 0f);
  1365. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.FMax, 5e8f);
  1366. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.StopCFM, 0f);
  1367. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.StopERP, 0.8f);
  1368. i++;
  1369. j += 256;
  1370. }
  1371. if (axisZ)
  1372. {
  1373. ax = (new Vector3(0, 0, 1)) * curr;
  1374. UBOdeNative.JointSetAMotorAxis(Amotor, i, 0, ax.X, ax.Y, ax.Z);
  1375. UBOdeNative.JointSetAMotorAngle(Amotor, i, 0);
  1376. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.LoStop, 0f);
  1377. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.HiStop, 0f);
  1378. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.Vel, 0);
  1379. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.FudgeFactor, 0.0001f);
  1380. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.Bounce, 0f);
  1381. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.CFM, 0f);
  1382. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.FMax, 5e8f);
  1383. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.StopCFM, 0f);
  1384. UBOdeNative.JointSetAMotorParam(Amotor, j + (int)UBOdeNative.JointParam.StopERP, 0.8f);
  1385. }
  1386. }
  1387. private void SetGeom(IntPtr geom)
  1388. {
  1389. m_prim_geom = geom;
  1390. //Console.WriteLine("SetGeom to " + prim_geom + " for " + Name);
  1391. if (m_prim_geom != IntPtr.Zero)
  1392. {
  1393. if (m_NoColide)
  1394. {
  1395. UBOdeNative.GeomSetCategoryBits(m_prim_geom, 0);
  1396. if (m_isphysical)
  1397. {
  1398. UBOdeNative.GeomSetCollideBits(m_prim_geom, (uint)CollisionCategories.Land);
  1399. }
  1400. else
  1401. {
  1402. UBOdeNative.GeomSetCollideBits(m_prim_geom, 0);
  1403. UBOdeNative.GeomDisable(m_prim_geom);
  1404. }
  1405. }
  1406. else
  1407. {
  1408. UBOdeNative.GeomSetCategoryBits(m_prim_geom, (uint)m_collisionCategories);
  1409. UBOdeNative.GeomSetCollideBits(m_prim_geom, (uint)m_collisionFlags);
  1410. }
  1411. UpdatePrimBodyData();
  1412. m_parentScene.actor_name_map[m_prim_geom] = this;
  1413. /*
  1414. // debug
  1415. d.AABB aabb;
  1416. d.GeomGetAABB(prim_geom, out aabb);
  1417. float x = aabb.MaxX - aabb.MinX;
  1418. float y = aabb.MaxY - aabb.MinY;
  1419. float z = aabb.MaxZ - aabb.MinZ;
  1420. if( x > 60.0f || y > 60.0f || z > 60.0f)
  1421. m_log.WarnFormat("[PHYSICS]: large prim geo {0},size {1}, AABBsize <{2},{3},{4}, mesh {5} at {6}",
  1422. Name, _size.ToString(), x, y, z, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh", _position.ToString());
  1423. else if (x < 0.001f || y < 0.001f || z < 0.001f)
  1424. m_log.WarnFormat("[PHYSICS]: small prim geo {0},size {1}, AABBsize <{2},{3},{4}, mesh {5} at {6}",
  1425. Name, _size.ToString(), x, y, z, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh", _position.ToString());
  1426. */
  1427. }
  1428. else
  1429. m_log.Warn("Setting bad Geom");
  1430. }
  1431. private bool GetMeshGeom()
  1432. {
  1433. IMesh mesh = m_mesh;
  1434. if (mesh is null)
  1435. return false;
  1436. mesh.getVertexListAsPtrToFloatArray(out IntPtr vertices, out int vertexStride, out int vertexCount);
  1437. mesh.getIndexListAsPtrToIntArray(out IntPtr indices, out int triStride, out int indexCount);
  1438. if (vertexCount == 0 || indexCount == 0)
  1439. {
  1440. m_log.WarnFormat("[PHYSICS]: Invalid mesh data on OdePrim {0}, mesh {1} at {2}",
  1441. Name, m_pbs.SculptEntry ? m_pbs.SculptTexture.ToString() : "primMesh", m_position.ToString());
  1442. //m_hasOBB = false;
  1443. m_OBBOffset = Vector3.Zero;
  1444. m_OBB = m_size * 0.5f;
  1445. m_physCost = 0.1f;
  1446. m_streamCost = 1.0f;
  1447. m_parentScene.mesher.ReleaseMesh(mesh);
  1448. m_meshState = MeshState.MeshFailed;
  1449. m_mesh = null;
  1450. return false;
  1451. }
  1452. if (vertexCount > 64000 || indexCount > 64000)
  1453. {
  1454. m_log.WarnFormat("[PHYSICS]: large mesh data on OdePrim {0}, mesh {1} at {2}, {3} vertices, {4} indexes",
  1455. Name, m_pbs.SculptEntry ? m_pbs.SculptTexture.ToString() : "primMesh",
  1456. m_position.ToString() ,vertexCount , indexCount );
  1457. }
  1458. IntPtr geo = IntPtr.Zero;
  1459. try
  1460. {
  1461. _triMeshData = UBOdeNative.GeomTriMeshDataCreate();
  1462. UBOdeNative.GeomTriMeshDataBuildSimple(_triMeshData, vertices, vertexStride, vertexCount, indices, indexCount, triStride);
  1463. UBOdeNative.GeomTriMeshDataPreprocess(_triMeshData);
  1464. geo = UBOdeNative.CreateTriMesh(m_targetSpace, _triMeshData, null, null, null);
  1465. }
  1466. catch (Exception e)
  1467. {
  1468. m_log.ErrorFormat("[PHYSICS]: SetGeom Mesh failed for {0} exception: {1}", Name, e);
  1469. if (_triMeshData != IntPtr.Zero)
  1470. {
  1471. try
  1472. {
  1473. UBOdeNative.GeomTriMeshDataDestroy(_triMeshData);
  1474. }
  1475. catch
  1476. {
  1477. }
  1478. }
  1479. _triMeshData = IntPtr.Zero;
  1480. //m_hasOBB = false;
  1481. m_OBBOffset = Vector3.Zero;
  1482. m_OBB = m_size * 0.5f;
  1483. m_physCost = 0.1f;
  1484. m_streamCost = 1.0f;
  1485. m_parentScene.mesher.ReleaseMesh(mesh);
  1486. m_meshState = MeshState.MeshFailed;
  1487. m_mesh = null;
  1488. return false;
  1489. }
  1490. m_physCost = 0.0013f * (float)indexCount;
  1491. // todo
  1492. m_streamCost = 1.0f;
  1493. SetGeom(geo);
  1494. return true;
  1495. }
  1496. private void CreateGeom(bool OverrideToBox)
  1497. {
  1498. bool hasMesh = false;
  1499. m_NoColide = false;
  1500. if ((m_meshState & MeshState.MeshNoColide) != 0)
  1501. m_NoColide = true;
  1502. else if(!OverrideToBox && m_mesh != null)
  1503. {
  1504. if (GetMeshGeom())
  1505. hasMesh = true;
  1506. else
  1507. m_NoColide = true;
  1508. }
  1509. if (!hasMesh)
  1510. {
  1511. IntPtr geo;
  1512. if (m_pbs.ProfileShape == ProfileShape.HalfCircle && m_pbs.PathCurve == (byte)Extrusion.Curve1
  1513. && m_size.X == m_size.Y && m_size.Y == m_size.Z)
  1514. { // it's a sphere
  1515. try
  1516. {
  1517. geo = UBOdeNative.CreateSphere(m_targetSpace, m_size.X * 0.5f);
  1518. }
  1519. catch (Exception e)
  1520. {
  1521. m_log.WarnFormat("[PHYSICS]: Create sphere failed: {0}", e);
  1522. return;
  1523. }
  1524. }
  1525. else
  1526. {// do it as a box
  1527. try
  1528. {
  1529. geo = UBOdeNative.CreateBox(m_targetSpace, m_size.X, m_size.Y, m_size.Z);
  1530. }
  1531. catch (Exception e)
  1532. {
  1533. m_log.Warn("[PHYSICS]: Create box failed: {0}", e);
  1534. return;
  1535. }
  1536. }
  1537. m_physCost = 0.1f;
  1538. m_streamCost = 1.0f;
  1539. SetGeom(geo);
  1540. }
  1541. }
  1542. private void RemoveGeom()
  1543. {
  1544. if (m_prim_geom != IntPtr.Zero)
  1545. {
  1546. m_parentScene.actor_name_map.Remove(m_prim_geom);
  1547. try
  1548. {
  1549. UBOdeNative.GeomDestroy(m_prim_geom);
  1550. if (_triMeshData != IntPtr.Zero)
  1551. {
  1552. UBOdeNative.GeomTriMeshDataDestroy(_triMeshData);
  1553. _triMeshData = IntPtr.Zero;
  1554. }
  1555. }
  1556. catch (Exception e)
  1557. {
  1558. m_log.ErrorFormat("[PHYSICS]: PrimGeom destruction failed for {0} exception {1}", Name, e);
  1559. }
  1560. m_prim_geom = IntPtr.Zero;
  1561. m_collide_geom = IntPtr.Zero;
  1562. m_targetSpace = IntPtr.Zero;
  1563. }
  1564. else
  1565. {
  1566. m_log.ErrorFormat("[PHYSICS]: PrimGeom destruction BAD {0}", Name);
  1567. }
  1568. lock (m_meshlock)
  1569. {
  1570. if (m_mesh != null)
  1571. {
  1572. m_parentScene.mesher.ReleaseMesh(m_mesh);
  1573. m_mesh = null;
  1574. }
  1575. }
  1576. Body = IntPtr.Zero;
  1577. //m_hasOBB = false;
  1578. }
  1579. //sets non physical prim m_targetSpace to right space in spaces grid for static prims
  1580. // should only be called for non physical prims unless they are becoming non physical
  1581. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1582. private void SetInStaticSpace(OdePrim prim)
  1583. {
  1584. IntPtr targetSpace = m_parentScene.MoveGeomToStaticSpace(prim.m_prim_geom, prim.m_targetSpace);
  1585. prim.m_targetSpace = targetSpace;
  1586. m_collide_geom = IntPtr.Zero;
  1587. }
  1588. public void enableBodySoft()
  1589. {
  1590. m_disabled = false;
  1591. if (!childPrim && !m_isSelected)
  1592. {
  1593. if (m_isphysical && Body != IntPtr.Zero)
  1594. {
  1595. UpdateCollisionCatFlags();
  1596. ApplyCollisionCatFlags();
  1597. _zeroFlag = true;
  1598. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  1599. UBOdeNative.BodyEnable(Body);
  1600. }
  1601. }
  1602. resetCollisionAccounting();
  1603. }
  1604. private void disableBodySoft()
  1605. {
  1606. m_disabled = true;
  1607. if (!childPrim)
  1608. {
  1609. if (m_isphysical && Body != IntPtr.Zero)
  1610. {
  1611. if (m_isSelected)
  1612. m_collisionFlags = CollisionCategories.Selected;
  1613. else
  1614. m_collisionCategories = 0;
  1615. m_collisionFlags = 0;
  1616. ApplyCollisionCatFlags();
  1617. UBOdeNative.BodyDisable(Body);
  1618. }
  1619. }
  1620. }
  1621. private void MakeBody()
  1622. {
  1623. if (!m_isphysical) // only physical get bodies
  1624. return;
  1625. if (childPrim) // child prims don't get bodies;
  1626. return;
  1627. if (m_building)
  1628. return;
  1629. if (m_prim_geom == IntPtr.Zero)
  1630. {
  1631. m_log.Warn("[PHYSICS]: Unable to link the linkset. Root has no geom yet");
  1632. return;
  1633. }
  1634. if (Body != IntPtr.Zero)
  1635. {
  1636. DestroyBody();
  1637. m_log.Warn("[PHYSICS]: MakeBody called having a body");
  1638. }
  1639. if (UBOdeNative.GeomGetBody(m_prim_geom) != IntPtr.Zero)
  1640. {
  1641. UBOdeNative.GeomSetBody(m_prim_geom, IntPtr.Zero);
  1642. m_log.Warn("[PHYSICS]: MakeBody root geom already had a body");
  1643. }
  1644. bool noInertiaOverride = (m_InertiaOverride == null);
  1645. Body = UBOdeNative.BodyCreate(m_parentScene.world);
  1646. // set the body rotation
  1647. UBOdeNative.Matrix3 mymat = new();
  1648. UBOdeNative.RfromQ(ref mymat, ref m_orientation);
  1649. UBOdeNative.BodySetRotation(Body, ref mymat);
  1650. UBOdeNative.Mass objdmass = new();
  1651. if (noInertiaOverride)
  1652. {
  1653. objdmass = primdMass;
  1654. UBOdeNative.MassRotate(ref objdmass, ref mymat);
  1655. }
  1656. // recompute full object inertia if needed
  1657. if (childrenPrim.Count > 0)
  1658. {
  1659. UBOdeNative.Matrix3 mat = new();
  1660. UBOdeNative.Mass tmpdmass;
  1661. Vector3 rcm;
  1662. rcm = m_position;
  1663. lock (childrenPrim)
  1664. {
  1665. foreach (OdePrim prm in childrenPrim)
  1666. {
  1667. if (prm.m_prim_geom == IntPtr.Zero)
  1668. {
  1669. m_log.Warn("[PHYSICS]: Unable to link one of the linkset elements, skipping it. No geom yet");
  1670. continue;
  1671. }
  1672. UBOdeNative.RfromQ(ref mat, ref prm.m_orientation);
  1673. // fix prim colision cats
  1674. if (UBOdeNative.GeomGetBody(prm.m_prim_geom) != IntPtr.Zero)
  1675. {
  1676. UBOdeNative.GeomSetBody(prm.m_prim_geom, IntPtr.Zero);
  1677. m_log.Warn("[PHYSICS]: MakeBody child geom already had a body");
  1678. }
  1679. UBOdeNative.GeomClearOffset(prm.m_prim_geom);
  1680. UBOdeNative.GeomSetBody(prm.m_prim_geom, Body);
  1681. prm.Body = Body;
  1682. UBOdeNative.GeomSetOffsetWorldRotation(prm.m_prim_geom, ref mat); // set relative rotation
  1683. if(noInertiaOverride)
  1684. {
  1685. tmpdmass = prm.primdMass;
  1686. UBOdeNative.MassRotate(ref tmpdmass, ref mat);
  1687. Vector3 ppos = prm.m_position;
  1688. ppos -= rcm;
  1689. // refer inertia to root prim center of mass position
  1690. UBOdeNative.MassTranslate(ref tmpdmass,
  1691. ppos.X,
  1692. ppos.Y,
  1693. ppos.Z);
  1694. UBOdeNative.MassAdd(ref objdmass, ref tmpdmass); // add to total object inertia
  1695. }
  1696. }
  1697. }
  1698. }
  1699. UBOdeNative.GeomClearOffset(m_prim_geom); // make sure we don't have a hidden offset
  1700. // associate root geom with body
  1701. UBOdeNative.GeomSetBody(m_prim_geom, Body);
  1702. if(noInertiaOverride)
  1703. UBOdeNative.BodySetPosition(Body, m_position.X + objdmass.c.X, m_position.Y + objdmass.c.Y, m_position.Z + objdmass.c.Z);
  1704. else
  1705. {
  1706. Vector3 ncm = m_InertiaOverride.CenterOfMass * m_orientation;
  1707. UBOdeNative.BodySetPosition(Body,
  1708. m_position.X + ncm.X,
  1709. m_position.Y + ncm.Y,
  1710. m_position.Z + ncm.Z);
  1711. }
  1712. UBOdeNative.GeomSetOffsetWorldPosition(m_prim_geom, m_position.X, m_position.Y, m_position.Z);
  1713. if(noInertiaOverride)
  1714. {
  1715. UBOdeNative.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
  1716. Quaternion mr = Quaternion.Conjugate(m_orientation);
  1717. UBOdeNative.RfromQ(ref mymat, ref mr);
  1718. UBOdeNative.MassRotate(ref objdmass, ref mymat);
  1719. UBOdeNative.BodySetMass(Body, ref objdmass);
  1720. m_mass = objdmass.mass;
  1721. }
  1722. else
  1723. {
  1724. objdmass.c.X = 0;
  1725. objdmass.c.Y = 0;
  1726. objdmass.c.Z = 0;
  1727. objdmass.I.M00 = m_InertiaOverride.Inertia.X;
  1728. objdmass.I.M11 = m_InertiaOverride.Inertia.Y;
  1729. objdmass.I.M22 = m_InertiaOverride.Inertia.Z;
  1730. objdmass.mass = m_InertiaOverride.TotalMass;
  1731. if(MathF.Abs(m_InertiaOverride.InertiaRotation.W) < 0.999f)
  1732. {
  1733. UBOdeNative.Matrix3 inertiarotmat = new();
  1734. UBOdeNative.RfromQ(ref inertiarotmat, ref m_InertiaOverride.InertiaRotation);
  1735. UBOdeNative.MassRotate(ref objdmass, ref inertiarotmat);
  1736. }
  1737. UBOdeNative.BodySetMass(Body, ref objdmass);
  1738. m_mass = objdmass.mass;
  1739. }
  1740. // disconnect from world gravity so we can apply buoyancy
  1741. UBOdeNative.BodySetGravityMode(Body, false);
  1742. UBOdeNative.BodySetAutoDisableFlag(Body, true);
  1743. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  1744. UBOdeNative.BodySetAutoDisableAngularThreshold(Body, 0.001f);
  1745. UBOdeNative.BodySetAutoDisableLinearThreshold(Body, 0.01f);
  1746. UBOdeNative.BodySetDamping(Body, .002f, .0005f);
  1747. if (m_targetSpace != IntPtr.Zero)
  1748. {
  1749. //m_parentScene.waitForSpaceUnlock(m_targetSpace);
  1750. if (UBOdeNative.SpaceQuery(m_targetSpace, m_prim_geom))
  1751. UBOdeNative.SpaceRemove(m_targetSpace, m_prim_geom);
  1752. }
  1753. if (childrenPrim.Count == 0)
  1754. {
  1755. m_collide_geom = m_prim_geom;
  1756. m_targetSpace = m_parentScene.ActiveSpace;
  1757. }
  1758. else
  1759. {
  1760. m_targetSpace = UBOdeNative.SimpleSpaceCreate(m_parentScene.ActiveSpace);
  1761. UBOdeNative.SpaceSetSublevel(m_targetSpace, 0);
  1762. UBOdeNative.SpaceSetCleanup(m_targetSpace, false);
  1763. UBOdeNative.GeomSetCategoryBits(m_targetSpace, (uint)(CollisionCategories.Space |
  1764. CollisionCategories.Geom |
  1765. CollisionCategories.Phantom |
  1766. CollisionCategories.VolumeDtc
  1767. ));
  1768. UBOdeNative.GeomSetCollideBits(m_targetSpace, 0);
  1769. m_collide_geom = m_targetSpace;
  1770. }
  1771. if (UBOdeNative.SpaceQuery(m_targetSpace, m_prim_geom))
  1772. m_log.Debug("[PRIM]: parent already in target space");
  1773. else
  1774. UBOdeNative.SpaceAdd(m_targetSpace, m_prim_geom);
  1775. if (m_delaySelect)
  1776. {
  1777. m_isSelected = true;
  1778. m_delaySelect = false;
  1779. }
  1780. m_collisionscore = 0;
  1781. UpdateCollisionCatFlags();
  1782. ApplyCollisionCatFlags();
  1783. m_parentScene.addActivePrim(this);
  1784. lock (childrenPrim)
  1785. {
  1786. foreach (OdePrim prm in childrenPrim)
  1787. {
  1788. IntPtr prmgeom = prm.m_prim_geom;
  1789. if (prmgeom == IntPtr.Zero)
  1790. continue;
  1791. Vector3 ppos = prm.m_position;
  1792. UBOdeNative.GeomSetOffsetWorldPosition(prm.m_prim_geom, ppos.X, ppos.Y, ppos.Z); // set relative position
  1793. IntPtr prmspace = prm.m_targetSpace;
  1794. if (prmspace != m_targetSpace)
  1795. {
  1796. if (prmspace != IntPtr.Zero)
  1797. {
  1798. //m_parentScene.waitForSpaceUnlock(prmspace);
  1799. if (UBOdeNative.SpaceQuery(prmspace, prmgeom))
  1800. UBOdeNative.SpaceRemove(prmspace, prmgeom);
  1801. }
  1802. prm.m_targetSpace = m_targetSpace;
  1803. if (UBOdeNative.SpaceQuery(m_targetSpace, prmgeom))
  1804. m_log.Debug("[PRIM]: child already in target space");
  1805. else
  1806. UBOdeNative.SpaceAdd(m_targetSpace, prmgeom);
  1807. }
  1808. prm.m_collisionscore = 0;
  1809. if(!m_disabled)
  1810. prm.m_disabled = false;
  1811. m_parentScene.addActivePrim(prm);
  1812. }
  1813. }
  1814. // The body doesn't already have a finite rotation mode set here
  1815. if (m_angularlocks != 0 && _parent == null)
  1816. {
  1817. createAMotor(m_angularlocks);
  1818. }
  1819. if (m_isSelected || m_disabled)
  1820. {
  1821. UBOdeNative.BodyDisable(Body);
  1822. _zeroFlag = true;
  1823. }
  1824. else
  1825. {
  1826. UBOdeNative.BodySetAngularVel(Body, m_rotationalVelocity.X, m_rotationalVelocity.Y, m_rotationalVelocity.Z);
  1827. UBOdeNative.BodySetLinearVel(Body, _velocity.X, _velocity.Y, _velocity.Z);
  1828. _zeroFlag = false;
  1829. m_bodydisablecontrol = 0;
  1830. }
  1831. m_parentScene.addActiveGroups(this);
  1832. }
  1833. private void DestroyBody()
  1834. {
  1835. if (Body != IntPtr.Zero)
  1836. {
  1837. m_parentScene.remActivePrim(this);
  1838. m_collide_geom = IntPtr.Zero;
  1839. if (m_disabled)
  1840. m_collisionCategories = 0;
  1841. else if (m_isSelected)
  1842. m_collisionCategories = CollisionCategories.Selected;
  1843. else if (m_isVolumeDetect)
  1844. m_collisionCategories = CollisionCategories.VolumeDtc;
  1845. else if (m_isphantom)
  1846. m_collisionCategories = CollisionCategories.Phantom;
  1847. else
  1848. m_collisionCategories = CollisionCategories.Geom;
  1849. m_collisionFlags = 0;
  1850. if (m_prim_geom != IntPtr.Zero)
  1851. {
  1852. if (m_NoColide)
  1853. {
  1854. UBOdeNative.GeomSetCategoryBits(m_prim_geom, 0);
  1855. UBOdeNative.GeomSetCollideBits(m_prim_geom, 0);
  1856. }
  1857. else
  1858. {
  1859. UBOdeNative.GeomSetCategoryBits(m_prim_geom, (uint)m_collisionCategories);
  1860. UBOdeNative.GeomSetCollideBits(m_prim_geom, (uint)m_collisionFlags);
  1861. }
  1862. UpdateDataFromGeom();
  1863. UBOdeNative.GeomSetBody(m_prim_geom, IntPtr.Zero);
  1864. SetInStaticSpace(this);
  1865. }
  1866. if (!childPrim)
  1867. {
  1868. lock (childrenPrim)
  1869. {
  1870. foreach (OdePrim prm in childrenPrim)
  1871. {
  1872. m_parentScene.remActivePrim(prm);
  1873. if (prm.m_isSelected)
  1874. prm.m_collisionCategories = CollisionCategories.Selected;
  1875. else if (prm.m_isVolumeDetect)
  1876. prm.m_collisionCategories = CollisionCategories.VolumeDtc;
  1877. else if (prm.m_isphantom)
  1878. prm.m_collisionCategories = CollisionCategories.Phantom;
  1879. else
  1880. prm.m_collisionCategories = CollisionCategories.Geom;
  1881. prm.m_collisionFlags = 0;
  1882. if (prm.m_prim_geom != IntPtr.Zero)
  1883. {
  1884. if (prm.m_NoColide)
  1885. {
  1886. UBOdeNative.GeomSetCategoryBits(prm.m_prim_geom, 0);
  1887. UBOdeNative.GeomSetCollideBits(prm.m_prim_geom, 0);
  1888. }
  1889. else
  1890. {
  1891. UBOdeNative.GeomSetCategoryBits(prm.m_prim_geom, (uint)prm.m_collisionCategories);
  1892. UBOdeNative.GeomSetCollideBits(prm.m_prim_geom, (uint)prm.m_collisionFlags);
  1893. }
  1894. prm.UpdateDataFromGeom();
  1895. SetInStaticSpace(prm);
  1896. }
  1897. prm.Body = IntPtr.Zero;
  1898. prm.m_mass = prm.primMass;
  1899. prm.m_collisionscore = 0;
  1900. }
  1901. }
  1902. if (Amotor != IntPtr.Zero)
  1903. {
  1904. UBOdeNative.JointDestroy(Amotor);
  1905. Amotor = IntPtr.Zero;
  1906. }
  1907. m_parentScene.remActiveGroup(this);
  1908. UBOdeNative.BodyDestroy(Body);
  1909. }
  1910. Body = IntPtr.Zero;
  1911. }
  1912. m_mass = primMass;
  1913. m_collisionscore = 0;
  1914. }
  1915. /*
  1916. private void FixInertia(Vector3 NewPos,Quaternion newrot)
  1917. {
  1918. UBOdeNative.BodyGetMass(Body, out UBOdeNative.Mass tmpdmass);
  1919. UBOdeNative.Mass objdmass = tmpdmass;
  1920. UBOdeNative.Vector3 dobjpos;
  1921. UBOdeNative.Vector3 thispos;
  1922. // get current object position and rotation
  1923. dobjpos = UBOdeNative.BodyGetPosition(Body);
  1924. // get prim own inertia in its local frame
  1925. tmpdmass = primdMass;
  1926. // transform to object frame
  1927. UBOdeNative.Matrix3 mat = UBOdeNative.GeomGetOffsetRotation(m_prim_geom);
  1928. UBOdeNative.MassRotate(ref tmpdmass, ref mat);
  1929. thispos = UBOdeNative.GeomGetOffsetPosition(m_prim_geom);
  1930. UBOdeNative.MassTranslate(ref tmpdmass,
  1931. thispos.X,
  1932. thispos.Y,
  1933. thispos.Z);
  1934. // subtract current prim inertia from object
  1935. DMassSubPartFromObj(ref tmpdmass, ref objdmass);
  1936. // back prim own inertia
  1937. tmpdmass = primdMass;
  1938. // update to new position and orientation
  1939. m_position = NewPos;
  1940. UBOdeNative.GeomSetOffsetWorldPosition(m_prim_geom, NewPos.X, NewPos.Y, NewPos.Z);
  1941. m_orientation = newrot;
  1942. UBOdeNative.Quaternion quat = new()
  1943. {
  1944. X = newrot.X,
  1945. Y = newrot.Y,
  1946. Z = newrot.Z,
  1947. W = newrot.W
  1948. };
  1949. UBOdeNative.GeomSetOffsetWorldQuaternion(m_prim_geom, ref quat);
  1950. mat = UBOdeNative.GeomGetOffsetRotation(m_prim_geom);
  1951. UBOdeNative.MassRotate(ref tmpdmass, ref mat);
  1952. thispos = UBOdeNative.GeomGetOffsetPosition(m_prim_geom);
  1953. UBOdeNative.MassTranslate(ref tmpdmass,
  1954. thispos.X,
  1955. thispos.Y,
  1956. thispos.Z);
  1957. UBOdeNative.MassAdd(ref objdmass, ref tmpdmass);
  1958. // fix all positions
  1959. IntPtr g = UBOdeNative.BodyGetFirstGeom(Body);
  1960. while (g != IntPtr.Zero)
  1961. {
  1962. thispos = UBOdeNative.GeomGetOffsetPosition(g);
  1963. thispos.X -= objdmass.c.X;
  1964. thispos.Y -= objdmass.c.Y;
  1965. thispos.Z -= objdmass.c.Z;
  1966. UBOdeNative.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z);
  1967. g = UBOdeNative.dBodyGetNextGeom(g);
  1968. }
  1969. UBOdeNative.BodyVectorToWorld(Body,objdmass.c.X, objdmass.c.Y, objdmass.c.Z,out thispos);
  1970. UBOdeNative.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z);
  1971. UBOdeNative.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
  1972. UBOdeNative.BodySetMass(Body, ref objdmass);
  1973. m_mass = objdmass.mass;
  1974. }
  1975. */
  1976. private void FixInertia(Vector3 NewPos)
  1977. {
  1978. UBOdeNative.Matrix3 primmat;
  1979. UBOdeNative.Mass tmpdmass;
  1980. UBOdeNative.Mass primmass;
  1981. UBOdeNative.Vector3 dobjpos;
  1982. UBOdeNative.Vector3 thispos;
  1983. UBOdeNative.BodyGetMass(Body, out UBOdeNative.Mass objdmass);
  1984. // get prim own inertia in its local frame
  1985. primmass = primdMass;
  1986. // transform to object frame
  1987. primmat = UBOdeNative.GeomGetOffsetRotation(m_prim_geom);
  1988. UBOdeNative.MassRotate(ref primmass, ref primmat);
  1989. tmpdmass = primmass;
  1990. thispos = UBOdeNative.GeomGetOffsetPosition(m_prim_geom);
  1991. UBOdeNative.MassTranslate(ref tmpdmass,
  1992. thispos.X,
  1993. thispos.Y,
  1994. thispos.Z);
  1995. // subtract current prim inertia from object
  1996. DMassSubPartFromObj(ref tmpdmass, ref objdmass);
  1997. // update to new position
  1998. m_position = NewPos;
  1999. UBOdeNative.GeomSetOffsetWorldPosition(m_prim_geom, NewPos.X, NewPos.Y, NewPos.Z);
  2000. thispos = UBOdeNative.GeomGetOffsetPosition(m_prim_geom);
  2001. UBOdeNative.MassTranslate(ref primmass,
  2002. thispos.X,
  2003. thispos.Y,
  2004. thispos.Z);
  2005. UBOdeNative.MassAdd(ref objdmass, ref primmass);
  2006. // fix all positions
  2007. IntPtr g = UBOdeNative.BodyGetFirstGeom(Body);
  2008. while (g != IntPtr.Zero)
  2009. {
  2010. thispos = UBOdeNative.GeomGetOffsetPosition(g);
  2011. thispos.X -= objdmass.c.X;
  2012. thispos.Y -= objdmass.c.Y;
  2013. thispos.Z -= objdmass.c.Z;
  2014. UBOdeNative.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z);
  2015. g = UBOdeNative.dBodyGetNextGeom(g);
  2016. }
  2017. UBOdeNative.BodyVectorToWorld(Body, objdmass.c.X, objdmass.c.Y, objdmass.c.Z, out thispos);
  2018. // get current object position and rotation
  2019. dobjpos = UBOdeNative.BodyGetPosition(Body);
  2020. UBOdeNative.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z);
  2021. UBOdeNative.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
  2022. UBOdeNative.BodySetMass(Body, ref objdmass);
  2023. m_mass = objdmass.mass;
  2024. }
  2025. /*
  2026. private void FixInertia(Quaternion newrot)
  2027. {
  2028. UBOdeNative.Matrix3 mat;
  2029. UBOdeNative.Quaternion quat = new();
  2030. UBOdeNative.Vector3 dobjpos;
  2031. UBOdeNative.Vector3 thispos;
  2032. UBOdeNative.BodyGetMass(Body, out UBOdeNative.Mass objdmass);
  2033. // get prim own inertia in its local frame
  2034. UBOdeNative.Mass tmpdmass = primdMass;
  2035. mat = UBOdeNative.GeomGetOffsetRotation(m_prim_geom);
  2036. UBOdeNative.MassRotate(ref tmpdmass, ref mat);
  2037. // transform to object frame
  2038. thispos = UBOdeNative.GeomGetOffsetPosition(m_prim_geom);
  2039. UBOdeNative.MassTranslate(ref tmpdmass,
  2040. thispos.X,
  2041. thispos.Y,
  2042. thispos.Z);
  2043. // subtract current prim inertia from object
  2044. DMassSubPartFromObj(ref tmpdmass, ref objdmass);
  2045. // update to new orientation
  2046. m_orientation = newrot;
  2047. quat.X = newrot.X;
  2048. quat.Y = newrot.Y;
  2049. quat.Z = newrot.Z;
  2050. quat.W = newrot.W;
  2051. UBOdeNative.GeomSetOffsetWorldQuaternion(m_prim_geom, ref quat);
  2052. tmpdmass = primdMass;
  2053. mat = UBOdeNative.GeomGetOffsetRotation(m_prim_geom);
  2054. UBOdeNative.MassRotate(ref tmpdmass, ref mat);
  2055. UBOdeNative.MassTranslate(ref tmpdmass,
  2056. thispos.X,
  2057. thispos.Y,
  2058. thispos.Z);
  2059. UBOdeNative.MassAdd(ref objdmass, ref tmpdmass);
  2060. // fix all positions
  2061. IntPtr g = UBOdeNative.BodyGetFirstGeom(Body);
  2062. while (g != IntPtr.Zero)
  2063. {
  2064. thispos = UBOdeNative.GeomGetOffsetPosition(g);
  2065. thispos.X -= objdmass.c.X;
  2066. thispos.Y -= objdmass.c.Y;
  2067. thispos.Z -= objdmass.c.Z;
  2068. UBOdeNative.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z);
  2069. g = UBOdeNative.dBodyGetNextGeom(g);
  2070. }
  2071. UBOdeNative.BodyVectorToWorld(Body, objdmass.c.X, objdmass.c.Y, objdmass.c.Z, out thispos);
  2072. // get current object position and rotation
  2073. dobjpos = UBOdeNative.BodyGetPosition(Body);
  2074. UBOdeNative.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z);
  2075. UBOdeNative.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
  2076. UBOdeNative.BodySetMass(Body, ref objdmass);
  2077. m_mass = objdmass.mass;
  2078. }
  2079. */
  2080. #region Mass Calculation
  2081. private void UpdatePrimBodyData()
  2082. {
  2083. primMass = m_density * primVolume;
  2084. if (primMass <= 0)
  2085. primMass = 0.0001f;//ckrinke: Mass must be greater then zero.
  2086. if (primMass > m_parentScene.maximumMassObject)
  2087. primMass = m_parentScene.maximumMassObject;
  2088. m_mass = primMass; // just in case
  2089. UBOdeNative.MassSetBoxTotal(out primdMass, primMass, 2.0f * m_OBB.X, 2.0f * m_OBB.Y, 2.0f * m_OBB.Z);
  2090. UBOdeNative.MassTranslate(ref primdMass,
  2091. m_OBBOffset.X,
  2092. m_OBBOffset.Y,
  2093. m_OBBOffset.Z);
  2094. primOOBradiusSQ = m_OBB.LengthSquared();
  2095. if (_triMeshData != IntPtr.Zero)
  2096. {
  2097. float pc = m_physCost;
  2098. float psf = primOOBradiusSQ;
  2099. psf *= 1.33f * .2f;
  2100. pc *= psf;
  2101. if (pc < 0.1f)
  2102. pc = 0.1f;
  2103. m_physCost = pc;
  2104. }
  2105. else
  2106. m_physCost = 0.1f;
  2107. m_streamCost = 1.0f;
  2108. }
  2109. #endregion
  2110. /// <summary>
  2111. /// Add a child prim to this parent prim.
  2112. /// </summary>
  2113. /// <param name="prim">Child prim</param>
  2114. // I'm the parent
  2115. // prim is the child
  2116. public void ParentPrim(OdePrim prim)
  2117. {
  2118. //Console.WriteLine("ParentPrim " + m_primName);
  2119. if (m_baseLocalID != prim.m_baseLocalID)
  2120. {
  2121. DestroyBody(); // for now we need to rebuil entire object on link change
  2122. lock (childrenPrim)
  2123. {
  2124. // adopt the prim
  2125. if (!childrenPrim.Contains(prim))
  2126. childrenPrim.Add(prim);
  2127. // see if this prim has kids and adopt them also
  2128. // should not happen for now
  2129. foreach (OdePrim prm in prim.childrenPrim)
  2130. {
  2131. if (!childrenPrim.Contains(prm))
  2132. {
  2133. if (prm.Body != IntPtr.Zero)
  2134. {
  2135. if (prm.m_prim_geom != IntPtr.Zero)
  2136. UBOdeNative.GeomSetBody(prm.m_prim_geom, IntPtr.Zero);
  2137. if (prm.Body != prim.Body)
  2138. prm.DestroyBody(); // don't loose bodies around
  2139. prm.Body = IntPtr.Zero;
  2140. }
  2141. childrenPrim.Add(prm);
  2142. prm._parent = this;
  2143. }
  2144. }
  2145. }
  2146. //Remove old children from the prim
  2147. prim.childrenPrim.Clear();
  2148. if (prim.Body != IntPtr.Zero)
  2149. {
  2150. if (prim.m_prim_geom != IntPtr.Zero)
  2151. UBOdeNative.GeomSetBody(prim.m_prim_geom, IntPtr.Zero);
  2152. prim.DestroyBody(); // don't loose bodies around
  2153. prim.Body = IntPtr.Zero;
  2154. }
  2155. prim.childPrim = true;
  2156. prim._parent = this;
  2157. MakeBody(); // full nasty reconstruction
  2158. }
  2159. }
  2160. /*
  2161. private void UpdateChildsfromgeom()
  2162. {
  2163. if (childrenPrim.Count > 0)
  2164. {
  2165. foreach (OdePrim prm in childrenPrim)
  2166. prm.UpdateDataFromGeom();
  2167. }
  2168. }
  2169. */
  2170. private void UpdateDataFromGeom()
  2171. {
  2172. if (m_prim_geom != IntPtr.Zero)
  2173. {
  2174. m_orientation = UBOdeNative.GeomGetQuaternionOMV(m_prim_geom);
  2175. /*
  2176. // Debug
  2177. float qlen = _orientation.Length();
  2178. if (qlen > 1.01f || qlen < 0.99)
  2179. m_log.WarnFormat("[PHYSICS]: Got nonnorm quaternion from geom in Object {0} norm {1}", Name, qlen);
  2180. */
  2181. m_orientation.Normalize();
  2182. m_position = UBOdeNative.GeomGetPositionOMV(m_prim_geom);
  2183. }
  2184. }
  2185. private void ChildDelink(OdePrim odePrim, bool remakebodies)
  2186. {
  2187. // Okay, we have a delinked child.. destroy all body and remake
  2188. if (odePrim != this && !childrenPrim.Contains(odePrim))
  2189. return;
  2190. DestroyBody();
  2191. if (odePrim == this) // delinking the root prim
  2192. {
  2193. OdePrim newroot = null;
  2194. lock (childrenPrim)
  2195. {
  2196. if (childrenPrim.Count > 0)
  2197. {
  2198. newroot = childrenPrim[0];
  2199. childrenPrim.RemoveAt(0);
  2200. foreach (OdePrim prm in childrenPrim)
  2201. {
  2202. newroot.childrenPrim.Add(prm);
  2203. }
  2204. childrenPrim.Clear();
  2205. }
  2206. if (newroot != null)
  2207. {
  2208. newroot.childPrim = false;
  2209. newroot._parent = null;
  2210. if (remakebodies)
  2211. newroot.MakeBody();
  2212. }
  2213. }
  2214. }
  2215. else
  2216. {
  2217. lock (childrenPrim)
  2218. {
  2219. childrenPrim.Remove(odePrim);
  2220. odePrim.childPrim = false;
  2221. odePrim._parent = null;
  2222. //odePrim.UpdateDataFromGeom();
  2223. if (remakebodies)
  2224. odePrim.MakeBody();
  2225. }
  2226. }
  2227. if (remakebodies)
  2228. MakeBody();
  2229. }
  2230. protected void ChildRemove(OdePrim odePrim, bool reMakeBody)
  2231. {
  2232. // Okay, we have a delinked child.. destroy all body and remake
  2233. if (odePrim != this && !childrenPrim.Contains(odePrim))
  2234. return;
  2235. DestroyBody();
  2236. if (odePrim == this)
  2237. {
  2238. OdePrim newroot = null;
  2239. lock (childrenPrim)
  2240. {
  2241. if (childrenPrim.Count > 0)
  2242. {
  2243. newroot = childrenPrim[0];
  2244. childrenPrim.RemoveAt(0);
  2245. foreach (OdePrim prm in childrenPrim)
  2246. {
  2247. newroot.childrenPrim.Add(prm);
  2248. }
  2249. childrenPrim.Clear();
  2250. }
  2251. if (newroot != null)
  2252. {
  2253. newroot.childPrim = false;
  2254. newroot._parent = null;
  2255. newroot.MakeBody();
  2256. }
  2257. }
  2258. if (reMakeBody)
  2259. MakeBody();
  2260. return;
  2261. }
  2262. else
  2263. {
  2264. lock (childrenPrim)
  2265. {
  2266. childrenPrim.Remove(odePrim);
  2267. odePrim.childPrim = false;
  2268. odePrim._parent = null;
  2269. if (reMakeBody)
  2270. odePrim.MakeBody();
  2271. }
  2272. }
  2273. MakeBody();
  2274. }
  2275. #region changes
  2276. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2277. private void changeadd()
  2278. {
  2279. m_parentScene.addToPrims(this);
  2280. }
  2281. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2282. private void changeAngularLock(byte newLocks)
  2283. {
  2284. // do we have a Physical object?
  2285. if (Body != IntPtr.Zero)
  2286. {
  2287. //Check that we have a Parent
  2288. //If we have a parent then we're not authorative here
  2289. if (_parent == null)
  2290. {
  2291. if (newLocks != 0)
  2292. {
  2293. createAMotor(newLocks);
  2294. }
  2295. else
  2296. {
  2297. if (Amotor != IntPtr.Zero)
  2298. {
  2299. UBOdeNative.JointDestroy(Amotor);
  2300. Amotor = IntPtr.Zero;
  2301. }
  2302. }
  2303. }
  2304. }
  2305. // Store this for later in case we get turned into a separate body
  2306. m_angularlocks = newLocks;
  2307. }
  2308. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2309. private void changeLink(OdePrim NewParent)
  2310. {
  2311. if (_parent == null && NewParent != null)
  2312. {
  2313. NewParent.ParentPrim(this);
  2314. }
  2315. else if (_parent != null)
  2316. {
  2317. if (_parent is OdePrim)
  2318. {
  2319. if (NewParent != _parent)
  2320. {
  2321. (_parent as OdePrim).ChildDelink(this, false); // for now...
  2322. childPrim = false;
  2323. NewParent?.ParentPrim(this);
  2324. }
  2325. }
  2326. }
  2327. _parent = NewParent;
  2328. }
  2329. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2330. private void Stop()
  2331. {
  2332. if (!childPrim)
  2333. {
  2334. //m_force = Vector3.Zero;
  2335. m_forceacc = Vector3.Zero;
  2336. m_angularForceacc = Vector3.Zero;
  2337. //m_torque = Vector3.Zero;
  2338. _velocity = Vector3.Zero;
  2339. m_acceleration = Vector3.Zero;
  2340. m_rotationalVelocity = Vector3.Zero;
  2341. _target_velocity = Vector3.Zero;
  2342. if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
  2343. m_vehicle.Stop();
  2344. _zeroFlag = false;
  2345. base.RequestPhysicsterseUpdate();
  2346. }
  2347. if (Body != IntPtr.Zero)
  2348. {
  2349. UBOdeNative.BodySetForce(Body, 0f, 0f, 0f);
  2350. UBOdeNative.BodySetTorque(Body, 0f, 0f, 0f);
  2351. UBOdeNative.BodySetLinearVel(Body, 0f, 0f, 0f);
  2352. UBOdeNative.BodySetAngularVel(Body, 0f, 0f, 0f);
  2353. }
  2354. }
  2355. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2356. private void changePhantomStatus(bool newval)
  2357. {
  2358. m_isphantom = newval;
  2359. UpdateCollisionCatFlags();
  2360. ApplyCollisionCatFlags();
  2361. }
  2362. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2363. private void changeSelectedStatus(bool newval)
  2364. {
  2365. if (m_lastdoneSelected == newval)
  2366. return;
  2367. m_lastdoneSelected = newval;
  2368. DoSelectedStatus(newval);
  2369. }
  2370. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2371. private void CheckDelaySelect()
  2372. {
  2373. if (m_delaySelect)
  2374. {
  2375. DoSelectedStatus(m_isSelected);
  2376. }
  2377. }
  2378. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2379. private void DoSelectedStatus(bool newval)
  2380. {
  2381. m_isSelected = newval;
  2382. Stop();
  2383. if (newval)
  2384. {
  2385. if (!childPrim && Body != IntPtr.Zero)
  2386. UBOdeNative.BodyDisable(Body);
  2387. if (m_delaySelect || m_isphysical)
  2388. {
  2389. m_collisionCategories = CollisionCategories.Selected;
  2390. m_collisionFlags = 0;
  2391. if (!childPrim)
  2392. {
  2393. foreach (OdePrim prm in childrenPrim)
  2394. {
  2395. prm.m_collisionCategories = m_collisionCategories;
  2396. prm.m_collisionFlags = m_collisionFlags;
  2397. if (prm.m_prim_geom != IntPtr.Zero)
  2398. {
  2399. if (prm.m_NoColide)
  2400. {
  2401. UBOdeNative.GeomSetCategoryBits(prm.m_prim_geom, 0);
  2402. UBOdeNative.GeomSetCollideBits(prm.m_prim_geom, 0);
  2403. }
  2404. else
  2405. {
  2406. UBOdeNative.GeomSetCategoryBits(prm.m_prim_geom, (uint)m_collisionCategories);
  2407. UBOdeNative.GeomSetCollideBits(prm.m_prim_geom, (uint)m_collisionFlags);
  2408. }
  2409. }
  2410. prm.m_delaySelect = false;
  2411. }
  2412. }
  2413. if (m_prim_geom != IntPtr.Zero)
  2414. {
  2415. if (m_NoColide)
  2416. {
  2417. UBOdeNative.GeomSetCategoryBits(m_prim_geom, 0);
  2418. UBOdeNative.GeomSetCollideBits(m_prim_geom, 0);
  2419. if (m_collide_geom != m_prim_geom && m_collide_geom != IntPtr.Zero)
  2420. {
  2421. UBOdeNative.GeomSetCategoryBits(m_collide_geom, 0);
  2422. UBOdeNative.GeomSetCollideBits(m_collide_geom, 0);
  2423. }
  2424. }
  2425. else
  2426. {
  2427. UBOdeNative.GeomSetCategoryBits(m_prim_geom, (uint)m_collisionCategories);
  2428. UBOdeNative.GeomSetCollideBits(m_prim_geom, (uint)m_collisionFlags);
  2429. if (m_collide_geom != m_prim_geom && m_collide_geom != IntPtr.Zero)
  2430. {
  2431. UBOdeNative.GeomSetCategoryBits(m_collide_geom, (uint)m_collisionCategories);
  2432. UBOdeNative.GeomSetCollideBits(m_collide_geom, (uint)m_collisionFlags);
  2433. }
  2434. }
  2435. }
  2436. m_delaySelect = false;
  2437. }
  2438. else if(!m_isphysical)
  2439. {
  2440. m_delaySelect = true;
  2441. }
  2442. }
  2443. else
  2444. {
  2445. if (!childPrim)
  2446. {
  2447. if (Body != IntPtr.Zero && !m_disabled)
  2448. {
  2449. _zeroFlag = true;
  2450. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2451. UBOdeNative.BodyEnable(Body);
  2452. }
  2453. }
  2454. UpdateCollisionCatFlags();
  2455. ApplyCollisionCatFlags();
  2456. m_delaySelect = false;
  2457. }
  2458. resetCollisionAccounting();
  2459. }
  2460. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2461. private void changePosition(Vector3 newPos)
  2462. {
  2463. CheckDelaySelect();
  2464. if (m_isphysical)
  2465. {
  2466. if (childPrim) // inertia is messed, must rebuild
  2467. {
  2468. if (m_building)
  2469. {
  2470. m_position = newPos;
  2471. }
  2472. else if (m_forcePosOrRotation && Body != IntPtr.Zero && m_position.NotEqual(newPos))
  2473. {
  2474. FixInertia(newPos);
  2475. if (!UBOdeNative.BodyIsEnabled(Body))
  2476. {
  2477. _zeroFlag = true;
  2478. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2479. UBOdeNative.BodyEnable(Body);
  2480. }
  2481. }
  2482. }
  2483. else
  2484. {
  2485. if (m_position.NotEqual(newPos))
  2486. {
  2487. UBOdeNative.GeomSetPosition(m_prim_geom, newPos.X, newPos.Y, newPos.Z);
  2488. m_position = newPos;
  2489. }
  2490. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  2491. {
  2492. _zeroFlag = true;
  2493. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2494. UBOdeNative.BodyEnable(Body);
  2495. }
  2496. }
  2497. }
  2498. else
  2499. {
  2500. if (m_prim_geom != IntPtr.Zero)
  2501. {
  2502. if (m_position.NotEqual(newPos))
  2503. {
  2504. UBOdeNative.GeomSetPosition(m_prim_geom, newPos.X, newPos.Y, newPos.Z);
  2505. m_position = newPos;
  2506. m_targetSpace = m_parentScene.MoveGeomToStaticSpace(m_prim_geom, m_targetSpace);
  2507. }
  2508. }
  2509. }
  2510. m_givefakepos--;
  2511. if (m_givefakepos < 0)
  2512. m_givefakepos = 0;
  2513. //changeSelectedStatus();
  2514. resetCollisionAccounting();
  2515. }
  2516. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2517. private void changeOrientation(Quaternion newOri)
  2518. {
  2519. CheckDelaySelect();
  2520. if (m_isphysical)
  2521. {
  2522. if (childPrim) // inertia is messed, must rebuild
  2523. {
  2524. if (m_building)
  2525. {
  2526. m_orientation = newOri;
  2527. }
  2528. /*
  2529. else if (m_forcePosOrRotation && _orientation != newOri && Body != IntPtr.Zero)
  2530. {
  2531. FixInertia(_position, newOri);
  2532. if (!d.BodyIsEnabled(Body))
  2533. d.BodyEnable(Body);
  2534. }
  2535. */
  2536. }
  2537. else
  2538. {
  2539. if (newOri.NotEqual(m_orientation))
  2540. {
  2541. UBOdeNative.Quaternion myrot = new()
  2542. {
  2543. X = newOri.X,
  2544. Y = newOri.Y,
  2545. Z = newOri.Z,
  2546. W = newOri.W
  2547. };
  2548. UBOdeNative.GeomSetQuaternion(m_prim_geom, ref myrot);
  2549. m_orientation = newOri;
  2550. if (Body != IntPtr.Zero)
  2551. {
  2552. if(m_angularlocks != 0)
  2553. createAMotor(m_angularlocks);
  2554. }
  2555. }
  2556. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  2557. {
  2558. _zeroFlag = true;
  2559. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2560. UBOdeNative.BodyEnable(Body);
  2561. }
  2562. }
  2563. }
  2564. else
  2565. {
  2566. if (m_prim_geom != IntPtr.Zero)
  2567. {
  2568. if (newOri.NotEqual(m_orientation))
  2569. {
  2570. UBOdeNative.Quaternion myrot = new()
  2571. {
  2572. X = newOri.X,
  2573. Y = newOri.Y,
  2574. Z = newOri.Z,
  2575. W = newOri.W
  2576. };
  2577. UBOdeNative.GeomSetQuaternion(m_prim_geom, ref myrot);
  2578. m_orientation = newOri;
  2579. }
  2580. }
  2581. }
  2582. m_givefakeori--;
  2583. if (m_givefakeori < 0)
  2584. m_givefakeori = 0;
  2585. resetCollisionAccounting();
  2586. }
  2587. /*
  2588. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2589. private void changePositionAndOrientation(Vector3 newPos, Quaternion newOri)
  2590. {
  2591. CheckDelaySelect();
  2592. if (m_isphysical)
  2593. {
  2594. if (childPrim && m_building) // inertia is messed, must rebuild
  2595. {
  2596. m_position = newPos;
  2597. m_orientation = newOri;
  2598. }
  2599. else
  2600. {
  2601. if (newOri.NotEqual(m_orientation))
  2602. {
  2603. UBOdeNative.Quaternion myrot = new()
  2604. {
  2605. X = newOri.X,
  2606. Y = newOri.Y,
  2607. Z = newOri.Z,
  2608. W = newOri.W
  2609. };
  2610. UBOdeNative.GeomSetQuaternion(m_prim_geom, ref myrot);
  2611. m_orientation = newOri;
  2612. if (Body != IntPtr.Zero && m_angularlocks != 0)
  2613. createAMotor(m_angularlocks);
  2614. }
  2615. if (m_position.NotEqual(newPos))
  2616. {
  2617. UBOdeNative.GeomSetPosition(m_prim_geom, newPos.X, newPos.Y, newPos.Z);
  2618. m_position = newPos;
  2619. }
  2620. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  2621. {
  2622. _zeroFlag = true;
  2623. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2624. UBOdeNative.BodyEnable(Body);
  2625. }
  2626. }
  2627. }
  2628. else
  2629. {
  2630. // string primScenAvatarIn = _parent_scene.whichspaceamIin(_position);
  2631. // int[] arrayitem = _parent_scene.calculateSpaceArrayItemFromPos(_position);
  2632. if (m_prim_geom != IntPtr.Zero)
  2633. {
  2634. if (newOri.NotEqual(m_orientation))
  2635. {
  2636. UBOdeNative.Quaternion myrot = new()
  2637. {
  2638. X = newOri.X,
  2639. Y = newOri.Y,
  2640. Z = newOri.Z,
  2641. W = newOri.W
  2642. };
  2643. UBOdeNative.GeomSetQuaternion(m_prim_geom, ref myrot);
  2644. m_orientation = newOri;
  2645. }
  2646. if (newPos.NotEqual(m_position))
  2647. {
  2648. UBOdeNative.GeomSetPosition(m_prim_geom, newPos.X, newPos.Y, newPos.Z);
  2649. m_position = newPos;
  2650. m_targetSpace = m_parentScene.MoveGeomToStaticSpace(m_prim_geom, m_targetSpace);
  2651. }
  2652. }
  2653. }
  2654. m_givefakepos--;
  2655. if (m_givefakepos < 0)
  2656. m_givefakepos = 0;
  2657. m_givefakeori--;
  2658. if (m_givefakeori < 0)
  2659. m_givefakeori = 0;
  2660. resetCollisionAccounting();
  2661. }
  2662. */
  2663. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2664. private void changeDisable(bool disable)
  2665. {
  2666. if (disable)
  2667. {
  2668. if (!m_disabled)
  2669. disableBodySoft();
  2670. }
  2671. else
  2672. {
  2673. if (m_disabled)
  2674. enableBodySoft();
  2675. }
  2676. }
  2677. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2678. private void changePhysicsStatus(bool NewStatus)
  2679. {
  2680. CheckDelaySelect();
  2681. m_isphysical = NewStatus;
  2682. if (!childPrim)
  2683. {
  2684. if (NewStatus)
  2685. {
  2686. if (Body == IntPtr.Zero)
  2687. MakeBody();
  2688. }
  2689. else
  2690. {
  2691. if (Body != IntPtr.Zero)
  2692. {
  2693. DestroyBody();
  2694. }
  2695. Stop();
  2696. }
  2697. }
  2698. resetCollisionAccounting();
  2699. }
  2700. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2701. private static void changeSize(Vector3 newSize)
  2702. {
  2703. }
  2704. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2705. private static void changeShape(PrimitiveBaseShape newShape)
  2706. {
  2707. }
  2708. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2709. private void changeAddPhysRep(ODEPhysRepData repData)
  2710. {
  2711. m_size = repData.size; //??
  2712. m_pbs = repData.pbs;
  2713. m_mesh = repData.mesh;
  2714. //m_assetID = repData.assetID;
  2715. m_meshState = repData.meshState;
  2716. //m_hasOBB = repData.hasOBB;
  2717. m_OBBOffset = repData.OBBOffset;
  2718. m_OBB = repData.OBB;
  2719. primVolume = repData.volume;
  2720. CreateGeom(repData.isTooSmall);
  2721. if (m_prim_geom != IntPtr.Zero)
  2722. {
  2723. UBOdeNative.GeomSetPosition(m_prim_geom, m_position.X, m_position.Y, m_position.Z);
  2724. UBOdeNative.Quaternion myrot = new()
  2725. {
  2726. X = m_orientation.X,
  2727. Y = m_orientation.Y,
  2728. Z = m_orientation.Z,
  2729. W = m_orientation.W
  2730. };
  2731. UBOdeNative.GeomSetQuaternion(m_prim_geom, ref myrot);
  2732. }
  2733. if (!m_isphysical)
  2734. {
  2735. SetInStaticSpace(this);
  2736. UpdateCollisionCatFlags();
  2737. ApplyCollisionCatFlags();
  2738. }
  2739. else
  2740. MakeBody();
  2741. if ((m_meshState & MeshState.NeedMask) != 0)
  2742. {
  2743. repData.size = m_size;
  2744. repData.pbs = m_pbs;
  2745. repData.shapetype = m_fakeShapetype;
  2746. m_parentScene.m_meshWorker.RequestMesh(repData);
  2747. }
  2748. else
  2749. m_shapetype = repData.shapetype;
  2750. }
  2751. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2752. private void changePhysRepData(ODEPhysRepData repData)
  2753. {
  2754. if(m_size == repData.size &&
  2755. m_pbs == repData.pbs &&
  2756. m_shapetype == repData.shapetype &&
  2757. m_mesh == repData.mesh &&
  2758. primVolume == repData.volume)
  2759. return;
  2760. CheckDelaySelect();
  2761. OdePrim parent = (OdePrim)_parent;
  2762. bool chp = childPrim;
  2763. if (chp)
  2764. {
  2765. parent?.DestroyBody();
  2766. }
  2767. else
  2768. {
  2769. DestroyBody();
  2770. }
  2771. RemoveGeom();
  2772. m_size = repData.size;
  2773. m_pbs = repData.pbs;
  2774. m_mesh = repData.mesh;
  2775. //m_assetID = repData.assetID;
  2776. m_meshState = repData.meshState;
  2777. //m_hasOBB = repData.hasOBB;
  2778. m_OBBOffset = repData.OBBOffset;
  2779. m_OBB = repData.OBB;
  2780. primVolume = repData.volume;
  2781. CreateGeom(repData.isTooSmall);
  2782. if (m_prim_geom != IntPtr.Zero)
  2783. {
  2784. UBOdeNative.GeomSetPosition(m_prim_geom, m_position.X, m_position.Y, m_position.Z);
  2785. UBOdeNative.Quaternion myrot = new()
  2786. {
  2787. X = m_orientation.X,
  2788. Y = m_orientation.Y,
  2789. Z = m_orientation.Z,
  2790. W = m_orientation.W
  2791. };
  2792. UBOdeNative.GeomSetQuaternion(m_prim_geom, ref myrot);
  2793. }
  2794. if (m_isphysical)
  2795. {
  2796. if (chp)
  2797. {
  2798. parent?.MakeBody();
  2799. }
  2800. else
  2801. MakeBody();
  2802. }
  2803. else
  2804. {
  2805. SetInStaticSpace(this);
  2806. UpdateCollisionCatFlags();
  2807. ApplyCollisionCatFlags();
  2808. }
  2809. resetCollisionAccounting();
  2810. if ((m_meshState & MeshState.NeedMask) != 0)
  2811. {
  2812. repData.size = m_size;
  2813. repData.pbs = m_pbs;
  2814. repData.shapetype = m_fakeShapetype;
  2815. m_parentScene.m_meshWorker.RequestMesh(repData);
  2816. }
  2817. else
  2818. m_shapetype = repData.shapetype;
  2819. }
  2820. /*
  2821. private void changeFloatOnWater(bool newval)
  2822. {
  2823. m_collidesWater = newval;
  2824. UpdateCollisionCatFlags();
  2825. ApplyCollisionCatFlags();
  2826. }
  2827. */
  2828. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2829. private void changeSetTorque(Vector3 newtorque)
  2830. {
  2831. if (!m_isSelected && !m_outbounds)
  2832. {
  2833. if (m_isphysical && Body != IntPtr.Zero)
  2834. {
  2835. if (m_disabled)
  2836. enableBodySoft();
  2837. else if (!UBOdeNative.BodyIsEnabled(Body))
  2838. {
  2839. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2840. UBOdeNative.BodyEnable(Body);
  2841. }
  2842. }
  2843. m_torque = newtorque;
  2844. }
  2845. }
  2846. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2847. private void changeForce(Vector3 force)
  2848. {
  2849. m_force = force;
  2850. if (!m_isSelected && !m_outbounds && Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  2851. {
  2852. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2853. UBOdeNative.BodyEnable(Body);
  2854. }
  2855. }
  2856. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2857. private void changeAddForce(Vector3 theforce)
  2858. {
  2859. m_forceacc += theforce;
  2860. if (!m_isSelected && !m_outbounds)
  2861. {
  2862. lock (this)
  2863. {
  2864. //m_log.Info("[PHYSICS]: dequeing forcelist");
  2865. if (m_isphysical && Body != IntPtr.Zero)
  2866. {
  2867. if (m_disabled)
  2868. enableBodySoft();
  2869. else if (!UBOdeNative.BodyIsEnabled(Body))
  2870. {
  2871. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2872. UBOdeNative.BodyEnable(Body);
  2873. }
  2874. }
  2875. }
  2876. m_collisionscore = 0;
  2877. }
  2878. }
  2879. // actually angular impulse
  2880. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2881. private void changeAddAngularImpulse(Vector3 aimpulse)
  2882. {
  2883. m_angularForceacc += aimpulse * m_sceneInverseTimeStep;
  2884. if (!m_isSelected && !m_outbounds)
  2885. {
  2886. lock (this)
  2887. {
  2888. if (m_isphysical && Body != IntPtr.Zero)
  2889. {
  2890. if (m_disabled)
  2891. enableBodySoft();
  2892. else if (!UBOdeNative.BodyIsEnabled(Body))
  2893. {
  2894. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2895. UBOdeNative.BodyEnable(Body);
  2896. }
  2897. }
  2898. }
  2899. m_collisionscore = 0;
  2900. }
  2901. }
  2902. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2903. private void changevelocity(Vector3 newVel)
  2904. {
  2905. float len = newVel.LengthSquared();
  2906. if (len > 100000.0f) // limit to 100m/s
  2907. {
  2908. len = 100.0f / MathF.Sqrt(len);
  2909. newVel *= len;
  2910. }
  2911. if (!m_isSelected && !m_outbounds)
  2912. {
  2913. if (Body != IntPtr.Zero)
  2914. {
  2915. if (m_disabled)
  2916. enableBodySoft();
  2917. else if (!UBOdeNative.BodyIsEnabled(Body))
  2918. {
  2919. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2920. UBOdeNative.BodyEnable(Body);
  2921. }
  2922. UBOdeNative.BodySetLinearVel(Body, newVel.X, newVel.Y, newVel.Z);
  2923. }
  2924. //resetCollisionAccounting();
  2925. }
  2926. _velocity = newVel;
  2927. }
  2928. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2929. private void changeangvelocity(Vector3 newAngVel)
  2930. {
  2931. float len = newAngVel.LengthSquared();
  2932. if (len > m_parentScene.maxAngVelocitySQ)
  2933. {
  2934. len = m_parentScene.maximumAngularVelocity / MathF.Sqrt(len);
  2935. newAngVel *= len;
  2936. }
  2937. if (!m_isSelected && !m_outbounds)
  2938. {
  2939. if (Body != IntPtr.Zero)
  2940. {
  2941. if (m_disabled)
  2942. enableBodySoft();
  2943. else if (!UBOdeNative.BodyIsEnabled(Body))
  2944. {
  2945. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2946. UBOdeNative.BodyEnable(Body);
  2947. }
  2948. UBOdeNative.BodySetAngularVel(Body, newAngVel.X, newAngVel.Y, newAngVel.Z);
  2949. }
  2950. //resetCollisionAccounting();
  2951. }
  2952. m_rotationalVelocity = newAngVel;
  2953. }
  2954. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2955. private void changeVolumedetetion(bool newVolDtc)
  2956. {
  2957. m_isVolumeDetect = newVolDtc;
  2958. m_fakeisVolumeDetect = newVolDtc;
  2959. UpdateCollisionCatFlags();
  2960. ApplyCollisionCatFlags();
  2961. }
  2962. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2963. protected void changeBuilding(bool newbuilding)
  2964. {
  2965. // Check if we need to do anything
  2966. if (newbuilding == m_building)
  2967. return;
  2968. if ((bool)newbuilding)
  2969. {
  2970. m_building = true;
  2971. if (!childPrim)
  2972. DestroyBody();
  2973. }
  2974. else
  2975. {
  2976. m_building = false;
  2977. CheckDelaySelect();
  2978. if (!childPrim)
  2979. MakeBody();
  2980. }
  2981. if (!childPrim && childrenPrim.Count > 0)
  2982. {
  2983. foreach (OdePrim prm in childrenPrim)
  2984. prm.changeBuilding(m_building); // call directly
  2985. }
  2986. }
  2987. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2988. public void changeSetVehicle(VehicleData vdata)
  2989. {
  2990. m_vehicle ??= new ODEDynamics(this);
  2991. m_vehicle.DoSetVehicle(vdata);
  2992. }
  2993. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2994. private void changeVehicleType(int value)
  2995. {
  2996. if (value == (int)Vehicle.TYPE_NONE)
  2997. {
  2998. if (m_vehicle != null)
  2999. m_vehicle = null;
  3000. }
  3001. else
  3002. {
  3003. m_vehicle ??= new ODEDynamics(this);
  3004. m_vehicle.ProcessTypeChange((Vehicle)value);
  3005. }
  3006. }
  3007. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3008. private void changeVehicleFloatParam(strVehicleFloatParam fp)
  3009. {
  3010. if (m_vehicle == null)
  3011. return;
  3012. m_vehicle.ProcessFloatVehicleParam((Vehicle)fp.param, fp.value);
  3013. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  3014. {
  3015. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  3016. UBOdeNative.BodyEnable(Body);
  3017. }
  3018. }
  3019. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3020. private void changeVehicleVectorParam(strVehicleVectorParam vp)
  3021. {
  3022. if (m_vehicle == null)
  3023. return;
  3024. m_vehicle.ProcessVectorVehicleParam((Vehicle)vp.param, vp.value);
  3025. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  3026. {
  3027. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  3028. UBOdeNative.BodyEnable(Body);
  3029. }
  3030. }
  3031. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3032. private void changeVehicleRotationParam(strVehicleQuatParam qp)
  3033. {
  3034. if (m_vehicle == null)
  3035. return;
  3036. m_vehicle.ProcessRotationVehicleParam((Vehicle)qp.param, qp.value);
  3037. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  3038. {
  3039. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  3040. UBOdeNative.BodyEnable(Body);
  3041. }
  3042. }
  3043. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3044. private void changeVehicleFlags(strVehicleBoolParam bp)
  3045. {
  3046. if (m_vehicle == null)
  3047. return;
  3048. m_vehicle.ProcessVehicleFlags(bp.param, bp.value);
  3049. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  3050. {
  3051. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  3052. UBOdeNative.BodyEnable(Body);
  3053. }
  3054. }
  3055. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3056. private void changeBuoyancy(float b)
  3057. {
  3058. m_buoyancy = b;
  3059. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  3060. {
  3061. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  3062. UBOdeNative.BodyEnable(Body);
  3063. }
  3064. }
  3065. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3066. private void changePIDTarget(Vector3 trg)
  3067. {
  3068. m_PIDTarget = trg;
  3069. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  3070. {
  3071. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  3072. UBOdeNative.BodyEnable(Body);
  3073. }
  3074. }
  3075. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3076. private void changePIDTau(float tau)
  3077. {
  3078. m_PIDTau = tau;
  3079. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  3080. {
  3081. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  3082. UBOdeNative.BodyEnable(Body);
  3083. }
  3084. }
  3085. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3086. private void changePIDActive(bool val)
  3087. {
  3088. m_usePID = val;
  3089. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  3090. {
  3091. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  3092. UBOdeNative.BodyEnable(Body);
  3093. }
  3094. }
  3095. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3096. private void changePIDHoverHeight(float val)
  3097. {
  3098. m_PIDHoverHeight = val;
  3099. if (val == 0)
  3100. m_useHoverPID = false;
  3101. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  3102. {
  3103. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  3104. UBOdeNative.BodyEnable(Body);
  3105. }
  3106. }
  3107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3108. private void changePIDHoverType(PIDHoverType type)
  3109. {
  3110. m_PIDHoverType = type;
  3111. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  3112. {
  3113. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  3114. UBOdeNative.BodyEnable(Body);
  3115. }
  3116. }
  3117. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3118. private void changePIDHoverTau(float tau)
  3119. {
  3120. m_PIDHoverTau = tau;
  3121. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  3122. {
  3123. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  3124. UBOdeNative.BodyEnable(Body);
  3125. }
  3126. }
  3127. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3128. private void changePIDHoverActive(bool active)
  3129. {
  3130. m_useHoverPID = active;
  3131. if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
  3132. {
  3133. UBOdeNative.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  3134. UBOdeNative.BodyEnable(Body);
  3135. }
  3136. }
  3137. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3138. private void changeInertia(PhysicsInertiaData inertia)
  3139. {
  3140. m_InertiaOverride = inertia;
  3141. if (Body != IntPtr.Zero)
  3142. DestroyBody();
  3143. MakeBody();
  3144. }
  3145. #endregion
  3146. public void Move()
  3147. {
  3148. if (!childPrim && m_isphysical && Body != IntPtr.Zero &&
  3149. !m_disabled && !m_isSelected && !m_building && !m_outbounds)
  3150. {
  3151. if (!UBOdeNative.BodyIsEnabled(Body))
  3152. {
  3153. // let vehicles sleep
  3154. if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
  3155. return;
  3156. if (++m_bodydisablecontrol < 50)
  3157. return;
  3158. // clear residuals
  3159. UBOdeNative.BodySetAngularVel(Body,0f,0f,0f);
  3160. UBOdeNative.BodySetLinearVel(Body,0f,0f,0f);
  3161. _zeroFlag = true;
  3162. UBOdeNative.BodySetAutoDisableSteps(Body, 1);
  3163. UBOdeNative.BodyEnable(Body);
  3164. m_bodydisablecontrol = -3;
  3165. }
  3166. if(m_bodydisablecontrol < 0)
  3167. m_bodydisablecontrol++;
  3168. if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
  3169. {
  3170. // 'VEHICLES' are dealt with in ODEDynamics.cs
  3171. m_vehicle.Step();
  3172. return;
  3173. }
  3174. float fx = 0;
  3175. float fy = 0;
  3176. float fz = 0;
  3177. Vector3 lpos = UBOdeNative.GeomGetPositionOMV(m_prim_geom); // root position that is seem by rest of simulator
  3178. if (m_usePID && m_PIDTau > 0)
  3179. {
  3180. // for now position error
  3181. _target_velocity = m_PIDTarget - lpos;
  3182. if (_target_velocity.ApproxZero(0.02f))
  3183. {
  3184. UBOdeNative.BodySetPosition(Body, m_PIDTarget.X, m_PIDTarget.Y, m_PIDTarget.Z);
  3185. UBOdeNative.BodySetLinearVel(Body, 0, 0, 0);
  3186. return;
  3187. }
  3188. else
  3189. {
  3190. _zeroFlag = false;
  3191. float tmp = 1 / m_PIDTau;
  3192. _target_velocity *= tmp;
  3193. // apply limits
  3194. tmp = _target_velocity.Length();
  3195. if (tmp > 50.0f)
  3196. {
  3197. tmp = 50 / tmp;
  3198. _target_velocity *= tmp;
  3199. }
  3200. else if (tmp < 0.05f)
  3201. {
  3202. tmp = 0.05f / tmp;
  3203. _target_velocity *= tmp;
  3204. }
  3205. UBOdeNative.Vector3 vel = UBOdeNative.BodyGetLinearVel(Body);
  3206. fx = (_target_velocity.X - vel.X) * m_sceneInverseTimeStep;
  3207. fy = (_target_velocity.Y - vel.Y) * m_sceneInverseTimeStep;
  3208. fz = (_target_velocity.Z - vel.Z) * m_sceneInverseTimeStep;
  3209. // d.BodySetLinearVel(Body, _target_velocity.X, _target_velocity.Y, _target_velocity.Z);
  3210. }
  3211. } // end if (m_usePID)
  3212. // Hover PID Controller needs to be mutually exlusive to MoveTo PID controller
  3213. else if (m_useHoverPID && m_PIDHoverTau != 0 && m_PIDHoverHeight != 0)
  3214. {
  3215. // Non-Vehicles have a limited set of Hover options.
  3216. // determine what our target height really is based on HoverType
  3217. float groundHeight = m_parentScene.GetTerrainHeightAtXY(lpos.X, lpos.Y);
  3218. switch (m_PIDHoverType)
  3219. {
  3220. case PIDHoverType.Ground:
  3221. m_targetHoverHeight = groundHeight + m_PIDHoverHeight;
  3222. break;
  3223. case PIDHoverType.GroundAndWater:
  3224. if (groundHeight > m_parentScene.WaterLevel)
  3225. m_targetHoverHeight = groundHeight + m_PIDHoverHeight;
  3226. else
  3227. m_targetHoverHeight = m_parentScene.WaterLevel + m_PIDHoverHeight;
  3228. break;
  3229. } // end switch (m_PIDHoverType)
  3230. // don't go underground unless volumedetector
  3231. if (m_targetHoverHeight > groundHeight || m_isVolumeDetect)
  3232. {
  3233. UBOdeNative.Vector3 vel = UBOdeNative.BodyGetLinearVel(Body);
  3234. fz = (m_targetHoverHeight - lpos.Z);
  3235. // if error is zero, use position control; otherwise, velocity control
  3236. if (MathF.Abs(fz) < 0.01f)
  3237. {
  3238. UBOdeNative.BodySetPosition(Body, lpos.X, lpos.Y, m_targetHoverHeight);
  3239. UBOdeNative.BodySetLinearVel(Body, vel.X, vel.Y, 0);
  3240. }
  3241. else
  3242. {
  3243. _zeroFlag = false;
  3244. fz /= m_PIDHoverTau;
  3245. if(fz < 0)
  3246. {
  3247. if (fz < -50f)
  3248. fz = -50f;
  3249. else if (fz > -0.1f)
  3250. fz = -0.1f;
  3251. }
  3252. else
  3253. {
  3254. if (fx > 50f)
  3255. fz = 50f;
  3256. else if (fz < 0.1f)
  3257. fz = 0.1f;
  3258. }
  3259. fz = ((fz - vel.Z) * m_sceneInverseTimeStep);
  3260. }
  3261. }
  3262. }
  3263. else
  3264. {
  3265. float b = (1.0f - m_buoyancy) * m_gravmod;
  3266. fx = m_parentScene.gravityx * b;
  3267. fy = m_parentScene.gravityy * b;
  3268. fz = m_parentScene.gravityz * b;
  3269. }
  3270. //aceleration to force + constant force + acc
  3271. fx = m_mass * fx + m_force.X + m_forceacc.X;
  3272. fy = m_mass * fy + m_force.Y + m_forceacc.Y;
  3273. fz = m_mass * fz + m_force.Z + m_forceacc.Z;
  3274. m_forceacc = Vector3.Zero;
  3275. //m_log.Info("[OBJPID]: X:" + fx.ToString() + " Y:" + fy.ToString() + " Z:" + fz.ToString());
  3276. if (fz != 0 || fx != 0 || fy != 0)
  3277. {
  3278. UBOdeNative.BodyAddForce(Body, fx, fy, fz);
  3279. //Console.WriteLine("AddForce " + fx + "," + fy + "," + fz);
  3280. }
  3281. Vector3 trq = m_torque + m_angularForceacc;
  3282. m_angularForceacc = Vector3.Zero;
  3283. if (trq.X != 0 || trq.Y != 0 || trq.Z != 0)
  3284. {
  3285. UBOdeNative.BodyAddTorque(Body, trq.X, trq.Y, trq.Z);
  3286. }
  3287. }
  3288. else
  3289. { // is not physical, or is not a body or is selected
  3290. // _zeroPosition = d.BodyGetPosition(Body);
  3291. //Console.WriteLine("Nothing " + Name);
  3292. }
  3293. }
  3294. public void UpdatePositionAndVelocity(int frame)
  3295. {
  3296. if (_parent == null && !m_isSelected && !m_disabled && !m_building && !m_outbounds && Body != IntPtr.Zero)
  3297. {
  3298. if(m_bodydisablecontrol < 0)
  3299. return;
  3300. bool bodyenabled = UBOdeNative.BodyIsEnabled(Body);
  3301. if (bodyenabled || !_zeroFlag)
  3302. {
  3303. bool lastZeroFlag = _zeroFlag;
  3304. Vector3 lpos = UBOdeNative.GeomGetPositionOMV(m_prim_geom);
  3305. // check outside region
  3306. if (lpos.Z < -100 || lpos.Z > 100000f)
  3307. {
  3308. m_outbounds = true;
  3309. lpos.Z = Utils.Clamp(lpos.Z, -100f, 100000f);
  3310. m_acceleration = Vector3.Zero;
  3311. _velocity = Vector3.Zero;
  3312. m_rotationalVelocity = Vector3.Zero;
  3313. UBOdeNative.BodySetLinearVel(Body, 0, 0, 0); // stop it
  3314. UBOdeNative.BodySetAngularVel(Body, 0, 0, 0); // stop it
  3315. UBOdeNative.BodySetPosition(Body, lpos.X, lpos.Y, lpos.Z); // put it somewhere
  3316. //m_lastposition = m_position;
  3317. //m_lastorientation = m_orientation;
  3318. base.RequestPhysicsterseUpdate();
  3319. // throttleCounter = 0;
  3320. _zeroFlag = true;
  3321. disableBodySoft(); // disable it and colisions
  3322. base.RaiseOutOfBounds(m_position);
  3323. return;
  3324. }
  3325. if (lpos.X < 0f)
  3326. {
  3327. m_position.X = Utils.Clamp(lpos.X, -2f, -0.1f);
  3328. m_outbounds = true;
  3329. }
  3330. else if (lpos.X > m_parentScene.WorldExtents.X)
  3331. {
  3332. m_position.X = Utils.Clamp(lpos.X, m_parentScene.WorldExtents.X + 0.1f, m_parentScene.WorldExtents.X + 2f);
  3333. m_outbounds = true;
  3334. }
  3335. if (lpos.Y < 0f)
  3336. {
  3337. m_position.Y = Utils.Clamp(lpos.Y, -2f, -0.1f);
  3338. m_outbounds = true;
  3339. }
  3340. else if (lpos.Y > m_parentScene.WorldExtents.Y)
  3341. {
  3342. m_position.Y = Utils.Clamp(lpos.Y, m_parentScene.WorldExtents.Y + 0.1f, m_parentScene.WorldExtents.Y + 2f);
  3343. m_outbounds = true;
  3344. }
  3345. if (m_outbounds)
  3346. {
  3347. //m_lastposition = m_position;
  3348. //m_lastorientation = m_orientation;
  3349. _velocity = UBOdeNative.BodyGetLinearVelOMV(Body);
  3350. m_rotationalVelocity = UBOdeNative.BodyGetAngularVelOMV(Body);
  3351. UBOdeNative.BodySetLinearVel(Body, 0, 0, 0); // stop it
  3352. UBOdeNative.BodySetAngularVel(Body, 0, 0, 0);
  3353. UBOdeNative.GeomSetPosition(m_prim_geom, m_position.X, m_position.Y, m_position.Z);
  3354. disableBodySoft(); // stop collisions
  3355. UnSubscribeEvents();
  3356. base.RequestPhysicsterseUpdate();
  3357. return;
  3358. }
  3359. Quaternion ori = UBOdeNative.GeomGetQuaternionOMV(m_prim_geom);
  3360. // decide if moving
  3361. // use positions since this are integrated quantities
  3362. // tolerance values depende a lot on simulation noise...
  3363. // use simple math.abs since we dont need to be exact
  3364. if(!bodyenabled)
  3365. {
  3366. _zeroFlag = true;
  3367. }
  3368. else
  3369. {
  3370. float poserror;
  3371. float angerror;
  3372. if(_zeroFlag)
  3373. {
  3374. poserror = 0.01f;
  3375. angerror = 0.001f;
  3376. }
  3377. else
  3378. {
  3379. poserror = 0.005f;
  3380. angerror = 0.0005f;
  3381. }
  3382. if (
  3383. (MathF.Abs(m_position.X - lpos.X) < poserror)
  3384. && (MathF.Abs(m_position.Y - lpos.Y) < poserror)
  3385. && (MathF.Abs(m_position.Z - lpos.Z) < poserror)
  3386. && (MathF.Abs(m_orientation.X - ori.X) < angerror)
  3387. && (MathF.Abs(m_orientation.Y - ori.Y) < angerror)
  3388. && (MathF.Abs(m_orientation.Z - ori.Z) < angerror) // ignore W
  3389. )
  3390. _zeroFlag = true;
  3391. else
  3392. _zeroFlag = false;
  3393. }
  3394. // update position
  3395. if (!(_zeroFlag && lastZeroFlag))
  3396. {
  3397. m_position = lpos;
  3398. m_orientation = ori;
  3399. }
  3400. // update velocities and acceleration
  3401. if (_zeroFlag || lastZeroFlag)
  3402. {
  3403. // disable interpolators
  3404. _velocity = Vector3.Zero;
  3405. m_acceleration = Vector3.Zero;
  3406. m_rotationalVelocity = Vector3.Zero;
  3407. }
  3408. else
  3409. {
  3410. Vector3 vel = UBOdeNative.BodyGetLinearVelOMV(Body);
  3411. m_acceleration = _velocity;
  3412. if (vel.ApproxZero(0.005f))
  3413. {
  3414. _velocity = Vector3.Zero;
  3415. float t = -m_sceneInverseTimeStep;
  3416. m_acceleration *= t;
  3417. }
  3418. else
  3419. {
  3420. _velocity = vel;
  3421. m_acceleration = (_velocity - m_acceleration) * m_sceneInverseTimeStep;
  3422. }
  3423. if (m_acceleration.ApproxZero(0.01f))
  3424. {
  3425. m_acceleration = Vector3.Zero;
  3426. }
  3427. vel = UBOdeNative.BodyGetAngularVelOMV(Body);
  3428. if (vel.ApproxZero(0.0001f))
  3429. {
  3430. m_rotationalVelocity = Vector3.Zero;
  3431. }
  3432. else
  3433. {
  3434. m_rotationalVelocity = vel;
  3435. }
  3436. }
  3437. if (_zeroFlag)
  3438. {
  3439. if (!m_lastUpdateSent)
  3440. {
  3441. base.RequestPhysicsterseUpdate();
  3442. if (lastZeroFlag)
  3443. m_lastUpdateSent = true;
  3444. }
  3445. return;
  3446. }
  3447. base.RequestPhysicsterseUpdate();
  3448. m_lastUpdateSent = false;
  3449. }
  3450. }
  3451. }
  3452. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3453. internal static bool QuaternionIsFinite(Quaternion q)
  3454. {
  3455. if (Single.IsNaN(q.X) || Single.IsInfinity(q.X))
  3456. return false;
  3457. if (Single.IsNaN(q.Y) || Single.IsInfinity(q.Y))
  3458. return false;
  3459. if (Single.IsNaN(q.Z) || Single.IsInfinity(q.Z))
  3460. return false;
  3461. if (Single.IsNaN(q.W) || Single.IsInfinity(q.W))
  3462. return false;
  3463. return true;
  3464. }
  3465. internal static void DMassSubPartFromObj(ref UBOdeNative.Mass part, ref UBOdeNative.Mass theobj)
  3466. {
  3467. // assumes object center of mass is zero
  3468. float smass = part.mass;
  3469. theobj.mass -= smass;
  3470. smass *= 1.0f / (theobj.mass);
  3471. theobj.c.X -= part.c.X * smass;
  3472. theobj.c.Y -= part.c.Y * smass;
  3473. theobj.c.Z -= part.c.Z * smass;
  3474. theobj.I.M00 -= part.I.M00;
  3475. theobj.I.M01 -= part.I.M01;
  3476. theobj.I.M02 -= part.I.M02;
  3477. theobj.I.M10 -= part.I.M10;
  3478. theobj.I.M11 -= part.I.M11;
  3479. theobj.I.M12 -= part.I.M12;
  3480. theobj.I.M20 -= part.I.M20;
  3481. theobj.I.M21 -= part.I.M21;
  3482. theobj.I.M22 -= part.I.M22;
  3483. }
  3484. private static void donullchange()
  3485. {
  3486. }
  3487. public bool DoAChange(changes what, object arg)
  3488. {
  3489. if (m_prim_geom == IntPtr.Zero && what != changes.Add && what != changes.AddPhysRep && what != changes.Remove)
  3490. {
  3491. return false;
  3492. }
  3493. // nasty switch
  3494. switch (what)
  3495. {
  3496. case changes.Add:
  3497. changeadd();
  3498. break;
  3499. case changes.AddPhysRep:
  3500. changeAddPhysRep((ODEPhysRepData)arg);
  3501. break;
  3502. case changes.Remove:
  3503. //If its being removed, we don't want to rebuild the physical rep at all, so ignore this stuff...
  3504. //When we return true, it destroys all of the prims in the linkset anyway
  3505. if (_parent != null)
  3506. {
  3507. OdePrim parent = (OdePrim)_parent;
  3508. parent.ChildRemove(this, false);
  3509. }
  3510. else
  3511. ChildRemove(this, false);
  3512. m_vehicle = null;
  3513. RemoveGeom();
  3514. m_targetSpace = IntPtr.Zero;
  3515. UnSubscribeEvents();
  3516. return true;
  3517. case changes.Link:
  3518. OdePrim tmp = (OdePrim)arg;
  3519. changeLink(tmp);
  3520. break;
  3521. case changes.DeLink:
  3522. changeLink(null);
  3523. break;
  3524. case changes.Position:
  3525. changePosition((Vector3)arg);
  3526. break;
  3527. case changes.Orientation:
  3528. changeOrientation((Quaternion)arg);
  3529. break;
  3530. /*
  3531. case changes.PosOffset:
  3532. donullchange();
  3533. break;
  3534. case changes.OriOffset:
  3535. donullchange();
  3536. break;
  3537. */
  3538. case changes.Velocity:
  3539. changevelocity((Vector3)arg);
  3540. break;
  3541. //case changes.TargetVelocity:
  3542. // break;
  3543. //case changes.Acceleration:
  3544. // changeacceleration((Vector3)arg);
  3545. // break;
  3546. case changes.AngVelocity:
  3547. changeangvelocity((Vector3)arg);
  3548. break;
  3549. case changes.Force:
  3550. changeForce((Vector3)arg);
  3551. break;
  3552. case changes.Torque:
  3553. changeSetTorque((Vector3)arg);
  3554. break;
  3555. case changes.AddForce:
  3556. changeAddForce((Vector3)arg);
  3557. break;
  3558. case changes.AddAngForce:
  3559. changeAddAngularImpulse((Vector3)arg);
  3560. break;
  3561. case changes.AngLock:
  3562. changeAngularLock((byte)arg);
  3563. break;
  3564. case changes.Size:
  3565. changeSize((Vector3)arg);
  3566. break;
  3567. case changes.Shape:
  3568. changeShape((PrimitiveBaseShape)arg);
  3569. break;
  3570. case changes.PhysRepData:
  3571. changePhysRepData((ODEPhysRepData) arg);
  3572. break;
  3573. //case changes.CollidesWater:
  3574. // changeFloatOnWater((bool)arg);
  3575. // break;
  3576. case changes.VolumeDtc:
  3577. changeVolumedetetion((bool)arg);
  3578. break;
  3579. case changes.Phantom:
  3580. changePhantomStatus((bool)arg);
  3581. break;
  3582. case changes.Physical:
  3583. changePhysicsStatus((bool)arg);
  3584. break;
  3585. case changes.Selected:
  3586. changeSelectedStatus((bool)arg);
  3587. break;
  3588. case changes.disabled:
  3589. changeDisable((bool)arg);
  3590. break;
  3591. case changes.building:
  3592. changeBuilding((bool)arg);
  3593. break;
  3594. case changes.VehicleType:
  3595. changeVehicleType((int)arg);
  3596. break;
  3597. case changes.VehicleFlags:
  3598. changeVehicleFlags((strVehicleBoolParam) arg);
  3599. break;
  3600. case changes.VehicleFloatParam:
  3601. changeVehicleFloatParam((strVehicleFloatParam) arg);
  3602. break;
  3603. case changes.VehicleVectorParam:
  3604. changeVehicleVectorParam((strVehicleVectorParam) arg);
  3605. break;
  3606. case changes.VehicleRotationParam:
  3607. changeVehicleRotationParam((strVehicleQuatParam) arg);
  3608. break;
  3609. case changes.SetVehicle:
  3610. changeSetVehicle((VehicleData) arg);
  3611. break;
  3612. case changes.Buoyancy:
  3613. changeBuoyancy((float)arg);
  3614. break;
  3615. case changes.PIDTarget:
  3616. changePIDTarget((Vector3)arg);
  3617. break;
  3618. case changes.PIDTau:
  3619. changePIDTau((float)arg);
  3620. break;
  3621. case changes.PIDActive:
  3622. changePIDActive((bool)arg);
  3623. break;
  3624. case changes.PIDHoverHeight:
  3625. changePIDHoverHeight((float)arg);
  3626. break;
  3627. case changes.PIDHoverType:
  3628. changePIDHoverType((PIDHoverType)arg);
  3629. break;
  3630. case changes.PIDHoverTau:
  3631. changePIDHoverTau((float)arg);
  3632. break;
  3633. case changes.PIDHoverActive:
  3634. changePIDHoverActive((bool)arg);
  3635. break;
  3636. case changes.SetInertia:
  3637. changeInertia((PhysicsInertiaData) arg);
  3638. break;
  3639. case changes.Null:
  3640. donullchange();
  3641. break;
  3642. default:
  3643. donullchange();
  3644. break;
  3645. }
  3646. return false;
  3647. }
  3648. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  3649. public void AddChange(changes what, object arg)
  3650. {
  3651. m_parentScene.AddChange(this, what, arg);
  3652. }
  3653. private struct strVehicleBoolParam
  3654. {
  3655. public int param;
  3656. public bool value;
  3657. }
  3658. private struct strVehicleFloatParam
  3659. {
  3660. public int param;
  3661. public float value;
  3662. }
  3663. private struct strVehicleQuatParam
  3664. {
  3665. public int param;
  3666. public Quaternion value;
  3667. }
  3668. private struct strVehicleVectorParam
  3669. {
  3670. public int param;
  3671. public Vector3 value;
  3672. }
  3673. }
  3674. }