ODEPrim.cs 112 KB

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