1
0

ODEPrim.cs 118 KB

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