ODEPrim.cs 139 KB

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