ODEPrim.cs 139 KB

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