ODEPrim.cs 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255
  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 CollisionEvents = null;
  159. private CollisionEventUpdate CollisionVDTCEvents = 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. if(prim_geom != IntPtr.Zero)
  898. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  899. disableBodySoft(); // stop collisions
  900. UnSubscribeEvents();
  901. }
  902. }
  903. public override void SetMomentum(Vector3 momentum)
  904. {
  905. }
  906. public override void SetMaterial(int pMaterial)
  907. {
  908. m_material = pMaterial;
  909. mu = _parent_scene.m_materialContactsData[pMaterial].mu;
  910. bounce = _parent_scene.m_materialContactsData[pMaterial].bounce;
  911. }
  912. public override float Density
  913. {
  914. get
  915. {
  916. return m_density * 100f;
  917. }
  918. set
  919. {
  920. float old = m_density;
  921. m_density = value / 100f;
  922. // if(m_density != old)
  923. // UpdatePrimBodyData();
  924. }
  925. }
  926. public override float GravModifier
  927. {
  928. get
  929. {
  930. return m_gravmod;
  931. }
  932. set
  933. {
  934. m_gravmod = value;
  935. if (m_vehicle != null)
  936. m_vehicle.GravMod = m_gravmod;
  937. }
  938. }
  939. public override float Friction
  940. {
  941. get
  942. {
  943. return mu;
  944. }
  945. set
  946. {
  947. mu = value;
  948. }
  949. }
  950. public override float Restitution
  951. {
  952. get
  953. {
  954. return bounce;
  955. }
  956. set
  957. {
  958. bounce = value;
  959. }
  960. }
  961. public void setPrimForRemoval()
  962. {
  963. AddChange(changes.Remove, null);
  964. }
  965. public override void link(PhysicsActor obj)
  966. {
  967. AddChange(changes.Link, obj);
  968. }
  969. public override void delink()
  970. {
  971. AddChange(changes.DeLink, null);
  972. }
  973. public override void LockAngularMotion(byte axislock)
  974. {
  975. // m_log.DebugFormat("[axislock]: <{0},{1},{2}>", axis.X, axis.Y, axis.Z);
  976. AddChange(changes.AngLock, axislock);
  977. }
  978. public override void SubscribeEvents(int ms)
  979. {
  980. m_eventsubscription = ms;
  981. m_cureventsubscription = 0;
  982. if (CollisionEvents == null)
  983. CollisionEvents = new CollisionEventUpdate();
  984. if (CollisionVDTCEvents == null)
  985. CollisionVDTCEvents = new CollisionEventUpdate();
  986. SentEmptyCollisionsEvent = false;
  987. }
  988. public override void UnSubscribeEvents()
  989. {
  990. if (CollisionVDTCEvents != null)
  991. {
  992. CollisionVDTCEvents.Clear();
  993. CollisionVDTCEvents = null;
  994. }
  995. if (CollisionEvents != null)
  996. {
  997. CollisionEvents.Clear();
  998. CollisionEvents = null;
  999. }
  1000. m_eventsubscription = 0;
  1001. _parent_scene.RemoveCollisionEventReporting(this);
  1002. }
  1003. public override void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
  1004. {
  1005. if (CollisionEvents == null)
  1006. CollisionEvents = new CollisionEventUpdate();
  1007. CollisionEvents.AddCollider(CollidedWith, contact);
  1008. _parent_scene.AddCollisionEventReporting(this);
  1009. }
  1010. public override void AddVDTCCollisionEvent(uint CollidedWith, ContactPoint contact)
  1011. {
  1012. if (CollisionVDTCEvents == null)
  1013. CollisionVDTCEvents = new CollisionEventUpdate();
  1014. CollisionVDTCEvents.AddCollider(CollidedWith, contact);
  1015. _parent_scene.AddCollisionEventReporting(this);
  1016. }
  1017. internal void SleeperAddCollisionEvents()
  1018. {
  1019. if(CollisionEvents != null && CollisionEvents.m_objCollisionList.Count != 0)
  1020. {
  1021. foreach(KeyValuePair<uint,ContactPoint> kvp in CollisionEvents.m_objCollisionList)
  1022. {
  1023. if(kvp.Key == 0)
  1024. continue;
  1025. OdePrim other = _parent_scene.getPrim(kvp.Key);
  1026. if(other == null)
  1027. continue;
  1028. ContactPoint cp = kvp.Value;
  1029. cp.SurfaceNormal = - cp.SurfaceNormal;
  1030. cp.RelativeSpeed = -cp.RelativeSpeed;
  1031. other.AddCollisionEvent(ParentActor.LocalID,cp);
  1032. }
  1033. }
  1034. if(CollisionVDTCEvents != null && CollisionVDTCEvents.m_objCollisionList.Count != 0)
  1035. {
  1036. foreach(KeyValuePair<uint,ContactPoint> kvp in CollisionVDTCEvents.m_objCollisionList)
  1037. {
  1038. OdePrim other = _parent_scene.getPrim(kvp.Key);
  1039. if(other == null)
  1040. continue;
  1041. ContactPoint cp = kvp.Value;
  1042. cp.SurfaceNormal = - cp.SurfaceNormal;
  1043. cp.RelativeSpeed = -cp.RelativeSpeed;
  1044. other.AddCollisionEvent(ParentActor.LocalID,cp);
  1045. }
  1046. }
  1047. }
  1048. internal void clearSleeperCollisions()
  1049. {
  1050. if(CollisionVDTCEvents != null && CollisionVDTCEvents.Count >0 )
  1051. CollisionVDTCEvents.Clear();
  1052. }
  1053. public void SendCollisions(int timestep)
  1054. {
  1055. if (m_cureventsubscription < 50000)
  1056. m_cureventsubscription += timestep;
  1057. if (m_cureventsubscription < m_eventsubscription)
  1058. return;
  1059. if (CollisionEvents == null)
  1060. return;
  1061. int ncolisions = CollisionEvents.m_objCollisionList.Count;
  1062. if (!SentEmptyCollisionsEvent || ncolisions > 0)
  1063. {
  1064. base.SendCollisionUpdate(CollisionEvents);
  1065. m_cureventsubscription = 0;
  1066. if (ncolisions == 0)
  1067. {
  1068. SentEmptyCollisionsEvent = true;
  1069. // _parent_scene.RemoveCollisionEventReporting(this);
  1070. }
  1071. else if(Body == IntPtr.Zero || (d.BodyIsEnabled(Body) && m_bodydisablecontrol >= 0 ))
  1072. {
  1073. SentEmptyCollisionsEvent = false;
  1074. CollisionEvents.Clear();
  1075. }
  1076. }
  1077. }
  1078. public override bool SubscribedEvents()
  1079. {
  1080. if (m_eventsubscription > 0)
  1081. return true;
  1082. return false;
  1083. }
  1084. public OdePrim(String primName, ODEScene parent_scene, Vector3 pos, Vector3 size,
  1085. Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical,bool pisPhantom,byte _shapeType,uint plocalID)
  1086. {
  1087. _parent_scene = parent_scene;
  1088. Name = primName;
  1089. m_localID = plocalID;
  1090. m_vehicle = null;
  1091. if (!pos.IsFinite())
  1092. {
  1093. pos = new Vector3(((float)Constants.RegionSize * 0.5f), ((float)Constants.RegionSize * 0.5f),
  1094. parent_scene.GetTerrainHeightAtXY(((float)Constants.RegionSize * 0.5f), ((float)Constants.RegionSize * 0.5f)) + 0.5f);
  1095. m_log.WarnFormat("[PHYSICS]: Got nonFinite Object create Position for {0}", Name);
  1096. }
  1097. _position = pos;
  1098. givefakepos = 0;
  1099. m_timeStep = parent_scene.ODE_STEPSIZE;
  1100. m_invTimeStep = 1f / m_timeStep;
  1101. m_density = parent_scene.geomDefaultDensity;
  1102. m_body_autodisable_frames = parent_scene.bodyFramesAutoDisable;
  1103. prim_geom = IntPtr.Zero;
  1104. collide_geom = IntPtr.Zero;
  1105. Body = IntPtr.Zero;
  1106. if (!size.IsFinite())
  1107. {
  1108. size = new Vector3(0.5f, 0.5f, 0.5f);
  1109. m_log.WarnFormat("[PHYSICS]: Got nonFinite Object create Size for {0}", Name);
  1110. }
  1111. if (size.X <= 0) size.X = 0.01f;
  1112. if (size.Y <= 0) size.Y = 0.01f;
  1113. if (size.Z <= 0) size.Z = 0.01f;
  1114. _size = size;
  1115. if (!QuaternionIsFinite(rotation))
  1116. {
  1117. rotation = Quaternion.Identity;
  1118. m_log.WarnFormat("[PHYSICS]: Got nonFinite Object create Rotation for {0}", Name);
  1119. }
  1120. _orientation = rotation;
  1121. givefakeori = 0;
  1122. _pbs = pbs;
  1123. m_targetSpace = IntPtr.Zero;
  1124. if (pos.Z < 0)
  1125. {
  1126. m_isphysical = false;
  1127. }
  1128. else
  1129. {
  1130. m_isphysical = pisPhysical;
  1131. }
  1132. m_fakeisphysical = m_isphysical;
  1133. m_isVolumeDetect = false;
  1134. m_fakeisVolumeDetect = false;
  1135. m_force = Vector3.Zero;
  1136. m_iscolliding = false;
  1137. m_colliderfilter = 0;
  1138. m_NoColide = false;
  1139. _triMeshData = IntPtr.Zero;
  1140. m_fakeShapetype = _shapeType;
  1141. m_lastdoneSelected = false;
  1142. m_isSelected = false;
  1143. m_delaySelect = false;
  1144. m_isphantom = pisPhantom;
  1145. m_fakeisphantom = pisPhantom;
  1146. mu = parent_scene.m_materialContactsData[(int)Material.Wood].mu;
  1147. bounce = parent_scene.m_materialContactsData[(int)Material.Wood].bounce;
  1148. m_building = true; // control must set this to false when done
  1149. AddChange(changes.Add, null);
  1150. // get basic mass parameters
  1151. ODEPhysRepData repData = _parent_scene.m_meshWorker.NewActorPhysRep(this, _pbs, _size, _shapeType);
  1152. primVolume = repData.volume;
  1153. m_OBB = repData.OBB;
  1154. m_OBBOffset = repData.OBBOffset;
  1155. UpdatePrimBodyData();
  1156. }
  1157. private void resetCollisionAccounting()
  1158. {
  1159. m_collisionscore = 0;
  1160. }
  1161. private void UpdateCollisionCatFlags()
  1162. {
  1163. if(m_isphysical && m_disabled)
  1164. {
  1165. m_collisionCategories = 0;
  1166. m_collisionFlags = 0;
  1167. }
  1168. else if (m_isSelected)
  1169. {
  1170. m_collisionCategories = CollisionCategories.Selected;
  1171. m_collisionFlags = 0;
  1172. }
  1173. else if (m_isVolumeDetect)
  1174. {
  1175. m_collisionCategories = CollisionCategories.VolumeDtc;
  1176. if (m_isphysical)
  1177. m_collisionFlags = CollisionCategories.Geom | CollisionCategories.Character;
  1178. else
  1179. m_collisionFlags = 0;
  1180. }
  1181. else if (m_isphantom)
  1182. {
  1183. m_collisionCategories = CollisionCategories.Phantom;
  1184. if (m_isphysical)
  1185. m_collisionFlags = CollisionCategories.Land;
  1186. else
  1187. m_collisionFlags = 0;
  1188. }
  1189. else
  1190. {
  1191. m_collisionCategories = CollisionCategories.Geom;
  1192. if (m_isphysical)
  1193. m_collisionFlags = m_default_collisionFlagsPhysical;
  1194. else
  1195. m_collisionFlags = m_default_collisionFlagsNotPhysical;
  1196. }
  1197. }
  1198. private void ApplyCollisionCatFlags()
  1199. {
  1200. if (prim_geom != IntPtr.Zero)
  1201. {
  1202. if (!childPrim && childrenPrim.Count > 0)
  1203. {
  1204. foreach (OdePrim prm in childrenPrim)
  1205. {
  1206. if (m_isphysical && m_disabled)
  1207. {
  1208. prm.m_collisionCategories = 0;
  1209. prm.m_collisionFlags = 0;
  1210. }
  1211. else
  1212. {
  1213. // preserve some
  1214. if (prm.m_isSelected)
  1215. {
  1216. prm.m_collisionCategories = CollisionCategories.Selected;
  1217. prm.m_collisionFlags = 0;
  1218. }
  1219. else if (prm.m_isVolumeDetect)
  1220. {
  1221. prm.m_collisionCategories = CollisionCategories.VolumeDtc;
  1222. if (m_isphysical)
  1223. prm.m_collisionFlags = CollisionCategories.Geom | CollisionCategories.Character;
  1224. else
  1225. prm.m_collisionFlags = 0;
  1226. }
  1227. else if (prm.m_isphantom)
  1228. {
  1229. prm.m_collisionCategories = CollisionCategories.Phantom;
  1230. if (m_isphysical)
  1231. prm.m_collisionFlags = CollisionCategories.Land;
  1232. else
  1233. prm.m_collisionFlags = 0;
  1234. }
  1235. else
  1236. {
  1237. prm.m_collisionCategories = m_collisionCategories;
  1238. prm.m_collisionFlags = m_collisionFlags;
  1239. }
  1240. }
  1241. if (prm.prim_geom != IntPtr.Zero)
  1242. {
  1243. if (prm.m_NoColide)
  1244. {
  1245. d.GeomSetCategoryBits(prm.prim_geom, 0);
  1246. if (m_isphysical)
  1247. d.GeomSetCollideBits(prm.prim_geom, (int)CollisionCategories.Land);
  1248. else
  1249. d.GeomSetCollideBits(prm.prim_geom, 0);
  1250. }
  1251. else
  1252. {
  1253. d.GeomSetCategoryBits(prm.prim_geom, (uint)prm.m_collisionCategories);
  1254. d.GeomSetCollideBits(prm.prim_geom, (uint)prm.m_collisionFlags);
  1255. }
  1256. }
  1257. }
  1258. }
  1259. if (m_NoColide)
  1260. {
  1261. d.GeomSetCategoryBits(prim_geom, 0);
  1262. d.GeomSetCollideBits(prim_geom, (uint)CollisionCategories.Land);
  1263. if (collide_geom != prim_geom && collide_geom != IntPtr.Zero)
  1264. {
  1265. d.GeomSetCategoryBits(collide_geom, 0);
  1266. d.GeomSetCollideBits(collide_geom, (uint)CollisionCategories.Land);
  1267. }
  1268. }
  1269. else
  1270. {
  1271. d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories);
  1272. d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags);
  1273. if (collide_geom != prim_geom && collide_geom != IntPtr.Zero)
  1274. {
  1275. d.GeomSetCategoryBits(collide_geom, (uint)m_collisionCategories);
  1276. d.GeomSetCollideBits(collide_geom, (uint)m_collisionFlags);
  1277. }
  1278. }
  1279. }
  1280. }
  1281. private void createAMotor(byte axislock)
  1282. {
  1283. if (Body == IntPtr.Zero)
  1284. return;
  1285. if (Amotor != IntPtr.Zero)
  1286. {
  1287. d.JointDestroy(Amotor);
  1288. Amotor = IntPtr.Zero;
  1289. }
  1290. int axisnum = 0;
  1291. bool axisX = false;
  1292. bool axisY = false;
  1293. bool axisZ = false;
  1294. if((axislock & 0x02) != 0)
  1295. {
  1296. axisnum++;
  1297. axisX = true;
  1298. }
  1299. if((axislock & 0x04) != 0)
  1300. {
  1301. axisnum++;
  1302. axisY = true;
  1303. }
  1304. if((axislock & 0x08) != 0)
  1305. {
  1306. axisnum++;
  1307. axisZ = true;
  1308. }
  1309. if(axisnum == 0)
  1310. return;
  1311. // stop it
  1312. d.BodySetTorque(Body, 0, 0, 0);
  1313. d.BodySetAngularVel(Body, 0, 0, 0);
  1314. Amotor = d.JointCreateAMotor(_parent_scene.world, IntPtr.Zero);
  1315. d.JointAttach(Amotor, Body, IntPtr.Zero);
  1316. d.JointSetAMotorMode(Amotor, 0);
  1317. d.JointSetAMotorNumAxes(Amotor, axisnum);
  1318. // get current orientation to lock
  1319. d.Quaternion dcur = d.BodyGetQuaternion(Body);
  1320. Quaternion curr; // crap convertion between identical things
  1321. curr.X = dcur.X;
  1322. curr.Y = dcur.Y;
  1323. curr.Z = dcur.Z;
  1324. curr.W = dcur.W;
  1325. Vector3 ax;
  1326. int i = 0;
  1327. int j = 0;
  1328. if (axisX)
  1329. {
  1330. ax = (new Vector3(1, 0, 0)) * curr; // rotate world X to current local X
  1331. d.JointSetAMotorAxis(Amotor, 0, 0, ax.X, ax.Y, ax.Z);
  1332. d.JointSetAMotorAngle(Amotor, 0, 0);
  1333. d.JointSetAMotorParam(Amotor, (int)d.JointParam.LoStop, 0f);
  1334. d.JointSetAMotorParam(Amotor, (int)d.JointParam.HiStop, 0f);
  1335. d.JointSetAMotorParam(Amotor, (int)d.JointParam.Vel, 0);
  1336. d.JointSetAMotorParam(Amotor, (int)d.JointParam.FudgeFactor, 0.0001f);
  1337. d.JointSetAMotorParam(Amotor, (int)d.JointParam.Bounce, 0f);
  1338. d.JointSetAMotorParam(Amotor, (int)d.JointParam.CFM, 0f);
  1339. d.JointSetAMotorParam(Amotor, (int)d.JointParam.FMax, 5e8f);
  1340. d.JointSetAMotorParam(Amotor, (int)d.JointParam.StopCFM, 0f);
  1341. d.JointSetAMotorParam(Amotor, (int)d.JointParam.StopERP, 0.8f);
  1342. i++;
  1343. j = 256; // move to next axis set
  1344. }
  1345. if (axisY)
  1346. {
  1347. ax = (new Vector3(0, 1, 0)) * curr;
  1348. d.JointSetAMotorAxis(Amotor, i, 0, ax.X, ax.Y, ax.Z);
  1349. d.JointSetAMotorAngle(Amotor, i, 0);
  1350. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.LoStop, 0f);
  1351. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.HiStop, 0f);
  1352. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.Vel, 0);
  1353. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.FudgeFactor, 0.0001f);
  1354. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.Bounce, 0f);
  1355. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.CFM, 0f);
  1356. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.FMax, 5e8f);
  1357. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.StopCFM, 0f);
  1358. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.StopERP, 0.8f);
  1359. i++;
  1360. j += 256;
  1361. }
  1362. if (axisZ)
  1363. {
  1364. ax = (new Vector3(0, 0, 1)) * curr;
  1365. d.JointSetAMotorAxis(Amotor, i, 0, ax.X, ax.Y, ax.Z);
  1366. d.JointSetAMotorAngle(Amotor, i, 0);
  1367. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.LoStop, 0f);
  1368. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.HiStop, 0f);
  1369. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.Vel, 0);
  1370. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.FudgeFactor, 0.0001f);
  1371. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.Bounce, 0f);
  1372. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.CFM, 0f);
  1373. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.FMax, 5e8f);
  1374. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.StopCFM, 0f);
  1375. d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.StopERP, 0.8f);
  1376. }
  1377. }
  1378. private void SetGeom(IntPtr geom)
  1379. {
  1380. prim_geom = geom;
  1381. //Console.WriteLine("SetGeom to " + prim_geom + " for " + Name);
  1382. if (prim_geom != IntPtr.Zero)
  1383. {
  1384. if (m_NoColide)
  1385. {
  1386. d.GeomSetCategoryBits(prim_geom, 0);
  1387. if (m_isphysical)
  1388. {
  1389. d.GeomSetCollideBits(prim_geom, (uint)CollisionCategories.Land);
  1390. }
  1391. else
  1392. {
  1393. d.GeomSetCollideBits(prim_geom, 0);
  1394. d.GeomDisable(prim_geom);
  1395. }
  1396. }
  1397. else
  1398. {
  1399. d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories);
  1400. d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags);
  1401. }
  1402. UpdatePrimBodyData();
  1403. _parent_scene.actor_name_map[prim_geom] = this;
  1404. /*
  1405. // debug
  1406. d.AABB aabb;
  1407. d.GeomGetAABB(prim_geom, out aabb);
  1408. float x = aabb.MaxX - aabb.MinX;
  1409. float y = aabb.MaxY - aabb.MinY;
  1410. float z = aabb.MaxZ - aabb.MinZ;
  1411. if( x > 60.0f || y > 60.0f || z > 60.0f)
  1412. m_log.WarnFormat("[PHYSICS]: large prim geo {0},size {1}, AABBsize <{2},{3},{4}, mesh {5} at {6}",
  1413. Name, _size.ToString(), x, y, z, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh", _position.ToString());
  1414. else if (x < 0.001f || y < 0.001f || z < 0.001f)
  1415. m_log.WarnFormat("[PHYSICS]: small prim geo {0},size {1}, AABBsize <{2},{3},{4}, mesh {5} at {6}",
  1416. Name, _size.ToString(), x, y, z, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh", _position.ToString());
  1417. */
  1418. }
  1419. else
  1420. m_log.Warn("Setting bad Geom");
  1421. }
  1422. private bool GetMeshGeom()
  1423. {
  1424. IntPtr vertices, indices;
  1425. int vertexCount, indexCount;
  1426. int vertexStride, triStride;
  1427. IMesh mesh = m_mesh;
  1428. if (mesh == null)
  1429. return false;
  1430. mesh.getVertexListAsPtrToFloatArray(out vertices, out vertexStride, out vertexCount);
  1431. mesh.getIndexListAsPtrToIntArray(out indices, out triStride, out indexCount);
  1432. if (vertexCount == 0 || indexCount == 0)
  1433. {
  1434. m_log.WarnFormat("[PHYSICS]: Invalid mesh data on OdePrim {0}, mesh {1} at {2}",
  1435. Name, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh",_position.ToString());
  1436. m_hasOBB = false;
  1437. m_OBBOffset = Vector3.Zero;
  1438. m_OBB = _size * 0.5f;
  1439. m_physCost = 0.1f;
  1440. m_streamCost = 1.0f;
  1441. _parent_scene.mesher.ReleaseMesh(mesh);
  1442. m_meshState = MeshState.MeshFailed;
  1443. m_mesh = null;
  1444. return false;
  1445. }
  1446. if (vertexCount > 64000 || indexCount > 64000)
  1447. {
  1448. m_log.WarnFormat("[PHYSICS]: large mesh data on OdePrim {0}, mesh {1} at {2}, {3} vertices, {4} indexes",
  1449. Name, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh",
  1450. _position.ToString() ,vertexCount , indexCount );
  1451. }
  1452. IntPtr geo = IntPtr.Zero;
  1453. try
  1454. {
  1455. _triMeshData = d.GeomTriMeshDataCreate();
  1456. d.GeomTriMeshDataBuildSimple(_triMeshData, vertices, vertexStride, vertexCount, indices, indexCount, triStride);
  1457. d.GeomTriMeshDataPreprocess(_triMeshData);
  1458. geo = d.CreateTriMesh(m_targetSpace, _triMeshData, null, null, null);
  1459. }
  1460. catch (Exception e)
  1461. {
  1462. m_log.ErrorFormat("[PHYSICS]: SetGeom Mesh failed for {0} exception: {1}", Name, e);
  1463. if (_triMeshData != IntPtr.Zero)
  1464. {
  1465. try
  1466. {
  1467. d.GeomTriMeshDataDestroy(_triMeshData);
  1468. }
  1469. catch
  1470. {
  1471. }
  1472. }
  1473. _triMeshData = IntPtr.Zero;
  1474. m_hasOBB = false;
  1475. m_OBBOffset = Vector3.Zero;
  1476. m_OBB = _size * 0.5f;
  1477. m_physCost = 0.1f;
  1478. m_streamCost = 1.0f;
  1479. _parent_scene.mesher.ReleaseMesh(mesh);
  1480. m_meshState = MeshState.MeshFailed;
  1481. m_mesh = null;
  1482. return false;
  1483. }
  1484. m_physCost = 0.0013f * (float)indexCount;
  1485. // todo
  1486. m_streamCost = 1.0f;
  1487. SetGeom(geo);
  1488. return true;
  1489. }
  1490. private void CreateGeom(bool OverrideToBox)
  1491. {
  1492. bool hasMesh = false;
  1493. m_NoColide = false;
  1494. if ((m_meshState & MeshState.MeshNoColide) != 0)
  1495. m_NoColide = true;
  1496. else if(!OverrideToBox && m_mesh != null)
  1497. {
  1498. if (GetMeshGeom())
  1499. hasMesh = true;
  1500. else
  1501. m_NoColide = true;
  1502. }
  1503. if (!hasMesh)
  1504. {
  1505. IntPtr geo = IntPtr.Zero;
  1506. if (_pbs.ProfileShape == ProfileShape.HalfCircle && _pbs.PathCurve == (byte)Extrusion.Curve1
  1507. && _size.X == _size.Y && _size.Y == _size.Z)
  1508. { // it's a sphere
  1509. try
  1510. {
  1511. geo = d.CreateSphere(m_targetSpace, _size.X * 0.5f);
  1512. }
  1513. catch (Exception e)
  1514. {
  1515. m_log.WarnFormat("[PHYSICS]: Create sphere failed: {0}", e);
  1516. return;
  1517. }
  1518. }
  1519. else
  1520. {// do it as a box
  1521. try
  1522. {
  1523. geo = d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z);
  1524. }
  1525. catch (Exception e)
  1526. {
  1527. m_log.Warn("[PHYSICS]: Create box failed: {0}", e);
  1528. return;
  1529. }
  1530. }
  1531. m_physCost = 0.1f;
  1532. m_streamCost = 1.0f;
  1533. SetGeom(geo);
  1534. }
  1535. }
  1536. private void RemoveGeom()
  1537. {
  1538. if (prim_geom != IntPtr.Zero)
  1539. {
  1540. _parent_scene.actor_name_map.Remove(prim_geom);
  1541. try
  1542. {
  1543. d.GeomDestroy(prim_geom);
  1544. if (_triMeshData != IntPtr.Zero)
  1545. {
  1546. d.GeomTriMeshDataDestroy(_triMeshData);
  1547. _triMeshData = IntPtr.Zero;
  1548. }
  1549. }
  1550. catch (Exception e)
  1551. {
  1552. m_log.ErrorFormat("[PHYSICS]: PrimGeom destruction failed for {0} exception {1}", Name, e);
  1553. }
  1554. prim_geom = IntPtr.Zero;
  1555. collide_geom = IntPtr.Zero;
  1556. m_targetSpace = IntPtr.Zero;
  1557. }
  1558. else
  1559. {
  1560. m_log.ErrorFormat("[PHYSICS]: PrimGeom destruction BAD {0}", Name);
  1561. }
  1562. lock (m_meshlock)
  1563. {
  1564. if (m_mesh != null)
  1565. {
  1566. _parent_scene.mesher.ReleaseMesh(m_mesh);
  1567. m_mesh = null;
  1568. }
  1569. }
  1570. Body = IntPtr.Zero;
  1571. m_hasOBB = false;
  1572. }
  1573. //sets non physical prim m_targetSpace to right space in spaces grid for static prims
  1574. // should only be called for non physical prims unless they are becoming non physical
  1575. private void SetInStaticSpace(OdePrim prim)
  1576. {
  1577. IntPtr targetSpace = _parent_scene.MoveGeomToStaticSpace(prim.prim_geom, prim.m_targetSpace);
  1578. prim.m_targetSpace = targetSpace;
  1579. collide_geom = IntPtr.Zero;
  1580. }
  1581. public void enableBodySoft()
  1582. {
  1583. m_disabled = false;
  1584. if (!childPrim && !m_isSelected)
  1585. {
  1586. if (m_isphysical && Body != IntPtr.Zero)
  1587. {
  1588. UpdateCollisionCatFlags();
  1589. ApplyCollisionCatFlags();
  1590. _zeroFlag = true;
  1591. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  1592. d.BodyEnable(Body);
  1593. }
  1594. }
  1595. resetCollisionAccounting();
  1596. }
  1597. private void disableBodySoft()
  1598. {
  1599. m_disabled = true;
  1600. if (!childPrim)
  1601. {
  1602. if (m_isphysical && Body != IntPtr.Zero)
  1603. {
  1604. if (m_isSelected)
  1605. m_collisionFlags = CollisionCategories.Selected;
  1606. else
  1607. m_collisionCategories = 0;
  1608. m_collisionFlags = 0;
  1609. ApplyCollisionCatFlags();
  1610. d.BodyDisable(Body);
  1611. }
  1612. }
  1613. }
  1614. private void MakeBody()
  1615. {
  1616. if (!m_isphysical) // only physical get bodies
  1617. return;
  1618. if (childPrim) // child prims don't get bodies;
  1619. return;
  1620. if (m_building)
  1621. return;
  1622. if (prim_geom == IntPtr.Zero)
  1623. {
  1624. m_log.Warn("[PHYSICS]: Unable to link the linkset. Root has no geom yet");
  1625. return;
  1626. }
  1627. if (Body != IntPtr.Zero)
  1628. {
  1629. DestroyBody();
  1630. m_log.Warn("[PHYSICS]: MakeBody called having a body");
  1631. }
  1632. if (d.GeomGetBody(prim_geom) != IntPtr.Zero)
  1633. {
  1634. d.GeomSetBody(prim_geom, IntPtr.Zero);
  1635. m_log.Warn("[PHYSICS]: MakeBody root geom already had a body");
  1636. }
  1637. bool noInertiaOverride = (m_InertiaOverride == null);
  1638. Body = d.BodyCreate(_parent_scene.world);
  1639. d.Matrix3 mymat = new d.Matrix3();
  1640. d.Quaternion myrot = new d.Quaternion();
  1641. d.Mass objdmass = new d.Mass { };
  1642. myrot.X = _orientation.X;
  1643. myrot.Y = _orientation.Y;
  1644. myrot.Z = _orientation.Z;
  1645. myrot.W = _orientation.W;
  1646. d.RfromQ(out mymat, ref myrot);
  1647. // set the body rotation
  1648. d.BodySetRotation(Body, ref mymat);
  1649. if(noInertiaOverride)
  1650. {
  1651. objdmass = primdMass;
  1652. d.MassRotate(ref objdmass, ref mymat);
  1653. }
  1654. // recompute full object inertia if needed
  1655. if (childrenPrim.Count > 0)
  1656. {
  1657. d.Matrix3 mat = new d.Matrix3();
  1658. d.Quaternion quat = new d.Quaternion();
  1659. d.Mass tmpdmass = new d.Mass { };
  1660. Vector3 rcm;
  1661. rcm.X = _position.X;
  1662. rcm.Y = _position.Y;
  1663. rcm.Z = _position.Z;
  1664. lock (childrenPrim)
  1665. {
  1666. foreach (OdePrim prm in childrenPrim)
  1667. {
  1668. if (prm.prim_geom == IntPtr.Zero)
  1669. {
  1670. m_log.Warn("[PHYSICS]: Unable to link one of the linkset elements, skipping it. No geom yet");
  1671. continue;
  1672. }
  1673. quat.X = prm._orientation.X;
  1674. quat.Y = prm._orientation.Y;
  1675. quat.Z = prm._orientation.Z;
  1676. quat.W = prm._orientation.W;
  1677. d.RfromQ(out mat, ref quat);
  1678. // fix prim colision cats
  1679. if (d.GeomGetBody(prm.prim_geom) != IntPtr.Zero)
  1680. {
  1681. d.GeomSetBody(prm.prim_geom, IntPtr.Zero);
  1682. m_log.Warn("[PHYSICS]: MakeBody child geom already had a body");
  1683. }
  1684. d.GeomClearOffset(prm.prim_geom);
  1685. d.GeomSetBody(prm.prim_geom, Body);
  1686. prm.Body = Body;
  1687. d.GeomSetOffsetWorldRotation(prm.prim_geom, ref mat); // set relative rotation
  1688. if(noInertiaOverride)
  1689. {
  1690. tmpdmass = prm.primdMass;
  1691. d.MassRotate(ref tmpdmass, ref mat);
  1692. Vector3 ppos = prm._position;
  1693. ppos.X -= rcm.X;
  1694. ppos.Y -= rcm.Y;
  1695. ppos.Z -= rcm.Z;
  1696. // refer inertia to root prim center of mass position
  1697. d.MassTranslate(ref tmpdmass,
  1698. ppos.X,
  1699. ppos.Y,
  1700. ppos.Z);
  1701. d.MassAdd(ref objdmass, ref tmpdmass); // add to total object inertia
  1702. }
  1703. }
  1704. }
  1705. }
  1706. d.GeomClearOffset(prim_geom); // make sure we don't have a hidden offset
  1707. // associate root geom with body
  1708. d.GeomSetBody(prim_geom, Body);
  1709. if(noInertiaOverride)
  1710. d.BodySetPosition(Body, _position.X + objdmass.c.X, _position.Y + objdmass.c.Y, _position.Z + objdmass.c.Z);
  1711. else
  1712. {
  1713. Vector3 ncm = m_InertiaOverride.CenterOfMass * _orientation;
  1714. d.BodySetPosition(Body,
  1715. _position.X + ncm.X,
  1716. _position.Y + ncm.Y,
  1717. _position.Z + ncm.Z);
  1718. }
  1719. d.GeomSetOffsetWorldPosition(prim_geom, _position.X, _position.Y, _position.Z);
  1720. if(noInertiaOverride)
  1721. {
  1722. d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
  1723. myrot.X = -myrot.X;
  1724. myrot.Y = -myrot.Y;
  1725. myrot.Z = -myrot.Z;
  1726. d.RfromQ(out mymat, ref myrot);
  1727. d.MassRotate(ref objdmass, ref mymat);
  1728. d.BodySetMass(Body, ref objdmass);
  1729. m_mass = objdmass.mass;
  1730. }
  1731. else
  1732. {
  1733. objdmass.c.X = 0;
  1734. objdmass.c.Y = 0;
  1735. objdmass.c.Z = 0;
  1736. objdmass.I.M00 = m_InertiaOverride.Inertia.X;
  1737. objdmass.I.M11 = m_InertiaOverride.Inertia.Y;
  1738. objdmass.I.M22 = m_InertiaOverride.Inertia.Z;
  1739. objdmass.mass = m_InertiaOverride.TotalMass;
  1740. if(Math.Abs(m_InertiaOverride.InertiaRotation.W) < 0.999)
  1741. {
  1742. d.Matrix3 inertiarotmat = new d.Matrix3();
  1743. d.Quaternion inertiarot = new d.Quaternion();
  1744. inertiarot.X = m_InertiaOverride.InertiaRotation.X;
  1745. inertiarot.Y = m_InertiaOverride.InertiaRotation.Y;
  1746. inertiarot.Z = m_InertiaOverride.InertiaRotation.Z;
  1747. inertiarot.W = m_InertiaOverride.InertiaRotation.W;
  1748. d.RfromQ(out inertiarotmat, ref inertiarot);
  1749. d.MassRotate(ref objdmass, ref inertiarotmat);
  1750. }
  1751. d.BodySetMass(Body, ref objdmass);
  1752. m_mass = objdmass.mass;
  1753. }
  1754. // disconnect from world gravity so we can apply buoyancy
  1755. d.BodySetGravityMode(Body, false);
  1756. d.BodySetAutoDisableFlag(Body, true);
  1757. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  1758. d.BodySetAutoDisableAngularThreshold(Body, 0.05f);
  1759. d.BodySetAutoDisableLinearThreshold(Body, 0.05f);
  1760. d.BodySetDamping(Body, .004f, .001f);
  1761. if (m_targetSpace != IntPtr.Zero)
  1762. {
  1763. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1764. if (d.SpaceQuery(m_targetSpace, prim_geom))
  1765. d.SpaceRemove(m_targetSpace, prim_geom);
  1766. }
  1767. if (childrenPrim.Count == 0)
  1768. {
  1769. collide_geom = prim_geom;
  1770. m_targetSpace = _parent_scene.ActiveSpace;
  1771. }
  1772. else
  1773. {
  1774. m_targetSpace = d.SimpleSpaceCreate(_parent_scene.ActiveSpace);
  1775. d.SpaceSetSublevel(m_targetSpace, 3);
  1776. d.SpaceSetCleanup(m_targetSpace, false);
  1777. d.GeomSetCategoryBits(m_targetSpace, (uint)(CollisionCategories.Space |
  1778. CollisionCategories.Geom |
  1779. CollisionCategories.Phantom |
  1780. CollisionCategories.VolumeDtc
  1781. ));
  1782. d.GeomSetCollideBits(m_targetSpace, 0);
  1783. collide_geom = m_targetSpace;
  1784. }
  1785. d.SpaceAdd(m_targetSpace, prim_geom);
  1786. if (m_delaySelect)
  1787. {
  1788. m_isSelected = true;
  1789. m_delaySelect = false;
  1790. }
  1791. m_collisionscore = 0;
  1792. UpdateCollisionCatFlags();
  1793. ApplyCollisionCatFlags();
  1794. _parent_scene.addActivePrim(this);
  1795. lock (childrenPrim)
  1796. {
  1797. foreach (OdePrim prm in childrenPrim)
  1798. {
  1799. if (prm.prim_geom == IntPtr.Zero)
  1800. continue;
  1801. Vector3 ppos = prm._position;
  1802. d.GeomSetOffsetWorldPosition(prm.prim_geom, ppos.X, ppos.Y, ppos.Z); // set relative position
  1803. if (prm.m_targetSpace != m_targetSpace)
  1804. {
  1805. if (prm.m_targetSpace != IntPtr.Zero)
  1806. {
  1807. _parent_scene.waitForSpaceUnlock(prm.m_targetSpace);
  1808. if (d.SpaceQuery(prm.m_targetSpace, prm.prim_geom))
  1809. d.SpaceRemove(prm.m_targetSpace, prm.prim_geom);
  1810. }
  1811. prm.m_targetSpace = m_targetSpace;
  1812. d.SpaceAdd(m_targetSpace, prm.prim_geom);
  1813. }
  1814. prm.m_collisionscore = 0;
  1815. if(!m_disabled)
  1816. prm.m_disabled = false;
  1817. _parent_scene.addActivePrim(prm);
  1818. }
  1819. }
  1820. // The body doesn't already have a finite rotation mode set here
  1821. if (m_angularlocks != 0 && _parent == null)
  1822. {
  1823. createAMotor(m_angularlocks);
  1824. }
  1825. if (m_isSelected || m_disabled)
  1826. {
  1827. d.BodyDisable(Body);
  1828. _zeroFlag = true;
  1829. }
  1830. else
  1831. {
  1832. d.BodySetAngularVel(Body, m_rotationalVelocity.X, m_rotationalVelocity.Y, m_rotationalVelocity.Z);
  1833. d.BodySetLinearVel(Body, _velocity.X, _velocity.Y, _velocity.Z);
  1834. _zeroFlag = false;
  1835. m_bodydisablecontrol = 0;
  1836. }
  1837. _parent_scene.addActiveGroups(this);
  1838. }
  1839. private void DestroyBody()
  1840. {
  1841. if (Body != IntPtr.Zero)
  1842. {
  1843. _parent_scene.remActivePrim(this);
  1844. collide_geom = IntPtr.Zero;
  1845. if (m_disabled)
  1846. m_collisionCategories = 0;
  1847. else if (m_isSelected)
  1848. m_collisionCategories = CollisionCategories.Selected;
  1849. else if (m_isVolumeDetect)
  1850. m_collisionCategories = CollisionCategories.VolumeDtc;
  1851. else if (m_isphantom)
  1852. m_collisionCategories = CollisionCategories.Phantom;
  1853. else
  1854. m_collisionCategories = CollisionCategories.Geom;
  1855. m_collisionFlags = 0;
  1856. if (prim_geom != IntPtr.Zero)
  1857. {
  1858. if (m_NoColide)
  1859. {
  1860. d.GeomSetCategoryBits(prim_geom, 0);
  1861. d.GeomSetCollideBits(prim_geom, 0);
  1862. }
  1863. else
  1864. {
  1865. d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories);
  1866. d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags);
  1867. }
  1868. UpdateDataFromGeom();
  1869. d.GeomSetBody(prim_geom, IntPtr.Zero);
  1870. SetInStaticSpace(this);
  1871. }
  1872. if (!childPrim)
  1873. {
  1874. lock (childrenPrim)
  1875. {
  1876. foreach (OdePrim prm in childrenPrim)
  1877. {
  1878. _parent_scene.remActivePrim(prm);
  1879. if (prm.m_isSelected)
  1880. prm.m_collisionCategories = CollisionCategories.Selected;
  1881. else if (prm.m_isVolumeDetect)
  1882. prm.m_collisionCategories = CollisionCategories.VolumeDtc;
  1883. else if (prm.m_isphantom)
  1884. prm.m_collisionCategories = CollisionCategories.Phantom;
  1885. else
  1886. prm.m_collisionCategories = CollisionCategories.Geom;
  1887. prm.m_collisionFlags = 0;
  1888. if (prm.prim_geom != IntPtr.Zero)
  1889. {
  1890. if (prm.m_NoColide)
  1891. {
  1892. d.GeomSetCategoryBits(prm.prim_geom, 0);
  1893. d.GeomSetCollideBits(prm.prim_geom, 0);
  1894. }
  1895. else
  1896. {
  1897. d.GeomSetCategoryBits(prm.prim_geom, (uint)prm.m_collisionCategories);
  1898. d.GeomSetCollideBits(prm.prim_geom, (uint)prm.m_collisionFlags);
  1899. }
  1900. prm.UpdateDataFromGeom();
  1901. SetInStaticSpace(prm);
  1902. }
  1903. prm.Body = IntPtr.Zero;
  1904. prm.m_mass = prm.primMass;
  1905. prm.m_collisionscore = 0;
  1906. }
  1907. }
  1908. if (Amotor != IntPtr.Zero)
  1909. {
  1910. d.JointDestroy(Amotor);
  1911. Amotor = IntPtr.Zero;
  1912. }
  1913. _parent_scene.remActiveGroup(this);
  1914. d.BodyDestroy(Body);
  1915. }
  1916. Body = IntPtr.Zero;
  1917. }
  1918. m_mass = primMass;
  1919. m_collisionscore = 0;
  1920. }
  1921. private void FixInertia(Vector3 NewPos,Quaternion newrot)
  1922. {
  1923. d.Matrix3 mat = new d.Matrix3();
  1924. d.Quaternion quat = new d.Quaternion();
  1925. d.Mass tmpdmass = new d.Mass { };
  1926. d.Mass objdmass = new d.Mass { };
  1927. d.BodyGetMass(Body, out tmpdmass);
  1928. objdmass = tmpdmass;
  1929. d.Vector3 dobjpos;
  1930. d.Vector3 thispos;
  1931. // get current object position and rotation
  1932. dobjpos = d.BodyGetPosition(Body);
  1933. // get prim own inertia in its local frame
  1934. tmpdmass = primdMass;
  1935. // transform to object frame
  1936. mat = d.GeomGetOffsetRotation(prim_geom);
  1937. d.MassRotate(ref tmpdmass, ref mat);
  1938. thispos = d.GeomGetOffsetPosition(prim_geom);
  1939. d.MassTranslate(ref tmpdmass,
  1940. thispos.X,
  1941. thispos.Y,
  1942. thispos.Z);
  1943. // subtract current prim inertia from object
  1944. DMassSubPartFromObj(ref tmpdmass, ref objdmass);
  1945. // back prim own inertia
  1946. tmpdmass = primdMass;
  1947. // update to new position and orientation
  1948. _position = NewPos;
  1949. d.GeomSetOffsetWorldPosition(prim_geom, NewPos.X, NewPos.Y, NewPos.Z);
  1950. _orientation = newrot;
  1951. quat.X = newrot.X;
  1952. quat.Y = newrot.Y;
  1953. quat.Z = newrot.Z;
  1954. quat.W = newrot.W;
  1955. d.GeomSetOffsetWorldQuaternion(prim_geom, ref quat);
  1956. mat = d.GeomGetOffsetRotation(prim_geom);
  1957. d.MassRotate(ref tmpdmass, ref mat);
  1958. thispos = d.GeomGetOffsetPosition(prim_geom);
  1959. d.MassTranslate(ref tmpdmass,
  1960. thispos.X,
  1961. thispos.Y,
  1962. thispos.Z);
  1963. d.MassAdd(ref objdmass, ref tmpdmass);
  1964. // fix all positions
  1965. IntPtr g = d.BodyGetFirstGeom(Body);
  1966. while (g != IntPtr.Zero)
  1967. {
  1968. thispos = d.GeomGetOffsetPosition(g);
  1969. thispos.X -= objdmass.c.X;
  1970. thispos.Y -= objdmass.c.Y;
  1971. thispos.Z -= objdmass.c.Z;
  1972. d.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z);
  1973. g = d.dBodyGetNextGeom(g);
  1974. }
  1975. d.BodyVectorToWorld(Body,objdmass.c.X, objdmass.c.Y, objdmass.c.Z,out thispos);
  1976. d.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z);
  1977. d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
  1978. d.BodySetMass(Body, ref objdmass);
  1979. m_mass = objdmass.mass;
  1980. }
  1981. private void FixInertia(Vector3 NewPos)
  1982. {
  1983. d.Matrix3 primmat = new d.Matrix3();
  1984. d.Mass tmpdmass = new d.Mass { };
  1985. d.Mass objdmass = new d.Mass { };
  1986. d.Mass primmass = new d.Mass { };
  1987. d.Vector3 dobjpos;
  1988. d.Vector3 thispos;
  1989. d.BodyGetMass(Body, out objdmass);
  1990. // get prim own inertia in its local frame
  1991. primmass = primdMass;
  1992. // transform to object frame
  1993. primmat = d.GeomGetOffsetRotation(prim_geom);
  1994. d.MassRotate(ref primmass, ref primmat);
  1995. tmpdmass = primmass;
  1996. thispos = d.GeomGetOffsetPosition(prim_geom);
  1997. d.MassTranslate(ref tmpdmass,
  1998. thispos.X,
  1999. thispos.Y,
  2000. thispos.Z);
  2001. // subtract current prim inertia from object
  2002. DMassSubPartFromObj(ref tmpdmass, ref objdmass);
  2003. // update to new position
  2004. _position = NewPos;
  2005. d.GeomSetOffsetWorldPosition(prim_geom, NewPos.X, NewPos.Y, NewPos.Z);
  2006. thispos = d.GeomGetOffsetPosition(prim_geom);
  2007. d.MassTranslate(ref primmass,
  2008. thispos.X,
  2009. thispos.Y,
  2010. thispos.Z);
  2011. d.MassAdd(ref objdmass, ref primmass);
  2012. // fix all positions
  2013. IntPtr g = d.BodyGetFirstGeom(Body);
  2014. while (g != IntPtr.Zero)
  2015. {
  2016. thispos = d.GeomGetOffsetPosition(g);
  2017. thispos.X -= objdmass.c.X;
  2018. thispos.Y -= objdmass.c.Y;
  2019. thispos.Z -= objdmass.c.Z;
  2020. d.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z);
  2021. g = d.dBodyGetNextGeom(g);
  2022. }
  2023. d.BodyVectorToWorld(Body, objdmass.c.X, objdmass.c.Y, objdmass.c.Z, out thispos);
  2024. // get current object position and rotation
  2025. dobjpos = d.BodyGetPosition(Body);
  2026. d.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z);
  2027. d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
  2028. d.BodySetMass(Body, ref objdmass);
  2029. m_mass = objdmass.mass;
  2030. }
  2031. private void FixInertia(Quaternion newrot)
  2032. {
  2033. d.Matrix3 mat = new d.Matrix3();
  2034. d.Quaternion quat = new d.Quaternion();
  2035. d.Mass tmpdmass = new d.Mass { };
  2036. d.Mass objdmass = new d.Mass { };
  2037. d.Vector3 dobjpos;
  2038. d.Vector3 thispos;
  2039. d.BodyGetMass(Body, out objdmass);
  2040. // get prim own inertia in its local frame
  2041. tmpdmass = primdMass;
  2042. mat = d.GeomGetOffsetRotation(prim_geom);
  2043. d.MassRotate(ref tmpdmass, ref mat);
  2044. // transform to object frame
  2045. thispos = d.GeomGetOffsetPosition(prim_geom);
  2046. d.MassTranslate(ref tmpdmass,
  2047. thispos.X,
  2048. thispos.Y,
  2049. thispos.Z);
  2050. // subtract current prim inertia from object
  2051. DMassSubPartFromObj(ref tmpdmass, ref objdmass);
  2052. // update to new orientation
  2053. _orientation = newrot;
  2054. quat.X = newrot.X;
  2055. quat.Y = newrot.Y;
  2056. quat.Z = newrot.Z;
  2057. quat.W = newrot.W;
  2058. d.GeomSetOffsetWorldQuaternion(prim_geom, ref quat);
  2059. tmpdmass = primdMass;
  2060. mat = d.GeomGetOffsetRotation(prim_geom);
  2061. d.MassRotate(ref tmpdmass, ref mat);
  2062. d.MassTranslate(ref tmpdmass,
  2063. thispos.X,
  2064. thispos.Y,
  2065. thispos.Z);
  2066. d.MassAdd(ref objdmass, ref tmpdmass);
  2067. // fix all positions
  2068. IntPtr g = d.BodyGetFirstGeom(Body);
  2069. while (g != IntPtr.Zero)
  2070. {
  2071. thispos = d.GeomGetOffsetPosition(g);
  2072. thispos.X -= objdmass.c.X;
  2073. thispos.Y -= objdmass.c.Y;
  2074. thispos.Z -= objdmass.c.Z;
  2075. d.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z);
  2076. g = d.dBodyGetNextGeom(g);
  2077. }
  2078. d.BodyVectorToWorld(Body, objdmass.c.X, objdmass.c.Y, objdmass.c.Z, out thispos);
  2079. // get current object position and rotation
  2080. dobjpos = d.BodyGetPosition(Body);
  2081. d.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z);
  2082. d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
  2083. d.BodySetMass(Body, ref objdmass);
  2084. m_mass = objdmass.mass;
  2085. }
  2086. #region Mass Calculation
  2087. private void UpdatePrimBodyData()
  2088. {
  2089. primMass = m_density * primVolume;
  2090. if (primMass <= 0)
  2091. primMass = 0.0001f;//ckrinke: Mass must be greater then zero.
  2092. if (primMass > _parent_scene.maximumMassObject)
  2093. primMass = _parent_scene.maximumMassObject;
  2094. m_mass = primMass; // just in case
  2095. d.MassSetBoxTotal(out primdMass, primMass, 2.0f * m_OBB.X, 2.0f * m_OBB.Y, 2.0f * m_OBB.Z);
  2096. d.MassTranslate(ref primdMass,
  2097. m_OBBOffset.X,
  2098. m_OBBOffset.Y,
  2099. m_OBBOffset.Z);
  2100. primOOBradiusSQ = m_OBB.LengthSquared();
  2101. if (_triMeshData != IntPtr.Zero)
  2102. {
  2103. float pc = m_physCost;
  2104. float psf = primOOBradiusSQ;
  2105. psf *= 1.33f * .2f;
  2106. pc *= psf;
  2107. if (pc < 0.1f)
  2108. pc = 0.1f;
  2109. m_physCost = pc;
  2110. }
  2111. else
  2112. m_physCost = 0.1f;
  2113. m_streamCost = 1.0f;
  2114. }
  2115. #endregion
  2116. /// <summary>
  2117. /// Add a child prim to this parent prim.
  2118. /// </summary>
  2119. /// <param name="prim">Child prim</param>
  2120. // I'm the parent
  2121. // prim is the child
  2122. public void ParentPrim(OdePrim prim)
  2123. {
  2124. //Console.WriteLine("ParentPrim " + m_primName);
  2125. if (this.m_localID != prim.m_localID)
  2126. {
  2127. DestroyBody(); // for now we need to rebuil entire object on link change
  2128. lock (childrenPrim)
  2129. {
  2130. // adopt the prim
  2131. if (!childrenPrim.Contains(prim))
  2132. childrenPrim.Add(prim);
  2133. // see if this prim has kids and adopt them also
  2134. // should not happen for now
  2135. foreach (OdePrim prm in prim.childrenPrim)
  2136. {
  2137. if (!childrenPrim.Contains(prm))
  2138. {
  2139. if (prm.Body != IntPtr.Zero)
  2140. {
  2141. if (prm.prim_geom != IntPtr.Zero)
  2142. d.GeomSetBody(prm.prim_geom, IntPtr.Zero);
  2143. if (prm.Body != prim.Body)
  2144. prm.DestroyBody(); // don't loose bodies around
  2145. prm.Body = IntPtr.Zero;
  2146. }
  2147. childrenPrim.Add(prm);
  2148. prm._parent = this;
  2149. }
  2150. }
  2151. }
  2152. //Remove old children from the prim
  2153. prim.childrenPrim.Clear();
  2154. if (prim.Body != IntPtr.Zero)
  2155. {
  2156. if (prim.prim_geom != IntPtr.Zero)
  2157. d.GeomSetBody(prim.prim_geom, IntPtr.Zero);
  2158. prim.DestroyBody(); // don't loose bodies around
  2159. prim.Body = IntPtr.Zero;
  2160. }
  2161. prim.childPrim = true;
  2162. prim._parent = this;
  2163. MakeBody(); // full nasty reconstruction
  2164. }
  2165. }
  2166. private void UpdateChildsfromgeom()
  2167. {
  2168. if (childrenPrim.Count > 0)
  2169. {
  2170. foreach (OdePrim prm in childrenPrim)
  2171. prm.UpdateDataFromGeom();
  2172. }
  2173. }
  2174. private void UpdateDataFromGeom()
  2175. {
  2176. if (prim_geom != IntPtr.Zero)
  2177. {
  2178. d.Quaternion qtmp;
  2179. d.GeomCopyQuaternion(prim_geom, out qtmp);
  2180. _orientation.X = qtmp.X;
  2181. _orientation.Y = qtmp.Y;
  2182. _orientation.Z = qtmp.Z;
  2183. _orientation.W = qtmp.W;
  2184. /*
  2185. // Debug
  2186. float qlen = _orientation.Length();
  2187. if (qlen > 1.01f || qlen < 0.99)
  2188. m_log.WarnFormat("[PHYSICS]: Got nonnorm quaternion from geom in Object {0} norm {1}", Name, qlen);
  2189. //
  2190. */
  2191. _orientation.Normalize();
  2192. d.Vector3 lpos = d.GeomGetPosition(prim_geom);
  2193. _position.X = lpos.X;
  2194. _position.Y = lpos.Y;
  2195. _position.Z = lpos.Z;
  2196. }
  2197. }
  2198. private void ChildDelink(OdePrim odePrim, bool remakebodies)
  2199. {
  2200. // Okay, we have a delinked child.. destroy all body and remake
  2201. if (odePrim != this && !childrenPrim.Contains(odePrim))
  2202. return;
  2203. DestroyBody();
  2204. if (odePrim == this) // delinking the root prim
  2205. {
  2206. OdePrim newroot = null;
  2207. lock (childrenPrim)
  2208. {
  2209. if (childrenPrim.Count > 0)
  2210. {
  2211. newroot = childrenPrim[0];
  2212. childrenPrim.RemoveAt(0);
  2213. foreach (OdePrim prm in childrenPrim)
  2214. {
  2215. newroot.childrenPrim.Add(prm);
  2216. }
  2217. childrenPrim.Clear();
  2218. }
  2219. if (newroot != null)
  2220. {
  2221. newroot.childPrim = false;
  2222. newroot._parent = null;
  2223. if (remakebodies)
  2224. newroot.MakeBody();
  2225. }
  2226. }
  2227. }
  2228. else
  2229. {
  2230. lock (childrenPrim)
  2231. {
  2232. childrenPrim.Remove(odePrim);
  2233. odePrim.childPrim = false;
  2234. odePrim._parent = null;
  2235. // odePrim.UpdateDataFromGeom();
  2236. if (remakebodies)
  2237. odePrim.MakeBody();
  2238. }
  2239. }
  2240. if (remakebodies)
  2241. MakeBody();
  2242. }
  2243. protected void ChildRemove(OdePrim odePrim, bool reMakeBody)
  2244. {
  2245. // Okay, we have a delinked child.. destroy all body and remake
  2246. if (odePrim != this && !childrenPrim.Contains(odePrim))
  2247. return;
  2248. DestroyBody();
  2249. if (odePrim == this)
  2250. {
  2251. OdePrim newroot = null;
  2252. lock (childrenPrim)
  2253. {
  2254. if (childrenPrim.Count > 0)
  2255. {
  2256. newroot = childrenPrim[0];
  2257. childrenPrim.RemoveAt(0);
  2258. foreach (OdePrim prm in childrenPrim)
  2259. {
  2260. newroot.childrenPrim.Add(prm);
  2261. }
  2262. childrenPrim.Clear();
  2263. }
  2264. if (newroot != null)
  2265. {
  2266. newroot.childPrim = false;
  2267. newroot._parent = null;
  2268. newroot.MakeBody();
  2269. }
  2270. }
  2271. if (reMakeBody)
  2272. MakeBody();
  2273. return;
  2274. }
  2275. else
  2276. {
  2277. lock (childrenPrim)
  2278. {
  2279. childrenPrim.Remove(odePrim);
  2280. odePrim.childPrim = false;
  2281. odePrim._parent = null;
  2282. if (reMakeBody)
  2283. odePrim.MakeBody();
  2284. }
  2285. }
  2286. MakeBody();
  2287. }
  2288. #region changes
  2289. private void changeadd()
  2290. {
  2291. _parent_scene.addToPrims(this);
  2292. }
  2293. private void changeAngularLock(byte newLocks)
  2294. {
  2295. // do we have a Physical object?
  2296. if (Body != IntPtr.Zero)
  2297. {
  2298. //Check that we have a Parent
  2299. //If we have a parent then we're not authorative here
  2300. if (_parent == null)
  2301. {
  2302. if (newLocks != 0)
  2303. {
  2304. createAMotor(newLocks);
  2305. }
  2306. else
  2307. {
  2308. if (Amotor != IntPtr.Zero)
  2309. {
  2310. d.JointDestroy(Amotor);
  2311. Amotor = IntPtr.Zero;
  2312. }
  2313. }
  2314. }
  2315. }
  2316. // Store this for later in case we get turned into a separate body
  2317. m_angularlocks = newLocks;
  2318. }
  2319. private void changeLink(OdePrim NewParent)
  2320. {
  2321. if (_parent == null && NewParent != null)
  2322. {
  2323. NewParent.ParentPrim(this);
  2324. }
  2325. else if (_parent != null)
  2326. {
  2327. if (_parent is OdePrim)
  2328. {
  2329. if (NewParent != _parent)
  2330. {
  2331. (_parent as OdePrim).ChildDelink(this, false); // for now...
  2332. childPrim = false;
  2333. if (NewParent != null)
  2334. {
  2335. NewParent.ParentPrim(this);
  2336. }
  2337. }
  2338. }
  2339. }
  2340. _parent = NewParent;
  2341. }
  2342. private void Stop()
  2343. {
  2344. if (!childPrim)
  2345. {
  2346. // m_force = Vector3.Zero;
  2347. m_forceacc = Vector3.Zero;
  2348. m_angularForceacc = Vector3.Zero;
  2349. // m_torque = Vector3.Zero;
  2350. _velocity = Vector3.Zero;
  2351. m_acceleration = Vector3.Zero;
  2352. m_rotationalVelocity = Vector3.Zero;
  2353. _target_velocity = Vector3.Zero;
  2354. if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
  2355. m_vehicle.Stop();
  2356. _zeroFlag = false;
  2357. base.RequestPhysicsterseUpdate();
  2358. }
  2359. if (Body != IntPtr.Zero)
  2360. {
  2361. d.BodySetForce(Body, 0f, 0f, 0f);
  2362. d.BodySetTorque(Body, 0f, 0f, 0f);
  2363. d.BodySetLinearVel(Body, 0f, 0f, 0f);
  2364. d.BodySetAngularVel(Body, 0f, 0f, 0f);
  2365. }
  2366. }
  2367. private void changePhantomStatus(bool newval)
  2368. {
  2369. m_isphantom = newval;
  2370. UpdateCollisionCatFlags();
  2371. ApplyCollisionCatFlags();
  2372. }
  2373. /* not in use
  2374. internal void ChildSelectedChange(bool childSelect)
  2375. {
  2376. if(childPrim)
  2377. return;
  2378. if (childSelect == m_isSelected)
  2379. return;
  2380. if (childSelect)
  2381. {
  2382. DoSelectedStatus(true);
  2383. }
  2384. else
  2385. {
  2386. foreach (OdePrim prm in childrenPrim)
  2387. {
  2388. if (prm.m_isSelected)
  2389. return;
  2390. }
  2391. DoSelectedStatus(false);
  2392. }
  2393. }
  2394. */
  2395. private void changeSelectedStatus(bool newval)
  2396. {
  2397. if (m_lastdoneSelected == newval)
  2398. return;
  2399. m_lastdoneSelected = newval;
  2400. DoSelectedStatus(newval);
  2401. }
  2402. private void CheckDelaySelect()
  2403. {
  2404. if (m_delaySelect)
  2405. {
  2406. DoSelectedStatus(m_isSelected);
  2407. }
  2408. }
  2409. private void DoSelectedStatus(bool newval)
  2410. {
  2411. m_isSelected = newval;
  2412. Stop();
  2413. if (newval)
  2414. {
  2415. if (!childPrim && Body != IntPtr.Zero)
  2416. d.BodyDisable(Body);
  2417. if (m_delaySelect || m_isphysical)
  2418. {
  2419. m_collisionCategories = CollisionCategories.Selected;
  2420. m_collisionFlags = 0;
  2421. if (!childPrim)
  2422. {
  2423. foreach (OdePrim prm in childrenPrim)
  2424. {
  2425. prm.m_collisionCategories = m_collisionCategories;
  2426. prm.m_collisionFlags = m_collisionFlags;
  2427. if (prm.prim_geom != IntPtr.Zero)
  2428. {
  2429. if (prm.m_NoColide)
  2430. {
  2431. d.GeomSetCategoryBits(prm.prim_geom, 0);
  2432. d.GeomSetCollideBits(prm.prim_geom, 0);
  2433. }
  2434. else
  2435. {
  2436. d.GeomSetCategoryBits(prm.prim_geom, (uint)m_collisionCategories);
  2437. d.GeomSetCollideBits(prm.prim_geom, (uint)m_collisionFlags);
  2438. }
  2439. }
  2440. prm.m_delaySelect = false;
  2441. }
  2442. }
  2443. // else if (_parent != null)
  2444. // ((OdePrim)_parent).ChildSelectedChange(true);
  2445. if (prim_geom != IntPtr.Zero)
  2446. {
  2447. if (m_NoColide)
  2448. {
  2449. d.GeomSetCategoryBits(prim_geom, 0);
  2450. d.GeomSetCollideBits(prim_geom, 0);
  2451. if (collide_geom != prim_geom && collide_geom != IntPtr.Zero)
  2452. {
  2453. d.GeomSetCategoryBits(collide_geom, 0);
  2454. d.GeomSetCollideBits(collide_geom, 0);
  2455. }
  2456. }
  2457. else
  2458. {
  2459. d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories);
  2460. d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags);
  2461. if (collide_geom != prim_geom && collide_geom != IntPtr.Zero)
  2462. {
  2463. d.GeomSetCategoryBits(collide_geom, (uint)m_collisionCategories);
  2464. d.GeomSetCollideBits(collide_geom, (uint)m_collisionFlags);
  2465. }
  2466. }
  2467. }
  2468. m_delaySelect = false;
  2469. }
  2470. else if(!m_isphysical)
  2471. {
  2472. m_delaySelect = true;
  2473. }
  2474. }
  2475. else
  2476. {
  2477. if (!childPrim)
  2478. {
  2479. if (Body != IntPtr.Zero && !m_disabled)
  2480. {
  2481. _zeroFlag = true;
  2482. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2483. d.BodyEnable(Body);
  2484. }
  2485. }
  2486. // else if (_parent != null)
  2487. // ((OdePrim)_parent).ChildSelectedChange(false);
  2488. UpdateCollisionCatFlags();
  2489. ApplyCollisionCatFlags();
  2490. m_delaySelect = false;
  2491. }
  2492. resetCollisionAccounting();
  2493. }
  2494. private void changePosition(Vector3 newPos)
  2495. {
  2496. CheckDelaySelect();
  2497. if (m_isphysical)
  2498. {
  2499. if (childPrim) // inertia is messed, must rebuild
  2500. {
  2501. if (m_building)
  2502. {
  2503. _position = newPos;
  2504. }
  2505. else if (m_forcePosOrRotation && _position != newPos && Body != IntPtr.Zero)
  2506. {
  2507. FixInertia(newPos);
  2508. if (!d.BodyIsEnabled(Body))
  2509. {
  2510. _zeroFlag = true;
  2511. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2512. d.BodyEnable(Body);
  2513. }
  2514. }
  2515. }
  2516. else
  2517. {
  2518. if (_position != newPos)
  2519. {
  2520. d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
  2521. _position = newPos;
  2522. }
  2523. if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
  2524. {
  2525. _zeroFlag = true;
  2526. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2527. d.BodyEnable(Body);
  2528. }
  2529. }
  2530. }
  2531. else
  2532. {
  2533. if (prim_geom != IntPtr.Zero)
  2534. {
  2535. if (newPos != _position)
  2536. {
  2537. d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
  2538. _position = newPos;
  2539. m_targetSpace = _parent_scene.MoveGeomToStaticSpace(prim_geom, m_targetSpace);
  2540. }
  2541. }
  2542. }
  2543. givefakepos--;
  2544. if (givefakepos < 0)
  2545. givefakepos = 0;
  2546. // changeSelectedStatus();
  2547. resetCollisionAccounting();
  2548. }
  2549. private void changeOrientation(Quaternion newOri)
  2550. {
  2551. CheckDelaySelect();
  2552. if (m_isphysical)
  2553. {
  2554. if (childPrim) // inertia is messed, must rebuild
  2555. {
  2556. if (m_building)
  2557. {
  2558. _orientation = newOri;
  2559. }
  2560. /*
  2561. else if (m_forcePosOrRotation && _orientation != newOri && Body != IntPtr.Zero)
  2562. {
  2563. FixInertia(_position, newOri);
  2564. if (!d.BodyIsEnabled(Body))
  2565. d.BodyEnable(Body);
  2566. }
  2567. */
  2568. }
  2569. else
  2570. {
  2571. if (newOri != _orientation)
  2572. {
  2573. d.Quaternion myrot = new d.Quaternion();
  2574. myrot.X = newOri.X;
  2575. myrot.Y = newOri.Y;
  2576. myrot.Z = newOri.Z;
  2577. myrot.W = newOri.W;
  2578. d.GeomSetQuaternion(prim_geom, ref myrot);
  2579. _orientation = newOri;
  2580. if (Body != IntPtr.Zero)
  2581. {
  2582. if(m_angularlocks != 0)
  2583. createAMotor(m_angularlocks);
  2584. }
  2585. }
  2586. if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
  2587. {
  2588. _zeroFlag = true;
  2589. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2590. d.BodyEnable(Body);
  2591. }
  2592. }
  2593. }
  2594. else
  2595. {
  2596. if (prim_geom != IntPtr.Zero)
  2597. {
  2598. if (newOri != _orientation)
  2599. {
  2600. d.Quaternion myrot = new d.Quaternion();
  2601. myrot.X = newOri.X;
  2602. myrot.Y = newOri.Y;
  2603. myrot.Z = newOri.Z;
  2604. myrot.W = newOri.W;
  2605. d.GeomSetQuaternion(prim_geom, ref myrot);
  2606. _orientation = newOri;
  2607. }
  2608. }
  2609. }
  2610. givefakeori--;
  2611. if (givefakeori < 0)
  2612. givefakeori = 0;
  2613. resetCollisionAccounting();
  2614. }
  2615. private void changePositionAndOrientation(Vector3 newPos, Quaternion newOri)
  2616. {
  2617. CheckDelaySelect();
  2618. if (m_isphysical)
  2619. {
  2620. if (childPrim && m_building) // inertia is messed, must rebuild
  2621. {
  2622. _position = newPos;
  2623. _orientation = newOri;
  2624. }
  2625. else
  2626. {
  2627. if (newOri != _orientation)
  2628. {
  2629. d.Quaternion myrot = new d.Quaternion();
  2630. myrot.X = newOri.X;
  2631. myrot.Y = newOri.Y;
  2632. myrot.Z = newOri.Z;
  2633. myrot.W = newOri.W;
  2634. d.GeomSetQuaternion(prim_geom, ref myrot);
  2635. _orientation = newOri;
  2636. if (Body != IntPtr.Zero && m_angularlocks != 0)
  2637. createAMotor(m_angularlocks);
  2638. }
  2639. if (_position != newPos)
  2640. {
  2641. d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
  2642. _position = newPos;
  2643. }
  2644. if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
  2645. {
  2646. _zeroFlag = true;
  2647. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2648. d.BodyEnable(Body);
  2649. }
  2650. }
  2651. }
  2652. else
  2653. {
  2654. // string primScenAvatarIn = _parent_scene.whichspaceamIin(_position);
  2655. // int[] arrayitem = _parent_scene.calculateSpaceArrayItemFromPos(_position);
  2656. if (prim_geom != IntPtr.Zero)
  2657. {
  2658. if (newOri != _orientation)
  2659. {
  2660. d.Quaternion myrot = new d.Quaternion();
  2661. myrot.X = newOri.X;
  2662. myrot.Y = newOri.Y;
  2663. myrot.Z = newOri.Z;
  2664. myrot.W = newOri.W;
  2665. d.GeomSetQuaternion(prim_geom, ref myrot);
  2666. _orientation = newOri;
  2667. }
  2668. if (newPos != _position)
  2669. {
  2670. d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
  2671. _position = newPos;
  2672. m_targetSpace = _parent_scene.MoveGeomToStaticSpace(prim_geom, m_targetSpace);
  2673. }
  2674. }
  2675. }
  2676. givefakepos--;
  2677. if (givefakepos < 0)
  2678. givefakepos = 0;
  2679. givefakeori--;
  2680. if (givefakeori < 0)
  2681. givefakeori = 0;
  2682. resetCollisionAccounting();
  2683. }
  2684. private void changeDisable(bool disable)
  2685. {
  2686. if (disable)
  2687. {
  2688. if (!m_disabled)
  2689. disableBodySoft();
  2690. }
  2691. else
  2692. {
  2693. if (m_disabled)
  2694. enableBodySoft();
  2695. }
  2696. }
  2697. private void changePhysicsStatus(bool NewStatus)
  2698. {
  2699. CheckDelaySelect();
  2700. m_isphysical = NewStatus;
  2701. if (!childPrim)
  2702. {
  2703. if (NewStatus)
  2704. {
  2705. if (Body == IntPtr.Zero)
  2706. MakeBody();
  2707. }
  2708. else
  2709. {
  2710. if (Body != IntPtr.Zero)
  2711. {
  2712. DestroyBody();
  2713. }
  2714. Stop();
  2715. }
  2716. }
  2717. resetCollisionAccounting();
  2718. }
  2719. private void changeSize(Vector3 newSize)
  2720. {
  2721. }
  2722. private void changeShape(PrimitiveBaseShape newShape)
  2723. {
  2724. }
  2725. private void changeAddPhysRep(ODEPhysRepData repData)
  2726. {
  2727. _size = repData.size; //??
  2728. _pbs = repData.pbs;
  2729. m_mesh = repData.mesh;
  2730. m_assetID = repData.assetID;
  2731. m_meshState = repData.meshState;
  2732. m_hasOBB = repData.hasOBB;
  2733. m_OBBOffset = repData.OBBOffset;
  2734. m_OBB = repData.OBB;
  2735. primVolume = repData.volume;
  2736. CreateGeom(repData.isTooSmall);
  2737. if (prim_geom != IntPtr.Zero)
  2738. {
  2739. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  2740. d.Quaternion myrot = new d.Quaternion();
  2741. myrot.X = _orientation.X;
  2742. myrot.Y = _orientation.Y;
  2743. myrot.Z = _orientation.Z;
  2744. myrot.W = _orientation.W;
  2745. d.GeomSetQuaternion(prim_geom, ref myrot);
  2746. }
  2747. if (!m_isphysical)
  2748. {
  2749. SetInStaticSpace(this);
  2750. UpdateCollisionCatFlags();
  2751. ApplyCollisionCatFlags();
  2752. }
  2753. else
  2754. MakeBody();
  2755. if ((m_meshState & MeshState.NeedMask) != 0)
  2756. {
  2757. repData.size = _size;
  2758. repData.pbs = _pbs;
  2759. repData.shapetype = m_fakeShapetype;
  2760. _parent_scene.m_meshWorker.RequestMesh(repData);
  2761. }
  2762. else
  2763. m_shapetype = repData.shapetype;
  2764. }
  2765. private void changePhysRepData(ODEPhysRepData repData)
  2766. {
  2767. if(_size == repData.size &&
  2768. _pbs == repData.pbs &&
  2769. m_shapetype == repData.shapetype &&
  2770. m_mesh == repData.mesh &&
  2771. primVolume == repData.volume)
  2772. return;
  2773. CheckDelaySelect();
  2774. OdePrim parent = (OdePrim)_parent;
  2775. bool chp = childPrim;
  2776. if (chp)
  2777. {
  2778. if (parent != null)
  2779. {
  2780. parent.DestroyBody();
  2781. }
  2782. }
  2783. else
  2784. {
  2785. DestroyBody();
  2786. }
  2787. RemoveGeom();
  2788. _size = repData.size;
  2789. _pbs = repData.pbs;
  2790. m_mesh = repData.mesh;
  2791. m_assetID = repData.assetID;
  2792. m_meshState = repData.meshState;
  2793. m_hasOBB = repData.hasOBB;
  2794. m_OBBOffset = repData.OBBOffset;
  2795. m_OBB = repData.OBB;
  2796. primVolume = repData.volume;
  2797. CreateGeom(repData.isTooSmall);
  2798. if (prim_geom != IntPtr.Zero)
  2799. {
  2800. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  2801. d.Quaternion myrot = new d.Quaternion();
  2802. myrot.X = _orientation.X;
  2803. myrot.Y = _orientation.Y;
  2804. myrot.Z = _orientation.Z;
  2805. myrot.W = _orientation.W;
  2806. d.GeomSetQuaternion(prim_geom, ref myrot);
  2807. }
  2808. if (m_isphysical)
  2809. {
  2810. if (chp)
  2811. {
  2812. if (parent != null)
  2813. {
  2814. parent.MakeBody();
  2815. }
  2816. }
  2817. else
  2818. MakeBody();
  2819. }
  2820. else
  2821. {
  2822. SetInStaticSpace(this);
  2823. UpdateCollisionCatFlags();
  2824. ApplyCollisionCatFlags();
  2825. }
  2826. resetCollisionAccounting();
  2827. if ((m_meshState & MeshState.NeedMask) != 0)
  2828. {
  2829. repData.size = _size;
  2830. repData.pbs = _pbs;
  2831. repData.shapetype = m_fakeShapetype;
  2832. _parent_scene.m_meshWorker.RequestMesh(repData);
  2833. }
  2834. else
  2835. m_shapetype = repData.shapetype;
  2836. }
  2837. private void changeFloatOnWater(bool newval)
  2838. {
  2839. m_collidesWater = newval;
  2840. UpdateCollisionCatFlags();
  2841. ApplyCollisionCatFlags();
  2842. }
  2843. private void changeSetTorque(Vector3 newtorque)
  2844. {
  2845. if (!m_isSelected && !m_outbounds)
  2846. {
  2847. if (m_isphysical && Body != IntPtr.Zero)
  2848. {
  2849. if (m_disabled)
  2850. enableBodySoft();
  2851. else if (!d.BodyIsEnabled(Body))
  2852. {
  2853. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2854. d.BodyEnable(Body);
  2855. }
  2856. }
  2857. m_torque = newtorque;
  2858. }
  2859. }
  2860. private void changeForce(Vector3 force)
  2861. {
  2862. m_force = force;
  2863. if (!m_isSelected && !m_outbounds && Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
  2864. {
  2865. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2866. d.BodyEnable(Body);
  2867. }
  2868. }
  2869. private void changeAddForce(Vector3 theforce)
  2870. {
  2871. m_forceacc += theforce;
  2872. if (!m_isSelected && !m_outbounds)
  2873. {
  2874. lock (this)
  2875. {
  2876. //m_log.Info("[PHYSICS]: dequeing forcelist");
  2877. if (m_isphysical && Body != IntPtr.Zero)
  2878. {
  2879. if (m_disabled)
  2880. enableBodySoft();
  2881. else if (!d.BodyIsEnabled(Body))
  2882. {
  2883. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2884. d.BodyEnable(Body);
  2885. }
  2886. }
  2887. }
  2888. m_collisionscore = 0;
  2889. }
  2890. }
  2891. // actually angular impulse
  2892. private void changeAddAngularImpulse(Vector3 aimpulse)
  2893. {
  2894. m_angularForceacc += aimpulse * m_invTimeStep;
  2895. if (!m_isSelected && !m_outbounds)
  2896. {
  2897. lock (this)
  2898. {
  2899. if (m_isphysical && Body != IntPtr.Zero)
  2900. {
  2901. if (m_disabled)
  2902. enableBodySoft();
  2903. else if (!d.BodyIsEnabled(Body))
  2904. {
  2905. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2906. d.BodyEnable(Body);
  2907. }
  2908. }
  2909. }
  2910. m_collisionscore = 0;
  2911. }
  2912. }
  2913. private void changevelocity(Vector3 newVel)
  2914. {
  2915. float len = newVel.LengthSquared();
  2916. if (len > 100000.0f) // limit to 100m/s
  2917. {
  2918. len = 100.0f / (float)Math.Sqrt(len);
  2919. newVel *= len;
  2920. }
  2921. if (!m_isSelected && !m_outbounds)
  2922. {
  2923. if (Body != IntPtr.Zero)
  2924. {
  2925. if (m_disabled)
  2926. enableBodySoft();
  2927. else if (!d.BodyIsEnabled(Body))
  2928. {
  2929. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2930. d.BodyEnable(Body);
  2931. }
  2932. d.BodySetLinearVel(Body, newVel.X, newVel.Y, newVel.Z);
  2933. }
  2934. //resetCollisionAccounting();
  2935. }
  2936. _velocity = newVel;
  2937. }
  2938. private void changeangvelocity(Vector3 newAngVel)
  2939. {
  2940. float len = newAngVel.LengthSquared();
  2941. if (len > _parent_scene.maxAngVelocitySQ)
  2942. {
  2943. len = _parent_scene.maximumAngularVelocity / (float)Math.Sqrt(len);
  2944. newAngVel *= len;
  2945. }
  2946. if (!m_isSelected && !m_outbounds)
  2947. {
  2948. if (Body != IntPtr.Zero)
  2949. {
  2950. if (m_disabled)
  2951. enableBodySoft();
  2952. else if (!d.BodyIsEnabled(Body))
  2953. {
  2954. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2955. d.BodyEnable(Body);
  2956. }
  2957. d.BodySetAngularVel(Body, newAngVel.X, newAngVel.Y, newAngVel.Z);
  2958. }
  2959. //resetCollisionAccounting();
  2960. }
  2961. m_rotationalVelocity = newAngVel;
  2962. }
  2963. private void changeVolumedetetion(bool newVolDtc)
  2964. {
  2965. m_isVolumeDetect = newVolDtc;
  2966. m_fakeisVolumeDetect = newVolDtc;
  2967. UpdateCollisionCatFlags();
  2968. ApplyCollisionCatFlags();
  2969. }
  2970. protected void changeBuilding(bool newbuilding)
  2971. {
  2972. // Check if we need to do anything
  2973. if (newbuilding == m_building)
  2974. return;
  2975. if ((bool)newbuilding)
  2976. {
  2977. m_building = true;
  2978. if (!childPrim)
  2979. DestroyBody();
  2980. }
  2981. else
  2982. {
  2983. m_building = false;
  2984. CheckDelaySelect();
  2985. if (!childPrim)
  2986. MakeBody();
  2987. }
  2988. if (!childPrim && childrenPrim.Count > 0)
  2989. {
  2990. foreach (OdePrim prm in childrenPrim)
  2991. prm.changeBuilding(m_building); // call directly
  2992. }
  2993. }
  2994. public void changeSetVehicle(VehicleData vdata)
  2995. {
  2996. if (m_vehicle == null)
  2997. m_vehicle = new ODEDynamics(this);
  2998. m_vehicle.DoSetVehicle(vdata);
  2999. }
  3000. private void changeVehicleType(int value)
  3001. {
  3002. if (value == (int)Vehicle.TYPE_NONE)
  3003. {
  3004. if (m_vehicle != null)
  3005. m_vehicle = null;
  3006. }
  3007. else
  3008. {
  3009. if (m_vehicle == null)
  3010. m_vehicle = new ODEDynamics(this);
  3011. m_vehicle.ProcessTypeChange((Vehicle)value);
  3012. }
  3013. }
  3014. private void changeVehicleFloatParam(strVehicleFloatParam fp)
  3015. {
  3016. if (m_vehicle == null)
  3017. return;
  3018. m_vehicle.ProcessFloatVehicleParam((Vehicle)fp.param, fp.value);
  3019. }
  3020. private void changeVehicleVectorParam(strVehicleVectorParam vp)
  3021. {
  3022. if (m_vehicle == null)
  3023. return;
  3024. m_vehicle.ProcessVectorVehicleParam((Vehicle)vp.param, vp.value);
  3025. }
  3026. private void changeVehicleRotationParam(strVehicleQuatParam qp)
  3027. {
  3028. if (m_vehicle == null)
  3029. return;
  3030. m_vehicle.ProcessRotationVehicleParam((Vehicle)qp.param, qp.value);
  3031. }
  3032. private void changeVehicleFlags(strVehicleBoolParam bp)
  3033. {
  3034. if (m_vehicle == null)
  3035. return;
  3036. m_vehicle.ProcessVehicleFlags(bp.param, bp.value);
  3037. }
  3038. private void changeBuoyancy(float b)
  3039. {
  3040. m_buoyancy = b;
  3041. }
  3042. private void changePIDTarget(Vector3 trg)
  3043. {
  3044. m_PIDTarget = trg;
  3045. }
  3046. private void changePIDTau(float tau)
  3047. {
  3048. m_PIDTau = tau;
  3049. }
  3050. private void changePIDActive(bool val)
  3051. {
  3052. m_usePID = val;
  3053. }
  3054. private void changePIDHoverHeight(float val)
  3055. {
  3056. m_PIDHoverHeight = val;
  3057. if (val == 0)
  3058. m_useHoverPID = false;
  3059. }
  3060. private void changePIDHoverType(PIDHoverType type)
  3061. {
  3062. m_PIDHoverType = type;
  3063. }
  3064. private void changePIDHoverTau(float tau)
  3065. {
  3066. m_PIDHoverTau = tau;
  3067. }
  3068. private void changePIDHoverActive(bool active)
  3069. {
  3070. m_useHoverPID = active;
  3071. }
  3072. private void changeInertia(PhysicsInertiaData inertia)
  3073. {
  3074. m_InertiaOverride = inertia;
  3075. if (Body != IntPtr.Zero)
  3076. DestroyBody();
  3077. MakeBody();
  3078. }
  3079. #endregion
  3080. public void Move()
  3081. {
  3082. if (!childPrim && m_isphysical && Body != IntPtr.Zero &&
  3083. !m_disabled && !m_isSelected && !m_building && !m_outbounds)
  3084. {
  3085. if (!d.BodyIsEnabled(Body))
  3086. {
  3087. // let vehicles sleep
  3088. if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
  3089. return;
  3090. if (++m_bodydisablecontrol < 50)
  3091. return;
  3092. // clear residuals
  3093. d.BodySetAngularVel(Body,0f,0f,0f);
  3094. d.BodySetLinearVel(Body,0f,0f,0f);
  3095. _zeroFlag = true;
  3096. d.BodySetAutoDisableSteps(Body, 1);
  3097. d.BodyEnable(Body);
  3098. m_bodydisablecontrol = -3;
  3099. }
  3100. if(m_bodydisablecontrol < 0)
  3101. m_bodydisablecontrol++;
  3102. d.Vector3 lpos = d.GeomGetPosition(prim_geom); // root position that is seem by rest of simulator
  3103. if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
  3104. {
  3105. // 'VEHICLES' are dealt with in ODEDynamics.cs
  3106. m_vehicle.Step();
  3107. return;
  3108. }
  3109. float fx = 0;
  3110. float fy = 0;
  3111. float fz = 0;
  3112. float mass = m_mass;
  3113. if (m_usePID && m_PIDTau > 0)
  3114. {
  3115. // for now position error
  3116. _target_velocity =
  3117. new Vector3(
  3118. (m_PIDTarget.X - lpos.X),
  3119. (m_PIDTarget.Y - lpos.Y),
  3120. (m_PIDTarget.Z - lpos.Z)
  3121. );
  3122. if (_target_velocity.ApproxEquals(Vector3.Zero, 0.02f))
  3123. {
  3124. d.BodySetPosition(Body, m_PIDTarget.X, m_PIDTarget.Y, m_PIDTarget.Z);
  3125. d.BodySetLinearVel(Body, 0, 0, 0);
  3126. return;
  3127. }
  3128. else
  3129. {
  3130. _zeroFlag = false;
  3131. float tmp = 1 / m_PIDTau;
  3132. _target_velocity *= tmp;
  3133. // apply limits
  3134. tmp = _target_velocity.Length();
  3135. if (tmp > 50.0f)
  3136. {
  3137. tmp = 50 / tmp;
  3138. _target_velocity *= tmp;
  3139. }
  3140. else if (tmp < 0.05f)
  3141. {
  3142. tmp = 0.05f / tmp;
  3143. _target_velocity *= tmp;
  3144. }
  3145. d.Vector3 vel = d.BodyGetLinearVel(Body);
  3146. fx = (_target_velocity.X - vel.X) * m_invTimeStep;
  3147. fy = (_target_velocity.Y - vel.Y) * m_invTimeStep;
  3148. fz = (_target_velocity.Z - vel.Z) * m_invTimeStep;
  3149. // d.BodySetLinearVel(Body, _target_velocity.X, _target_velocity.Y, _target_velocity.Z);
  3150. }
  3151. } // end if (m_usePID)
  3152. // Hover PID Controller needs to be mutually exlusive to MoveTo PID controller
  3153. else if (m_useHoverPID && m_PIDHoverTau != 0 && m_PIDHoverHeight != 0)
  3154. {
  3155. // Non-Vehicles have a limited set of Hover options.
  3156. // determine what our target height really is based on HoverType
  3157. m_groundHeight = _parent_scene.GetTerrainHeightAtXY(lpos.X, lpos.Y);
  3158. switch (m_PIDHoverType)
  3159. {
  3160. case PIDHoverType.Ground:
  3161. m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight;
  3162. break;
  3163. case PIDHoverType.GroundAndWater:
  3164. m_waterHeight = _parent_scene.GetWaterLevel();
  3165. if (m_groundHeight > m_waterHeight)
  3166. m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight;
  3167. else
  3168. m_targetHoverHeight = m_waterHeight + m_PIDHoverHeight;
  3169. break;
  3170. } // end switch (m_PIDHoverType)
  3171. // don't go underground unless volumedetector
  3172. if (m_targetHoverHeight > m_groundHeight || m_isVolumeDetect)
  3173. {
  3174. d.Vector3 vel = d.BodyGetLinearVel(Body);
  3175. fz = (m_targetHoverHeight - lpos.Z);
  3176. // if error is zero, use position control; otherwise, velocity control
  3177. if (Math.Abs(fz) < 0.01f)
  3178. {
  3179. d.BodySetPosition(Body, lpos.X, lpos.Y, m_targetHoverHeight);
  3180. d.BodySetLinearVel(Body, vel.X, vel.Y, 0);
  3181. }
  3182. else
  3183. {
  3184. _zeroFlag = false;
  3185. fz /= m_PIDHoverTau;
  3186. float tmp = Math.Abs(fz);
  3187. if (tmp > 50)
  3188. fz = 50 * Math.Sign(fz);
  3189. else if (tmp < 0.1)
  3190. fz = 0.1f * Math.Sign(fz);
  3191. fz = ((fz - vel.Z) * m_invTimeStep);
  3192. }
  3193. }
  3194. }
  3195. else
  3196. {
  3197. float b = (1.0f - m_buoyancy) * m_gravmod;
  3198. fx = _parent_scene.gravityx * b;
  3199. fy = _parent_scene.gravityy * b;
  3200. fz = _parent_scene.gravityz * b;
  3201. }
  3202. fx *= mass;
  3203. fy *= mass;
  3204. fz *= mass;
  3205. // constant force
  3206. fx += m_force.X;
  3207. fy += m_force.Y;
  3208. fz += m_force.Z;
  3209. fx += m_forceacc.X;
  3210. fy += m_forceacc.Y;
  3211. fz += m_forceacc.Z;
  3212. m_forceacc = Vector3.Zero;
  3213. //m_log.Info("[OBJPID]: X:" + fx.ToString() + " Y:" + fy.ToString() + " Z:" + fz.ToString());
  3214. if (fx != 0 || fy != 0 || fz != 0)
  3215. {
  3216. d.BodyAddForce(Body, fx, fy, fz);
  3217. //Console.WriteLine("AddForce " + fx + "," + fy + "," + fz);
  3218. }
  3219. Vector3 trq;
  3220. trq = m_torque;
  3221. trq += m_angularForceacc;
  3222. m_angularForceacc = Vector3.Zero;
  3223. if (trq.X != 0 || trq.Y != 0 || trq.Z != 0)
  3224. {
  3225. d.BodyAddTorque(Body, trq.X, trq.Y, trq.Z);
  3226. }
  3227. }
  3228. else
  3229. { // is not physical, or is not a body or is selected
  3230. // _zeroPosition = d.BodyGetPosition(Body);
  3231. return;
  3232. //Console.WriteLine("Nothing " + Name);
  3233. }
  3234. }
  3235. public void UpdatePositionAndVelocity(int frame)
  3236. {
  3237. if (_parent == null && !m_isSelected && !m_disabled && !m_building && !m_outbounds && Body != IntPtr.Zero)
  3238. {
  3239. if(m_bodydisablecontrol < 0)
  3240. return;
  3241. bool bodyenabled = d.BodyIsEnabled(Body);
  3242. if (bodyenabled || !_zeroFlag)
  3243. {
  3244. bool lastZeroFlag = _zeroFlag;
  3245. d.Vector3 lpos = d.GeomGetPosition(prim_geom);
  3246. // check outside region
  3247. if (lpos.Z < -100 || lpos.Z > 100000f)
  3248. {
  3249. m_outbounds = true;
  3250. lpos.Z = Util.Clip(lpos.Z, -100f, 100000f);
  3251. m_acceleration.X = 0;
  3252. m_acceleration.Y = 0;
  3253. m_acceleration.Z = 0;
  3254. _velocity.X = 0;
  3255. _velocity.Y = 0;
  3256. _velocity.Z = 0;
  3257. m_rotationalVelocity.X = 0;
  3258. m_rotationalVelocity.Y = 0;
  3259. m_rotationalVelocity.Z = 0;
  3260. d.BodySetLinearVel(Body, 0, 0, 0); // stop it
  3261. d.BodySetAngularVel(Body, 0, 0, 0); // stop it
  3262. d.BodySetPosition(Body, lpos.X, lpos.Y, lpos.Z); // put it somewhere
  3263. m_lastposition = _position;
  3264. m_lastorientation = _orientation;
  3265. base.RequestPhysicsterseUpdate();
  3266. // throttleCounter = 0;
  3267. _zeroFlag = true;
  3268. disableBodySoft(); // disable it and colisions
  3269. base.RaiseOutOfBounds(_position);
  3270. return;
  3271. }
  3272. if (lpos.X < 0f)
  3273. {
  3274. _position.X = Util.Clip(lpos.X, -2f, -0.1f);
  3275. m_outbounds = true;
  3276. }
  3277. else if (lpos.X > _parent_scene.WorldExtents.X)
  3278. {
  3279. _position.X = Util.Clip(lpos.X, _parent_scene.WorldExtents.X + 0.1f, _parent_scene.WorldExtents.X + 2f);
  3280. m_outbounds = true;
  3281. }
  3282. if (lpos.Y < 0f)
  3283. {
  3284. _position.Y = Util.Clip(lpos.Y, -2f, -0.1f);
  3285. m_outbounds = true;
  3286. }
  3287. else if (lpos.Y > _parent_scene.WorldExtents.Y)
  3288. {
  3289. _position.Y = Util.Clip(lpos.Y, _parent_scene.WorldExtents.Y + 0.1f, _parent_scene.WorldExtents.Y + 2f);
  3290. m_outbounds = true;
  3291. }
  3292. if (m_outbounds)
  3293. {
  3294. m_lastposition = _position;
  3295. m_lastorientation = _orientation;
  3296. d.Vector3 dtmp = d.BodyGetAngularVel(Body);
  3297. m_rotationalVelocity.X = dtmp.X;
  3298. m_rotationalVelocity.Y = dtmp.Y;
  3299. m_rotationalVelocity.Z = dtmp.Z;
  3300. dtmp = d.BodyGetLinearVel(Body);
  3301. _velocity.X = dtmp.X;
  3302. _velocity.Y = dtmp.Y;
  3303. _velocity.Z = dtmp.Z;
  3304. d.BodySetLinearVel(Body, 0, 0, 0); // stop it
  3305. d.BodySetAngularVel(Body, 0, 0, 0);
  3306. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  3307. disableBodySoft(); // stop collisions
  3308. UnSubscribeEvents();
  3309. base.RequestPhysicsterseUpdate();
  3310. return;
  3311. }
  3312. d.Quaternion ori;
  3313. d.GeomCopyQuaternion(prim_geom, out ori);
  3314. // decide if moving
  3315. // use positions since this are integrated quantities
  3316. // tolerance values depende a lot on simulation noise...
  3317. // use simple math.abs since we dont need to be exact
  3318. if(!bodyenabled)
  3319. {
  3320. _zeroFlag = true;
  3321. }
  3322. else
  3323. {
  3324. float poserror;
  3325. float angerror;
  3326. if(_zeroFlag)
  3327. {
  3328. poserror = 0.01f;
  3329. angerror = 0.001f;
  3330. }
  3331. else
  3332. {
  3333. poserror = 0.005f;
  3334. angerror = 0.0005f;
  3335. }
  3336. if (
  3337. (Math.Abs(_position.X - lpos.X) < poserror)
  3338. && (Math.Abs(_position.Y - lpos.Y) < poserror)
  3339. && (Math.Abs(_position.Z - lpos.Z) < poserror)
  3340. && (Math.Abs(_orientation.X - ori.X) < angerror)
  3341. && (Math.Abs(_orientation.Y - ori.Y) < angerror)
  3342. && (Math.Abs(_orientation.Z - ori.Z) < angerror) // ignore W
  3343. )
  3344. _zeroFlag = true;
  3345. else
  3346. _zeroFlag = false;
  3347. }
  3348. // update position
  3349. if (!(_zeroFlag && lastZeroFlag))
  3350. {
  3351. _position.X = lpos.X;
  3352. _position.Y = lpos.Y;
  3353. _position.Z = lpos.Z;
  3354. _orientation.X = ori.X;
  3355. _orientation.Y = ori.Y;
  3356. _orientation.Z = ori.Z;
  3357. _orientation.W = ori.W;
  3358. }
  3359. // update velocities and acceleration
  3360. if (_zeroFlag || lastZeroFlag)
  3361. {
  3362. // disable interpolators
  3363. _velocity = Vector3.Zero;
  3364. m_acceleration = Vector3.Zero;
  3365. m_rotationalVelocity = Vector3.Zero;
  3366. }
  3367. else
  3368. {
  3369. d.Vector3 vel = d.BodyGetLinearVel(Body);
  3370. m_acceleration = _velocity;
  3371. if ((Math.Abs(vel.X) < 0.005f) &&
  3372. (Math.Abs(vel.Y) < 0.005f) &&
  3373. (Math.Abs(vel.Z) < 0.005f))
  3374. {
  3375. _velocity = Vector3.Zero;
  3376. float t = -m_invTimeStep;
  3377. m_acceleration = m_acceleration * t;
  3378. }
  3379. else
  3380. {
  3381. _velocity.X = vel.X;
  3382. _velocity.Y = vel.Y;
  3383. _velocity.Z = vel.Z;
  3384. m_acceleration = (_velocity - m_acceleration) * m_invTimeStep;
  3385. }
  3386. if ((Math.Abs(m_acceleration.X) < 0.01f) &&
  3387. (Math.Abs(m_acceleration.Y) < 0.01f) &&
  3388. (Math.Abs(m_acceleration.Z) < 0.01f))
  3389. {
  3390. m_acceleration = Vector3.Zero;
  3391. }
  3392. vel = d.BodyGetAngularVel(Body);
  3393. if ((Math.Abs(vel.X) < 0.0001) &&
  3394. (Math.Abs(vel.Y) < 0.0001) &&
  3395. (Math.Abs(vel.Z) < 0.0001)
  3396. )
  3397. {
  3398. m_rotationalVelocity = Vector3.Zero;
  3399. }
  3400. else
  3401. {
  3402. m_rotationalVelocity.X = vel.X;
  3403. m_rotationalVelocity.Y = vel.Y;
  3404. m_rotationalVelocity.Z = vel.Z;
  3405. }
  3406. }
  3407. if (_zeroFlag)
  3408. {
  3409. if (!m_lastUpdateSent)
  3410. {
  3411. base.RequestPhysicsterseUpdate();
  3412. if (lastZeroFlag)
  3413. m_lastUpdateSent = true;
  3414. }
  3415. return;
  3416. }
  3417. base.RequestPhysicsterseUpdate();
  3418. m_lastUpdateSent = false;
  3419. }
  3420. }
  3421. }
  3422. internal static bool QuaternionIsFinite(Quaternion q)
  3423. {
  3424. if (Single.IsNaN(q.X) || Single.IsInfinity(q.X))
  3425. return false;
  3426. if (Single.IsNaN(q.Y) || Single.IsInfinity(q.Y))
  3427. return false;
  3428. if (Single.IsNaN(q.Z) || Single.IsInfinity(q.Z))
  3429. return false;
  3430. if (Single.IsNaN(q.W) || Single.IsInfinity(q.W))
  3431. return false;
  3432. return true;
  3433. }
  3434. internal static void DMassSubPartFromObj(ref d.Mass part, ref d.Mass theobj)
  3435. {
  3436. // assumes object center of mass is zero
  3437. float smass = part.mass;
  3438. theobj.mass -= smass;
  3439. smass *= 1.0f / (theobj.mass); ;
  3440. theobj.c.X -= part.c.X * smass;
  3441. theobj.c.Y -= part.c.Y * smass;
  3442. theobj.c.Z -= part.c.Z * smass;
  3443. theobj.I.M00 -= part.I.M00;
  3444. theobj.I.M01 -= part.I.M01;
  3445. theobj.I.M02 -= part.I.M02;
  3446. theobj.I.M10 -= part.I.M10;
  3447. theobj.I.M11 -= part.I.M11;
  3448. theobj.I.M12 -= part.I.M12;
  3449. theobj.I.M20 -= part.I.M20;
  3450. theobj.I.M21 -= part.I.M21;
  3451. theobj.I.M22 -= part.I.M22;
  3452. }
  3453. private void donullchange()
  3454. {
  3455. }
  3456. public bool DoAChange(changes what, object arg)
  3457. {
  3458. if (prim_geom == IntPtr.Zero && what != changes.Add && what != changes.AddPhysRep && what != changes.Remove)
  3459. {
  3460. return false;
  3461. }
  3462. // nasty switch
  3463. switch (what)
  3464. {
  3465. case changes.Add:
  3466. changeadd();
  3467. break;
  3468. case changes.AddPhysRep:
  3469. changeAddPhysRep((ODEPhysRepData)arg);
  3470. break;
  3471. case changes.Remove:
  3472. //If its being removed, we don't want to rebuild the physical rep at all, so ignore this stuff...
  3473. //When we return true, it destroys all of the prims in the linkset anyway
  3474. if (_parent != null)
  3475. {
  3476. OdePrim parent = (OdePrim)_parent;
  3477. parent.ChildRemove(this, false);
  3478. }
  3479. else
  3480. ChildRemove(this, false);
  3481. m_vehicle = null;
  3482. RemoveGeom();
  3483. m_targetSpace = IntPtr.Zero;
  3484. UnSubscribeEvents();
  3485. return true;
  3486. case changes.Link:
  3487. OdePrim tmp = (OdePrim)arg;
  3488. changeLink(tmp);
  3489. break;
  3490. case changes.DeLink:
  3491. changeLink(null);
  3492. break;
  3493. case changes.Position:
  3494. changePosition((Vector3)arg);
  3495. break;
  3496. case changes.Orientation:
  3497. changeOrientation((Quaternion)arg);
  3498. break;
  3499. case changes.PosOffset:
  3500. donullchange();
  3501. break;
  3502. case changes.OriOffset:
  3503. donullchange();
  3504. break;
  3505. case changes.Velocity:
  3506. changevelocity((Vector3)arg);
  3507. break;
  3508. case changes.TargetVelocity:
  3509. break;
  3510. // case changes.Acceleration:
  3511. // changeacceleration((Vector3)arg);
  3512. // break;
  3513. case changes.AngVelocity:
  3514. changeangvelocity((Vector3)arg);
  3515. break;
  3516. case changes.Force:
  3517. changeForce((Vector3)arg);
  3518. break;
  3519. case changes.Torque:
  3520. changeSetTorque((Vector3)arg);
  3521. break;
  3522. case changes.AddForce:
  3523. changeAddForce((Vector3)arg);
  3524. break;
  3525. case changes.AddAngForce:
  3526. changeAddAngularImpulse((Vector3)arg);
  3527. break;
  3528. case changes.AngLock:
  3529. changeAngularLock((byte)arg);
  3530. break;
  3531. case changes.Size:
  3532. changeSize((Vector3)arg);
  3533. break;
  3534. case changes.Shape:
  3535. changeShape((PrimitiveBaseShape)arg);
  3536. break;
  3537. case changes.PhysRepData:
  3538. changePhysRepData((ODEPhysRepData) arg);
  3539. break;
  3540. case changes.CollidesWater:
  3541. changeFloatOnWater((bool)arg);
  3542. break;
  3543. case changes.VolumeDtc:
  3544. changeVolumedetetion((bool)arg);
  3545. break;
  3546. case changes.Phantom:
  3547. changePhantomStatus((bool)arg);
  3548. break;
  3549. case changes.Physical:
  3550. changePhysicsStatus((bool)arg);
  3551. break;
  3552. case changes.Selected:
  3553. changeSelectedStatus((bool)arg);
  3554. break;
  3555. case changes.disabled:
  3556. changeDisable((bool)arg);
  3557. break;
  3558. case changes.building:
  3559. changeBuilding((bool)arg);
  3560. break;
  3561. case changes.VehicleType:
  3562. changeVehicleType((int)arg);
  3563. break;
  3564. case changes.VehicleFlags:
  3565. changeVehicleFlags((strVehicleBoolParam) arg);
  3566. break;
  3567. case changes.VehicleFloatParam:
  3568. changeVehicleFloatParam((strVehicleFloatParam) arg);
  3569. break;
  3570. case changes.VehicleVectorParam:
  3571. changeVehicleVectorParam((strVehicleVectorParam) arg);
  3572. break;
  3573. case changes.VehicleRotationParam:
  3574. changeVehicleRotationParam((strVehicleQuatParam) arg);
  3575. break;
  3576. case changes.SetVehicle:
  3577. changeSetVehicle((VehicleData) arg);
  3578. break;
  3579. case changes.Buoyancy:
  3580. changeBuoyancy((float)arg);
  3581. break;
  3582. case changes.PIDTarget:
  3583. changePIDTarget((Vector3)arg);
  3584. break;
  3585. case changes.PIDTau:
  3586. changePIDTau((float)arg);
  3587. break;
  3588. case changes.PIDActive:
  3589. changePIDActive((bool)arg);
  3590. break;
  3591. case changes.PIDHoverHeight:
  3592. changePIDHoverHeight((float)arg);
  3593. break;
  3594. case changes.PIDHoverType:
  3595. changePIDHoverType((PIDHoverType)arg);
  3596. break;
  3597. case changes.PIDHoverTau:
  3598. changePIDHoverTau((float)arg);
  3599. break;
  3600. case changes.PIDHoverActive:
  3601. changePIDHoverActive((bool)arg);
  3602. break;
  3603. case changes.SetInertia:
  3604. changeInertia((PhysicsInertiaData) arg);
  3605. break;
  3606. case changes.Null:
  3607. donullchange();
  3608. break;
  3609. default:
  3610. donullchange();
  3611. break;
  3612. }
  3613. return false;
  3614. }
  3615. public void AddChange(changes what, object arg)
  3616. {
  3617. _parent_scene.AddChange((PhysicsActor) this, what, arg);
  3618. }
  3619. private struct strVehicleBoolParam
  3620. {
  3621. public int param;
  3622. public bool value;
  3623. }
  3624. private struct strVehicleFloatParam
  3625. {
  3626. public int param;
  3627. public float value;
  3628. }
  3629. private struct strVehicleQuatParam
  3630. {
  3631. public int param;
  3632. public Quaternion value;
  3633. }
  3634. private struct strVehicleVectorParam
  3635. {
  3636. public int param;
  3637. public Vector3 value;
  3638. }
  3639. }
  3640. }