ODEPrim.cs 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * Revised August 26 2009 by Kitto Flora. ODEDynamics.cs replaces
  29. * ODEVehicleSettings.cs. It and ODEPrim.cs are re-organised:
  30. * ODEPrim.cs contains methods dealing with Prim editing, Prim
  31. * characteristics and Kinetic motion.
  32. * ODEDynamics.cs contains methods dealing with Prim Physical motion
  33. * (dynamics) and the associated settings. Old Linear and angular
  34. * motors for dynamic motion have been replace with MoveLinear()
  35. * and MoveAngular(); 'Physical' is used only to switch ODE dynamic
  36. * simualtion on/off; VEHICAL_TYPE_NONE/VEHICAL_TYPE_<other> is to
  37. * switch between 'VEHICLE' parameter use and general dynamics
  38. * settings use.
  39. */
  40. //#define SPAM
  41. using System;
  42. using System.Collections.Generic;
  43. using System.Reflection;
  44. using System.Runtime.InteropServices;
  45. using System.Threading;
  46. using log4net;
  47. using OpenMetaverse;
  48. using OdeAPI;
  49. using OpenSim.Framework;
  50. using OpenSim.Region.PhysicsModules.SharedBase;
  51. namespace OpenSim.Region.PhysicsModule.ubOde
  52. {
  53. public class OdePrim : PhysicsActor
  54. {
  55. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  56. private bool m_isphysical;
  57. private bool m_fakeisphysical;
  58. private bool m_isphantom;
  59. private bool m_fakeisphantom;
  60. internal bool m_isVolumeDetect; // If true, this prim only detects collisions but doesn't collide actively
  61. private bool m_fakeisVolumeDetect; // If true, this prim only detects collisions but doesn't collide actively
  62. internal bool m_building;
  63. protected bool m_forcePosOrRotation;
  64. private bool m_iscolliding;
  65. internal bool m_isSelected;
  66. private bool m_delaySelect;
  67. private bool m_lastdoneSelected;
  68. internal bool m_outbounds;
  69. private byte m_angularlocks = 0;
  70. private Quaternion m_lastorientation;
  71. private Quaternion _orientation;
  72. private Vector3 _position;
  73. private Vector3 _velocity;
  74. private Vector3 m_lastVelocity;
  75. private Vector3 m_lastposition;
  76. private Vector3 m_rotationalVelocity;
  77. private Vector3 _size;
  78. private Vector3 m_acceleration;
  79. private IntPtr Amotor;
  80. internal Vector3 m_force;
  81. internal Vector3 m_forceacc;
  82. internal Vector3 m_torque;
  83. internal Vector3 m_angularForceacc;
  84. private float m_invTimeStep;
  85. private float m_timeStep;
  86. private Vector3 m_PIDTarget;
  87. private float m_PIDTau;
  88. private bool m_usePID;
  89. private float m_PIDHoverHeight;
  90. private float m_PIDHoverTau;
  91. private bool m_useHoverPID;
  92. private PIDHoverType m_PIDHoverType;
  93. private float m_targetHoverHeight;
  94. private float m_groundHeight;
  95. private float m_waterHeight;
  96. private float m_buoyancy; //KF: m_buoyancy should be set by llSetBuoyancy() for non-vehicle.
  97. private int m_body_autodisable_frames;
  98. public int m_bodydisablecontrol = 0;
  99. private float m_gravmod = 1.0f;
  100. // Default we're a Geometry
  101. private CollisionCategories m_collisionCategories = (CollisionCategories.Geom);
  102. // Default colide nonphysical don't try to colide with anything
  103. private const CollisionCategories m_default_collisionFlagsNotPhysical = 0;
  104. private const CollisionCategories m_default_collisionFlagsPhysical = (CollisionCategories.Geom |
  105. CollisionCategories.Character |
  106. CollisionCategories.Land |
  107. CollisionCategories.VolumeDtc);
  108. // private bool m_collidesLand = true;
  109. private bool m_collidesWater;
  110. // public bool m_returnCollisions;
  111. private bool m_NoColide; // for now only for internal use for bad meshs
  112. // Default, Collide with Other Geometries, spaces and Bodies
  113. private CollisionCategories m_collisionFlags = m_default_collisionFlagsNotPhysical;
  114. public bool m_disabled;
  115. private uint m_localID;
  116. private IMesh m_mesh;
  117. private object m_meshlock = new object();
  118. private PrimitiveBaseShape _pbs;
  119. private UUID? m_assetID;
  120. private MeshState m_meshState;
  121. public ODEScene _parent_scene;
  122. /// <summary>
  123. /// The physics space which contains prim geometry
  124. /// </summary>
  125. public IntPtr m_targetSpace;
  126. public IntPtr prim_geom;
  127. public IntPtr _triMeshData;
  128. private PhysicsActor _parent;
  129. private List<OdePrim> childrenPrim = new List<OdePrim>();
  130. public float m_collisionscore;
  131. private int m_colliderfilter = 0;
  132. public IntPtr collide_geom; // for objects: geom if single prim space it linkset
  133. private float m_density;
  134. private byte m_shapetype;
  135. private byte m_fakeShapetype;
  136. public bool _zeroFlag;
  137. private bool m_lastUpdateSent;
  138. public IntPtr Body;
  139. private Vector3 _target_velocity;
  140. public Vector3 m_OBBOffset;
  141. public Vector3 m_OBB;
  142. public float primOOBradiusSQ;
  143. private bool m_hasOBB = true;
  144. private float m_physCost;
  145. private float m_streamCost;
  146. public d.Mass primdMass; // prim inertia information on it's own referencial
  147. private PhysicsInertiaData m_InertiaOverride;
  148. float primMass; // prim own mass
  149. float primVolume; // prim own volume;
  150. float m_mass; // object mass acording to case
  151. public int givefakepos;
  152. private Vector3 fakepos;
  153. public int givefakeori;
  154. private Quaternion fakeori;
  155. private PhysicsInertiaData m_fakeInertiaOverride;
  156. private int m_eventsubscription;
  157. private int m_cureventsubscription;
  158. private CollisionEventUpdate CollisionEventsThisFrame = null;
  159. private CollisionEventUpdate CollisionVDTCEventsThisFrame = null;
  160. private bool SentEmptyCollisionsEvent;
  161. public volatile bool childPrim;
  162. public ODEDynamics m_vehicle;
  163. internal int m_material = (int)Material.Wood;
  164. private float mu;
  165. private float bounce;
  166. /// <summary>
  167. /// Is this prim subject to physics? Even if not, it's still solid for collision purposes.
  168. /// </summary>
  169. public override bool IsPhysical // this is not reliable for internal use
  170. {
  171. get { return m_fakeisphysical; }
  172. set
  173. {
  174. m_fakeisphysical = value; // we show imediatly to outside that we changed physical
  175. // and also to stop imediatly some updates
  176. // but real change will only happen in taintprocessing
  177. if (!value) // Zero the remembered last velocity
  178. m_lastVelocity = Vector3.Zero;
  179. AddChange(changes.Physical, value);
  180. }
  181. }
  182. public override bool IsVolumeDtc
  183. {
  184. get { return m_fakeisVolumeDetect; }
  185. set
  186. {
  187. m_fakeisVolumeDetect = value;
  188. AddChange(changes.VolumeDtc, value);
  189. }
  190. }
  191. public override bool Phantom // this is not reliable for internal use
  192. {
  193. get { return m_fakeisphantom; }
  194. set
  195. {
  196. m_fakeisphantom = value;
  197. AddChange(changes.Phantom, value);
  198. }
  199. }
  200. public override bool Building // this is not reliable for internal use
  201. {
  202. get { return m_building; }
  203. set
  204. {
  205. // if (value)
  206. // m_building = true;
  207. AddChange(changes.building, value);
  208. }
  209. }
  210. public override void getContactData(ref ContactData cdata)
  211. {
  212. cdata.mu = mu;
  213. cdata.bounce = bounce;
  214. // cdata.softcolide = m_softcolide;
  215. cdata.softcolide = false;
  216. if (m_isphysical)
  217. {
  218. ODEDynamics veh;
  219. if (_parent != null)
  220. veh = ((OdePrim)_parent).m_vehicle;
  221. else
  222. veh = m_vehicle;
  223. if (veh != null && veh.Type != Vehicle.TYPE_NONE)
  224. cdata.mu *= veh.FrictionFactor;
  225. // cdata.mu *= 0;
  226. }
  227. }
  228. public override float PhysicsCost
  229. {
  230. get
  231. {
  232. return m_physCost;
  233. }
  234. }
  235. public override float StreamCost
  236. {
  237. get
  238. {
  239. return m_streamCost;
  240. }
  241. }
  242. public override int PhysicsActorType
  243. {
  244. get { return (int)ActorTypes.Prim; }
  245. set { return; }
  246. }
  247. public override bool SetAlwaysRun
  248. {
  249. get { return false; }
  250. set { return; }
  251. }
  252. public override uint LocalID
  253. {
  254. get { return m_localID; }
  255. set
  256. {
  257. uint oldid = m_localID;
  258. m_localID = value;
  259. _parent_scene.changePrimID(this, oldid);
  260. }
  261. }
  262. public override PhysicsActor ParentActor
  263. {
  264. get
  265. {
  266. if (childPrim)
  267. return _parent;
  268. else
  269. return (PhysicsActor)this;
  270. }
  271. }
  272. public override bool Grabbed
  273. {
  274. set { return; }
  275. }
  276. public override bool Selected
  277. {
  278. set
  279. {
  280. if (value)
  281. m_isSelected = value; // if true set imediatly to stop moves etc
  282. AddChange(changes.Selected, value);
  283. }
  284. }
  285. public override bool Flying
  286. {
  287. // no flying prims for you
  288. get { return false; }
  289. set { }
  290. }
  291. public override bool IsColliding
  292. {
  293. get { return m_iscolliding; }
  294. set
  295. {
  296. if (value)
  297. {
  298. m_colliderfilter += 2;
  299. if (m_colliderfilter > 2)
  300. m_colliderfilter = 2;
  301. }
  302. else
  303. {
  304. m_colliderfilter--;
  305. if (m_colliderfilter < 0)
  306. m_colliderfilter = 0;
  307. }
  308. if (m_colliderfilter == 0)
  309. m_iscolliding = false;
  310. else
  311. m_iscolliding = true;
  312. }
  313. }
  314. public override bool CollidingGround
  315. {
  316. get { return false; }
  317. set { return; }
  318. }
  319. public override bool CollidingObj
  320. {
  321. get { return false; }
  322. set { return; }
  323. }
  324. public override bool ThrottleUpdates {get;set;}
  325. public override bool Stopped
  326. {
  327. get { return _zeroFlag; }
  328. }
  329. public override Vector3 Position
  330. {
  331. get
  332. {
  333. if (givefakepos > 0)
  334. return fakepos;
  335. else
  336. return _position;
  337. }
  338. set
  339. {
  340. fakepos = value;
  341. givefakepos++;
  342. AddChange(changes.Position, value);
  343. }
  344. }
  345. public override Vector3 Size
  346. {
  347. get { return _size; }
  348. set
  349. {
  350. if (value.IsFinite())
  351. {
  352. _parent_scene.m_meshWorker.ChangeActorPhysRep(this, _pbs, value, m_fakeShapetype);
  353. }
  354. else
  355. {
  356. m_log.WarnFormat("[PHYSICS]: Got NaN Size on object {0}", Name);
  357. }
  358. }
  359. }
  360. public override float Mass
  361. {
  362. get { return primMass; }
  363. }
  364. public override Vector3 Force
  365. {
  366. get { return m_force; }
  367. set
  368. {
  369. if (value.IsFinite())
  370. {
  371. AddChange(changes.Force, value);
  372. }
  373. else
  374. {
  375. m_log.WarnFormat("[PHYSICS]: NaN in Force Applied to an Object {0}", Name);
  376. }
  377. }
  378. }
  379. public override void SetVolumeDetect(int param)
  380. {
  381. m_fakeisVolumeDetect = (param != 0);
  382. AddChange(changes.VolumeDtc, m_fakeisVolumeDetect);
  383. }
  384. public override Vector3 GeometricCenter
  385. {
  386. // this is not real geometric center but a average of positions relative to root prim acording to
  387. // http://wiki.secondlife.com/wiki/llGetGeometricCenter
  388. // ignoring tortured prims details since sl also seems to ignore
  389. // so no real use in doing it on physics
  390. get
  391. {
  392. return Vector3.Zero;
  393. }
  394. }
  395. public override PhysicsInertiaData GetInertiaData()
  396. {
  397. PhysicsInertiaData inertia;
  398. if(childPrim)
  399. {
  400. if(_parent != null)
  401. return _parent.GetInertiaData();
  402. else
  403. {
  404. inertia = new PhysicsInertiaData();
  405. inertia.TotalMass = -1;
  406. return inertia;
  407. }
  408. }
  409. inertia = new PhysicsInertiaData();
  410. // double buffering
  411. if(m_fakeInertiaOverride != null)
  412. {
  413. d.Mass objdmass = new d.Mass();
  414. objdmass.I.M00 = m_fakeInertiaOverride.Inertia.X;
  415. objdmass.I.M11 = m_fakeInertiaOverride.Inertia.Y;
  416. objdmass.I.M22 = m_fakeInertiaOverride.Inertia.Z;
  417. objdmass.mass = m_fakeInertiaOverride.TotalMass;
  418. if(Math.Abs(m_fakeInertiaOverride.InertiaRotation.W) < 0.999)
  419. {
  420. d.Matrix3 inertiarotmat = new d.Matrix3();
  421. d.Quaternion inertiarot = new d.Quaternion();
  422. inertiarot.X = m_fakeInertiaOverride.InertiaRotation.X;
  423. inertiarot.Y = m_fakeInertiaOverride.InertiaRotation.Y;
  424. inertiarot.Z = m_fakeInertiaOverride.InertiaRotation.Z;
  425. inertiarot.W = m_fakeInertiaOverride.InertiaRotation.W;
  426. d.RfromQ(out inertiarotmat, ref inertiarot);
  427. d.MassRotate(ref objdmass, ref inertiarotmat);
  428. }
  429. inertia.TotalMass = m_fakeInertiaOverride.TotalMass;
  430. inertia.CenterOfMass = m_fakeInertiaOverride.CenterOfMass;
  431. inertia.Inertia.X = objdmass.I.M00;
  432. inertia.Inertia.Y = objdmass.I.M11;
  433. inertia.Inertia.Z = objdmass.I.M22;
  434. inertia.InertiaRotation.X = objdmass.I.M01;
  435. inertia.InertiaRotation.Y = objdmass.I.M02;
  436. inertia.InertiaRotation.Z = objdmass.I.M12;
  437. return inertia;
  438. }
  439. inertia.TotalMass = m_mass;
  440. if(Body == IntPtr.Zero || prim_geom == IntPtr.Zero)
  441. {
  442. inertia.CenterOfMass = Vector3.Zero;
  443. inertia.Inertia = Vector3.Zero;
  444. inertia.InertiaRotation = Vector4.Zero;
  445. return inertia;
  446. }
  447. d.Vector3 dtmp;
  448. d.Mass m = new d.Mass();
  449. lock(_parent_scene.OdeLock)
  450. {
  451. d.AllocateODEDataForThread(0);
  452. dtmp = d.GeomGetOffsetPosition(prim_geom);
  453. d.BodyGetMass(Body, out m);
  454. }
  455. Vector3 cm = new Vector3(-dtmp.X, -dtmp.Y, -dtmp.Z);
  456. inertia.CenterOfMass = cm;
  457. inertia.Inertia = new Vector3(m.I.M00, m.I.M11, m.I.M22);
  458. inertia.InertiaRotation = new Vector4(m.I.M01, m.I.M02 , m.I.M12, 0);
  459. return inertia;
  460. }
  461. public override void SetInertiaData(PhysicsInertiaData inertia)
  462. {
  463. if(childPrim)
  464. {
  465. if(_parent != null)
  466. _parent.SetInertiaData(inertia);
  467. return;
  468. }
  469. if(inertia.TotalMass > 0)
  470. m_fakeInertiaOverride = new PhysicsInertiaData(inertia);
  471. else
  472. m_fakeInertiaOverride = null;
  473. if (inertia.TotalMass > _parent_scene.maximumMassObject)
  474. inertia.TotalMass = _parent_scene.maximumMassObject;
  475. AddChange(changes.SetInertia,(object)m_fakeInertiaOverride);
  476. }
  477. public override Vector3 CenterOfMass
  478. {
  479. get
  480. {
  481. lock (_parent_scene.OdeLock)
  482. {
  483. d.AllocateODEDataForThread(0);
  484. d.Vector3 dtmp;
  485. if (!childPrim && Body != IntPtr.Zero)
  486. {
  487. dtmp = d.BodyGetPosition(Body);
  488. return new Vector3(dtmp.X, dtmp.Y, dtmp.Z);
  489. }
  490. else if (prim_geom != IntPtr.Zero)
  491. {
  492. d.Quaternion dq;
  493. d.GeomCopyQuaternion(prim_geom, out dq);
  494. Quaternion q;
  495. q.X = dq.X;
  496. q.Y = dq.Y;
  497. q.Z = dq.Z;
  498. q.W = dq.W;
  499. Vector3 Ptot = m_OBBOffset * q;
  500. dtmp = d.GeomGetPosition(prim_geom);
  501. Ptot.X += dtmp.X;
  502. Ptot.Y += dtmp.Y;
  503. Ptot.Z += dtmp.Z;
  504. // if(childPrim) we only know about physical linksets
  505. return Ptot;
  506. /*
  507. float tmass = _mass;
  508. Ptot *= tmass;
  509. float m;
  510. foreach (OdePrim prm in childrenPrim)
  511. {
  512. m = prm._mass;
  513. Ptot += prm.CenterOfMass * m;
  514. tmass += m;
  515. }
  516. if (tmass == 0)
  517. tmass = 0;
  518. else
  519. tmass = 1.0f / tmass;
  520. Ptot *= tmass;
  521. return Ptot;
  522. */
  523. }
  524. else
  525. return _position;
  526. }
  527. }
  528. }
  529. public override PrimitiveBaseShape Shape
  530. {
  531. set
  532. {
  533. // AddChange(changes.Shape, value);
  534. _parent_scene.m_meshWorker.ChangeActorPhysRep(this, value, _size, m_fakeShapetype);
  535. }
  536. }
  537. public override byte PhysicsShapeType
  538. {
  539. get
  540. {
  541. return m_fakeShapetype;
  542. }
  543. set
  544. {
  545. m_fakeShapetype = value;
  546. _parent_scene.m_meshWorker.ChangeActorPhysRep(this, _pbs, _size, value);
  547. }
  548. }
  549. public override Vector3 rootVelocity
  550. {
  551. get
  552. {
  553. if(_parent != null)
  554. return ((OdePrim)_parent).Velocity;
  555. return Velocity;
  556. }
  557. }
  558. public override Vector3 Velocity
  559. {
  560. get
  561. {
  562. if (_zeroFlag)
  563. return Vector3.Zero;
  564. return _velocity;
  565. }
  566. set
  567. {
  568. if (value.IsFinite())
  569. {
  570. if(m_outbounds)
  571. _velocity = value;
  572. else
  573. AddChange(changes.Velocity, value);
  574. }
  575. else
  576. {
  577. m_log.WarnFormat("[PHYSICS]: Got NaN Velocity in Object {0}", Name);
  578. }
  579. }
  580. }
  581. public override Vector3 Torque
  582. {
  583. get
  584. {
  585. if (!IsPhysical || Body == IntPtr.Zero)
  586. return Vector3.Zero;
  587. return m_torque;
  588. }
  589. set
  590. {
  591. if (value.IsFinite())
  592. {
  593. AddChange(changes.Torque, value);
  594. }
  595. else
  596. {
  597. m_log.WarnFormat("[PHYSICS]: Got NaN Torque in Object {0}", Name);
  598. }
  599. }
  600. }
  601. public override float CollisionScore
  602. {
  603. get { return m_collisionscore; }
  604. set { m_collisionscore = value; }
  605. }
  606. public override bool Kinematic
  607. {
  608. get { return false; }
  609. set { }
  610. }
  611. public override Quaternion Orientation
  612. {
  613. get
  614. {
  615. if (givefakeori > 0)
  616. return fakeori;
  617. else
  618. return _orientation;
  619. }
  620. set
  621. {
  622. if (QuaternionIsFinite(value))
  623. {
  624. fakeori = value;
  625. givefakeori++;
  626. value.Normalize();
  627. AddChange(changes.Orientation, value);
  628. }
  629. else
  630. m_log.WarnFormat("[PHYSICS]: Got NaN quaternion Orientation from Scene in Object {0}", Name);
  631. }
  632. }
  633. public override Vector3 Acceleration
  634. {
  635. get { return m_acceleration; }
  636. set
  637. {
  638. if(m_outbounds)
  639. m_acceleration = value;
  640. }
  641. }
  642. public override Vector3 RotationalVelocity
  643. {
  644. get
  645. {
  646. Vector3 pv = Vector3.Zero;
  647. if (_zeroFlag)
  648. return pv;
  649. if (m_rotationalVelocity.ApproxEquals(pv, 0.0001f))
  650. return pv;
  651. return m_rotationalVelocity;
  652. }
  653. set
  654. {
  655. if (value.IsFinite())
  656. {
  657. if(m_outbounds)
  658. m_rotationalVelocity = value;
  659. else
  660. AddChange(changes.AngVelocity, value);
  661. }
  662. else
  663. {
  664. m_log.WarnFormat("[PHYSICS]: Got NaN RotationalVelocity in Object {0}", Name);
  665. }
  666. }
  667. }
  668. public override float Buoyancy
  669. {
  670. get { return m_buoyancy; }
  671. set
  672. {
  673. AddChange(changes.Buoyancy,value);
  674. }
  675. }
  676. public override bool FloatOnWater
  677. {
  678. set
  679. {
  680. AddChange(changes.CollidesWater, value);
  681. }
  682. }
  683. public override Vector3 PIDTarget
  684. {
  685. set
  686. {
  687. if (value.IsFinite())
  688. {
  689. AddChange(changes.PIDTarget,value);
  690. }
  691. else
  692. m_log.WarnFormat("[PHYSICS]: Got NaN PIDTarget from Scene on Object {0}", Name);
  693. }
  694. }
  695. public override bool PIDActive
  696. {
  697. get
  698. {
  699. return m_usePID;
  700. }
  701. set
  702. {
  703. AddChange(changes.PIDActive,value);
  704. }
  705. }
  706. public override float PIDTau
  707. {
  708. set
  709. {
  710. float tmp = 0;
  711. if (value > 0)
  712. {
  713. float mint = (0.05f > m_timeStep ? 0.05f : m_timeStep);
  714. if (value < mint)
  715. tmp = mint;
  716. else
  717. tmp = value;
  718. }
  719. AddChange(changes.PIDTau,tmp);
  720. }
  721. }
  722. public override float PIDHoverHeight
  723. {
  724. set
  725. {
  726. AddChange(changes.PIDHoverHeight,value);
  727. }
  728. }
  729. public override bool PIDHoverActive
  730. {
  731. get
  732. {
  733. return m_useHoverPID;
  734. }
  735. set
  736. {
  737. AddChange(changes.PIDHoverActive, value);
  738. }
  739. }
  740. public override PIDHoverType PIDHoverType
  741. {
  742. set
  743. {
  744. AddChange(changes.PIDHoverType,value);
  745. }
  746. }
  747. public override float PIDHoverTau
  748. {
  749. set
  750. {
  751. float tmp =0;
  752. if (value > 0)
  753. {
  754. float mint = (0.05f > m_timeStep ? 0.05f : m_timeStep);
  755. if (value < mint)
  756. tmp = mint;
  757. else
  758. tmp = value;
  759. }
  760. AddChange(changes.PIDHoverTau, tmp);
  761. }
  762. }
  763. public override Quaternion APIDTarget { set { return; } }
  764. public override bool APIDActive { set { return; } }
  765. public override float APIDStrength { set { return; } }
  766. public override float APIDDamping { set { return; } }
  767. public override int VehicleType
  768. {
  769. // we may need to put a fake on this
  770. get
  771. {
  772. if (m_vehicle == null)
  773. return (int)Vehicle.TYPE_NONE;
  774. else
  775. return (int)m_vehicle.Type;
  776. }
  777. set
  778. {
  779. AddChange(changes.VehicleType, value);
  780. }
  781. }
  782. public override void VehicleFloatParam(int param, float value)
  783. {
  784. strVehicleFloatParam fp = new strVehicleFloatParam();
  785. fp.param = param;
  786. fp.value = value;
  787. AddChange(changes.VehicleFloatParam, fp);
  788. }
  789. public override void VehicleVectorParam(int param, Vector3 value)
  790. {
  791. strVehicleVectorParam fp = new strVehicleVectorParam();
  792. fp.param = param;
  793. fp.value = value;
  794. AddChange(changes.VehicleVectorParam, fp);
  795. }
  796. public override void VehicleRotationParam(int param, Quaternion value)
  797. {
  798. strVehicleQuatParam fp = new strVehicleQuatParam();
  799. fp.param = param;
  800. fp.value = value;
  801. AddChange(changes.VehicleRotationParam, fp);
  802. }
  803. public override void VehicleFlags(int param, bool value)
  804. {
  805. strVehicleBoolParam bp = new strVehicleBoolParam();
  806. bp.param = param;
  807. bp.value = value;
  808. AddChange(changes.VehicleFlags, bp);
  809. }
  810. public override void SetVehicle(object vdata)
  811. {
  812. AddChange(changes.SetVehicle, vdata);
  813. }
  814. public void SetAcceleration(Vector3 accel)
  815. {
  816. m_acceleration = accel;
  817. }
  818. public override void AddForce(Vector3 force, bool pushforce)
  819. {
  820. if (force.IsFinite())
  821. {
  822. if(pushforce)
  823. AddChange(changes.AddForce, force);
  824. else // a impulse
  825. AddChange(changes.AddForce, force * m_invTimeStep);
  826. }
  827. else
  828. {
  829. m_log.WarnFormat("[PHYSICS]: Got Invalid linear force vector from Scene in Object {0}", Name);
  830. }
  831. //m_log.Info("[PHYSICS]: Added Force:" + force.ToString() + " to prim at " + Position.ToString());
  832. }
  833. public override void AddAngularForce(Vector3 force, bool pushforce)
  834. {
  835. if (force.IsFinite())
  836. {
  837. // if(pushforce) for now applyrotationimpulse seems more happy applied as a force
  838. AddChange(changes.AddAngForce, force);
  839. // else // a impulse
  840. // AddChange(changes.AddAngForce, force * m_invTimeStep);
  841. }
  842. else
  843. {
  844. m_log.WarnFormat("[PHYSICS]: Got Invalid Angular force vector from Scene in Object {0}", Name);
  845. }
  846. }
  847. public override void CrossingFailure()
  848. {
  849. lock(_parent_scene.OdeLock)
  850. {
  851. if (m_outbounds)
  852. {
  853. _position.X = Util.Clip(_position.X, 0.5f, _parent_scene.WorldExtents.X - 0.5f);
  854. _position.Y = Util.Clip(_position.Y, 0.5f, _parent_scene.WorldExtents.Y - 0.5f);
  855. _position.Z = Util.Clip(_position.Z + 0.2f, -100f, 50000f);
  856. m_lastposition = _position;
  857. _velocity.X = 0;
  858. _velocity.Y = 0;
  859. _velocity.Z = 0;
  860. d.AllocateODEDataForThread(0);
  861. m_lastVelocity = _velocity;
  862. if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
  863. m_vehicle.Stop();
  864. if(Body != IntPtr.Zero)
  865. d.BodySetLinearVel(Body, 0, 0, 0); // stop it
  866. if (prim_geom != IntPtr.Zero)
  867. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  868. m_outbounds = false;
  869. changeDisable(false);
  870. base.RequestPhysicsterseUpdate();
  871. }
  872. }
  873. }
  874. public override void CrossingStart()
  875. {
  876. lock(_parent_scene.OdeLock)
  877. {
  878. if (m_outbounds || childPrim)
  879. return;
  880. m_outbounds = true;
  881. m_lastposition = _position;
  882. m_lastorientation = _orientation;
  883. d.AllocateODEDataForThread(0);
  884. if(Body != IntPtr.Zero)
  885. {
  886. d.Vector3 dtmp = d.BodyGetAngularVel(Body);
  887. m_rotationalVelocity.X = dtmp.X;
  888. m_rotationalVelocity.Y = dtmp.Y;
  889. m_rotationalVelocity.Z = dtmp.Z;
  890. dtmp = d.BodyGetLinearVel(Body);
  891. _velocity.X = dtmp.X;
  892. _velocity.Y = dtmp.Y;
  893. _velocity.Z = dtmp.Z;
  894. d.BodySetLinearVel(Body, 0, 0, 0); // stop it
  895. d.BodySetAngularVel(Body, 0, 0, 0);
  896. }
  897. 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 (CollisionEventsThisFrame == null)
  983. CollisionEventsThisFrame = new CollisionEventUpdate();
  984. if (CollisionVDTCEventsThisFrame == null)
  985. CollisionVDTCEventsThisFrame = new CollisionEventUpdate();
  986. SentEmptyCollisionsEvent = false;
  987. }
  988. public override void UnSubscribeEvents()
  989. {
  990. if (CollisionVDTCEventsThisFrame != null)
  991. {
  992. CollisionVDTCEventsThisFrame.Clear();
  993. CollisionVDTCEventsThisFrame = null;
  994. }
  995. if (CollisionEventsThisFrame != null)
  996. {
  997. CollisionEventsThisFrame.Clear();
  998. CollisionEventsThisFrame = null;
  999. }
  1000. m_eventsubscription = 0;
  1001. _parent_scene.RemoveCollisionEventReporting(this);
  1002. }
  1003. public override void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
  1004. {
  1005. if (CollisionEventsThisFrame == null)
  1006. CollisionEventsThisFrame = new CollisionEventUpdate();
  1007. CollisionEventsThisFrame.AddCollider(CollidedWith, contact);
  1008. _parent_scene.AddCollisionEventReporting(this);
  1009. }
  1010. public override void AddVDTCCollisionEvent(uint CollidedWith, ContactPoint contact)
  1011. {
  1012. if (CollisionVDTCEventsThisFrame == null)
  1013. CollisionVDTCEventsThisFrame = new CollisionEventUpdate();
  1014. CollisionVDTCEventsThisFrame.AddCollider(CollidedWith, contact);
  1015. _parent_scene.AddCollisionEventReporting(this);
  1016. }
  1017. internal void SleeperAddCollisionEvents()
  1018. {
  1019. if(CollisionEventsThisFrame != null && CollisionEventsThisFrame.m_objCollisionList.Count != 0)
  1020. {
  1021. foreach(KeyValuePair<uint,ContactPoint> kvp in CollisionEventsThisFrame.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(CollisionVDTCEventsThisFrame != null && CollisionVDTCEventsThisFrame.m_objCollisionList.Count != 0)
  1035. {
  1036. foreach(KeyValuePair<uint,ContactPoint> kvp in CollisionVDTCEventsThisFrame.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(CollisionVDTCEventsThisFrame != null && CollisionVDTCEventsThisFrame.Count >0 )
  1051. CollisionVDTCEventsThisFrame.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 (CollisionEventsThisFrame == null)
  1060. return;
  1061. int ncolisions = CollisionEventsThisFrame.m_objCollisionList.Count;
  1062. if (!SentEmptyCollisionsEvent || ncolisions > 0)
  1063. {
  1064. base.SendCollisionUpdate(CollisionEventsThisFrame);
  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. CollisionEventsThisFrame.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._position, 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.HashSpaceCreate(_parent_scene.ActiveSpace);
  1775. d.HashSpaceSetLevels(m_targetSpace, -2, 8);
  1776. d.SpaceSetSublevel(m_targetSpace, 3);
  1777. d.SpaceSetCleanup(m_targetSpace, false);
  1778. d.GeomSetCategoryBits(m_targetSpace, (uint)(CollisionCategories.Space |
  1779. CollisionCategories.Geom |
  1780. CollisionCategories.Phantom |
  1781. CollisionCategories.VolumeDtc
  1782. ));
  1783. d.GeomSetCollideBits(m_targetSpace, 0);
  1784. collide_geom = m_targetSpace;
  1785. }
  1786. d.SpaceAdd(m_targetSpace, prim_geom);
  1787. if (m_delaySelect)
  1788. {
  1789. m_isSelected = true;
  1790. m_delaySelect = false;
  1791. }
  1792. m_collisionscore = 0;
  1793. UpdateCollisionCatFlags();
  1794. ApplyCollisionCatFlags();
  1795. _parent_scene.addActivePrim(this);
  1796. lock (childrenPrim)
  1797. {
  1798. foreach (OdePrim prm in childrenPrim)
  1799. {
  1800. if (prm.prim_geom == IntPtr.Zero)
  1801. continue;
  1802. Vector3 ppos = prm._position;
  1803. d.GeomSetOffsetWorldPosition(prm.prim_geom, ppos.X, ppos.Y, ppos.Z); // set relative position
  1804. if (prm.m_targetSpace != m_targetSpace)
  1805. {
  1806. if (prm.m_targetSpace != IntPtr.Zero)
  1807. {
  1808. _parent_scene.waitForSpaceUnlock(prm.m_targetSpace);
  1809. if (d.SpaceQuery(prm.m_targetSpace, prm.prim_geom))
  1810. d.SpaceRemove(prm.m_targetSpace, prm.prim_geom);
  1811. }
  1812. prm.m_targetSpace = m_targetSpace;
  1813. d.SpaceAdd(m_targetSpace, prm.prim_geom);
  1814. }
  1815. prm.m_collisionscore = 0;
  1816. if(!m_disabled)
  1817. prm.m_disabled = false;
  1818. _parent_scene.addActivePrim(prm);
  1819. }
  1820. }
  1821. // The body doesn't already have a finite rotation mode set here
  1822. if (m_angularlocks != 0 && _parent == null)
  1823. {
  1824. createAMotor(m_angularlocks);
  1825. }
  1826. if (m_isSelected || m_disabled)
  1827. {
  1828. d.BodyDisable(Body);
  1829. _zeroFlag = true;
  1830. }
  1831. else
  1832. {
  1833. d.BodySetAngularVel(Body, m_rotationalVelocity.X, m_rotationalVelocity.Y, m_rotationalVelocity.Z);
  1834. d.BodySetLinearVel(Body, _velocity.X, _velocity.Y, _velocity.Z);
  1835. _zeroFlag = false;
  1836. m_bodydisablecontrol = 0;
  1837. }
  1838. _parent_scene.addActiveGroups(this);
  1839. }
  1840. private void DestroyBody()
  1841. {
  1842. if (Body != IntPtr.Zero)
  1843. {
  1844. _parent_scene.remActivePrim(this);
  1845. collide_geom = IntPtr.Zero;
  1846. if (m_disabled)
  1847. m_collisionCategories = 0;
  1848. else if (m_isSelected)
  1849. m_collisionCategories = CollisionCategories.Selected;
  1850. else if (m_isVolumeDetect)
  1851. m_collisionCategories = CollisionCategories.VolumeDtc;
  1852. else if (m_isphantom)
  1853. m_collisionCategories = CollisionCategories.Phantom;
  1854. else
  1855. m_collisionCategories = CollisionCategories.Geom;
  1856. m_collisionFlags = 0;
  1857. if (prim_geom != IntPtr.Zero)
  1858. {
  1859. if (m_NoColide)
  1860. {
  1861. d.GeomSetCategoryBits(prim_geom, 0);
  1862. d.GeomSetCollideBits(prim_geom, 0);
  1863. }
  1864. else
  1865. {
  1866. d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories);
  1867. d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags);
  1868. }
  1869. UpdateDataFromGeom();
  1870. d.GeomSetBody(prim_geom, IntPtr.Zero);
  1871. SetInStaticSpace(this);
  1872. }
  1873. if (!childPrim)
  1874. {
  1875. lock (childrenPrim)
  1876. {
  1877. foreach (OdePrim prm in childrenPrim)
  1878. {
  1879. _parent_scene.remActivePrim(prm);
  1880. if (prm.m_isSelected)
  1881. prm.m_collisionCategories = CollisionCategories.Selected;
  1882. else if (prm.m_isVolumeDetect)
  1883. prm.m_collisionCategories = CollisionCategories.VolumeDtc;
  1884. else if (prm.m_isphantom)
  1885. prm.m_collisionCategories = CollisionCategories.Phantom;
  1886. else
  1887. prm.m_collisionCategories = CollisionCategories.Geom;
  1888. prm.m_collisionFlags = 0;
  1889. if (prm.prim_geom != IntPtr.Zero)
  1890. {
  1891. if (prm.m_NoColide)
  1892. {
  1893. d.GeomSetCategoryBits(prm.prim_geom, 0);
  1894. d.GeomSetCollideBits(prm.prim_geom, 0);
  1895. }
  1896. else
  1897. {
  1898. d.GeomSetCategoryBits(prm.prim_geom, (uint)prm.m_collisionCategories);
  1899. d.GeomSetCollideBits(prm.prim_geom, (uint)prm.m_collisionFlags);
  1900. }
  1901. prm.UpdateDataFromGeom();
  1902. SetInStaticSpace(prm);
  1903. }
  1904. prm.Body = IntPtr.Zero;
  1905. prm.m_mass = prm.primMass;
  1906. prm.m_collisionscore = 0;
  1907. }
  1908. }
  1909. if (Amotor != IntPtr.Zero)
  1910. {
  1911. d.JointDestroy(Amotor);
  1912. Amotor = IntPtr.Zero;
  1913. }
  1914. _parent_scene.remActiveGroup(this);
  1915. d.BodyDestroy(Body);
  1916. }
  1917. Body = IntPtr.Zero;
  1918. }
  1919. m_mass = primMass;
  1920. m_collisionscore = 0;
  1921. }
  1922. private void FixInertia(Vector3 NewPos,Quaternion newrot)
  1923. {
  1924. d.Matrix3 mat = new d.Matrix3();
  1925. d.Quaternion quat = new d.Quaternion();
  1926. d.Mass tmpdmass = new d.Mass { };
  1927. d.Mass objdmass = new d.Mass { };
  1928. d.BodyGetMass(Body, out tmpdmass);
  1929. objdmass = tmpdmass;
  1930. d.Vector3 dobjpos;
  1931. d.Vector3 thispos;
  1932. // get current object position and rotation
  1933. dobjpos = d.BodyGetPosition(Body);
  1934. // get prim own inertia in its local frame
  1935. tmpdmass = primdMass;
  1936. // transform to object frame
  1937. mat = d.GeomGetOffsetRotation(prim_geom);
  1938. d.MassRotate(ref tmpdmass, ref mat);
  1939. thispos = d.GeomGetOffsetPosition(prim_geom);
  1940. d.MassTranslate(ref tmpdmass,
  1941. thispos.X,
  1942. thispos.Y,
  1943. thispos.Z);
  1944. // subtract current prim inertia from object
  1945. DMassSubPartFromObj(ref tmpdmass, ref objdmass);
  1946. // back prim own inertia
  1947. tmpdmass = primdMass;
  1948. // update to new position and orientation
  1949. _position = NewPos;
  1950. d.GeomSetOffsetWorldPosition(prim_geom, NewPos.X, NewPos.Y, NewPos.Z);
  1951. _orientation = newrot;
  1952. quat.X = newrot.X;
  1953. quat.Y = newrot.Y;
  1954. quat.Z = newrot.Z;
  1955. quat.W = newrot.W;
  1956. d.GeomSetOffsetWorldQuaternion(prim_geom, ref quat);
  1957. mat = d.GeomGetOffsetRotation(prim_geom);
  1958. d.MassRotate(ref tmpdmass, ref mat);
  1959. thispos = d.GeomGetOffsetPosition(prim_geom);
  1960. d.MassTranslate(ref tmpdmass,
  1961. thispos.X,
  1962. thispos.Y,
  1963. thispos.Z);
  1964. d.MassAdd(ref objdmass, ref tmpdmass);
  1965. // fix all positions
  1966. IntPtr g = d.BodyGetFirstGeom(Body);
  1967. while (g != IntPtr.Zero)
  1968. {
  1969. thispos = d.GeomGetOffsetPosition(g);
  1970. thispos.X -= objdmass.c.X;
  1971. thispos.Y -= objdmass.c.Y;
  1972. thispos.Z -= objdmass.c.Z;
  1973. d.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z);
  1974. g = d.dBodyGetNextGeom(g);
  1975. }
  1976. d.BodyVectorToWorld(Body,objdmass.c.X, objdmass.c.Y, objdmass.c.Z,out thispos);
  1977. d.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z);
  1978. d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
  1979. d.BodySetMass(Body, ref objdmass);
  1980. m_mass = objdmass.mass;
  1981. }
  1982. private void FixInertia(Vector3 NewPos)
  1983. {
  1984. d.Matrix3 primmat = new d.Matrix3();
  1985. d.Mass tmpdmass = new d.Mass { };
  1986. d.Mass objdmass = new d.Mass { };
  1987. d.Mass primmass = new d.Mass { };
  1988. d.Vector3 dobjpos;
  1989. d.Vector3 thispos;
  1990. d.BodyGetMass(Body, out objdmass);
  1991. // get prim own inertia in its local frame
  1992. primmass = primdMass;
  1993. // transform to object frame
  1994. primmat = d.GeomGetOffsetRotation(prim_geom);
  1995. d.MassRotate(ref primmass, ref primmat);
  1996. tmpdmass = primmass;
  1997. thispos = d.GeomGetOffsetPosition(prim_geom);
  1998. d.MassTranslate(ref tmpdmass,
  1999. thispos.X,
  2000. thispos.Y,
  2001. thispos.Z);
  2002. // subtract current prim inertia from object
  2003. DMassSubPartFromObj(ref tmpdmass, ref objdmass);
  2004. // update to new position
  2005. _position = NewPos;
  2006. d.GeomSetOffsetWorldPosition(prim_geom, NewPos.X, NewPos.Y, NewPos.Z);
  2007. thispos = d.GeomGetOffsetPosition(prim_geom);
  2008. d.MassTranslate(ref primmass,
  2009. thispos.X,
  2010. thispos.Y,
  2011. thispos.Z);
  2012. d.MassAdd(ref objdmass, ref primmass);
  2013. // fix all positions
  2014. IntPtr g = d.BodyGetFirstGeom(Body);
  2015. while (g != IntPtr.Zero)
  2016. {
  2017. thispos = d.GeomGetOffsetPosition(g);
  2018. thispos.X -= objdmass.c.X;
  2019. thispos.Y -= objdmass.c.Y;
  2020. thispos.Z -= objdmass.c.Z;
  2021. d.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z);
  2022. g = d.dBodyGetNextGeom(g);
  2023. }
  2024. d.BodyVectorToWorld(Body, objdmass.c.X, objdmass.c.Y, objdmass.c.Z, out thispos);
  2025. // get current object position and rotation
  2026. dobjpos = d.BodyGetPosition(Body);
  2027. d.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z);
  2028. d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
  2029. d.BodySetMass(Body, ref objdmass);
  2030. m_mass = objdmass.mass;
  2031. }
  2032. private void FixInertia(Quaternion newrot)
  2033. {
  2034. d.Matrix3 mat = new d.Matrix3();
  2035. d.Quaternion quat = new d.Quaternion();
  2036. d.Mass tmpdmass = new d.Mass { };
  2037. d.Mass objdmass = new d.Mass { };
  2038. d.Vector3 dobjpos;
  2039. d.Vector3 thispos;
  2040. d.BodyGetMass(Body, out objdmass);
  2041. // get prim own inertia in its local frame
  2042. tmpdmass = primdMass;
  2043. mat = d.GeomGetOffsetRotation(prim_geom);
  2044. d.MassRotate(ref tmpdmass, ref mat);
  2045. // transform to object frame
  2046. thispos = d.GeomGetOffsetPosition(prim_geom);
  2047. d.MassTranslate(ref tmpdmass,
  2048. thispos.X,
  2049. thispos.Y,
  2050. thispos.Z);
  2051. // subtract current prim inertia from object
  2052. DMassSubPartFromObj(ref tmpdmass, ref objdmass);
  2053. // update to new orientation
  2054. _orientation = newrot;
  2055. quat.X = newrot.X;
  2056. quat.Y = newrot.Y;
  2057. quat.Z = newrot.Z;
  2058. quat.W = newrot.W;
  2059. d.GeomSetOffsetWorldQuaternion(prim_geom, ref quat);
  2060. tmpdmass = primdMass;
  2061. mat = d.GeomGetOffsetRotation(prim_geom);
  2062. d.MassRotate(ref tmpdmass, ref mat);
  2063. d.MassTranslate(ref tmpdmass,
  2064. thispos.X,
  2065. thispos.Y,
  2066. thispos.Z);
  2067. d.MassAdd(ref objdmass, ref tmpdmass);
  2068. // fix all positions
  2069. IntPtr g = d.BodyGetFirstGeom(Body);
  2070. while (g != IntPtr.Zero)
  2071. {
  2072. thispos = d.GeomGetOffsetPosition(g);
  2073. thispos.X -= objdmass.c.X;
  2074. thispos.Y -= objdmass.c.Y;
  2075. thispos.Z -= objdmass.c.Z;
  2076. d.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z);
  2077. g = d.dBodyGetNextGeom(g);
  2078. }
  2079. d.BodyVectorToWorld(Body, objdmass.c.X, objdmass.c.Y, objdmass.c.Z, out thispos);
  2080. // get current object position and rotation
  2081. dobjpos = d.BodyGetPosition(Body);
  2082. d.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z);
  2083. d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
  2084. d.BodySetMass(Body, ref objdmass);
  2085. m_mass = objdmass.mass;
  2086. }
  2087. #region Mass Calculation
  2088. private void UpdatePrimBodyData()
  2089. {
  2090. primMass = m_density * primVolume;
  2091. if (primMass <= 0)
  2092. primMass = 0.0001f;//ckrinke: Mass must be greater then zero.
  2093. if (primMass > _parent_scene.maximumMassObject)
  2094. primMass = _parent_scene.maximumMassObject;
  2095. m_mass = primMass; // just in case
  2096. d.MassSetBoxTotal(out primdMass, primMass, 2.0f * m_OBB.X, 2.0f * m_OBB.Y, 2.0f * m_OBB.Z);
  2097. d.MassTranslate(ref primdMass,
  2098. m_OBBOffset.X,
  2099. m_OBBOffset.Y,
  2100. m_OBBOffset.Z);
  2101. primOOBradiusSQ = m_OBB.LengthSquared();
  2102. if (_triMeshData != IntPtr.Zero)
  2103. {
  2104. float pc = m_physCost;
  2105. float psf = primOOBradiusSQ;
  2106. psf *= 1.33f * .2f;
  2107. pc *= psf;
  2108. if (pc < 0.1f)
  2109. pc = 0.1f;
  2110. m_physCost = pc;
  2111. }
  2112. else
  2113. m_physCost = 0.1f;
  2114. m_streamCost = 1.0f;
  2115. }
  2116. #endregion
  2117. /// <summary>
  2118. /// Add a child prim to this parent prim.
  2119. /// </summary>
  2120. /// <param name="prim">Child prim</param>
  2121. // I'm the parent
  2122. // prim is the child
  2123. public void ParentPrim(OdePrim prim)
  2124. {
  2125. //Console.WriteLine("ParentPrim " + m_primName);
  2126. if (this.m_localID != prim.m_localID)
  2127. {
  2128. DestroyBody(); // for now we need to rebuil entire object on link change
  2129. lock (childrenPrim)
  2130. {
  2131. // adopt the prim
  2132. if (!childrenPrim.Contains(prim))
  2133. childrenPrim.Add(prim);
  2134. // see if this prim has kids and adopt them also
  2135. // should not happen for now
  2136. foreach (OdePrim prm in prim.childrenPrim)
  2137. {
  2138. if (!childrenPrim.Contains(prm))
  2139. {
  2140. if (prm.Body != IntPtr.Zero)
  2141. {
  2142. if (prm.prim_geom != IntPtr.Zero)
  2143. d.GeomSetBody(prm.prim_geom, IntPtr.Zero);
  2144. if (prm.Body != prim.Body)
  2145. prm.DestroyBody(); // don't loose bodies around
  2146. prm.Body = IntPtr.Zero;
  2147. }
  2148. childrenPrim.Add(prm);
  2149. prm._parent = this;
  2150. }
  2151. }
  2152. }
  2153. //Remove old children from the prim
  2154. prim.childrenPrim.Clear();
  2155. if (prim.Body != IntPtr.Zero)
  2156. {
  2157. if (prim.prim_geom != IntPtr.Zero)
  2158. d.GeomSetBody(prim.prim_geom, IntPtr.Zero);
  2159. prim.DestroyBody(); // don't loose bodies around
  2160. prim.Body = IntPtr.Zero;
  2161. }
  2162. prim.childPrim = true;
  2163. prim._parent = this;
  2164. MakeBody(); // full nasty reconstruction
  2165. }
  2166. }
  2167. private void UpdateChildsfromgeom()
  2168. {
  2169. if (childrenPrim.Count > 0)
  2170. {
  2171. foreach (OdePrim prm in childrenPrim)
  2172. prm.UpdateDataFromGeom();
  2173. }
  2174. }
  2175. private void UpdateDataFromGeom()
  2176. {
  2177. if (prim_geom != IntPtr.Zero)
  2178. {
  2179. d.Quaternion qtmp;
  2180. d.GeomCopyQuaternion(prim_geom, out qtmp);
  2181. _orientation.X = qtmp.X;
  2182. _orientation.Y = qtmp.Y;
  2183. _orientation.Z = qtmp.Z;
  2184. _orientation.W = qtmp.W;
  2185. /*
  2186. // Debug
  2187. float qlen = _orientation.Length();
  2188. if (qlen > 1.01f || qlen < 0.99)
  2189. m_log.WarnFormat("[PHYSICS]: Got nonnorm quaternion from geom in Object {0} norm {1}", Name, qlen);
  2190. //
  2191. */
  2192. _orientation.Normalize();
  2193. d.Vector3 lpos = d.GeomGetPosition(prim_geom);
  2194. _position.X = lpos.X;
  2195. _position.Y = lpos.Y;
  2196. _position.Z = lpos.Z;
  2197. }
  2198. }
  2199. private void ChildDelink(OdePrim odePrim, bool remakebodies)
  2200. {
  2201. // Okay, we have a delinked child.. destroy all body and remake
  2202. if (odePrim != this && !childrenPrim.Contains(odePrim))
  2203. return;
  2204. DestroyBody();
  2205. if (odePrim == this) // delinking the root prim
  2206. {
  2207. OdePrim newroot = null;
  2208. lock (childrenPrim)
  2209. {
  2210. if (childrenPrim.Count > 0)
  2211. {
  2212. newroot = childrenPrim[0];
  2213. childrenPrim.RemoveAt(0);
  2214. foreach (OdePrim prm in childrenPrim)
  2215. {
  2216. newroot.childrenPrim.Add(prm);
  2217. }
  2218. childrenPrim.Clear();
  2219. }
  2220. if (newroot != null)
  2221. {
  2222. newroot.childPrim = false;
  2223. newroot._parent = null;
  2224. if (remakebodies)
  2225. newroot.MakeBody();
  2226. }
  2227. }
  2228. }
  2229. else
  2230. {
  2231. lock (childrenPrim)
  2232. {
  2233. childrenPrim.Remove(odePrim);
  2234. odePrim.childPrim = false;
  2235. odePrim._parent = null;
  2236. // odePrim.UpdateDataFromGeom();
  2237. if (remakebodies)
  2238. odePrim.MakeBody();
  2239. }
  2240. }
  2241. if (remakebodies)
  2242. MakeBody();
  2243. }
  2244. protected void ChildRemove(OdePrim odePrim, bool reMakeBody)
  2245. {
  2246. // Okay, we have a delinked child.. destroy all body and remake
  2247. if (odePrim != this && !childrenPrim.Contains(odePrim))
  2248. return;
  2249. DestroyBody();
  2250. if (odePrim == this)
  2251. {
  2252. OdePrim newroot = null;
  2253. lock (childrenPrim)
  2254. {
  2255. if (childrenPrim.Count > 0)
  2256. {
  2257. newroot = childrenPrim[0];
  2258. childrenPrim.RemoveAt(0);
  2259. foreach (OdePrim prm in childrenPrim)
  2260. {
  2261. newroot.childrenPrim.Add(prm);
  2262. }
  2263. childrenPrim.Clear();
  2264. }
  2265. if (newroot != null)
  2266. {
  2267. newroot.childPrim = false;
  2268. newroot._parent = null;
  2269. newroot.MakeBody();
  2270. }
  2271. }
  2272. if (reMakeBody)
  2273. MakeBody();
  2274. return;
  2275. }
  2276. else
  2277. {
  2278. lock (childrenPrim)
  2279. {
  2280. childrenPrim.Remove(odePrim);
  2281. odePrim.childPrim = false;
  2282. odePrim._parent = null;
  2283. if (reMakeBody)
  2284. odePrim.MakeBody();
  2285. }
  2286. }
  2287. MakeBody();
  2288. }
  2289. #region changes
  2290. private void changeadd()
  2291. {
  2292. _parent_scene.addToPrims(this);
  2293. }
  2294. private void changeAngularLock(byte newLocks)
  2295. {
  2296. // do we have a Physical object?
  2297. if (Body != IntPtr.Zero)
  2298. {
  2299. //Check that we have a Parent
  2300. //If we have a parent then we're not authorative here
  2301. if (_parent == null)
  2302. {
  2303. if (newLocks != 0)
  2304. {
  2305. createAMotor(newLocks);
  2306. }
  2307. else
  2308. {
  2309. if (Amotor != IntPtr.Zero)
  2310. {
  2311. d.JointDestroy(Amotor);
  2312. Amotor = IntPtr.Zero;
  2313. }
  2314. }
  2315. }
  2316. }
  2317. // Store this for later in case we get turned into a separate body
  2318. m_angularlocks = newLocks;
  2319. }
  2320. private void changeLink(OdePrim NewParent)
  2321. {
  2322. if (_parent == null && NewParent != null)
  2323. {
  2324. NewParent.ParentPrim(this);
  2325. }
  2326. else if (_parent != null)
  2327. {
  2328. if (_parent is OdePrim)
  2329. {
  2330. if (NewParent != _parent)
  2331. {
  2332. (_parent as OdePrim).ChildDelink(this, false); // for now...
  2333. childPrim = false;
  2334. if (NewParent != null)
  2335. {
  2336. NewParent.ParentPrim(this);
  2337. }
  2338. }
  2339. }
  2340. }
  2341. _parent = NewParent;
  2342. }
  2343. private void Stop()
  2344. {
  2345. if (!childPrim)
  2346. {
  2347. // m_force = Vector3.Zero;
  2348. m_forceacc = Vector3.Zero;
  2349. m_angularForceacc = Vector3.Zero;
  2350. // m_torque = Vector3.Zero;
  2351. _velocity = Vector3.Zero;
  2352. m_acceleration = Vector3.Zero;
  2353. m_rotationalVelocity = Vector3.Zero;
  2354. _target_velocity = Vector3.Zero;
  2355. if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
  2356. m_vehicle.Stop();
  2357. _zeroFlag = false;
  2358. base.RequestPhysicsterseUpdate();
  2359. }
  2360. if (Body != IntPtr.Zero)
  2361. {
  2362. d.BodySetForce(Body, 0f, 0f, 0f);
  2363. d.BodySetTorque(Body, 0f, 0f, 0f);
  2364. d.BodySetLinearVel(Body, 0f, 0f, 0f);
  2365. d.BodySetAngularVel(Body, 0f, 0f, 0f);
  2366. }
  2367. }
  2368. private void changePhantomStatus(bool newval)
  2369. {
  2370. m_isphantom = newval;
  2371. UpdateCollisionCatFlags();
  2372. ApplyCollisionCatFlags();
  2373. }
  2374. /* not in use
  2375. internal void ChildSelectedChange(bool childSelect)
  2376. {
  2377. if(childPrim)
  2378. return;
  2379. if (childSelect == m_isSelected)
  2380. return;
  2381. if (childSelect)
  2382. {
  2383. DoSelectedStatus(true);
  2384. }
  2385. else
  2386. {
  2387. foreach (OdePrim prm in childrenPrim)
  2388. {
  2389. if (prm.m_isSelected)
  2390. return;
  2391. }
  2392. DoSelectedStatus(false);
  2393. }
  2394. }
  2395. */
  2396. private void changeSelectedStatus(bool newval)
  2397. {
  2398. if (m_lastdoneSelected == newval)
  2399. return;
  2400. m_lastdoneSelected = newval;
  2401. DoSelectedStatus(newval);
  2402. }
  2403. private void CheckDelaySelect()
  2404. {
  2405. if (m_delaySelect)
  2406. {
  2407. DoSelectedStatus(m_isSelected);
  2408. }
  2409. }
  2410. private void DoSelectedStatus(bool newval)
  2411. {
  2412. m_isSelected = newval;
  2413. Stop();
  2414. if (newval)
  2415. {
  2416. if (!childPrim && Body != IntPtr.Zero)
  2417. d.BodyDisable(Body);
  2418. if (m_delaySelect || m_isphysical)
  2419. {
  2420. m_collisionCategories = CollisionCategories.Selected;
  2421. m_collisionFlags = 0;
  2422. if (!childPrim)
  2423. {
  2424. foreach (OdePrim prm in childrenPrim)
  2425. {
  2426. prm.m_collisionCategories = m_collisionCategories;
  2427. prm.m_collisionFlags = m_collisionFlags;
  2428. if (prm.prim_geom != IntPtr.Zero)
  2429. {
  2430. if (prm.m_NoColide)
  2431. {
  2432. d.GeomSetCategoryBits(prm.prim_geom, 0);
  2433. d.GeomSetCollideBits(prm.prim_geom, 0);
  2434. }
  2435. else
  2436. {
  2437. d.GeomSetCategoryBits(prm.prim_geom, (uint)m_collisionCategories);
  2438. d.GeomSetCollideBits(prm.prim_geom, (uint)m_collisionFlags);
  2439. }
  2440. }
  2441. prm.m_delaySelect = false;
  2442. }
  2443. }
  2444. // else if (_parent != null)
  2445. // ((OdePrim)_parent).ChildSelectedChange(true);
  2446. if (prim_geom != IntPtr.Zero)
  2447. {
  2448. if (m_NoColide)
  2449. {
  2450. d.GeomSetCategoryBits(prim_geom, 0);
  2451. d.GeomSetCollideBits(prim_geom, 0);
  2452. if (collide_geom != prim_geom && collide_geom != IntPtr.Zero)
  2453. {
  2454. d.GeomSetCategoryBits(collide_geom, 0);
  2455. d.GeomSetCollideBits(collide_geom, 0);
  2456. }
  2457. }
  2458. else
  2459. {
  2460. d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories);
  2461. d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags);
  2462. if (collide_geom != prim_geom && collide_geom != IntPtr.Zero)
  2463. {
  2464. d.GeomSetCategoryBits(collide_geom, (uint)m_collisionCategories);
  2465. d.GeomSetCollideBits(collide_geom, (uint)m_collisionFlags);
  2466. }
  2467. }
  2468. }
  2469. m_delaySelect = false;
  2470. }
  2471. else if(!m_isphysical)
  2472. {
  2473. m_delaySelect = true;
  2474. }
  2475. }
  2476. else
  2477. {
  2478. if (!childPrim)
  2479. {
  2480. if (Body != IntPtr.Zero && !m_disabled)
  2481. {
  2482. _zeroFlag = true;
  2483. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2484. d.BodyEnable(Body);
  2485. }
  2486. }
  2487. // else if (_parent != null)
  2488. // ((OdePrim)_parent).ChildSelectedChange(false);
  2489. UpdateCollisionCatFlags();
  2490. ApplyCollisionCatFlags();
  2491. m_delaySelect = false;
  2492. }
  2493. resetCollisionAccounting();
  2494. }
  2495. private void changePosition(Vector3 newPos)
  2496. {
  2497. CheckDelaySelect();
  2498. if (m_isphysical)
  2499. {
  2500. if (childPrim) // inertia is messed, must rebuild
  2501. {
  2502. if (m_building)
  2503. {
  2504. _position = newPos;
  2505. }
  2506. else if (m_forcePosOrRotation && _position != newPos && Body != IntPtr.Zero)
  2507. {
  2508. FixInertia(newPos);
  2509. if (!d.BodyIsEnabled(Body))
  2510. {
  2511. _zeroFlag = true;
  2512. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2513. d.BodyEnable(Body);
  2514. }
  2515. }
  2516. }
  2517. else
  2518. {
  2519. if (_position != newPos)
  2520. {
  2521. d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
  2522. _position = newPos;
  2523. }
  2524. if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
  2525. {
  2526. _zeroFlag = true;
  2527. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2528. d.BodyEnable(Body);
  2529. }
  2530. }
  2531. }
  2532. else
  2533. {
  2534. if (prim_geom != IntPtr.Zero)
  2535. {
  2536. if (newPos != _position)
  2537. {
  2538. d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
  2539. _position = newPos;
  2540. m_targetSpace = _parent_scene.MoveGeomToStaticSpace(prim_geom, _position, m_targetSpace);
  2541. }
  2542. }
  2543. }
  2544. givefakepos--;
  2545. if (givefakepos < 0)
  2546. givefakepos = 0;
  2547. // changeSelectedStatus();
  2548. resetCollisionAccounting();
  2549. }
  2550. private void changeOrientation(Quaternion newOri)
  2551. {
  2552. CheckDelaySelect();
  2553. if (m_isphysical)
  2554. {
  2555. if (childPrim) // inertia is messed, must rebuild
  2556. {
  2557. if (m_building)
  2558. {
  2559. _orientation = newOri;
  2560. }
  2561. /*
  2562. else if (m_forcePosOrRotation && _orientation != newOri && Body != IntPtr.Zero)
  2563. {
  2564. FixInertia(_position, newOri);
  2565. if (!d.BodyIsEnabled(Body))
  2566. d.BodyEnable(Body);
  2567. }
  2568. */
  2569. }
  2570. else
  2571. {
  2572. if (newOri != _orientation)
  2573. {
  2574. d.Quaternion myrot = new d.Quaternion();
  2575. myrot.X = newOri.X;
  2576. myrot.Y = newOri.Y;
  2577. myrot.Z = newOri.Z;
  2578. myrot.W = newOri.W;
  2579. d.GeomSetQuaternion(prim_geom, ref myrot);
  2580. _orientation = newOri;
  2581. if (Body != IntPtr.Zero)
  2582. {
  2583. if(m_angularlocks != 0)
  2584. createAMotor(m_angularlocks);
  2585. }
  2586. }
  2587. if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
  2588. {
  2589. _zeroFlag = true;
  2590. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2591. d.BodyEnable(Body);
  2592. }
  2593. }
  2594. }
  2595. else
  2596. {
  2597. if (prim_geom != IntPtr.Zero)
  2598. {
  2599. if (newOri != _orientation)
  2600. {
  2601. d.Quaternion myrot = new d.Quaternion();
  2602. myrot.X = newOri.X;
  2603. myrot.Y = newOri.Y;
  2604. myrot.Z = newOri.Z;
  2605. myrot.W = newOri.W;
  2606. d.GeomSetQuaternion(prim_geom, ref myrot);
  2607. _orientation = newOri;
  2608. }
  2609. }
  2610. }
  2611. givefakeori--;
  2612. if (givefakeori < 0)
  2613. givefakeori = 0;
  2614. resetCollisionAccounting();
  2615. }
  2616. private void changePositionAndOrientation(Vector3 newPos, Quaternion newOri)
  2617. {
  2618. CheckDelaySelect();
  2619. if (m_isphysical)
  2620. {
  2621. if (childPrim && m_building) // inertia is messed, must rebuild
  2622. {
  2623. _position = newPos;
  2624. _orientation = newOri;
  2625. }
  2626. else
  2627. {
  2628. if (newOri != _orientation)
  2629. {
  2630. d.Quaternion myrot = new d.Quaternion();
  2631. myrot.X = newOri.X;
  2632. myrot.Y = newOri.Y;
  2633. myrot.Z = newOri.Z;
  2634. myrot.W = newOri.W;
  2635. d.GeomSetQuaternion(prim_geom, ref myrot);
  2636. _orientation = newOri;
  2637. if (Body != IntPtr.Zero && m_angularlocks != 0)
  2638. createAMotor(m_angularlocks);
  2639. }
  2640. if (_position != newPos)
  2641. {
  2642. d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
  2643. _position = newPos;
  2644. }
  2645. if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
  2646. {
  2647. _zeroFlag = true;
  2648. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2649. d.BodyEnable(Body);
  2650. }
  2651. }
  2652. }
  2653. else
  2654. {
  2655. // string primScenAvatarIn = _parent_scene.whichspaceamIin(_position);
  2656. // int[] arrayitem = _parent_scene.calculateSpaceArrayItemFromPos(_position);
  2657. if (prim_geom != IntPtr.Zero)
  2658. {
  2659. if (newOri != _orientation)
  2660. {
  2661. d.Quaternion myrot = new d.Quaternion();
  2662. myrot.X = newOri.X;
  2663. myrot.Y = newOri.Y;
  2664. myrot.Z = newOri.Z;
  2665. myrot.W = newOri.W;
  2666. d.GeomSetQuaternion(prim_geom, ref myrot);
  2667. _orientation = newOri;
  2668. }
  2669. if (newPos != _position)
  2670. {
  2671. d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
  2672. _position = newPos;
  2673. m_targetSpace = _parent_scene.MoveGeomToStaticSpace(prim_geom, _position, m_targetSpace);
  2674. }
  2675. }
  2676. }
  2677. givefakepos--;
  2678. if (givefakepos < 0)
  2679. givefakepos = 0;
  2680. givefakeori--;
  2681. if (givefakeori < 0)
  2682. givefakeori = 0;
  2683. resetCollisionAccounting();
  2684. }
  2685. private void changeDisable(bool disable)
  2686. {
  2687. if (disable)
  2688. {
  2689. if (!m_disabled)
  2690. disableBodySoft();
  2691. }
  2692. else
  2693. {
  2694. if (m_disabled)
  2695. enableBodySoft();
  2696. }
  2697. }
  2698. private void changePhysicsStatus(bool NewStatus)
  2699. {
  2700. CheckDelaySelect();
  2701. m_isphysical = NewStatus;
  2702. if (!childPrim)
  2703. {
  2704. if (NewStatus)
  2705. {
  2706. if (Body == IntPtr.Zero)
  2707. MakeBody();
  2708. }
  2709. else
  2710. {
  2711. if (Body != IntPtr.Zero)
  2712. {
  2713. DestroyBody();
  2714. }
  2715. Stop();
  2716. }
  2717. }
  2718. resetCollisionAccounting();
  2719. }
  2720. private void changeSize(Vector3 newSize)
  2721. {
  2722. }
  2723. private void changeShape(PrimitiveBaseShape newShape)
  2724. {
  2725. }
  2726. private void changeAddPhysRep(ODEPhysRepData repData)
  2727. {
  2728. _size = repData.size; //??
  2729. _pbs = repData.pbs;
  2730. m_mesh = repData.mesh;
  2731. m_assetID = repData.assetID;
  2732. m_meshState = repData.meshState;
  2733. m_hasOBB = repData.hasOBB;
  2734. m_OBBOffset = repData.OBBOffset;
  2735. m_OBB = repData.OBB;
  2736. primVolume = repData.volume;
  2737. CreateGeom(repData.isTooSmall);
  2738. if (prim_geom != IntPtr.Zero)
  2739. {
  2740. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  2741. d.Quaternion myrot = new d.Quaternion();
  2742. myrot.X = _orientation.X;
  2743. myrot.Y = _orientation.Y;
  2744. myrot.Z = _orientation.Z;
  2745. myrot.W = _orientation.W;
  2746. d.GeomSetQuaternion(prim_geom, ref myrot);
  2747. }
  2748. if (!m_isphysical)
  2749. {
  2750. SetInStaticSpace(this);
  2751. UpdateCollisionCatFlags();
  2752. ApplyCollisionCatFlags();
  2753. }
  2754. else
  2755. MakeBody();
  2756. if ((m_meshState & MeshState.NeedMask) != 0)
  2757. {
  2758. repData.size = _size;
  2759. repData.pbs = _pbs;
  2760. repData.shapetype = m_fakeShapetype;
  2761. _parent_scene.m_meshWorker.RequestMesh(repData);
  2762. }
  2763. else
  2764. m_shapetype = repData.shapetype;
  2765. }
  2766. private void changePhysRepData(ODEPhysRepData repData)
  2767. {
  2768. if(_size == repData.size &&
  2769. _pbs == repData.pbs &&
  2770. m_shapetype == repData.shapetype &&
  2771. m_mesh == repData.mesh &&
  2772. primVolume == repData.volume)
  2773. return;
  2774. CheckDelaySelect();
  2775. OdePrim parent = (OdePrim)_parent;
  2776. bool chp = childPrim;
  2777. if (chp)
  2778. {
  2779. if (parent != null)
  2780. {
  2781. parent.DestroyBody();
  2782. }
  2783. }
  2784. else
  2785. {
  2786. DestroyBody();
  2787. }
  2788. RemoveGeom();
  2789. _size = repData.size;
  2790. _pbs = repData.pbs;
  2791. m_mesh = repData.mesh;
  2792. m_assetID = repData.assetID;
  2793. m_meshState = repData.meshState;
  2794. m_hasOBB = repData.hasOBB;
  2795. m_OBBOffset = repData.OBBOffset;
  2796. m_OBB = repData.OBB;
  2797. primVolume = repData.volume;
  2798. CreateGeom(repData.isTooSmall);
  2799. if (prim_geom != IntPtr.Zero)
  2800. {
  2801. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  2802. d.Quaternion myrot = new d.Quaternion();
  2803. myrot.X = _orientation.X;
  2804. myrot.Y = _orientation.Y;
  2805. myrot.Z = _orientation.Z;
  2806. myrot.W = _orientation.W;
  2807. d.GeomSetQuaternion(prim_geom, ref myrot);
  2808. }
  2809. if (m_isphysical)
  2810. {
  2811. if (chp)
  2812. {
  2813. if (parent != null)
  2814. {
  2815. parent.MakeBody();
  2816. }
  2817. }
  2818. else
  2819. MakeBody();
  2820. }
  2821. else
  2822. {
  2823. SetInStaticSpace(this);
  2824. UpdateCollisionCatFlags();
  2825. ApplyCollisionCatFlags();
  2826. }
  2827. resetCollisionAccounting();
  2828. if ((m_meshState & MeshState.NeedMask) != 0)
  2829. {
  2830. repData.size = _size;
  2831. repData.pbs = _pbs;
  2832. repData.shapetype = m_fakeShapetype;
  2833. _parent_scene.m_meshWorker.RequestMesh(repData);
  2834. }
  2835. else
  2836. m_shapetype = repData.shapetype;
  2837. }
  2838. private void changeFloatOnWater(bool newval)
  2839. {
  2840. m_collidesWater = newval;
  2841. UpdateCollisionCatFlags();
  2842. ApplyCollisionCatFlags();
  2843. }
  2844. private void changeSetTorque(Vector3 newtorque)
  2845. {
  2846. if (!m_isSelected && !m_outbounds)
  2847. {
  2848. if (m_isphysical && Body != IntPtr.Zero)
  2849. {
  2850. if (m_disabled)
  2851. enableBodySoft();
  2852. else if (!d.BodyIsEnabled(Body))
  2853. {
  2854. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2855. d.BodyEnable(Body);
  2856. }
  2857. }
  2858. m_torque = newtorque;
  2859. }
  2860. }
  2861. private void changeForce(Vector3 force)
  2862. {
  2863. m_force = force;
  2864. if (!m_isSelected && !m_outbounds && Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
  2865. {
  2866. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2867. d.BodyEnable(Body);
  2868. }
  2869. }
  2870. private void changeAddForce(Vector3 theforce)
  2871. {
  2872. m_forceacc += theforce;
  2873. if (!m_isSelected && !m_outbounds)
  2874. {
  2875. lock (this)
  2876. {
  2877. //m_log.Info("[PHYSICS]: dequeing forcelist");
  2878. if (m_isphysical && Body != IntPtr.Zero)
  2879. {
  2880. if (m_disabled)
  2881. enableBodySoft();
  2882. else if (!d.BodyIsEnabled(Body))
  2883. {
  2884. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2885. d.BodyEnable(Body);
  2886. }
  2887. }
  2888. }
  2889. m_collisionscore = 0;
  2890. }
  2891. }
  2892. // actually angular impulse
  2893. private void changeAddAngularImpulse(Vector3 aimpulse)
  2894. {
  2895. m_angularForceacc += aimpulse * m_invTimeStep;
  2896. if (!m_isSelected && !m_outbounds)
  2897. {
  2898. lock (this)
  2899. {
  2900. if (m_isphysical && Body != IntPtr.Zero)
  2901. {
  2902. if (m_disabled)
  2903. enableBodySoft();
  2904. else if (!d.BodyIsEnabled(Body))
  2905. {
  2906. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2907. d.BodyEnable(Body);
  2908. }
  2909. }
  2910. }
  2911. m_collisionscore = 0;
  2912. }
  2913. }
  2914. private void changevelocity(Vector3 newVel)
  2915. {
  2916. float len = newVel.LengthSquared();
  2917. if (len > 100000.0f) // limit to 100m/s
  2918. {
  2919. len = 100.0f / (float)Math.Sqrt(len);
  2920. newVel *= len;
  2921. }
  2922. if (!m_isSelected && !m_outbounds)
  2923. {
  2924. if (Body != IntPtr.Zero)
  2925. {
  2926. if (m_disabled)
  2927. enableBodySoft();
  2928. else if (!d.BodyIsEnabled(Body))
  2929. {
  2930. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2931. d.BodyEnable(Body);
  2932. }
  2933. d.BodySetLinearVel(Body, newVel.X, newVel.Y, newVel.Z);
  2934. }
  2935. //resetCollisionAccounting();
  2936. }
  2937. _velocity = newVel;
  2938. }
  2939. private void changeangvelocity(Vector3 newAngVel)
  2940. {
  2941. float len = newAngVel.LengthSquared();
  2942. if (len > _parent_scene.maxAngVelocitySQ)
  2943. {
  2944. len = _parent_scene.maximumAngularVelocity / (float)Math.Sqrt(len);
  2945. newAngVel *= len;
  2946. }
  2947. if (!m_isSelected && !m_outbounds)
  2948. {
  2949. if (Body != IntPtr.Zero)
  2950. {
  2951. if (m_disabled)
  2952. enableBodySoft();
  2953. else if (!d.BodyIsEnabled(Body))
  2954. {
  2955. d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
  2956. d.BodyEnable(Body);
  2957. }
  2958. d.BodySetAngularVel(Body, newAngVel.X, newAngVel.Y, newAngVel.Z);
  2959. }
  2960. //resetCollisionAccounting();
  2961. }
  2962. m_rotationalVelocity = newAngVel;
  2963. }
  2964. private void changeVolumedetetion(bool newVolDtc)
  2965. {
  2966. m_isVolumeDetect = newVolDtc;
  2967. m_fakeisVolumeDetect = newVolDtc;
  2968. UpdateCollisionCatFlags();
  2969. ApplyCollisionCatFlags();
  2970. }
  2971. protected void changeBuilding(bool newbuilding)
  2972. {
  2973. // Check if we need to do anything
  2974. if (newbuilding == m_building)
  2975. return;
  2976. if ((bool)newbuilding)
  2977. {
  2978. m_building = true;
  2979. if (!childPrim)
  2980. DestroyBody();
  2981. }
  2982. else
  2983. {
  2984. m_building = false;
  2985. CheckDelaySelect();
  2986. if (!childPrim)
  2987. MakeBody();
  2988. }
  2989. if (!childPrim && childrenPrim.Count > 0)
  2990. {
  2991. foreach (OdePrim prm in childrenPrim)
  2992. prm.changeBuilding(m_building); // call directly
  2993. }
  2994. }
  2995. public void changeSetVehicle(VehicleData vdata)
  2996. {
  2997. if (m_vehicle == null)
  2998. m_vehicle = new ODEDynamics(this);
  2999. m_vehicle.DoSetVehicle(vdata);
  3000. }
  3001. private void changeVehicleType(int value)
  3002. {
  3003. if (value == (int)Vehicle.TYPE_NONE)
  3004. {
  3005. if (m_vehicle != null)
  3006. m_vehicle = null;
  3007. }
  3008. else
  3009. {
  3010. if (m_vehicle == null)
  3011. m_vehicle = new ODEDynamics(this);
  3012. m_vehicle.ProcessTypeChange((Vehicle)value);
  3013. }
  3014. }
  3015. private void changeVehicleFloatParam(strVehicleFloatParam fp)
  3016. {
  3017. if (m_vehicle == null)
  3018. return;
  3019. m_vehicle.ProcessFloatVehicleParam((Vehicle)fp.param, fp.value);
  3020. }
  3021. private void changeVehicleVectorParam(strVehicleVectorParam vp)
  3022. {
  3023. if (m_vehicle == null)
  3024. return;
  3025. m_vehicle.ProcessVectorVehicleParam((Vehicle)vp.param, vp.value);
  3026. }
  3027. private void changeVehicleRotationParam(strVehicleQuatParam qp)
  3028. {
  3029. if (m_vehicle == null)
  3030. return;
  3031. m_vehicle.ProcessRotationVehicleParam((Vehicle)qp.param, qp.value);
  3032. }
  3033. private void changeVehicleFlags(strVehicleBoolParam bp)
  3034. {
  3035. if (m_vehicle == null)
  3036. return;
  3037. m_vehicle.ProcessVehicleFlags(bp.param, bp.value);
  3038. }
  3039. private void changeBuoyancy(float b)
  3040. {
  3041. m_buoyancy = b;
  3042. }
  3043. private void changePIDTarget(Vector3 trg)
  3044. {
  3045. m_PIDTarget = trg;
  3046. }
  3047. private void changePIDTau(float tau)
  3048. {
  3049. m_PIDTau = tau;
  3050. }
  3051. private void changePIDActive(bool val)
  3052. {
  3053. m_usePID = val;
  3054. }
  3055. private void changePIDHoverHeight(float val)
  3056. {
  3057. m_PIDHoverHeight = val;
  3058. if (val == 0)
  3059. m_useHoverPID = false;
  3060. }
  3061. private void changePIDHoverType(PIDHoverType type)
  3062. {
  3063. m_PIDHoverType = type;
  3064. }
  3065. private void changePIDHoverTau(float tau)
  3066. {
  3067. m_PIDHoverTau = tau;
  3068. }
  3069. private void changePIDHoverActive(bool active)
  3070. {
  3071. m_useHoverPID = active;
  3072. }
  3073. private void changeInertia(PhysicsInertiaData inertia)
  3074. {
  3075. m_InertiaOverride = inertia;
  3076. if (Body != IntPtr.Zero)
  3077. DestroyBody();
  3078. MakeBody();
  3079. }
  3080. #endregion
  3081. public void Move()
  3082. {
  3083. if (!childPrim && m_isphysical && Body != IntPtr.Zero &&
  3084. !m_disabled && !m_isSelected && !m_building && !m_outbounds)
  3085. {
  3086. if (!d.BodyIsEnabled(Body))
  3087. {
  3088. // let vehicles sleep
  3089. if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
  3090. return;
  3091. if (++m_bodydisablecontrol < 50)
  3092. return;
  3093. // clear residuals
  3094. d.BodySetAngularVel(Body,0f,0f,0f);
  3095. d.BodySetLinearVel(Body,0f,0f,0f);
  3096. _zeroFlag = true;
  3097. d.BodySetAutoDisableSteps(Body, 1);
  3098. d.BodyEnable(Body);
  3099. m_bodydisablecontrol = -3;
  3100. }
  3101. if(m_bodydisablecontrol < 0)
  3102. m_bodydisablecontrol++;
  3103. d.Vector3 lpos = d.GeomGetPosition(prim_geom); // root position that is seem by rest of simulator
  3104. if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
  3105. {
  3106. // 'VEHICLES' are dealt with in ODEDynamics.cs
  3107. m_vehicle.Step();
  3108. return;
  3109. }
  3110. float fx = 0;
  3111. float fy = 0;
  3112. float fz = 0;
  3113. float mass = m_mass;
  3114. if (m_usePID && m_PIDTau > 0)
  3115. {
  3116. // for now position error
  3117. _target_velocity =
  3118. new Vector3(
  3119. (m_PIDTarget.X - lpos.X),
  3120. (m_PIDTarget.Y - lpos.Y),
  3121. (m_PIDTarget.Z - lpos.Z)
  3122. );
  3123. if (_target_velocity.ApproxEquals(Vector3.Zero, 0.02f))
  3124. {
  3125. d.BodySetPosition(Body, m_PIDTarget.X, m_PIDTarget.Y, m_PIDTarget.Z);
  3126. d.BodySetLinearVel(Body, 0, 0, 0);
  3127. return;
  3128. }
  3129. else
  3130. {
  3131. _zeroFlag = false;
  3132. float tmp = 1 / m_PIDTau;
  3133. _target_velocity *= tmp;
  3134. // apply limits
  3135. tmp = _target_velocity.Length();
  3136. if (tmp > 50.0f)
  3137. {
  3138. tmp = 50 / tmp;
  3139. _target_velocity *= tmp;
  3140. }
  3141. else if (tmp < 0.05f)
  3142. {
  3143. tmp = 0.05f / tmp;
  3144. _target_velocity *= tmp;
  3145. }
  3146. d.Vector3 vel = d.BodyGetLinearVel(Body);
  3147. fx = (_target_velocity.X - vel.X) * m_invTimeStep;
  3148. fy = (_target_velocity.Y - vel.Y) * m_invTimeStep;
  3149. fz = (_target_velocity.Z - vel.Z) * m_invTimeStep;
  3150. // d.BodySetLinearVel(Body, _target_velocity.X, _target_velocity.Y, _target_velocity.Z);
  3151. }
  3152. } // end if (m_usePID)
  3153. // Hover PID Controller needs to be mutually exlusive to MoveTo PID controller
  3154. else if (m_useHoverPID && m_PIDHoverTau != 0 && m_PIDHoverHeight != 0)
  3155. {
  3156. // Non-Vehicles have a limited set of Hover options.
  3157. // determine what our target height really is based on HoverType
  3158. m_groundHeight = _parent_scene.GetTerrainHeightAtXY(lpos.X, lpos.Y);
  3159. switch (m_PIDHoverType)
  3160. {
  3161. case PIDHoverType.Ground:
  3162. m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight;
  3163. break;
  3164. case PIDHoverType.GroundAndWater:
  3165. m_waterHeight = _parent_scene.GetWaterLevel();
  3166. if (m_groundHeight > m_waterHeight)
  3167. m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight;
  3168. else
  3169. m_targetHoverHeight = m_waterHeight + m_PIDHoverHeight;
  3170. break;
  3171. } // end switch (m_PIDHoverType)
  3172. // don't go underground unless volumedetector
  3173. if (m_targetHoverHeight > m_groundHeight || m_isVolumeDetect)
  3174. {
  3175. d.Vector3 vel = d.BodyGetLinearVel(Body);
  3176. fz = (m_targetHoverHeight - lpos.Z);
  3177. // if error is zero, use position control; otherwise, velocity control
  3178. if (Math.Abs(fz) < 0.01f)
  3179. {
  3180. d.BodySetPosition(Body, lpos.X, lpos.Y, m_targetHoverHeight);
  3181. d.BodySetLinearVel(Body, vel.X, vel.Y, 0);
  3182. }
  3183. else
  3184. {
  3185. _zeroFlag = false;
  3186. fz /= m_PIDHoverTau;
  3187. float tmp = Math.Abs(fz);
  3188. if (tmp > 50)
  3189. fz = 50 * Math.Sign(fz);
  3190. else if (tmp < 0.1)
  3191. fz = 0.1f * Math.Sign(fz);
  3192. fz = ((fz - vel.Z) * m_invTimeStep);
  3193. }
  3194. }
  3195. }
  3196. else
  3197. {
  3198. float b = (1.0f - m_buoyancy) * m_gravmod;
  3199. fx = _parent_scene.gravityx * b;
  3200. fy = _parent_scene.gravityy * b;
  3201. fz = _parent_scene.gravityz * b;
  3202. }
  3203. fx *= mass;
  3204. fy *= mass;
  3205. fz *= mass;
  3206. // constant force
  3207. fx += m_force.X;
  3208. fy += m_force.Y;
  3209. fz += m_force.Z;
  3210. fx += m_forceacc.X;
  3211. fy += m_forceacc.Y;
  3212. fz += m_forceacc.Z;
  3213. m_forceacc = Vector3.Zero;
  3214. //m_log.Info("[OBJPID]: X:" + fx.ToString() + " Y:" + fy.ToString() + " Z:" + fz.ToString());
  3215. if (fx != 0 || fy != 0 || fz != 0)
  3216. {
  3217. d.BodyAddForce(Body, fx, fy, fz);
  3218. //Console.WriteLine("AddForce " + fx + "," + fy + "," + fz);
  3219. }
  3220. Vector3 trq;
  3221. trq = m_torque;
  3222. trq += m_angularForceacc;
  3223. m_angularForceacc = Vector3.Zero;
  3224. if (trq.X != 0 || trq.Y != 0 || trq.Z != 0)
  3225. {
  3226. d.BodyAddTorque(Body, trq.X, trq.Y, trq.Z);
  3227. }
  3228. }
  3229. else
  3230. { // is not physical, or is not a body or is selected
  3231. // _zeroPosition = d.BodyGetPosition(Body);
  3232. return;
  3233. //Console.WriteLine("Nothing " + Name);
  3234. }
  3235. }
  3236. public void UpdatePositionAndVelocity(int frame)
  3237. {
  3238. if (_parent == null && !m_isSelected && !m_disabled && !m_building && !m_outbounds && Body != IntPtr.Zero)
  3239. {
  3240. if(m_bodydisablecontrol < 0)
  3241. return;
  3242. bool bodyenabled = d.BodyIsEnabled(Body);
  3243. if (bodyenabled || !_zeroFlag)
  3244. {
  3245. bool lastZeroFlag = _zeroFlag;
  3246. d.Vector3 lpos = d.GeomGetPosition(prim_geom);
  3247. // check outside region
  3248. if (lpos.Z < -100 || lpos.Z > 100000f)
  3249. {
  3250. m_outbounds = true;
  3251. lpos.Z = Util.Clip(lpos.Z, -100f, 100000f);
  3252. m_acceleration.X = 0;
  3253. m_acceleration.Y = 0;
  3254. m_acceleration.Z = 0;
  3255. _velocity.X = 0;
  3256. _velocity.Y = 0;
  3257. _velocity.Z = 0;
  3258. m_rotationalVelocity.X = 0;
  3259. m_rotationalVelocity.Y = 0;
  3260. m_rotationalVelocity.Z = 0;
  3261. d.BodySetLinearVel(Body, 0, 0, 0); // stop it
  3262. d.BodySetAngularVel(Body, 0, 0, 0); // stop it
  3263. d.BodySetPosition(Body, lpos.X, lpos.Y, lpos.Z); // put it somewhere
  3264. m_lastposition = _position;
  3265. m_lastorientation = _orientation;
  3266. base.RequestPhysicsterseUpdate();
  3267. // throttleCounter = 0;
  3268. _zeroFlag = true;
  3269. disableBodySoft(); // disable it and colisions
  3270. base.RaiseOutOfBounds(_position);
  3271. return;
  3272. }
  3273. if (lpos.X < 0f)
  3274. {
  3275. _position.X = Util.Clip(lpos.X, -2f, -0.1f);
  3276. m_outbounds = true;
  3277. }
  3278. else if (lpos.X > _parent_scene.WorldExtents.X)
  3279. {
  3280. _position.X = Util.Clip(lpos.X, _parent_scene.WorldExtents.X + 0.1f, _parent_scene.WorldExtents.X + 2f);
  3281. m_outbounds = true;
  3282. }
  3283. if (lpos.Y < 0f)
  3284. {
  3285. _position.Y = Util.Clip(lpos.Y, -2f, -0.1f);
  3286. m_outbounds = true;
  3287. }
  3288. else if (lpos.Y > _parent_scene.WorldExtents.Y)
  3289. {
  3290. _position.Y = Util.Clip(lpos.Y, _parent_scene.WorldExtents.Y + 0.1f, _parent_scene.WorldExtents.Y + 2f);
  3291. m_outbounds = true;
  3292. }
  3293. if (m_outbounds)
  3294. {
  3295. m_lastposition = _position;
  3296. m_lastorientation = _orientation;
  3297. d.Vector3 dtmp = d.BodyGetAngularVel(Body);
  3298. m_rotationalVelocity.X = dtmp.X;
  3299. m_rotationalVelocity.Y = dtmp.Y;
  3300. m_rotationalVelocity.Z = dtmp.Z;
  3301. dtmp = d.BodyGetLinearVel(Body);
  3302. _velocity.X = dtmp.X;
  3303. _velocity.Y = dtmp.Y;
  3304. _velocity.Z = dtmp.Z;
  3305. d.BodySetLinearVel(Body, 0, 0, 0); // stop it
  3306. d.BodySetAngularVel(Body, 0, 0, 0);
  3307. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  3308. disableBodySoft(); // stop collisions
  3309. UnSubscribeEvents();
  3310. base.RequestPhysicsterseUpdate();
  3311. return;
  3312. }
  3313. d.Quaternion ori;
  3314. d.GeomCopyQuaternion(prim_geom, out ori);
  3315. // decide if moving
  3316. // use positions since this are integrated quantities
  3317. // tolerance values depende a lot on simulation noise...
  3318. // use simple math.abs since we dont need to be exact
  3319. if(!bodyenabled)
  3320. {
  3321. _zeroFlag = true;
  3322. }
  3323. else
  3324. {
  3325. float poserror;
  3326. float angerror;
  3327. if(_zeroFlag)
  3328. {
  3329. poserror = 0.01f;
  3330. angerror = 0.001f;
  3331. }
  3332. else
  3333. {
  3334. poserror = 0.005f;
  3335. angerror = 0.0005f;
  3336. }
  3337. if (
  3338. (Math.Abs(_position.X - lpos.X) < poserror)
  3339. && (Math.Abs(_position.Y - lpos.Y) < poserror)
  3340. && (Math.Abs(_position.Z - lpos.Z) < poserror)
  3341. && (Math.Abs(_orientation.X - ori.X) < angerror)
  3342. && (Math.Abs(_orientation.Y - ori.Y) < angerror)
  3343. && (Math.Abs(_orientation.Z - ori.Z) < angerror) // ignore W
  3344. )
  3345. _zeroFlag = true;
  3346. else
  3347. _zeroFlag = false;
  3348. }
  3349. // update position
  3350. if (!(_zeroFlag && lastZeroFlag))
  3351. {
  3352. _position.X = lpos.X;
  3353. _position.Y = lpos.Y;
  3354. _position.Z = lpos.Z;
  3355. _orientation.X = ori.X;
  3356. _orientation.Y = ori.Y;
  3357. _orientation.Z = ori.Z;
  3358. _orientation.W = ori.W;
  3359. }
  3360. // update velocities and acceleration
  3361. if (_zeroFlag || lastZeroFlag)
  3362. {
  3363. // disable interpolators
  3364. _velocity = Vector3.Zero;
  3365. m_acceleration = Vector3.Zero;
  3366. m_rotationalVelocity = Vector3.Zero;
  3367. }
  3368. else
  3369. {
  3370. d.Vector3 vel = d.BodyGetLinearVel(Body);
  3371. m_acceleration = _velocity;
  3372. if ((Math.Abs(vel.X) < 0.005f) &&
  3373. (Math.Abs(vel.Y) < 0.005f) &&
  3374. (Math.Abs(vel.Z) < 0.005f))
  3375. {
  3376. _velocity = Vector3.Zero;
  3377. float t = -m_invTimeStep;
  3378. m_acceleration = m_acceleration * t;
  3379. }
  3380. else
  3381. {
  3382. _velocity.X = vel.X;
  3383. _velocity.Y = vel.Y;
  3384. _velocity.Z = vel.Z;
  3385. m_acceleration = (_velocity - m_acceleration) * m_invTimeStep;
  3386. }
  3387. if ((Math.Abs(m_acceleration.X) < 0.01f) &&
  3388. (Math.Abs(m_acceleration.Y) < 0.01f) &&
  3389. (Math.Abs(m_acceleration.Z) < 0.01f))
  3390. {
  3391. m_acceleration = Vector3.Zero;
  3392. }
  3393. vel = d.BodyGetAngularVel(Body);
  3394. if ((Math.Abs(vel.X) < 0.0001) &&
  3395. (Math.Abs(vel.Y) < 0.0001) &&
  3396. (Math.Abs(vel.Z) < 0.0001)
  3397. )
  3398. {
  3399. m_rotationalVelocity = Vector3.Zero;
  3400. }
  3401. else
  3402. {
  3403. m_rotationalVelocity.X = vel.X;
  3404. m_rotationalVelocity.Y = vel.Y;
  3405. m_rotationalVelocity.Z = vel.Z;
  3406. }
  3407. }
  3408. if (_zeroFlag)
  3409. {
  3410. if (!m_lastUpdateSent)
  3411. {
  3412. base.RequestPhysicsterseUpdate();
  3413. if (lastZeroFlag)
  3414. m_lastUpdateSent = true;
  3415. }
  3416. return;
  3417. }
  3418. base.RequestPhysicsterseUpdate();
  3419. m_lastUpdateSent = false;
  3420. }
  3421. }
  3422. }
  3423. internal static bool QuaternionIsFinite(Quaternion q)
  3424. {
  3425. if (Single.IsNaN(q.X) || Single.IsInfinity(q.X))
  3426. return false;
  3427. if (Single.IsNaN(q.Y) || Single.IsInfinity(q.Y))
  3428. return false;
  3429. if (Single.IsNaN(q.Z) || Single.IsInfinity(q.Z))
  3430. return false;
  3431. if (Single.IsNaN(q.W) || Single.IsInfinity(q.W))
  3432. return false;
  3433. return true;
  3434. }
  3435. internal static void DMassSubPartFromObj(ref d.Mass part, ref d.Mass theobj)
  3436. {
  3437. // assumes object center of mass is zero
  3438. float smass = part.mass;
  3439. theobj.mass -= smass;
  3440. smass *= 1.0f / (theobj.mass); ;
  3441. theobj.c.X -= part.c.X * smass;
  3442. theobj.c.Y -= part.c.Y * smass;
  3443. theobj.c.Z -= part.c.Z * smass;
  3444. theobj.I.M00 -= part.I.M00;
  3445. theobj.I.M01 -= part.I.M01;
  3446. theobj.I.M02 -= part.I.M02;
  3447. theobj.I.M10 -= part.I.M10;
  3448. theobj.I.M11 -= part.I.M11;
  3449. theobj.I.M12 -= part.I.M12;
  3450. theobj.I.M20 -= part.I.M20;
  3451. theobj.I.M21 -= part.I.M21;
  3452. theobj.I.M22 -= part.I.M22;
  3453. }
  3454. private void donullchange()
  3455. {
  3456. }
  3457. public bool DoAChange(changes what, object arg)
  3458. {
  3459. if (prim_geom == IntPtr.Zero && what != changes.Add && what != changes.AddPhysRep && what != changes.Remove)
  3460. {
  3461. return false;
  3462. }
  3463. // nasty switch
  3464. switch (what)
  3465. {
  3466. case changes.Add:
  3467. changeadd();
  3468. break;
  3469. case changes.AddPhysRep:
  3470. changeAddPhysRep((ODEPhysRepData)arg);
  3471. break;
  3472. case changes.Remove:
  3473. //If its being removed, we don't want to rebuild the physical rep at all, so ignore this stuff...
  3474. //When we return true, it destroys all of the prims in the linkset anyway
  3475. if (_parent != null)
  3476. {
  3477. OdePrim parent = (OdePrim)_parent;
  3478. parent.ChildRemove(this, false);
  3479. }
  3480. else
  3481. ChildRemove(this, false);
  3482. m_vehicle = null;
  3483. RemoveGeom();
  3484. m_targetSpace = IntPtr.Zero;
  3485. UnSubscribeEvents();
  3486. return true;
  3487. case changes.Link:
  3488. OdePrim tmp = (OdePrim)arg;
  3489. changeLink(tmp);
  3490. break;
  3491. case changes.DeLink:
  3492. changeLink(null);
  3493. break;
  3494. case changes.Position:
  3495. changePosition((Vector3)arg);
  3496. break;
  3497. case changes.Orientation:
  3498. changeOrientation((Quaternion)arg);
  3499. break;
  3500. case changes.PosOffset:
  3501. donullchange();
  3502. break;
  3503. case changes.OriOffset:
  3504. donullchange();
  3505. break;
  3506. case changes.Velocity:
  3507. changevelocity((Vector3)arg);
  3508. break;
  3509. case changes.TargetVelocity:
  3510. break;
  3511. // case changes.Acceleration:
  3512. // changeacceleration((Vector3)arg);
  3513. // break;
  3514. case changes.AngVelocity:
  3515. changeangvelocity((Vector3)arg);
  3516. break;
  3517. case changes.Force:
  3518. changeForce((Vector3)arg);
  3519. break;
  3520. case changes.Torque:
  3521. changeSetTorque((Vector3)arg);
  3522. break;
  3523. case changes.AddForce:
  3524. changeAddForce((Vector3)arg);
  3525. break;
  3526. case changes.AddAngForce:
  3527. changeAddAngularImpulse((Vector3)arg);
  3528. break;
  3529. case changes.AngLock:
  3530. changeAngularLock((byte)arg);
  3531. break;
  3532. case changes.Size:
  3533. changeSize((Vector3)arg);
  3534. break;
  3535. case changes.Shape:
  3536. changeShape((PrimitiveBaseShape)arg);
  3537. break;
  3538. case changes.PhysRepData:
  3539. changePhysRepData((ODEPhysRepData) arg);
  3540. break;
  3541. case changes.CollidesWater:
  3542. changeFloatOnWater((bool)arg);
  3543. break;
  3544. case changes.VolumeDtc:
  3545. changeVolumedetetion((bool)arg);
  3546. break;
  3547. case changes.Phantom:
  3548. changePhantomStatus((bool)arg);
  3549. break;
  3550. case changes.Physical:
  3551. changePhysicsStatus((bool)arg);
  3552. break;
  3553. case changes.Selected:
  3554. changeSelectedStatus((bool)arg);
  3555. break;
  3556. case changes.disabled:
  3557. changeDisable((bool)arg);
  3558. break;
  3559. case changes.building:
  3560. changeBuilding((bool)arg);
  3561. break;
  3562. case changes.VehicleType:
  3563. changeVehicleType((int)arg);
  3564. break;
  3565. case changes.VehicleFlags:
  3566. changeVehicleFlags((strVehicleBoolParam) arg);
  3567. break;
  3568. case changes.VehicleFloatParam:
  3569. changeVehicleFloatParam((strVehicleFloatParam) arg);
  3570. break;
  3571. case changes.VehicleVectorParam:
  3572. changeVehicleVectorParam((strVehicleVectorParam) arg);
  3573. break;
  3574. case changes.VehicleRotationParam:
  3575. changeVehicleRotationParam((strVehicleQuatParam) arg);
  3576. break;
  3577. case changes.SetVehicle:
  3578. changeSetVehicle((VehicleData) arg);
  3579. break;
  3580. case changes.Buoyancy:
  3581. changeBuoyancy((float)arg);
  3582. break;
  3583. case changes.PIDTarget:
  3584. changePIDTarget((Vector3)arg);
  3585. break;
  3586. case changes.PIDTau:
  3587. changePIDTau((float)arg);
  3588. break;
  3589. case changes.PIDActive:
  3590. changePIDActive((bool)arg);
  3591. break;
  3592. case changes.PIDHoverHeight:
  3593. changePIDHoverHeight((float)arg);
  3594. break;
  3595. case changes.PIDHoverType:
  3596. changePIDHoverType((PIDHoverType)arg);
  3597. break;
  3598. case changes.PIDHoverTau:
  3599. changePIDHoverTau((float)arg);
  3600. break;
  3601. case changes.PIDHoverActive:
  3602. changePIDHoverActive((bool)arg);
  3603. break;
  3604. case changes.SetInertia:
  3605. changeInertia((PhysicsInertiaData) arg);
  3606. break;
  3607. case changes.Null:
  3608. donullchange();
  3609. break;
  3610. default:
  3611. donullchange();
  3612. break;
  3613. }
  3614. return false;
  3615. }
  3616. public void AddChange(changes what, object arg)
  3617. {
  3618. _parent_scene.AddChange((PhysicsActor) this, what, arg);
  3619. }
  3620. private struct strVehicleBoolParam
  3621. {
  3622. public int param;
  3623. public bool value;
  3624. }
  3625. private struct strVehicleFloatParam
  3626. {
  3627. public int param;
  3628. public float value;
  3629. }
  3630. private struct strVehicleQuatParam
  3631. {
  3632. public int param;
  3633. public Quaternion value;
  3634. }
  3635. private struct strVehicleVectorParam
  3636. {
  3637. public int param;
  3638. public Vector3 value;
  3639. }
  3640. }
  3641. }