ODEPrim.cs 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Runtime.InteropServices;
  31. using System.Threading;
  32. using log4net;
  33. using OpenMetaverse;
  34. using Ode.NET;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Physics.Manager;
  37. namespace OpenSim.Region.Physics.OdePlugin
  38. {
  39. /// <summary>
  40. /// Various properties that ODE uses for AMotors but isn't exposed in ODE.NET so we must define them ourselves.
  41. /// </summary>
  42. public class OdePrim : PhysicsActor
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private PhysicsVector _position;
  46. private PhysicsVector _velocity;
  47. private PhysicsVector _torque = new PhysicsVector(0,0,0);
  48. private PhysicsVector m_lastVelocity = new PhysicsVector(0.0f, 0.0f, 0.0f);
  49. private PhysicsVector m_lastposition = new PhysicsVector(0.0f, 0.0f, 0.0f);
  50. private Quaternion m_lastorientation = new Quaternion();
  51. private PhysicsVector m_rotationalVelocity;
  52. private PhysicsVector _size;
  53. private PhysicsVector _acceleration;
  54. // private d.Vector3 _zeroPosition = new d.Vector3(0.0f, 0.0f, 0.0f);
  55. private Quaternion _orientation;
  56. private PhysicsVector m_taintposition;
  57. private PhysicsVector m_taintsize;
  58. private PhysicsVector m_taintVelocity = new PhysicsVector(0, 0, 0);
  59. private PhysicsVector m_taintTorque = new PhysicsVector(0, 0, 0);
  60. private Quaternion m_taintrot;
  61. private PhysicsVector m_angularlock = new PhysicsVector(1f, 1f, 1f);
  62. private PhysicsVector m_taintAngularLock = new PhysicsVector(1f, 1f, 1f);
  63. private IntPtr Amotor = IntPtr.Zero;
  64. private PhysicsVector m_PIDTarget = new PhysicsVector(0, 0, 0);
  65. // private PhysicsVector m_taintPIDTarget = new PhysicsVector(0, 0, 0);
  66. private float m_PIDTau = 0f;
  67. private float PID_D = 35f;
  68. private float PID_G = 25f;
  69. private bool m_usePID = false;
  70. private float m_PIDHoverHeight = 0f;
  71. private float m_PIDHoverTau = 0f;
  72. private bool m_useHoverPID = false;
  73. private PIDHoverType m_PIDHoverType = PIDHoverType.Ground;
  74. private float m_targetHoverHeight = 0f;
  75. private float m_groundHeight = 0f;
  76. private float m_waterHeight = 0f;
  77. // private float m_tensor = 5f;
  78. private int body_autodisable_frames = 20;
  79. private const CollisionCategories m_default_collisionFlags = (CollisionCategories.Geom
  80. | CollisionCategories.Space
  81. | CollisionCategories.Body
  82. | CollisionCategories.Character
  83. );
  84. private bool m_taintshape = false;
  85. private bool m_taintPhysics = false;
  86. private bool m_collidesLand = true;
  87. private bool m_collidesWater = false;
  88. public bool m_returnCollisions = false;
  89. // Default we're a Geometry
  90. private CollisionCategories m_collisionCategories = (CollisionCategories.Geom);
  91. // Default, Collide with Other Geometries, spaces and Bodies
  92. private CollisionCategories m_collisionFlags = m_default_collisionFlags;
  93. public bool m_taintremove = false;
  94. public bool m_taintdisable = false;
  95. public bool m_disabled = false;
  96. public bool m_taintadd = false;
  97. public bool m_taintselected = false;
  98. public bool m_taintCollidesWater = false;
  99. public uint m_localID = 0;
  100. //public GCHandle gc;
  101. private CollisionLocker ode;
  102. private bool m_taintforce = false;
  103. private bool m_taintaddangularforce = false;
  104. private PhysicsVector m_force = new PhysicsVector(0.0f, 0.0f, 0.0f);
  105. private List<PhysicsVector> m_forcelist = new List<PhysicsVector>();
  106. private List<PhysicsVector> m_angularforcelist = new List<PhysicsVector>();
  107. private IMesh _mesh;
  108. private PrimitiveBaseShape _pbs;
  109. private OdeScene _parent_scene;
  110. public IntPtr m_targetSpace = (IntPtr) 0;
  111. public IntPtr prim_geom;
  112. public IntPtr prev_geom;
  113. public IntPtr _triMeshData;
  114. private IntPtr _linkJointGroup = (IntPtr)0;
  115. private PhysicsActor _parent = null;
  116. private PhysicsActor m_taintparent = null;
  117. private List<OdePrim> childrenPrim = new List<OdePrim>();
  118. private bool iscolliding = false;
  119. private bool m_isphysical = false;
  120. private bool m_isSelected = false;
  121. internal bool m_isVolumeDetect = false; // If true, this prim only detects collisions but doesn't collide actively
  122. private bool m_throttleUpdates = false;
  123. private int throttleCounter = 0;
  124. public int m_interpenetrationcount = 0;
  125. public float m_collisionscore = 0;
  126. public int m_roundsUnderMotionThreshold = 0;
  127. private int m_crossingfailures = 0;
  128. public float m_buoyancy = 0f;
  129. public bool outofBounds = false;
  130. private float m_density = 10.000006836f; // Aluminum g/cm3;
  131. public bool _zeroFlag = false;
  132. private bool m_lastUpdateSent = false;
  133. public IntPtr Body = (IntPtr) 0;
  134. private String m_primName;
  135. private PhysicsVector _target_velocity;
  136. public d.Mass pMass;
  137. public int m_eventsubscription = 0;
  138. private CollisionEventUpdate CollisionEventsThisFrame = null;
  139. private IntPtr m_linkJoint = (IntPtr)0;
  140. public volatile bool childPrim = false;
  141. private ODEVehicleSettings m_vehicle;
  142. internal int m_material = (int)Material.Wood;
  143. public OdePrim(String primName, OdeScene parent_scene, PhysicsVector pos, PhysicsVector size,
  144. Quaternion rotation, IMesh mesh, PrimitiveBaseShape pbs, bool pisPhysical, CollisionLocker dode)
  145. {
  146. _target_velocity = new PhysicsVector(0, 0, 0);
  147. m_vehicle = new ODEVehicleSettings();
  148. //gc = GCHandle.Alloc(prim_geom, GCHandleType.Pinned);
  149. ode = dode;
  150. _velocity = new PhysicsVector();
  151. if (!PhysicsVector.isFinite(pos))
  152. {
  153. pos = new PhysicsVector(((int)Constants.RegionSize * 0.5f), ((int)Constants.RegionSize * 0.5f), parent_scene.GetTerrainHeightAtXY(((int)Constants.RegionSize * 0.5f), ((int)Constants.RegionSize * 0.5f)) + 0.5f);
  154. m_log.Warn("[PHYSICS]: Got nonFinite Object create Position");
  155. }
  156. _position = pos;
  157. m_taintposition = pos;
  158. PID_D = parent_scene.bodyPIDD;
  159. PID_G = parent_scene.bodyPIDG;
  160. m_density = parent_scene.geomDefaultDensity;
  161. // m_tensor = parent_scene.bodyMotorJointMaxforceTensor;
  162. body_autodisable_frames = parent_scene.bodyFramesAutoDisable;
  163. prim_geom = IntPtr.Zero;
  164. prev_geom = IntPtr.Zero;
  165. if (!PhysicsVector.isFinite(pos))
  166. {
  167. size = new PhysicsVector(0.5f, 0.5f, 0.5f);
  168. m_log.Warn("[PHYSICS]: Got nonFinite Object create Size");
  169. }
  170. if (size.X <= 0) size.X = 0.01f;
  171. if (size.Y <= 0) size.Y = 0.01f;
  172. if (size.Z <= 0) size.Z = 0.01f;
  173. _size = size;
  174. m_taintsize = _size;
  175. _acceleration = new PhysicsVector();
  176. m_rotationalVelocity = PhysicsVector.Zero;
  177. if (!QuaternionIsFinite(rotation))
  178. {
  179. rotation = Quaternion.Identity;
  180. m_log.Warn("[PHYSICS]: Got nonFinite Object create Rotation");
  181. }
  182. _orientation = rotation;
  183. m_taintrot = _orientation;
  184. _mesh = mesh;
  185. _pbs = pbs;
  186. _parent_scene = parent_scene;
  187. m_targetSpace = (IntPtr)0;
  188. if (pos.Z < 0)
  189. m_isphysical = false;
  190. else
  191. {
  192. m_isphysical = pisPhysical;
  193. // If we're physical, we need to be in the master space for now.
  194. // linksets *should* be in a space together.. but are not currently
  195. if (m_isphysical)
  196. m_targetSpace = _parent_scene.space;
  197. }
  198. m_primName = primName;
  199. m_taintadd = true;
  200. _parent_scene.AddPhysicsActorTaint(this);
  201. // don't do .add() here; old geoms get recycled with the same hash
  202. }
  203. public override int PhysicsActorType
  204. {
  205. get { return (int) ActorTypes.Prim; }
  206. set { return; }
  207. }
  208. public override bool SetAlwaysRun
  209. {
  210. get { return false; }
  211. set { return; }
  212. }
  213. public override uint LocalID
  214. {
  215. set {
  216. //m_log.Info("[PHYSICS]: Setting TrackerID: " + value);
  217. m_localID = value; }
  218. }
  219. public override int GetHashCode()
  220. {
  221. return (int)m_localID;
  222. }
  223. public override bool Grabbed
  224. {
  225. set { return; }
  226. }
  227. public override bool Selected
  228. {
  229. set {
  230. // This only makes the object not collidable if the object
  231. // is physical or the object is modified somehow *IN THE FUTURE*
  232. // without this, if an avatar selects prim, they can walk right
  233. // through it while it's selected
  234. m_collisionscore = 0;
  235. if ((m_isphysical && !_zeroFlag) || !value)
  236. {
  237. m_taintselected = value;
  238. _parent_scene.AddPhysicsActorTaint(this);
  239. }
  240. else
  241. {
  242. m_taintselected = value;
  243. m_isSelected = value;
  244. }
  245. }
  246. }
  247. public void SetGeom(IntPtr geom)
  248. {
  249. prev_geom = prim_geom;
  250. prim_geom = geom;
  251. if (prim_geom != IntPtr.Zero)
  252. {
  253. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  254. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  255. }
  256. if (childPrim)
  257. {
  258. if (_parent != null && _parent is OdePrim)
  259. {
  260. OdePrim parent = (OdePrim)_parent;
  261. parent.ChildSetGeom(this);
  262. }
  263. }
  264. //m_log.Warn("Setting Geom to: " + prim_geom);
  265. }
  266. public void enableBodySoft()
  267. {
  268. if (!childPrim)
  269. {
  270. if (m_isphysical && Body != IntPtr.Zero)
  271. {
  272. d.BodyEnable(Body);
  273. m_vehicle.Enable(Body, _parent_scene);
  274. }
  275. m_disabled = false;
  276. }
  277. }
  278. public void disableBodySoft()
  279. {
  280. m_disabled = true;
  281. if (m_isphysical && Body != IntPtr.Zero)
  282. {
  283. d.BodyDisable(Body);
  284. m_vehicle.Disable();
  285. }
  286. }
  287. public void enableBody()
  288. {
  289. // Don't enable this body if we're a child prim
  290. // this should be taken care of in the parent function not here
  291. if (!childPrim)
  292. {
  293. // Sets the geom to a body
  294. Body = d.BodyCreate(_parent_scene.world);
  295. setMass();
  296. d.BodySetPosition(Body, _position.X, _position.Y, _position.Z);
  297. d.Quaternion myrot = new d.Quaternion();
  298. myrot.X = _orientation.X;
  299. myrot.Y = _orientation.Y;
  300. myrot.Z = _orientation.Z;
  301. myrot.W = _orientation.W;
  302. d.BodySetQuaternion(Body, ref myrot);
  303. d.GeomSetBody(prim_geom, Body);
  304. m_collisionCategories |= CollisionCategories.Body;
  305. m_collisionFlags |= (CollisionCategories.Land | CollisionCategories.Wind);
  306. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  307. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  308. d.BodySetAutoDisableFlag(Body, true);
  309. d.BodySetAutoDisableSteps(Body, body_autodisable_frames);
  310. m_interpenetrationcount = 0;
  311. m_collisionscore = 0;
  312. m_disabled = false;
  313. // The body doesn't already have a finite rotation mode set here
  314. if ((!m_angularlock.IsIdentical(PhysicsVector.Zero, 0)) && _parent == null)
  315. {
  316. createAMotor(m_angularlock);
  317. }
  318. if (m_vehicle.Type != Vehicle.TYPE_NONE)
  319. {
  320. m_vehicle.Enable(Body, _parent_scene);
  321. }
  322. _parent_scene.addActivePrim(this);
  323. }
  324. }
  325. #region Mass Calculation
  326. private float CalculateMass()
  327. {
  328. float volume = 0;
  329. // No material is passed to the physics engines yet.. soo..
  330. // we're using the m_density constant in the class definition
  331. float returnMass = 0;
  332. switch (_pbs.ProfileShape)
  333. {
  334. case ProfileShape.Square:
  335. // Profile Volume
  336. volume = _size.X*_size.Y*_size.Z;
  337. // If the user has 'hollowed out'
  338. // ProfileHollow is one of those 0 to 50000 values :P
  339. // we like percentages better.. so turning into a percentage
  340. if (((float) _pbs.ProfileHollow/50000f) > 0.0)
  341. {
  342. float hollowAmount = (float) _pbs.ProfileHollow/50000f;
  343. // calculate the hollow volume by it's shape compared to the prim shape
  344. float hollowVolume = 0;
  345. switch (_pbs.HollowShape)
  346. {
  347. case HollowShape.Square:
  348. case HollowShape.Same:
  349. // Cube Hollow volume calculation
  350. float hollowsizex = _size.X*hollowAmount;
  351. float hollowsizey = _size.Y*hollowAmount;
  352. float hollowsizez = _size.Z*hollowAmount;
  353. hollowVolume = hollowsizex*hollowsizey*hollowsizez;
  354. break;
  355. case HollowShape.Circle:
  356. // Hollow shape is a perfect cyllinder in respect to the cube's scale
  357. // Cyllinder hollow volume calculation
  358. float hRadius = _size.X/2;
  359. float hLength = _size.Z;
  360. // pi * r2 * h
  361. hollowVolume = ((float) (Math.PI*Math.Pow(hRadius, 2)*hLength)*hollowAmount);
  362. break;
  363. case HollowShape.Triangle:
  364. // Equilateral Triangular Prism volume hollow calculation
  365. // Triangle is an Equilateral Triangular Prism with aLength = to _size.Y
  366. float aLength = _size.Y;
  367. // 1/2 abh
  368. hollowVolume = (float) ((0.5*aLength*_size.X*_size.Z)*hollowAmount);
  369. break;
  370. default:
  371. hollowVolume = 0;
  372. break;
  373. }
  374. volume = volume - hollowVolume;
  375. }
  376. break;
  377. case ProfileShape.Circle:
  378. if (_pbs.PathCurve == (byte)Extrusion.Straight)
  379. {
  380. // Cylinder
  381. float volume1 = (float)(Math.PI * Math.Pow(_size.X/2, 2) * _size.Z);
  382. float volume2 = (float)(Math.PI * Math.Pow(_size.Y/2, 2) * _size.Z);
  383. // Approximating the cylinder's irregularity.
  384. if (volume1 > volume2)
  385. {
  386. volume = (float)volume1 - (volume1 - volume2);
  387. }
  388. else if (volume2 > volume1)
  389. {
  390. volume = (float)volume2 - (volume2 - volume1);
  391. }
  392. else
  393. {
  394. // Regular cylinder
  395. volume = volume1;
  396. }
  397. }
  398. else
  399. {
  400. // We don't know what the shape is yet, so use default
  401. volume = _size.X * _size.Y * _size.Z;
  402. }
  403. // If the user has 'hollowed out'
  404. // ProfileHollow is one of those 0 to 50000 values :P
  405. // we like percentages better.. so turning into a percentage
  406. if (((float)_pbs.ProfileHollow / 50000f) > 0.0)
  407. {
  408. float hollowAmount = (float)_pbs.ProfileHollow / 50000f;
  409. // calculate the hollow volume by it's shape compared to the prim shape
  410. float hollowVolume = 0;
  411. switch (_pbs.HollowShape)
  412. {
  413. case HollowShape.Same:
  414. case HollowShape.Circle:
  415. // Hollow shape is a perfect cyllinder in respect to the cube's scale
  416. // Cyllinder hollow volume calculation
  417. float hRadius = _size.X / 2;
  418. float hLength = _size.Z;
  419. // pi * r2 * h
  420. hollowVolume = ((float)(Math.PI * Math.Pow(hRadius, 2) * hLength) * hollowAmount);
  421. break;
  422. case HollowShape.Square:
  423. // Cube Hollow volume calculation
  424. float hollowsizex = _size.X * hollowAmount;
  425. float hollowsizey = _size.Y * hollowAmount;
  426. float hollowsizez = _size.Z * hollowAmount;
  427. hollowVolume = hollowsizex * hollowsizey * hollowsizez;
  428. break;
  429. case HollowShape.Triangle:
  430. // Equilateral Triangular Prism volume hollow calculation
  431. // Triangle is an Equilateral Triangular Prism with aLength = to _size.Y
  432. float aLength = _size.Y;
  433. // 1/2 abh
  434. hollowVolume = (float)((0.5 * aLength * _size.X * _size.Z) * hollowAmount);
  435. break;
  436. default:
  437. hollowVolume = 0;
  438. break;
  439. }
  440. volume = volume - hollowVolume;
  441. }
  442. break;
  443. case ProfileShape.HalfCircle:
  444. if (_pbs.PathCurve == (byte)Extrusion.Curve1)
  445. {
  446. if (_size.X == _size.Y && _size.Y == _size.Z)
  447. {
  448. // regular sphere
  449. // v = 4/3 * pi * r^3
  450. float sradius3 = (float)Math.Pow((_size.X / 2), 3);
  451. volume = (float)((4f / 3f) * Math.PI * sradius3);
  452. }
  453. else
  454. {
  455. // we treat this as a box currently
  456. volume = _size.X * _size.Y * _size.Z;
  457. }
  458. }
  459. else
  460. {
  461. // We don't know what the shape is yet, so use default
  462. volume = _size.X * _size.Y * _size.Z;
  463. }
  464. break;
  465. case ProfileShape.EquilateralTriangle:
  466. /*
  467. v = (abs((xB*yA-xA*yB)+(xC*yB-xB*yC)+(xA*yC-xC*yA))/2) * h
  468. // seed mesh
  469. Vertex MM = new Vertex(-0.25f, -0.45f, 0.0f);
  470. Vertex PM = new Vertex(+0.5f, 0f, 0.0f);
  471. Vertex PP = new Vertex(-0.25f, +0.45f, 0.0f);
  472. */
  473. float xA = -0.25f * _size.X;
  474. float yA = -0.45f * _size.Y;
  475. float xB = 0.5f * _size.X;
  476. float yB = 0;
  477. float xC = -0.25f * _size.X;
  478. float yC = 0.45f * _size.Y;
  479. volume = (float)((Math.Abs((xB * yA - xA * yB) + (xC * yB - xB * yC) + (xA * yC - xC * yA)) / 2) * _size.Z);
  480. // If the user has 'hollowed out'
  481. // ProfileHollow is one of those 0 to 50000 values :P
  482. // we like percentages better.. so turning into a percentage
  483. float fhollowFactor = ((float)_pbs.ProfileHollow / 1.9f);
  484. if (((float)fhollowFactor / 50000f) > 0.0)
  485. {
  486. float hollowAmount = (float)fhollowFactor / 50000f;
  487. // calculate the hollow volume by it's shape compared to the prim shape
  488. float hollowVolume = 0;
  489. switch (_pbs.HollowShape)
  490. {
  491. case HollowShape.Same:
  492. case HollowShape.Triangle:
  493. // Equilateral Triangular Prism volume hollow calculation
  494. // Triangle is an Equilateral Triangular Prism with aLength = to _size.Y
  495. float aLength = _size.Y;
  496. // 1/2 abh
  497. hollowVolume = (float)((0.5 * aLength * _size.X * _size.Z) * hollowAmount);
  498. break;
  499. case HollowShape.Square:
  500. // Cube Hollow volume calculation
  501. float hollowsizex = _size.X * hollowAmount;
  502. float hollowsizey = _size.Y * hollowAmount;
  503. float hollowsizez = _size.Z * hollowAmount;
  504. hollowVolume = hollowsizex * hollowsizey * hollowsizez;
  505. break;
  506. case HollowShape.Circle:
  507. // Hollow shape is a perfect cyllinder in respect to the cube's scale
  508. // Cyllinder hollow volume calculation
  509. float hRadius = _size.X / 2;
  510. float hLength = _size.Z;
  511. // pi * r2 * h
  512. hollowVolume = ((float)((Math.PI * Math.Pow(hRadius, 2) * hLength)/2) * hollowAmount);
  513. break;
  514. default:
  515. hollowVolume = 0;
  516. break;
  517. }
  518. volume = volume - hollowVolume;
  519. }
  520. break;
  521. default:
  522. // we don't have all of the volume formulas yet so
  523. // use the common volume formula for all
  524. volume = _size.X*_size.Y*_size.Z;
  525. break;
  526. }
  527. // Calculate Path cut effect on volume
  528. // Not exact, in the triangle hollow example
  529. // They should never be zero or less then zero..
  530. // we'll ignore it if it's less then zero
  531. // ProfileEnd and ProfileBegin are values
  532. // from 0 to 50000
  533. // Turning them back into percentages so that I can cut that percentage off the volume
  534. float PathCutEndAmount = _pbs.ProfileEnd;
  535. float PathCutStartAmount = _pbs.ProfileBegin;
  536. if (((PathCutStartAmount + PathCutEndAmount)/50000f) > 0.0f)
  537. {
  538. float pathCutAmount = ((PathCutStartAmount + PathCutEndAmount)/50000f);
  539. // Check the return amount for sanity
  540. if (pathCutAmount >= 0.99f)
  541. pathCutAmount = 0.99f;
  542. volume = volume - (volume*pathCutAmount);
  543. }
  544. UInt16 taperX = _pbs.PathScaleX;
  545. UInt16 taperY = _pbs.PathScaleY;
  546. float taperFactorX = 0;
  547. float taperFactorY = 0;
  548. // Mass = density * volume
  549. if (taperX != 100)
  550. {
  551. if (taperX > 100)
  552. {
  553. taperFactorX = 1.0f - ((float)taperX / 200);
  554. //m_log.Warn("taperTopFactorX: " + extr.taperTopFactorX.ToString());
  555. }
  556. else
  557. {
  558. taperFactorX = 1.0f - ((100 - (float)taperX) / 100);
  559. //m_log.Warn("taperBotFactorX: " + extr.taperBotFactorX.ToString());
  560. }
  561. volume = (float)volume * ((taperFactorX / 3f) + 0.001f);
  562. }
  563. if (taperY != 100)
  564. {
  565. if (taperY > 100)
  566. {
  567. taperFactorY = 1.0f - ((float)taperY / 200);
  568. //m_log.Warn("taperTopFactorY: " + extr.taperTopFactorY.ToString());
  569. }
  570. else
  571. {
  572. taperFactorY = 1.0f - ((100 - (float)taperY) / 100);
  573. //m_log.Warn("taperBotFactorY: " + extr.taperBotFactorY.ToString());
  574. }
  575. volume = (float)volume * ((taperFactorY / 3f) + 0.001f);
  576. }
  577. returnMass = m_density*volume;
  578. if (returnMass <= 0) returnMass = 0.0001f;//ckrinke: Mass must be greater then zero.
  579. // Recursively calculate mass
  580. bool HasChildPrim = false;
  581. lock (childrenPrim)
  582. {
  583. if (childrenPrim.Count > 0)
  584. {
  585. HasChildPrim = true;
  586. }
  587. }
  588. if (HasChildPrim)
  589. {
  590. OdePrim[] childPrimArr = new OdePrim[0];
  591. lock (childrenPrim)
  592. childPrimArr = childrenPrim.ToArray();
  593. for (int i = 0; i < childPrimArr.Length; i++)
  594. {
  595. if (childPrimArr[i] != null && !childPrimArr[i].m_taintremove)
  596. returnMass += childPrimArr[i].CalculateMass();
  597. // failsafe, this shouldn't happen but with OpenSim, you never know :)
  598. if (i > 256)
  599. break;
  600. }
  601. }
  602. return returnMass;
  603. }
  604. #endregion
  605. public void setMass()
  606. {
  607. if (Body != (IntPtr) 0)
  608. {
  609. float newmass = CalculateMass();
  610. //m_log.Info("[PHYSICS]: New Mass: " + newmass.ToString());
  611. d.MassSetBoxTotal(out pMass, newmass, _size.X, _size.Y, _size.Z);
  612. d.BodySetMass(Body, ref pMass);
  613. }
  614. }
  615. public void disableBody()
  616. {
  617. //this kills the body so things like 'mesh' can re-create it.
  618. lock (this)
  619. {
  620. if (!childPrim)
  621. {
  622. if (Body != IntPtr.Zero)
  623. {
  624. _parent_scene.remActivePrim(this);
  625. m_vehicle.Destroy();
  626. m_collisionCategories &= ~CollisionCategories.Body;
  627. m_collisionFlags &= ~(CollisionCategories.Wind | CollisionCategories.Land);
  628. if (prim_geom != IntPtr.Zero)
  629. {
  630. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  631. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  632. }
  633. d.BodyDestroy(Body);
  634. lock (childrenPrim)
  635. {
  636. if (childrenPrim.Count > 0)
  637. {
  638. foreach (OdePrim prm in childrenPrim)
  639. {
  640. _parent_scene.remActivePrim(prm);
  641. prm.Body = IntPtr.Zero;
  642. }
  643. }
  644. }
  645. Body = IntPtr.Zero;
  646. }
  647. }
  648. else
  649. {
  650. _parent_scene.remActivePrim(this);
  651. m_collisionCategories &= ~CollisionCategories.Body;
  652. m_collisionFlags &= ~(CollisionCategories.Wind | CollisionCategories.Land);
  653. if (prim_geom != IntPtr.Zero)
  654. {
  655. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  656. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  657. }
  658. Body = IntPtr.Zero;
  659. }
  660. }
  661. m_disabled = true;
  662. m_collisionscore = 0;
  663. }
  664. public void setMesh(OdeScene parent_scene, IMesh mesh)
  665. {
  666. // This sleeper is there to moderate how long it takes between
  667. // setting up the mesh and pre-processing it when we get rapid fire mesh requests on a single object
  668. Thread.Sleep(10);
  669. //Kill Body so that mesh can re-make the geom
  670. if (IsPhysical && Body != IntPtr.Zero)
  671. {
  672. if (childPrim)
  673. {
  674. if (_parent != null)
  675. {
  676. OdePrim parent = (OdePrim)_parent;
  677. parent.ChildDelink(this);
  678. }
  679. }
  680. else
  681. {
  682. disableBody();
  683. }
  684. }
  685. float[] vertexList = mesh.getVertexListAsFloatLocked(); // Note, that vertextList is pinned in memory
  686. int[] indexList = mesh.getIndexListAsIntLocked(); // Also pinned, needs release after usage
  687. mesh.releaseSourceMeshData(); // free up the original mesh data to save memory
  688. int VertexCount = vertexList.GetLength(0)/3;
  689. int IndexCount = indexList.GetLength(0);
  690. _triMeshData = d.GeomTriMeshDataCreate();
  691. d.GeomTriMeshDataBuildSimple(_triMeshData, vertexList, 3*sizeof (float), VertexCount, indexList, IndexCount,
  692. 3*sizeof (int));
  693. d.GeomTriMeshDataPreprocess(_triMeshData);
  694. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  695. try
  696. {
  697. if (prim_geom == IntPtr.Zero)
  698. {
  699. SetGeom(d.CreateTriMesh(m_targetSpace, _triMeshData, parent_scene.triCallback, null, null));
  700. }
  701. }
  702. catch (AccessViolationException)
  703. {
  704. m_log.Error("[PHYSICS]: MESH LOCKED");
  705. return;
  706. }
  707. // if (IsPhysical && Body == (IntPtr) 0)
  708. // {
  709. // Recreate the body
  710. // m_interpenetrationcount = 0;
  711. // m_collisionscore = 0;
  712. // enableBody();
  713. // }
  714. }
  715. public void ProcessTaints(float timestep)
  716. {
  717. if (m_taintadd)
  718. {
  719. changeadd(timestep);
  720. }
  721. if (prim_geom != IntPtr.Zero)
  722. {
  723. if (!_position.IsIdentical(m_taintposition,0f))
  724. changemove(timestep);
  725. if (m_taintrot != _orientation)
  726. rotate(timestep);
  727. //
  728. if (m_taintPhysics != m_isphysical && !(m_taintparent != _parent))
  729. changePhysicsStatus(timestep);
  730. //
  731. if (!_size.IsIdentical(m_taintsize,0))
  732. changesize(timestep);
  733. //
  734. if (m_taintshape)
  735. changeshape(timestep);
  736. //
  737. if (m_taintforce)
  738. changeAddForce(timestep);
  739. if (m_taintaddangularforce)
  740. changeAddAngularForce(timestep);
  741. if (!m_taintTorque.IsIdentical(PhysicsVector.Zero, 0.001f))
  742. changeSetTorque(timestep);
  743. if (m_taintdisable)
  744. changedisable(timestep);
  745. if (m_taintselected != m_isSelected)
  746. changeSelectedStatus(timestep);
  747. if (!m_taintVelocity.IsIdentical(PhysicsVector.Zero, 0.001f))
  748. changevelocity(timestep);
  749. if (m_taintparent != _parent)
  750. changelink(timestep);
  751. if (m_taintCollidesWater != m_collidesWater)
  752. changefloatonwater(timestep);
  753. if (!m_angularlock.IsIdentical(m_taintAngularLock,0))
  754. changeAngularLock(timestep);
  755. }
  756. else
  757. {
  758. m_log.Error("[PHYSICS]: The scene reused a disposed PhysActor! *waves finger*, Don't be evil. A couple of things can cause this. An improper prim breakdown(be sure to set prim_geom to zero after d.GeomDestroy! An improper buildup (creating the geom failed). Or, the Scene Reused a physics actor after disposing it.)");
  759. }
  760. }
  761. private void changeAngularLock(float timestep)
  762. {
  763. // do we have a Physical object?
  764. if (Body != IntPtr.Zero)
  765. {
  766. //Check that we have a Parent
  767. //If we have a parent then we're not authorative here
  768. if (_parent == null)
  769. {
  770. if (!m_taintAngularLock.IsIdentical(new PhysicsVector(1f,1f,1f), 0))
  771. {
  772. //d.BodySetFiniteRotationMode(Body, 0);
  773. //d.BodySetFiniteRotationAxis(Body,m_taintAngularLock.X,m_taintAngularLock.Y,m_taintAngularLock.Z);
  774. createAMotor(m_taintAngularLock);
  775. }
  776. else
  777. {
  778. if (Amotor != IntPtr.Zero)
  779. {
  780. d.JointDestroy(Amotor);
  781. Amotor = IntPtr.Zero;
  782. }
  783. }
  784. if (m_vehicle.Type != Vehicle.TYPE_NONE)
  785. {
  786. m_vehicle.Reset();
  787. }
  788. }
  789. }
  790. // Store this for later in case we get turned into a separate body
  791. m_angularlock = new PhysicsVector(m_taintAngularLock.X, m_taintAngularLock.Y, m_taintAngularLock.Z);
  792. }
  793. private void changelink(float timestep)
  794. {
  795. // If the newly set parent is not null
  796. // create link
  797. if (_parent == null && m_taintparent != null)
  798. {
  799. if (m_taintparent.PhysicsActorType == (int)ActorTypes.Prim)
  800. {
  801. OdePrim obj = (OdePrim)m_taintparent;
  802. //obj.disableBody();
  803. obj.ParentPrim(this);
  804. /*
  805. if (obj.Body != (IntPtr)0 && Body != (IntPtr)0 && obj.Body != Body)
  806. {
  807. _linkJointGroup = d.JointGroupCreate(0);
  808. m_linkJoint = d.JointCreateFixed(_parent_scene.world, _linkJointGroup);
  809. d.JointAttach(m_linkJoint, obj.Body, Body);
  810. d.JointSetFixed(m_linkJoint);
  811. }
  812. */
  813. }
  814. }
  815. // If the newly set parent is null
  816. // destroy link
  817. else if (_parent != null && m_taintparent == null)
  818. {
  819. if (_parent is OdePrim)
  820. {
  821. OdePrim obj = (OdePrim)_parent;
  822. obj.ChildDelink(this);
  823. childPrim = false;
  824. //_parent = null;
  825. }
  826. /*
  827. if (Body != (IntPtr)0 && _linkJointGroup != (IntPtr)0)
  828. d.JointGroupDestroy(_linkJointGroup);
  829. _linkJointGroup = (IntPtr)0;
  830. m_linkJoint = (IntPtr)0;
  831. */
  832. }
  833. _parent = m_taintparent;
  834. m_taintPhysics = m_isphysical;
  835. }
  836. // I'm the parent
  837. // prim is the child
  838. public void ParentPrim(OdePrim prim)
  839. {
  840. if (this.m_localID != prim.m_localID)
  841. {
  842. if (Body == IntPtr.Zero)
  843. {
  844. Body = d.BodyCreate(_parent_scene.world);
  845. setMass();
  846. }
  847. if (Body != IntPtr.Zero)
  848. {
  849. lock (childrenPrim)
  850. {
  851. if (!childrenPrim.Contains(prim))
  852. {
  853. childrenPrim.Add(prim);
  854. foreach (OdePrim prm in childrenPrim)
  855. {
  856. d.Mass m2;
  857. d.MassSetZero(out m2);
  858. d.MassSetBoxTotal(out m2, prim.CalculateMass(), prm._size.X, prm._size.Y, prm._size.Z);
  859. d.Quaternion quat = new d.Quaternion();
  860. quat.W = prm._orientation.W;
  861. quat.X = prm._orientation.X;
  862. quat.Y = prm._orientation.Y;
  863. quat.Z = prm._orientation.Z;
  864. d.Matrix3 mat = new d.Matrix3();
  865. d.RfromQ(out mat, ref quat);
  866. d.MassRotate(ref m2, ref mat);
  867. d.MassTranslate(ref m2, Position.X - prm.Position.X, Position.Y - prm.Position.Y, Position.Z - prm.Position.Z);
  868. d.MassAdd(ref pMass, ref m2);
  869. }
  870. foreach (OdePrim prm in childrenPrim)
  871. {
  872. prm.m_collisionCategories |= CollisionCategories.Body;
  873. prm.m_collisionFlags |= (CollisionCategories.Land | CollisionCategories.Wind);
  874. if (prm.prim_geom == IntPtr.Zero)
  875. {
  876. m_log.Warn("[PHYSICS]: Unable to link one of the linkset elements. No geom yet");
  877. continue;
  878. }
  879. d.GeomSetCategoryBits(prm.prim_geom, (int)prm.m_collisionCategories);
  880. d.GeomSetCollideBits(prm.prim_geom, (int)prm.m_collisionFlags);
  881. d.Quaternion quat = new d.Quaternion();
  882. quat.W = prm._orientation.W;
  883. quat.X = prm._orientation.X;
  884. quat.Y = prm._orientation.Y;
  885. quat.Z = prm._orientation.Z;
  886. d.Matrix3 mat = new d.Matrix3();
  887. d.RfromQ(out mat, ref quat);
  888. if (Body != IntPtr.Zero)
  889. {
  890. d.GeomSetBody(prm.prim_geom, Body);
  891. prm.childPrim = true;
  892. d.GeomSetOffsetWorldPosition(prm.prim_geom, prm.Position.X , prm.Position.Y, prm.Position.Z);
  893. //d.GeomSetOffsetPosition(prim.prim_geom,
  894. // (Position.X - prm.Position.X) - pMass.c.X,
  895. // (Position.Y - prm.Position.Y) - pMass.c.Y,
  896. // (Position.Z - prm.Position.Z) - pMass.c.Z);
  897. d.GeomSetOffsetWorldRotation(prm.prim_geom, ref mat);
  898. //d.GeomSetOffsetRotation(prm.prim_geom, ref mat);
  899. d.MassTranslate(ref pMass, -pMass.c.X, -pMass.c.Y, -pMass.c.Z);
  900. d.BodySetMass(Body, ref pMass);
  901. }
  902. else
  903. {
  904. m_log.Debug("[PHYSICS]:I ain't got no boooooooooddy, no body");
  905. }
  906. prm.m_interpenetrationcount = 0;
  907. prm.m_collisionscore = 0;
  908. prm.m_disabled = false;
  909. // The body doesn't already have a finite rotation mode set here
  910. if ((!m_angularlock.IsIdentical(PhysicsVector.Zero, 0)) && _parent == null)
  911. {
  912. prm.createAMotor(m_angularlock);
  913. }
  914. prm.Body = Body;
  915. _parent_scene.addActivePrim(prm);
  916. }
  917. m_collisionCategories |= CollisionCategories.Body;
  918. m_collisionFlags |= (CollisionCategories.Land | CollisionCategories.Wind);
  919. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  920. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  921. d.Quaternion quat2 = new d.Quaternion();
  922. quat2.W = _orientation.W;
  923. quat2.X = _orientation.X;
  924. quat2.Y = _orientation.Y;
  925. quat2.Z = _orientation.Z;
  926. d.Matrix3 mat2 = new d.Matrix3();
  927. d.RfromQ(out mat2, ref quat2);
  928. d.GeomSetBody(prim_geom, Body);
  929. d.GeomSetOffsetWorldPosition(prim_geom, Position.X - pMass.c.X, Position.Y - pMass.c.Y, Position.Z - pMass.c.Z);
  930. //d.GeomSetOffsetPosition(prim.prim_geom,
  931. // (Position.X - prm.Position.X) - pMass.c.X,
  932. // (Position.Y - prm.Position.Y) - pMass.c.Y,
  933. // (Position.Z - prm.Position.Z) - pMass.c.Z);
  934. //d.GeomSetOffsetRotation(prim_geom, ref mat2);
  935. d.MassTranslate(ref pMass, -pMass.c.X, -pMass.c.Y, -pMass.c.Z);
  936. d.BodySetMass(Body, ref pMass);
  937. d.BodySetAutoDisableFlag(Body, true);
  938. d.BodySetAutoDisableSteps(Body, body_autodisable_frames);
  939. m_interpenetrationcount = 0;
  940. m_collisionscore = 0;
  941. m_disabled = false;
  942. // The body doesn't already have a finite rotation mode set here
  943. if ((!m_angularlock.IsIdentical(PhysicsVector.Zero, 0)) && _parent == null)
  944. {
  945. createAMotor(m_angularlock);
  946. }
  947. d.BodySetPosition(Body, Position.X, Position.Y, Position.Z);
  948. m_vehicle.Enable(Body, _parent_scene);
  949. _parent_scene.addActivePrim(this);
  950. }
  951. }
  952. }
  953. }
  954. }
  955. private void ChildSetGeom(OdePrim odePrim)
  956. {
  957. //if (m_isphysical && Body != IntPtr.Zero)
  958. lock (childrenPrim)
  959. {
  960. foreach (OdePrim prm in childrenPrim)
  961. {
  962. //prm.childPrim = true;
  963. prm.disableBody();
  964. //prm.m_taintparent = null;
  965. //prm._parent = null;
  966. //prm.m_taintPhysics = false;
  967. //prm.m_disabled = true;
  968. //prm.childPrim = false;
  969. }
  970. }
  971. disableBody();
  972. if (Body != IntPtr.Zero)
  973. {
  974. _parent_scene.remActivePrim(this);
  975. }
  976. lock (childrenPrim)
  977. {
  978. foreach (OdePrim prm in childrenPrim)
  979. {
  980. ParentPrim(prm);
  981. }
  982. }
  983. }
  984. private void ChildDelink(OdePrim odePrim)
  985. {
  986. // Okay, we have a delinked child.. need to rebuild the body.
  987. lock (childrenPrim)
  988. {
  989. foreach (OdePrim prm in childrenPrim)
  990. {
  991. prm.childPrim = true;
  992. prm.disableBody();
  993. //prm.m_taintparent = null;
  994. //prm._parent = null;
  995. //prm.m_taintPhysics = false;
  996. //prm.m_disabled = true;
  997. //prm.childPrim = false;
  998. }
  999. }
  1000. disableBody();
  1001. lock (childrenPrim)
  1002. {
  1003. childrenPrim.Remove(odePrim);
  1004. }
  1005. if (Body != IntPtr.Zero)
  1006. {
  1007. _parent_scene.remActivePrim(this);
  1008. }
  1009. lock (childrenPrim)
  1010. {
  1011. foreach (OdePrim prm in childrenPrim)
  1012. {
  1013. ParentPrim(prm);
  1014. }
  1015. }
  1016. }
  1017. private void changeSelectedStatus(float timestep)
  1018. {
  1019. if (m_taintselected)
  1020. {
  1021. m_collisionCategories = CollisionCategories.Selected;
  1022. m_collisionFlags = (CollisionCategories.Sensor | CollisionCategories.Space);
  1023. // We do the body disable soft twice because 'in theory' a collision could have happened
  1024. // in between the disabling and the collision properties setting
  1025. // which would wake the physical body up from a soft disabling and potentially cause it to fall
  1026. // through the ground.
  1027. // NOTE FOR JOINTS: this doesn't always work for jointed assemblies because if you select
  1028. // just one part of the assembly, the rest of the assembly is non-selected and still simulating,
  1029. // so that causes the selected part to wake up and continue moving.
  1030. // even if you select all parts of a jointed assembly, it is not guaranteed that the entire
  1031. // assembly will stop simulating during the selection, because of the lack of atomicity
  1032. // of select operations (their processing could be interrupted by a thread switch, causing
  1033. // simulation to continue before all of the selected object notifications trickle down to
  1034. // the physics engine).
  1035. // e.g. we select 100 prims that are connected by joints. non-atomically, the first 50 are
  1036. // selected and disabled. then, due to a thread switch, the selection processing is
  1037. // interrupted and the physics engine continues to simulate, so the last 50 items, whose
  1038. // selection was not yet processed, continues to simulate. this wakes up ALL of the
  1039. // first 50 again. then the last 50 are disabled. then the first 50, which were just woken
  1040. // up, start simulating again, which in turn wakes up the last 50.
  1041. if (m_isphysical)
  1042. {
  1043. disableBodySoft();
  1044. }
  1045. if (prim_geom != IntPtr.Zero)
  1046. {
  1047. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  1048. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  1049. }
  1050. if (m_isphysical)
  1051. {
  1052. disableBodySoft();
  1053. }
  1054. }
  1055. else
  1056. {
  1057. m_collisionCategories = CollisionCategories.Geom;
  1058. if (m_isphysical)
  1059. m_collisionCategories |= CollisionCategories.Body;
  1060. m_collisionFlags = m_default_collisionFlags;
  1061. if (m_collidesLand)
  1062. m_collisionFlags |= CollisionCategories.Land;
  1063. if (m_collidesWater)
  1064. m_collisionFlags |= CollisionCategories.Water;
  1065. if (prim_geom != IntPtr.Zero)
  1066. {
  1067. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  1068. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  1069. }
  1070. if (m_isphysical)
  1071. {
  1072. if (Body != IntPtr.Zero)
  1073. {
  1074. d.BodySetLinearVel(Body, 0f, 0f, 0f);
  1075. d.BodySetForce(Body, 0, 0, 0);
  1076. enableBodySoft();
  1077. }
  1078. }
  1079. }
  1080. resetCollisionAccounting();
  1081. m_isSelected = m_taintselected;
  1082. }
  1083. public void ResetTaints()
  1084. {
  1085. m_taintposition = _position;
  1086. m_taintrot = _orientation;
  1087. m_taintPhysics = m_isphysical;
  1088. m_taintselected = m_isSelected;
  1089. m_taintsize = _size;
  1090. m_taintshape = false;
  1091. m_taintforce = false;
  1092. m_taintdisable = false;
  1093. m_taintVelocity = PhysicsVector.Zero;
  1094. }
  1095. public void CreateGeom(IntPtr m_targetSpace, IMesh _mesh)
  1096. {
  1097. if (_mesh != null)
  1098. {
  1099. setMesh(_parent_scene, _mesh);
  1100. }
  1101. else
  1102. {
  1103. if (_pbs.ProfileShape == ProfileShape.HalfCircle && _pbs.PathCurve == (byte)Extrusion.Curve1)
  1104. {
  1105. if (_size.X == _size.Y && _size.Y == _size.Z && _size.X == _size.Z)
  1106. {
  1107. if (((_size.X / 2f) > 0f))
  1108. {
  1109. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1110. try
  1111. {
  1112. SetGeom(d.CreateSphere(m_targetSpace, _size.X / 2));
  1113. }
  1114. catch (AccessViolationException)
  1115. {
  1116. m_log.Warn("[PHYSICS]: Unable to create physics proxy for object");
  1117. ode.dunlock(_parent_scene.world);
  1118. return;
  1119. }
  1120. }
  1121. else
  1122. {
  1123. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1124. try
  1125. {
  1126. SetGeom(d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z));
  1127. }
  1128. catch (AccessViolationException)
  1129. {
  1130. m_log.Warn("[PHYSICS]: Unable to create physics proxy for object");
  1131. ode.dunlock(_parent_scene.world);
  1132. return;
  1133. }
  1134. }
  1135. }
  1136. else
  1137. {
  1138. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1139. try
  1140. {
  1141. SetGeom(d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z));
  1142. }
  1143. catch (AccessViolationException)
  1144. {
  1145. m_log.Warn("[PHYSICS]: Unable to create physics proxy for object");
  1146. ode.dunlock(_parent_scene.world);
  1147. return;
  1148. }
  1149. }
  1150. }
  1151. else
  1152. {
  1153. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1154. try
  1155. {
  1156. SetGeom(d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z));
  1157. }
  1158. catch (AccessViolationException)
  1159. {
  1160. m_log.Warn("[PHYSICS]: Unable to create physics proxy for object");
  1161. ode.dunlock(_parent_scene.world);
  1162. return;
  1163. }
  1164. }
  1165. }
  1166. }
  1167. public void changeadd(float timestep)
  1168. {
  1169. int[] iprimspaceArrItem = _parent_scene.calculateSpaceArrayItemFromPos(_position);
  1170. IntPtr targetspace = _parent_scene.calculateSpaceForGeom(_position);
  1171. if (targetspace == IntPtr.Zero)
  1172. targetspace = _parent_scene.createprimspace(iprimspaceArrItem[0], iprimspaceArrItem[1]);
  1173. m_targetSpace = targetspace;
  1174. if (_mesh == null)
  1175. {
  1176. if (_parent_scene.needsMeshing(_pbs))
  1177. {
  1178. // Don't need to re-enable body.. it's done in SetMesh
  1179. _mesh = _parent_scene.mesher.CreateMesh(m_primName, _pbs, _size, _parent_scene.meshSculptLOD, IsPhysical);
  1180. // createmesh returns null when it's a shape that isn't a cube.
  1181. // m_log.Debug(m_localID);
  1182. }
  1183. }
  1184. lock (_parent_scene.OdeLock)
  1185. {
  1186. CreateGeom(m_targetSpace, _mesh);
  1187. if (prim_geom != IntPtr.Zero)
  1188. {
  1189. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  1190. d.Quaternion myrot = new d.Quaternion();
  1191. myrot.X = _orientation.X;
  1192. myrot.Y = _orientation.Y;
  1193. myrot.Z = _orientation.Z;
  1194. myrot.W = _orientation.W;
  1195. d.GeomSetQuaternion(prim_geom, ref myrot);
  1196. }
  1197. if (m_isphysical && Body == IntPtr.Zero)
  1198. {
  1199. enableBody();
  1200. }
  1201. }
  1202. _parent_scene.geom_name_map[prim_geom] = this.m_primName;
  1203. _parent_scene.actor_name_map[prim_geom] = (PhysicsActor)this;
  1204. changeSelectedStatus(timestep);
  1205. m_taintadd = false;
  1206. }
  1207. public void changemove(float timestep)
  1208. {
  1209. if (m_isphysical)
  1210. {
  1211. if (!m_disabled && !m_taintremove && !childPrim)
  1212. {
  1213. if (Body == IntPtr.Zero)
  1214. enableBody();
  1215. //Prim auto disable after 20 frames,
  1216. //if you move it, re-enable the prim manually.
  1217. if (_parent != null)
  1218. {
  1219. if (m_linkJoint != IntPtr.Zero)
  1220. {
  1221. d.JointDestroy(m_linkJoint);
  1222. m_linkJoint = IntPtr.Zero;
  1223. }
  1224. }
  1225. if (Body != IntPtr.Zero)
  1226. {
  1227. d.BodySetPosition(Body, _position.X, _position.Y, _position.Z);
  1228. if (_parent != null)
  1229. {
  1230. OdePrim odParent = (OdePrim)_parent;
  1231. if (Body != (IntPtr)0 && odParent.Body != (IntPtr)0 && Body != odParent.Body)
  1232. {
  1233. m_linkJoint = d.JointCreateFixed(_parent_scene.world, _linkJointGroup);
  1234. d.JointAttach(m_linkJoint, Body, odParent.Body);
  1235. d.JointSetFixed(m_linkJoint);
  1236. }
  1237. }
  1238. d.BodyEnable(Body);
  1239. if (m_vehicle.Type != Vehicle.TYPE_NONE)
  1240. {
  1241. m_vehicle.Enable(Body, _parent_scene);
  1242. }
  1243. }
  1244. else
  1245. {
  1246. m_log.Warn("[PHYSICS]: Body Still null after enableBody(). This is a crash scenario.");
  1247. }
  1248. }
  1249. //else
  1250. // {
  1251. //m_log.Debug("[BUG]: race!");
  1252. //}
  1253. }
  1254. else
  1255. {
  1256. // string primScenAvatarIn = _parent_scene.whichspaceamIin(_position);
  1257. // int[] arrayitem = _parent_scene.calculateSpaceArrayItemFromPos(_position);
  1258. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1259. IntPtr tempspace = _parent_scene.recalculateSpaceForGeom(prim_geom, _position, m_targetSpace);
  1260. m_targetSpace = tempspace;
  1261. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1262. if (prim_geom != IntPtr.Zero)
  1263. {
  1264. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  1265. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1266. d.SpaceAdd(m_targetSpace, prim_geom);
  1267. }
  1268. }
  1269. changeSelectedStatus(timestep);
  1270. resetCollisionAccounting();
  1271. m_taintposition = _position;
  1272. }
  1273. public void Move(float timestep)
  1274. {
  1275. float fx = 0;
  1276. float fy = 0;
  1277. float fz = 0;
  1278. if (IsPhysical && Body != IntPtr.Zero && !m_isSelected)
  1279. {
  1280. if (d.BodyIsEnabled(Body) && !m_angularlock.IsIdentical(PhysicsVector.Zero, 0.003f))
  1281. {
  1282. d.Vector3 avel2 = d.BodyGetAngularVel(Body);
  1283. if (m_angularlock.X == 1)
  1284. avel2.X = 0;
  1285. if (m_angularlock.Y == 1)
  1286. avel2.Y = 0;
  1287. if (m_angularlock.Z == 1)
  1288. avel2.Z = 0;
  1289. d.BodySetAngularVel(Body, avel2.X, avel2.Y, avel2.Z);
  1290. }
  1291. //float PID_P = 900.0f;
  1292. float m_mass = CalculateMass();
  1293. fz = 0f;
  1294. //m_log.Info(m_collisionFlags.ToString());
  1295. if (m_buoyancy != 0)
  1296. {
  1297. if (m_buoyancy > 0)
  1298. {
  1299. fz = (((-1 * _parent_scene.gravityz) * m_buoyancy) * m_mass);
  1300. //d.Vector3 l_velocity = d.BodyGetLinearVel(Body);
  1301. //m_log.Info("Using Buoyancy: " + buoyancy + " G: " + (_parent_scene.gravityz * m_buoyancy) + "mass:" + m_mass + " Pos: " + Position.ToString());
  1302. }
  1303. else
  1304. {
  1305. fz = (-1 * (((-1 * _parent_scene.gravityz) * (-1 * m_buoyancy)) * m_mass));
  1306. }
  1307. }
  1308. if (m_usePID)
  1309. {
  1310. //if (!d.BodyIsEnabled(Body))
  1311. //d.BodySetForce(Body, 0f, 0f, 0f);
  1312. // If we're using the PID controller, then we have no gravity
  1313. fz = (-1 * _parent_scene.gravityz) * m_mass;
  1314. // no lock; for now it's only called from within Simulate()
  1315. // If the PID Controller isn't active then we set our force
  1316. // calculating base velocity to the current position
  1317. if ((m_PIDTau < 1) && (m_PIDTau != 0))
  1318. {
  1319. //PID_G = PID_G / m_PIDTau;
  1320. m_PIDTau = 1;
  1321. }
  1322. if ((PID_G - m_PIDTau) <= 0)
  1323. {
  1324. PID_G = m_PIDTau + 1;
  1325. }
  1326. //PidStatus = true;
  1327. // PhysicsVector vec = new PhysicsVector();
  1328. d.Vector3 vel = d.BodyGetLinearVel(Body);
  1329. d.Vector3 pos = d.BodyGetPosition(Body);
  1330. _target_velocity =
  1331. new PhysicsVector(
  1332. (m_PIDTarget.X - pos.X) * ((PID_G - m_PIDTau) * timestep),
  1333. (m_PIDTarget.Y - pos.Y) * ((PID_G - m_PIDTau) * timestep),
  1334. (m_PIDTarget.Z - pos.Z) * ((PID_G - m_PIDTau) * timestep)
  1335. );
  1336. // if velocity is zero, use position control; otherwise, velocity control
  1337. if (_target_velocity.IsIdentical(PhysicsVector.Zero,0.1f))
  1338. {
  1339. // keep track of where we stopped. No more slippin' & slidin'
  1340. // We only want to deactivate the PID Controller if we think we want to have our surrogate
  1341. // react to the physics scene by moving it's position.
  1342. // Avatar to Avatar collisions
  1343. // Prim to avatar collisions
  1344. //fx = (_target_velocity.X - vel.X) * (PID_D) + (_zeroPosition.X - pos.X) * (PID_P * 2);
  1345. //fy = (_target_velocity.Y - vel.Y) * (PID_D) + (_zeroPosition.Y - pos.Y) * (PID_P * 2);
  1346. //fz = fz + (_target_velocity.Z - vel.Z) * (PID_D) + (_zeroPosition.Z - pos.Z) * PID_P;
  1347. d.BodySetPosition(Body, m_PIDTarget.X, m_PIDTarget.Y, m_PIDTarget.Z);
  1348. d.BodySetLinearVel(Body, 0, 0, 0);
  1349. d.BodyAddForce(Body, 0, 0, fz);
  1350. return;
  1351. }
  1352. else
  1353. {
  1354. _zeroFlag = false;
  1355. // We're flying and colliding with something
  1356. fx = ((_target_velocity.X) - vel.X) * (PID_D);
  1357. fy = ((_target_velocity.Y) - vel.Y) * (PID_D);
  1358. // vec.Z = (_target_velocity.Z - vel.Z) * PID_D + (_zeroPosition.Z - pos.Z) * PID_P;
  1359. fz = fz + ((_target_velocity.Z - vel.Z) * (PID_D) * m_mass);
  1360. }
  1361. }
  1362. // Hover PID Controller needs to be mutually exlusive to MoveTo PID controller
  1363. if (m_useHoverPID && !m_usePID)
  1364. {
  1365. // If we're using the PID controller, then we have no gravity
  1366. fz = (-1 * _parent_scene.gravityz) * m_mass;
  1367. // no lock; for now it's only called from within Simulate()
  1368. // If the PID Controller isn't active then we set our force
  1369. // calculating base velocity to the current position
  1370. if ((m_PIDTau < 1))
  1371. {
  1372. PID_G = PID_G / m_PIDTau;
  1373. }
  1374. if ((PID_G - m_PIDTau) <= 0)
  1375. {
  1376. PID_G = m_PIDTau + 1;
  1377. }
  1378. // Where are we, and where are we headed?
  1379. d.Vector3 pos = d.BodyGetPosition(Body);
  1380. d.Vector3 vel = d.BodyGetLinearVel(Body);
  1381. // determine what our target height really is based on HoverType
  1382. switch (m_PIDHoverType)
  1383. {
  1384. case PIDHoverType.Absolute:
  1385. m_targetHoverHeight = m_PIDHoverHeight;
  1386. break;
  1387. case PIDHoverType.Ground:
  1388. m_groundHeight = _parent_scene.GetTerrainHeightAtXY(pos.X, pos.Y);
  1389. m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight;
  1390. break;
  1391. case PIDHoverType.GroundAndWater:
  1392. m_groundHeight = _parent_scene.GetTerrainHeightAtXY(pos.X, pos.Y);
  1393. m_waterHeight = _parent_scene.GetWaterLevel();
  1394. if (m_groundHeight > m_waterHeight)
  1395. {
  1396. m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight;
  1397. }
  1398. else
  1399. {
  1400. m_targetHoverHeight = m_waterHeight + m_PIDHoverHeight;
  1401. }
  1402. break;
  1403. case PIDHoverType.Water:
  1404. m_waterHeight = _parent_scene.GetWaterLevel();
  1405. m_targetHoverHeight = m_waterHeight + m_PIDHoverHeight;
  1406. break;
  1407. }
  1408. _target_velocity =
  1409. new PhysicsVector(0.0f, 0.0f,
  1410. (m_targetHoverHeight - pos.Z) * ((PID_G - m_PIDHoverTau) * timestep)
  1411. );
  1412. // if velocity is zero, use position control; otherwise, velocity control
  1413. if (_target_velocity.IsIdentical(PhysicsVector.Zero, 0.1f))
  1414. {
  1415. // keep track of where we stopped. No more slippin' & slidin'
  1416. // We only want to deactivate the PID Controller if we think we want to have our surrogate
  1417. // react to the physics scene by moving it's position.
  1418. // Avatar to Avatar collisions
  1419. // Prim to avatar collisions
  1420. d.BodySetPosition(Body, pos.X, pos.Y, m_targetHoverHeight);
  1421. d.BodySetLinearVel(Body, vel.X, vel.Y, 0);
  1422. d.BodyAddForce(Body, 0, 0, fz);
  1423. return;
  1424. }
  1425. else
  1426. {
  1427. _zeroFlag = false;
  1428. // We're flying and colliding with something
  1429. fz = fz + ((_target_velocity.Z - vel.Z) * (PID_D) * m_mass);
  1430. }
  1431. }
  1432. fx *= m_mass;
  1433. fy *= m_mass;
  1434. //fz *= m_mass;
  1435. fx += m_force.X;
  1436. fy += m_force.Y;
  1437. fz += m_force.Z;
  1438. //m_log.Info("[OBJPID]: X:" + fx.ToString() + " Y:" + fy.ToString() + " Z:" + fz.ToString());
  1439. if (fx != 0 || fy != 0 || fz != 0)
  1440. {
  1441. //m_taintdisable = true;
  1442. //base.RaiseOutOfBounds(Position);
  1443. //d.BodySetLinearVel(Body, fx, fy, 0f);
  1444. if (!d.BodyIsEnabled(Body))
  1445. {
  1446. d.BodySetLinearVel(Body, 0f, 0f, 0f);
  1447. d.BodySetForce(Body, 0, 0, 0);
  1448. enableBodySoft();
  1449. }
  1450. // 35x10 = 350n times the mass per second applied maximum.
  1451. float nmax = 35f * m_mass;
  1452. float nmin = -35f * m_mass;
  1453. if (fx > nmax)
  1454. fx = nmax;
  1455. if (fx < nmin)
  1456. fx = nmin;
  1457. if (fy > nmax)
  1458. fy = nmax;
  1459. if (fy < nmin)
  1460. fy = nmin;
  1461. d.BodyAddForce(Body, fx, fy, fz);
  1462. }
  1463. if (m_vehicle.Body == IntPtr.Zero && m_vehicle.Type != Vehicle.TYPE_NONE)
  1464. m_vehicle.Enable(Body, _parent_scene);
  1465. m_vehicle.Step(timestep);
  1466. }
  1467. else
  1468. {
  1469. // _zeroPosition = d.BodyGetPosition(Body);
  1470. return;
  1471. }
  1472. }
  1473. public void rotate(float timestep)
  1474. {
  1475. d.Quaternion myrot = new d.Quaternion();
  1476. myrot.X = _orientation.X;
  1477. myrot.Y = _orientation.Y;
  1478. myrot.Z = _orientation.Z;
  1479. myrot.W = _orientation.W;
  1480. d.GeomSetQuaternion(prim_geom, ref myrot);
  1481. if (m_isphysical && Body != IntPtr.Zero)
  1482. {
  1483. d.BodySetQuaternion(Body, ref myrot);
  1484. if (!m_angularlock.IsIdentical(new PhysicsVector(1, 1, 1), 0))
  1485. createAMotor(m_angularlock);
  1486. }
  1487. resetCollisionAccounting();
  1488. m_taintrot = _orientation;
  1489. }
  1490. private void resetCollisionAccounting()
  1491. {
  1492. m_collisionscore = 0;
  1493. m_interpenetrationcount = 0;
  1494. m_disabled = false;
  1495. }
  1496. public void changedisable(float timestep)
  1497. {
  1498. m_disabled = true;
  1499. if (Body != IntPtr.Zero)
  1500. {
  1501. d.BodyDisable(Body);
  1502. Body = IntPtr.Zero;
  1503. }
  1504. m_taintdisable = false;
  1505. }
  1506. public void changePhysicsStatus(float timestep)
  1507. {
  1508. if (m_isphysical == true)
  1509. {
  1510. if (Body == IntPtr.Zero)
  1511. {
  1512. if (_pbs.SculptEntry && _parent_scene.meshSculptedPrim)
  1513. {
  1514. changeshape(2f);
  1515. }
  1516. else
  1517. {
  1518. enableBody();
  1519. }
  1520. }
  1521. }
  1522. else
  1523. {
  1524. if (Body != IntPtr.Zero)
  1525. {
  1526. if (_pbs.SculptEntry && _parent_scene.meshSculptedPrim)
  1527. {
  1528. if (prim_geom != IntPtr.Zero)
  1529. {
  1530. try
  1531. {
  1532. d.GeomDestroy(prim_geom);
  1533. prim_geom = IntPtr.Zero;
  1534. _mesh = null;
  1535. }
  1536. catch (System.AccessViolationException)
  1537. {
  1538. prim_geom = IntPtr.Zero;
  1539. m_log.Error("[PHYSICS]: PrimGeom dead");
  1540. }
  1541. }
  1542. changeadd(2f);
  1543. }
  1544. if (childPrim)
  1545. {
  1546. if (_parent != null)
  1547. {
  1548. OdePrim parent = (OdePrim)_parent;
  1549. parent.ChildDelink(this);
  1550. }
  1551. }
  1552. else
  1553. {
  1554. disableBody();
  1555. }
  1556. }
  1557. }
  1558. changeSelectedStatus(timestep);
  1559. resetCollisionAccounting();
  1560. m_taintPhysics = m_isphysical;
  1561. }
  1562. public void changesize(float timestamp)
  1563. {
  1564. string oldname = _parent_scene.geom_name_map[prim_geom];
  1565. if (_size.X <= 0) _size.X = 0.01f;
  1566. if (_size.Y <= 0) _size.Y = 0.01f;
  1567. if (_size.Z <= 0) _size.Z = 0.01f;
  1568. // Cleanup of old prim geometry
  1569. if (_mesh != null)
  1570. {
  1571. // Cleanup meshing here
  1572. }
  1573. //kill body to rebuild
  1574. if (IsPhysical && Body != IntPtr.Zero)
  1575. {
  1576. if (childPrim)
  1577. {
  1578. if (_parent != null)
  1579. {
  1580. OdePrim parent = (OdePrim)_parent;
  1581. parent.ChildDelink(this);
  1582. }
  1583. }
  1584. else
  1585. {
  1586. disableBody();
  1587. }
  1588. }
  1589. if (d.SpaceQuery(m_targetSpace, prim_geom))
  1590. {
  1591. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1592. d.SpaceRemove(m_targetSpace, prim_geom);
  1593. }
  1594. d.GeomDestroy(prim_geom);
  1595. prim_geom = IntPtr.Zero;
  1596. // we don't need to do space calculation because the client sends a position update also.
  1597. // Construction of new prim
  1598. if (_parent_scene.needsMeshing(_pbs))
  1599. {
  1600. float meshlod = _parent_scene.meshSculptLOD;
  1601. if (IsPhysical)
  1602. meshlod = _parent_scene.MeshSculptphysicalLOD;
  1603. // Don't need to re-enable body.. it's done in SetMesh
  1604. IMesh mesh = null;
  1605. if (_parent_scene.needsMeshing(_pbs))
  1606. mesh = _parent_scene.mesher.CreateMesh(oldname, _pbs, _size, meshlod, IsPhysical);
  1607. //IMesh mesh = _parent_scene.mesher.CreateMesh(oldname, _pbs, _size, meshlod, IsPhysical);
  1608. CreateGeom(m_targetSpace, mesh);
  1609. }
  1610. else
  1611. {
  1612. _mesh = null;
  1613. CreateGeom(m_targetSpace, _mesh);
  1614. }
  1615. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  1616. d.Quaternion myrot = new d.Quaternion();
  1617. myrot.X = _orientation.X;
  1618. myrot.Y = _orientation.Y;
  1619. myrot.Z = _orientation.Z;
  1620. myrot.W = _orientation.W;
  1621. d.GeomSetQuaternion(prim_geom, ref myrot);
  1622. //d.GeomBoxSetLengths(prim_geom, _size.X, _size.Y, _size.Z);
  1623. if (IsPhysical && Body == IntPtr.Zero && !childPrim)
  1624. {
  1625. // Re creates body on size.
  1626. // EnableBody also does setMass()
  1627. enableBody();
  1628. d.BodyEnable(Body);
  1629. }
  1630. _parent_scene.geom_name_map[prim_geom] = oldname;
  1631. changeSelectedStatus(timestamp);
  1632. if (childPrim)
  1633. {
  1634. if (_parent is OdePrim)
  1635. {
  1636. OdePrim parent = (OdePrim)_parent;
  1637. parent.ChildSetGeom(this);
  1638. }
  1639. }
  1640. resetCollisionAccounting();
  1641. m_taintsize = _size;
  1642. }
  1643. public void changefloatonwater(float timestep)
  1644. {
  1645. m_collidesWater = m_taintCollidesWater;
  1646. if (prim_geom != IntPtr.Zero)
  1647. {
  1648. if (m_collidesWater)
  1649. {
  1650. m_collisionFlags |= CollisionCategories.Water;
  1651. }
  1652. else
  1653. {
  1654. m_collisionFlags &= ~CollisionCategories.Water;
  1655. }
  1656. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  1657. }
  1658. }
  1659. public void changeshape(float timestamp)
  1660. {
  1661. string oldname = _parent_scene.geom_name_map[prim_geom];
  1662. // Cleanup of old prim geometry and Bodies
  1663. if (IsPhysical && Body != IntPtr.Zero)
  1664. {
  1665. if (childPrim)
  1666. {
  1667. if (_parent != null)
  1668. {
  1669. OdePrim parent = (OdePrim)_parent;
  1670. parent.ChildDelink(this);
  1671. }
  1672. }
  1673. else
  1674. {
  1675. disableBody();
  1676. }
  1677. }
  1678. try
  1679. {
  1680. d.GeomDestroy(prim_geom);
  1681. }
  1682. catch (System.AccessViolationException)
  1683. {
  1684. prim_geom = IntPtr.Zero;
  1685. m_log.Error("[PHYSICS]: PrimGeom dead");
  1686. }
  1687. prim_geom = IntPtr.Zero;
  1688. // we don't need to do space calculation because the client sends a position update also.
  1689. if (_size.X <= 0) _size.X = 0.01f;
  1690. if (_size.Y <= 0) _size.Y = 0.01f;
  1691. if (_size.Z <= 0) _size.Z = 0.01f;
  1692. // Construction of new prim
  1693. if (_parent_scene.needsMeshing(_pbs))
  1694. {
  1695. // Don't need to re-enable body.. it's done in SetMesh
  1696. float meshlod = _parent_scene.meshSculptLOD;
  1697. if (IsPhysical)
  1698. meshlod = _parent_scene.MeshSculptphysicalLOD;
  1699. IMesh mesh = _parent_scene.mesher.CreateMesh(oldname, _pbs, _size, meshlod, IsPhysical);
  1700. // createmesh returns null when it doesn't mesh.
  1701. CreateGeom(m_targetSpace, mesh);
  1702. }
  1703. else
  1704. {
  1705. _mesh = null;
  1706. CreateGeom(m_targetSpace, null);
  1707. }
  1708. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  1709. d.Quaternion myrot = new d.Quaternion();
  1710. //myrot.W = _orientation.w;
  1711. myrot.W = _orientation.W;
  1712. myrot.X = _orientation.X;
  1713. myrot.Y = _orientation.Y;
  1714. myrot.Z = _orientation.Z;
  1715. d.GeomSetQuaternion(prim_geom, ref myrot);
  1716. //d.GeomBoxSetLengths(prim_geom, _size.X, _size.Y, _size.Z);
  1717. if (IsPhysical && Body == IntPtr.Zero)
  1718. {
  1719. // Re creates body on size.
  1720. // EnableBody also does setMass()
  1721. enableBody();
  1722. d.BodyEnable(Body);
  1723. }
  1724. _parent_scene.geom_name_map[prim_geom] = oldname;
  1725. changeSelectedStatus(timestamp);
  1726. if (childPrim)
  1727. {
  1728. if (_parent is OdePrim)
  1729. {
  1730. OdePrim parent = (OdePrim)_parent;
  1731. parent.ChildSetGeom(this);
  1732. }
  1733. }
  1734. resetCollisionAccounting();
  1735. m_taintshape = false;
  1736. }
  1737. public void changeAddForce(float timestamp)
  1738. {
  1739. if (!m_isSelected)
  1740. {
  1741. lock (m_forcelist)
  1742. {
  1743. //m_log.Info("[PHYSICS]: dequeing forcelist");
  1744. if (IsPhysical)
  1745. {
  1746. PhysicsVector iforce = new PhysicsVector();
  1747. for (int i = 0; i < m_forcelist.Count; i++)
  1748. {
  1749. iforce = iforce + (m_forcelist[i] * 100);
  1750. }
  1751. d.BodyEnable(Body);
  1752. d.BodyAddForce(Body, iforce.X, iforce.Y, iforce.Z);
  1753. }
  1754. m_forcelist.Clear();
  1755. }
  1756. m_collisionscore = 0;
  1757. m_interpenetrationcount = 0;
  1758. }
  1759. m_taintforce = false;
  1760. }
  1761. public void changeSetTorque(float timestamp)
  1762. {
  1763. if (!m_isSelected)
  1764. {
  1765. if (IsPhysical && Body != IntPtr.Zero)
  1766. {
  1767. d.BodySetTorque(Body, m_taintTorque.X, m_taintTorque.Y, m_taintTorque.Z);
  1768. }
  1769. }
  1770. m_taintTorque = new PhysicsVector(0, 0, 0);
  1771. }
  1772. public void changeAddAngularForce(float timestamp)
  1773. {
  1774. if (!m_isSelected)
  1775. {
  1776. lock (m_angularforcelist)
  1777. {
  1778. //m_log.Info("[PHYSICS]: dequeing forcelist");
  1779. if (IsPhysical)
  1780. {
  1781. PhysicsVector iforce = new PhysicsVector();
  1782. for (int i = 0; i < m_angularforcelist.Count; i++)
  1783. {
  1784. iforce = iforce + (m_angularforcelist[i] * 100);
  1785. }
  1786. d.BodyEnable(Body);
  1787. d.BodyAddTorque(Body, iforce.X, iforce.Y, iforce.Z);
  1788. }
  1789. m_angularforcelist.Clear();
  1790. }
  1791. m_collisionscore = 0;
  1792. m_interpenetrationcount = 0;
  1793. }
  1794. m_taintaddangularforce = false;
  1795. }
  1796. private void changevelocity(float timestep)
  1797. {
  1798. if (!m_isSelected)
  1799. {
  1800. Thread.Sleep(20);
  1801. if (IsPhysical)
  1802. {
  1803. if (Body != IntPtr.Zero)
  1804. {
  1805. d.BodySetLinearVel(Body, m_taintVelocity.X, m_taintVelocity.Y, m_taintVelocity.Z);
  1806. }
  1807. }
  1808. //resetCollisionAccounting();
  1809. }
  1810. m_taintVelocity = PhysicsVector.Zero;
  1811. }
  1812. public override bool IsPhysical
  1813. {
  1814. get { return m_isphysical; }
  1815. set {
  1816. m_isphysical = value;
  1817. if (!m_isphysical) // Zero the remembered last velocity
  1818. m_lastVelocity = new PhysicsVector(0.0f, 0.0f, 0.0f);
  1819. }
  1820. }
  1821. public void setPrimForRemoval()
  1822. {
  1823. m_taintremove = true;
  1824. }
  1825. public override bool Flying
  1826. {
  1827. // no flying prims for you
  1828. get { return false; }
  1829. set { }
  1830. }
  1831. public override bool IsColliding
  1832. {
  1833. get { return iscolliding; }
  1834. set { iscolliding = value; }
  1835. }
  1836. public override bool CollidingGround
  1837. {
  1838. get { return false; }
  1839. set { return; }
  1840. }
  1841. public override bool CollidingObj
  1842. {
  1843. get { return false; }
  1844. set { return; }
  1845. }
  1846. public override bool ThrottleUpdates
  1847. {
  1848. get { return m_throttleUpdates; }
  1849. set { m_throttleUpdates = value; }
  1850. }
  1851. public override bool Stopped
  1852. {
  1853. get { return _zeroFlag; }
  1854. }
  1855. public override PhysicsVector Position
  1856. {
  1857. get { return _position; }
  1858. set { _position = value;
  1859. //m_log.Info("[PHYSICS]: " + _position.ToString());
  1860. }
  1861. }
  1862. public override PhysicsVector Size
  1863. {
  1864. get { return _size; }
  1865. set
  1866. {
  1867. if (PhysicsVector.isFinite(value))
  1868. {
  1869. _size = value;
  1870. }
  1871. else
  1872. {
  1873. m_log.Warn("[PHYSICS]: Got NaN Size on object");
  1874. }
  1875. }
  1876. }
  1877. public override float Mass
  1878. {
  1879. get { return CalculateMass(); }
  1880. }
  1881. public override PhysicsVector Force
  1882. {
  1883. //get { return PhysicsVector.Zero; }
  1884. get { return m_force; }
  1885. set
  1886. {
  1887. if (PhysicsVector.isFinite(value))
  1888. {
  1889. m_force = value;
  1890. }
  1891. else
  1892. {
  1893. m_log.Warn("[PHYSICS]: NaN in Force Applied to an Object");
  1894. }
  1895. }
  1896. }
  1897. public override int VehicleType
  1898. {
  1899. get { return (int)m_vehicle.Type; }
  1900. set { m_vehicle.ProcessTypeChange((Vehicle)value); }
  1901. }
  1902. public override void VehicleFloatParam(int param, float value)
  1903. {
  1904. m_vehicle.ProcessFloatVehicleParam((Vehicle) param, value);
  1905. }
  1906. public override void VehicleVectorParam(int param, PhysicsVector value)
  1907. {
  1908. m_vehicle.ProcessVectorVehicleParam((Vehicle) param, value);
  1909. }
  1910. public override void VehicleRotationParam(int param, Quaternion rotation)
  1911. {
  1912. m_vehicle.ProcessRotationVehicleParam((Vehicle) param, rotation);
  1913. }
  1914. public override void SetVolumeDetect(int param)
  1915. {
  1916. lock (_parent_scene.OdeLock)
  1917. {
  1918. m_isVolumeDetect = (param!=0);
  1919. }
  1920. }
  1921. public override PhysicsVector CenterOfMass
  1922. {
  1923. get { return PhysicsVector.Zero; }
  1924. }
  1925. public override PhysicsVector GeometricCenter
  1926. {
  1927. get { return PhysicsVector.Zero; }
  1928. }
  1929. public override PrimitiveBaseShape Shape
  1930. {
  1931. set
  1932. {
  1933. _pbs = value;
  1934. m_taintshape = true;
  1935. }
  1936. }
  1937. public override PhysicsVector Velocity
  1938. {
  1939. get
  1940. {
  1941. // Averate previous velocity with the new one so
  1942. // client object interpolation works a 'little' better
  1943. PhysicsVector returnVelocity = new PhysicsVector();
  1944. returnVelocity.X = (m_lastVelocity.X + _velocity.X)/2;
  1945. returnVelocity.Y = (m_lastVelocity.Y + _velocity.Y)/2;
  1946. returnVelocity.Z = (m_lastVelocity.Z + _velocity.Z)/2;
  1947. return returnVelocity;
  1948. }
  1949. set
  1950. {
  1951. if (PhysicsVector.isFinite(value))
  1952. {
  1953. _velocity = value;
  1954. m_taintVelocity = value;
  1955. _parent_scene.AddPhysicsActorTaint(this);
  1956. }
  1957. else
  1958. {
  1959. m_log.Warn("[PHYSICS]: Got NaN Velocity in Object");
  1960. }
  1961. }
  1962. }
  1963. public override PhysicsVector Torque
  1964. {
  1965. get
  1966. {
  1967. if (!m_isphysical || Body == IntPtr.Zero)
  1968. return new PhysicsVector(0,0,0);
  1969. return _torque;
  1970. }
  1971. set
  1972. {
  1973. if (PhysicsVector.isFinite(value))
  1974. {
  1975. m_taintTorque = value;
  1976. _parent_scene.AddPhysicsActorTaint(this);
  1977. }
  1978. else
  1979. {
  1980. m_log.Warn("[PHYSICS]: Got NaN Torque in Object");
  1981. }
  1982. }
  1983. }
  1984. public override float CollisionScore
  1985. {
  1986. get { return m_collisionscore; }
  1987. set { m_collisionscore = value; }
  1988. }
  1989. public override bool Kinematic
  1990. {
  1991. get { return false; }
  1992. set { }
  1993. }
  1994. public override Quaternion Orientation
  1995. {
  1996. get { return _orientation; }
  1997. set
  1998. {
  1999. if (QuaternionIsFinite(value))
  2000. _orientation = value;
  2001. else
  2002. m_log.Warn("[PHYSICS]: Got NaN quaternion Orientation from Scene in Object");
  2003. }
  2004. }
  2005. internal static bool QuaternionIsFinite(Quaternion q)
  2006. {
  2007. if (Single.IsNaN(q.X) || Single.IsInfinity(q.X))
  2008. return false;
  2009. if (Single.IsNaN(q.Y) || Single.IsInfinity(q.Y))
  2010. return false;
  2011. if (Single.IsNaN(q.Z) || Single.IsInfinity(q.Z))
  2012. return false;
  2013. if (Single.IsNaN(q.W) || Single.IsInfinity(q.W))
  2014. return false;
  2015. return true;
  2016. }
  2017. public override PhysicsVector Acceleration
  2018. {
  2019. get { return _acceleration; }
  2020. }
  2021. public void SetAcceleration(PhysicsVector accel)
  2022. {
  2023. _acceleration = accel;
  2024. }
  2025. public override void AddForce(PhysicsVector force, bool pushforce)
  2026. {
  2027. if (PhysicsVector.isFinite(force))
  2028. {
  2029. m_forcelist.Add(force);
  2030. m_taintforce = true;
  2031. }
  2032. else
  2033. {
  2034. m_log.Warn("[PHYSICS]: Got Invalid linear force vector from Scene in Object");
  2035. }
  2036. //m_log.Info("[PHYSICS]: Added Force:" + force.ToString() + " to prim at " + Position.ToString());
  2037. }
  2038. public override void AddAngularForce(PhysicsVector force, bool pushforce)
  2039. {
  2040. if (PhysicsVector.isFinite(force))
  2041. {
  2042. m_angularforcelist.Add(force);
  2043. m_taintaddangularforce = true;
  2044. }
  2045. else
  2046. {
  2047. m_log.Warn("[PHYSICS]: Got Invalid Angular force vector from Scene in Object");
  2048. }
  2049. }
  2050. public override PhysicsVector RotationalVelocity
  2051. {
  2052. get
  2053. {
  2054. PhysicsVector pv = new PhysicsVector(0, 0, 0);
  2055. if (_zeroFlag)
  2056. return pv;
  2057. m_lastUpdateSent = false;
  2058. if (m_rotationalVelocity.IsIdentical(pv, 0.2f))
  2059. return pv;
  2060. return m_rotationalVelocity;
  2061. }
  2062. set
  2063. {
  2064. if (PhysicsVector.isFinite(value))
  2065. {
  2066. m_rotationalVelocity = value;
  2067. }
  2068. else
  2069. {
  2070. m_log.Warn("[PHYSICS]: Got NaN RotationalVelocity in Object");
  2071. }
  2072. }
  2073. }
  2074. public override void CrossingFailure()
  2075. {
  2076. m_crossingfailures++;
  2077. if (m_crossingfailures > _parent_scene.geomCrossingFailuresBeforeOutofbounds)
  2078. {
  2079. base.RaiseOutOfBounds(_position);
  2080. return;
  2081. }
  2082. else if (m_crossingfailures == _parent_scene.geomCrossingFailuresBeforeOutofbounds)
  2083. {
  2084. m_log.Warn("[PHYSICS]: Too many crossing failures for: " + m_primName);
  2085. }
  2086. }
  2087. public override float Buoyancy
  2088. {
  2089. get { return m_buoyancy; }
  2090. set { m_buoyancy = value; }
  2091. }
  2092. public override void link(PhysicsActor obj)
  2093. {
  2094. m_taintparent = obj;
  2095. }
  2096. public override void delink()
  2097. {
  2098. m_taintparent = null;
  2099. }
  2100. public override void LockAngularMotion(PhysicsVector axis)
  2101. {
  2102. // reverse the zero/non zero values for ODE.
  2103. if (PhysicsVector.isFinite(axis))
  2104. {
  2105. axis.X = (axis.X > 0) ? 1f : 0f;
  2106. axis.Y = (axis.Y > 0) ? 1f : 0f;
  2107. axis.Z = (axis.Z > 0) ? 1f : 0f;
  2108. m_log.DebugFormat("[axislock]: <{0},{1},{2}>", axis.X, axis.Y, axis.Z);
  2109. m_taintAngularLock = new PhysicsVector(axis.X, axis.Y, axis.Z);
  2110. }
  2111. else
  2112. {
  2113. m_log.Warn("[PHYSICS]: Got NaN locking axis from Scene on Object");
  2114. }
  2115. }
  2116. public void UpdatePositionAndVelocity()
  2117. {
  2118. // no lock; called from Simulate() -- if you call this from elsewhere, gotta lock or do Monitor.Enter/Exit!
  2119. if (_parent == null)
  2120. {
  2121. PhysicsVector pv = new PhysicsVector(0, 0, 0);
  2122. bool lastZeroFlag = _zeroFlag;
  2123. if (Body != (IntPtr)0) // FIXME -> or if it is a joint
  2124. {
  2125. d.Vector3 vec = d.BodyGetPosition(Body);
  2126. d.Quaternion ori = d.BodyGetQuaternion(Body);
  2127. d.Vector3 vel = d.BodyGetLinearVel(Body);
  2128. d.Vector3 rotvel = d.BodyGetAngularVel(Body);
  2129. d.Vector3 torque = d.BodyGetTorque(Body);
  2130. _torque.setValues(torque.X, torque.Y, torque.Z);
  2131. PhysicsVector l_position = new PhysicsVector();
  2132. Quaternion l_orientation = new Quaternion();
  2133. // kluge to keep things in bounds. ODE lets dead avatars drift away (they should be removed!)
  2134. //if (vec.X < 0.0f) { vec.X = 0.0f; if (Body != (IntPtr)0) d.BodySetAngularVel(Body, 0, 0, 0); }
  2135. //if (vec.Y < 0.0f) { vec.Y = 0.0f; if (Body != (IntPtr)0) d.BodySetAngularVel(Body, 0, 0, 0); }
  2136. //if (vec.X > 255.95f) { vec.X = 255.95f; if (Body != (IntPtr)0) d.BodySetAngularVel(Body, 0, 0, 0); }
  2137. //if (vec.Y > 255.95f) { vec.Y = 255.95f; if (Body != (IntPtr)0) d.BodySetAngularVel(Body, 0, 0, 0); }
  2138. m_lastposition = _position;
  2139. m_lastorientation = _orientation;
  2140. l_position.X = vec.X;
  2141. l_position.Y = vec.Y;
  2142. l_position.Z = vec.Z;
  2143. l_orientation.X = ori.X;
  2144. l_orientation.Y = ori.Y;
  2145. l_orientation.Z = ori.Z;
  2146. l_orientation.W = ori.W;
  2147. if (l_position.X > ((int)_parent_scene.WorldExtents.X - 0.05f) || l_position.X < 0f || l_position.Y > ((int)_parent_scene.WorldExtents.Y - 0.05f) || l_position.Y < 0f)
  2148. {
  2149. //base.RaiseOutOfBounds(l_position);
  2150. if (m_crossingfailures < _parent_scene.geomCrossingFailuresBeforeOutofbounds)
  2151. {
  2152. _position = l_position;
  2153. //_parent_scene.remActivePrim(this);
  2154. if (_parent == null)
  2155. base.RequestPhysicsterseUpdate();
  2156. return;
  2157. }
  2158. else
  2159. {
  2160. if (_parent == null)
  2161. base.RaiseOutOfBounds(l_position);
  2162. return;
  2163. }
  2164. }
  2165. if (l_position.Z < 0)
  2166. {
  2167. // This is so prim that get lost underground don't fall forever and suck up
  2168. //
  2169. // Sim resources and memory.
  2170. // Disables the prim's movement physics....
  2171. // It's a hack and will generate a console message if it fails.
  2172. //IsPhysical = false;
  2173. if (_parent == null)
  2174. base.RaiseOutOfBounds(_position);
  2175. _acceleration.X = 0;
  2176. _acceleration.Y = 0;
  2177. _acceleration.Z = 0;
  2178. _velocity.X = 0;
  2179. _velocity.Y = 0;
  2180. _velocity.Z = 0;
  2181. m_rotationalVelocity.X = 0;
  2182. m_rotationalVelocity.Y = 0;
  2183. m_rotationalVelocity.Z = 0;
  2184. if (_parent == null)
  2185. base.RequestPhysicsterseUpdate();
  2186. m_throttleUpdates = false;
  2187. throttleCounter = 0;
  2188. _zeroFlag = true;
  2189. //outofBounds = true;
  2190. }
  2191. if ((Math.Abs(m_lastposition.X - l_position.X) < 0.02)
  2192. && (Math.Abs(m_lastposition.Y - l_position.Y) < 0.02)
  2193. && (Math.Abs(m_lastposition.Z - l_position.Z) < 0.02)
  2194. && (1.0 - Math.Abs(Quaternion.Dot(m_lastorientation, l_orientation)) < 0.01))
  2195. {
  2196. _zeroFlag = true;
  2197. m_throttleUpdates = false;
  2198. }
  2199. else
  2200. {
  2201. //m_log.Debug(Math.Abs(m_lastposition.X - l_position.X).ToString());
  2202. _zeroFlag = false;
  2203. }
  2204. if (_zeroFlag)
  2205. {
  2206. _velocity.X = 0.0f;
  2207. _velocity.Y = 0.0f;
  2208. _velocity.Z = 0.0f;
  2209. _acceleration.X = 0;
  2210. _acceleration.Y = 0;
  2211. _acceleration.Z = 0;
  2212. //_orientation.w = 0f;
  2213. //_orientation.X = 0f;
  2214. //_orientation.Y = 0f;
  2215. //_orientation.Z = 0f;
  2216. m_rotationalVelocity.X = 0;
  2217. m_rotationalVelocity.Y = 0;
  2218. m_rotationalVelocity.Z = 0;
  2219. if (!m_lastUpdateSent)
  2220. {
  2221. m_throttleUpdates = false;
  2222. throttleCounter = 0;
  2223. m_rotationalVelocity = pv;
  2224. if (_parent == null)
  2225. base.RequestPhysicsterseUpdate();
  2226. m_lastUpdateSent = true;
  2227. }
  2228. }
  2229. else
  2230. {
  2231. if (lastZeroFlag != _zeroFlag)
  2232. {
  2233. if (_parent == null)
  2234. base.RequestPhysicsterseUpdate();
  2235. }
  2236. m_lastVelocity = _velocity;
  2237. _position = l_position;
  2238. _velocity.X = vel.X;
  2239. _velocity.Y = vel.Y;
  2240. _velocity.Z = vel.Z;
  2241. _acceleration = ((_velocity - m_lastVelocity) / 0.1f);
  2242. _acceleration = new PhysicsVector(_velocity.X - m_lastVelocity.X / 0.1f, _velocity.Y - m_lastVelocity.Y / 0.1f, _velocity.Z - m_lastVelocity.Z / 0.1f);
  2243. //m_log.Info("[PHYSICS]: V1: " + _velocity + " V2: " + m_lastVelocity + " Acceleration: " + _acceleration.ToString());
  2244. if (_velocity.IsIdentical(pv, 0.5f))
  2245. {
  2246. m_rotationalVelocity = pv;
  2247. }
  2248. else
  2249. {
  2250. m_rotationalVelocity.setValues(rotvel.X, rotvel.Y, rotvel.Z);
  2251. }
  2252. //m_log.Debug("ODE: " + m_rotationalVelocity.ToString());
  2253. _orientation.X = ori.X;
  2254. _orientation.Y = ori.Y;
  2255. _orientation.Z = ori.Z;
  2256. _orientation.W = ori.W;
  2257. m_lastUpdateSent = false;
  2258. if (!m_throttleUpdates || throttleCounter > _parent_scene.geomUpdatesPerThrottledUpdate)
  2259. {
  2260. if (_parent == null)
  2261. base.RequestPhysicsterseUpdate();
  2262. }
  2263. else
  2264. {
  2265. throttleCounter++;
  2266. }
  2267. }
  2268. m_lastposition = l_position;
  2269. }
  2270. else
  2271. {
  2272. // Not a body.. so Make sure the client isn't interpolating
  2273. _velocity.X = 0;
  2274. _velocity.Y = 0;
  2275. _velocity.Z = 0;
  2276. _acceleration.X = 0;
  2277. _acceleration.Y = 0;
  2278. _acceleration.Z = 0;
  2279. m_rotationalVelocity.X = 0;
  2280. m_rotationalVelocity.Y = 0;
  2281. m_rotationalVelocity.Z = 0;
  2282. _zeroFlag = true;
  2283. }
  2284. }
  2285. }
  2286. public override bool FloatOnWater
  2287. {
  2288. set {
  2289. m_taintCollidesWater = value;
  2290. _parent_scene.AddPhysicsActorTaint(this);
  2291. }
  2292. }
  2293. public override void SetMomentum(PhysicsVector momentum)
  2294. {
  2295. }
  2296. public override PhysicsVector PIDTarget
  2297. {
  2298. set
  2299. {
  2300. if (PhysicsVector.isFinite(value))
  2301. {
  2302. m_PIDTarget = value;
  2303. }
  2304. else
  2305. m_log.Warn("[PHYSICS]: Got NaN PIDTarget from Scene on Object");
  2306. }
  2307. }
  2308. public override bool PIDActive { set { m_usePID = value; } }
  2309. public override float PIDTau { set { m_PIDTau = value; } }
  2310. public override float PIDHoverHeight { set { m_PIDHoverHeight = value; ; } }
  2311. public override bool PIDHoverActive { set { m_useHoverPID = value; } }
  2312. public override PIDHoverType PIDHoverType { set { m_PIDHoverType = value; } }
  2313. public override float PIDHoverTau { set { m_PIDHoverTau = value; } }
  2314. private void createAMotor(PhysicsVector axis)
  2315. {
  2316. if (Body == IntPtr.Zero)
  2317. return;
  2318. if (Amotor != IntPtr.Zero)
  2319. {
  2320. d.JointDestroy(Amotor);
  2321. Amotor = IntPtr.Zero;
  2322. }
  2323. float axisnum = 3;
  2324. axisnum = (axisnum - (axis.X + axis.Y + axis.Z));
  2325. // PhysicsVector totalSize = new PhysicsVector(_size.X, _size.Y, _size.Z);
  2326. // Inverse Inertia Matrix, set the X, Y, and/r Z inertia to 0 then invert it again.
  2327. d.Mass objMass;
  2328. d.MassSetZero(out objMass);
  2329. DMassCopy(ref pMass, ref objMass);
  2330. //m_log.DebugFormat("1-{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, ", objMass.I.M00, objMass.I.M01, objMass.I.M02, objMass.I.M10, objMass.I.M11, objMass.I.M12, objMass.I.M20, objMass.I.M21, objMass.I.M22);
  2331. Matrix4 dMassMat = FromDMass(objMass);
  2332. Matrix4 mathmat = Inverse(dMassMat);
  2333. /*
  2334. //m_log.DebugFormat("2-{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, ", mathmat[0, 0], mathmat[0, 1], mathmat[0, 2], mathmat[1, 0], mathmat[1, 1], mathmat[1, 2], mathmat[2, 0], mathmat[2, 1], mathmat[2, 2]);
  2335. mathmat = Inverse(mathmat);
  2336. objMass = FromMatrix4(mathmat, ref objMass);
  2337. //m_log.DebugFormat("3-{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, ", objMass.I.M00, objMass.I.M01, objMass.I.M02, objMass.I.M10, objMass.I.M11, objMass.I.M12, objMass.I.M20, objMass.I.M21, objMass.I.M22);
  2338. mathmat = Inverse(mathmat);
  2339. */
  2340. if (axis.X == 0)
  2341. {
  2342. mathmat.M33 = 50.0000001f;
  2343. //objMass.I.M22 = 0;
  2344. }
  2345. if (axis.Y == 0)
  2346. {
  2347. mathmat.M22 = 50.0000001f;
  2348. //objMass.I.M11 = 0;
  2349. }
  2350. if (axis.Z == 0)
  2351. {
  2352. mathmat.M11 = 50.0000001f;
  2353. //objMass.I.M00 = 0;
  2354. }
  2355. mathmat = Inverse(mathmat);
  2356. objMass = FromMatrix4(mathmat, ref objMass);
  2357. //m_log.DebugFormat("4-{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, ", objMass.I.M00, objMass.I.M01, objMass.I.M02, objMass.I.M10, objMass.I.M11, objMass.I.M12, objMass.I.M20, objMass.I.M21, objMass.I.M22);
  2358. //return;
  2359. if (d.MassCheck(ref objMass))
  2360. {
  2361. d.BodySetMass(Body, ref objMass);
  2362. }
  2363. else
  2364. {
  2365. //m_log.Debug("[PHYSICS]: Mass invalid, ignoring");
  2366. }
  2367. if (axisnum <= 0)
  2368. return;
  2369. // int dAMotorEuler = 1;
  2370. Amotor = d.JointCreateAMotor(_parent_scene.world, IntPtr.Zero);
  2371. d.JointAttach(Amotor, Body, IntPtr.Zero);
  2372. d.JointSetAMotorMode(Amotor, 0);
  2373. d.JointSetAMotorNumAxes(Amotor,(int)axisnum);
  2374. int i = 0;
  2375. if (axis.X == 0)
  2376. {
  2377. d.JointSetAMotorAxis(Amotor, i, 0, 1, 0, 0);
  2378. i++;
  2379. }
  2380. if (axis.Y == 0)
  2381. {
  2382. d.JointSetAMotorAxis(Amotor, i, 0, 0, 1, 0);
  2383. i++;
  2384. }
  2385. if (axis.Z == 0)
  2386. {
  2387. d.JointSetAMotorAxis(Amotor, i, 0, 0, 0, 1);
  2388. i++;
  2389. }
  2390. for (int j = 0; j < (int)axisnum; j++)
  2391. {
  2392. //d.JointSetAMotorAngle(Amotor, j, 0);
  2393. }
  2394. //d.JointSetAMotorAngle(Amotor, 1, 0);
  2395. //d.JointSetAMotorAngle(Amotor, 2, 0);
  2396. // These lowstops and high stops are effectively (no wiggle room)
  2397. d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, -0f);
  2398. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -0f);
  2399. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, -0f);
  2400. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 0f);
  2401. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0f);
  2402. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 0f);
  2403. //d.JointSetAMotorParam(Amotor, (int) dParam.Vel, 9000f);
  2404. d.JointSetAMotorParam(Amotor, (int)dParam.FudgeFactor, 0f);
  2405. d.JointSetAMotorParam(Amotor, (int)dParam.FMax, Mass * 50f);//
  2406. }
  2407. public Matrix4 FromDMass(d.Mass pMass)
  2408. {
  2409. Matrix4 obj;
  2410. obj.M11 = pMass.I.M00;
  2411. obj.M12 = pMass.I.M01;
  2412. obj.M13 = pMass.I.M02;
  2413. obj.M14 = 0;
  2414. obj.M21 = pMass.I.M10;
  2415. obj.M22 = pMass.I.M11;
  2416. obj.M23 = pMass.I.M12;
  2417. obj.M24 = 0;
  2418. obj.M31 = pMass.I.M20;
  2419. obj.M32 = pMass.I.M21;
  2420. obj.M33 = pMass.I.M22;
  2421. obj.M34 = 0;
  2422. obj.M41 = 0;
  2423. obj.M42 = 0;
  2424. obj.M43 = 0;
  2425. obj.M44 = 1;
  2426. return obj;
  2427. }
  2428. public d.Mass FromMatrix4(Matrix4 pMat, ref d.Mass obj)
  2429. {
  2430. obj.I.M00 = pMat[0, 0];
  2431. obj.I.M01 = pMat[0, 1];
  2432. obj.I.M02 = pMat[0, 2];
  2433. obj.I.M10 = pMat[1, 0];
  2434. obj.I.M11 = pMat[1, 1];
  2435. obj.I.M12 = pMat[1, 2];
  2436. obj.I.M20 = pMat[2, 0];
  2437. obj.I.M21 = pMat[2, 1];
  2438. obj.I.M22 = pMat[2, 2];
  2439. return obj;
  2440. }
  2441. public override void SubscribeEvents(int ms)
  2442. {
  2443. m_eventsubscription = ms;
  2444. _parent_scene.addCollisionEventReporting(this);
  2445. }
  2446. public override void UnSubscribeEvents()
  2447. {
  2448. _parent_scene.remCollisionEventReporting(this);
  2449. m_eventsubscription = 0;
  2450. }
  2451. public void AddCollisionEvent(uint CollidedWith, float depth)
  2452. {
  2453. if (CollisionEventsThisFrame == null)
  2454. CollisionEventsThisFrame = new CollisionEventUpdate();
  2455. CollisionEventsThisFrame.addCollider(CollidedWith,depth);
  2456. }
  2457. public void SendCollisions()
  2458. {
  2459. if (CollisionEventsThisFrame == null)
  2460. return;
  2461. base.SendCollisionUpdate(CollisionEventsThisFrame);
  2462. if (CollisionEventsThisFrame.m_objCollisionList.Count == 0)
  2463. CollisionEventsThisFrame = null;
  2464. else
  2465. CollisionEventsThisFrame = new CollisionEventUpdate();
  2466. }
  2467. public override bool SubscribedEvents()
  2468. {
  2469. if (m_eventsubscription > 0)
  2470. return true;
  2471. return false;
  2472. }
  2473. public static Matrix4 Inverse(Matrix4 pMat)
  2474. {
  2475. if (determinant3x3(pMat) == 0)
  2476. {
  2477. return Matrix4.Identity; // should probably throw an error. singluar matrix inverse not possible
  2478. }
  2479. return (Adjoint(pMat) / determinant3x3(pMat));
  2480. }
  2481. public static Matrix4 Adjoint(Matrix4 pMat)
  2482. {
  2483. Matrix4 adjointMatrix = new Matrix4();
  2484. for (int i=0; i<4; i++)
  2485. {
  2486. for (int j=0; j<4; j++)
  2487. {
  2488. Matrix4SetValue(ref adjointMatrix, i, j, (float)(Math.Pow(-1, i + j) * (determinant3x3(Minor(pMat, i, j)))));
  2489. }
  2490. }
  2491. adjointMatrix = Transpose(adjointMatrix);
  2492. return adjointMatrix;
  2493. }
  2494. public static Matrix4 Minor(Matrix4 matrix, int iRow, int iCol)
  2495. {
  2496. Matrix4 minor = new Matrix4();
  2497. int m = 0, n = 0;
  2498. for (int i = 0; i < 4; i++)
  2499. {
  2500. if (i == iRow)
  2501. continue;
  2502. n = 0;
  2503. for (int j = 0; j < 4; j++)
  2504. {
  2505. if (j == iCol)
  2506. continue;
  2507. Matrix4SetValue(ref minor, m,n, matrix[i, j]);
  2508. n++;
  2509. }
  2510. m++;
  2511. }
  2512. return minor;
  2513. }
  2514. public static Matrix4 Transpose(Matrix4 pMat)
  2515. {
  2516. Matrix4 transposeMatrix = new Matrix4();
  2517. for (int i = 0; i < 4; i++)
  2518. for (int j = 0; j < 4; j++)
  2519. Matrix4SetValue(ref transposeMatrix, i, j, pMat[j, i]);
  2520. return transposeMatrix;
  2521. }
  2522. public static void Matrix4SetValue(ref Matrix4 pMat, int r, int c, float val)
  2523. {
  2524. switch (r)
  2525. {
  2526. case 0:
  2527. switch (c)
  2528. {
  2529. case 0:
  2530. pMat.M11 = val;
  2531. break;
  2532. case 1:
  2533. pMat.M12 = val;
  2534. break;
  2535. case 2:
  2536. pMat.M13 = val;
  2537. break;
  2538. case 3:
  2539. pMat.M14 = val;
  2540. break;
  2541. }
  2542. break;
  2543. case 1:
  2544. switch (c)
  2545. {
  2546. case 0:
  2547. pMat.M21 = val;
  2548. break;
  2549. case 1:
  2550. pMat.M22 = val;
  2551. break;
  2552. case 2:
  2553. pMat.M23 = val;
  2554. break;
  2555. case 3:
  2556. pMat.M24 = val;
  2557. break;
  2558. }
  2559. break;
  2560. case 2:
  2561. switch (c)
  2562. {
  2563. case 0:
  2564. pMat.M31 = val;
  2565. break;
  2566. case 1:
  2567. pMat.M32 = val;
  2568. break;
  2569. case 2:
  2570. pMat.M33 = val;
  2571. break;
  2572. case 3:
  2573. pMat.M34 = val;
  2574. break;
  2575. }
  2576. break;
  2577. case 3:
  2578. switch (c)
  2579. {
  2580. case 0:
  2581. pMat.M41 = val;
  2582. break;
  2583. case 1:
  2584. pMat.M42 = val;
  2585. break;
  2586. case 2:
  2587. pMat.M43 = val;
  2588. break;
  2589. case 3:
  2590. pMat.M44 = val;
  2591. break;
  2592. }
  2593. break;
  2594. }
  2595. }
  2596. private static float determinant3x3(Matrix4 pMat)
  2597. {
  2598. float det = 0;
  2599. float diag1 = pMat[0, 0]*pMat[1, 1]*pMat[2, 2];
  2600. float diag2 = pMat[0, 1]*pMat[2, 1]*pMat[2, 0];
  2601. float diag3 = pMat[0, 2]*pMat[1, 0]*pMat[2, 1];
  2602. float diag4 = pMat[2, 0]*pMat[1, 1]*pMat[0, 2];
  2603. float diag5 = pMat[2, 1]*pMat[1, 2]*pMat[0, 0];
  2604. float diag6 = pMat[2, 2]*pMat[1, 0]*pMat[0, 1];
  2605. det = diag1 + diag2 + diag3 - (diag4 + diag5 + diag6);
  2606. return det;
  2607. }
  2608. private static void DMassCopy(ref d.Mass src, ref d.Mass dst)
  2609. {
  2610. dst.c.W = src.c.W;
  2611. dst.c.X = src.c.X;
  2612. dst.c.Y = src.c.Y;
  2613. dst.c.Z = src.c.Z;
  2614. dst.mass = src.mass;
  2615. dst.I.M00 = src.I.M00;
  2616. dst.I.M01 = src.I.M01;
  2617. dst.I.M02 = src.I.M02;
  2618. dst.I.M10 = src.I.M10;
  2619. dst.I.M11 = src.I.M11;
  2620. dst.I.M12 = src.I.M12;
  2621. dst.I.M20 = src.I.M20;
  2622. dst.I.M21 = src.I.M21;
  2623. dst.I.M22 = src.I.M22;
  2624. }
  2625. public override void SetMaterial(int pMaterial)
  2626. {
  2627. m_material = pMaterial;
  2628. }
  2629. }
  2630. }