BulletDotNETPrim.cs 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Runtime.InteropServices;
  31. using System.Threading;
  32. using log4net;
  33. using OpenMetaverse;
  34. using BulletDotNET;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Physics.Manager;
  37. namespace OpenSim.Region.Physics.BulletDotNETPlugin
  38. {
  39. public class BulletDotNETPrim : PhysicsActor
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private PhysicsVector _position;
  43. private PhysicsVector m_zeroPosition;
  44. private PhysicsVector _velocity;
  45. private PhysicsVector _torque = new PhysicsVector(0, 0, 0);
  46. private PhysicsVector m_lastVelocity = new PhysicsVector(0.0f, 0.0f, 0.0f);
  47. private PhysicsVector m_lastposition = new PhysicsVector(0.0f, 0.0f, 0.0f);
  48. private Quaternion m_lastorientation = new Quaternion();
  49. private PhysicsVector m_rotationalVelocity;
  50. private PhysicsVector _size;
  51. private PhysicsVector _acceleration;
  52. // private d.Vector3 _zeroPosition = new d.Vector3(0.0f, 0.0f, 0.0f);
  53. private Quaternion _orientation;
  54. private PhysicsVector m_taintposition;
  55. private PhysicsVector m_taintsize;
  56. private PhysicsVector m_taintVelocity = new PhysicsVector(0, 0, 0);
  57. private PhysicsVector m_taintTorque = new PhysicsVector(0, 0, 0);
  58. private Quaternion m_taintrot;
  59. private PhysicsVector m_angularlock = new PhysicsVector(1f, 1f, 1f);
  60. private PhysicsVector m_taintAngularLock = new PhysicsVector(1f, 1f, 1f);
  61. // private btGeneric6DofConstraint Amotor;
  62. private PhysicsVector m_PIDTarget = new PhysicsVector(0, 0, 0);
  63. private float m_PIDTau = 0f;
  64. private float m_PIDHoverHeight = 0f;
  65. private float m_PIDHoverTau = 0f;
  66. private bool m_useHoverPID = false;
  67. private PIDHoverType m_PIDHoverType = PIDHoverType.Ground;
  68. private float m_targetHoverHeight = 0f;
  69. private float m_groundHeight = 0f;
  70. private float m_waterHeight = 0f;
  71. private float PID_D = 35f;
  72. private float PID_G = 25f;
  73. // private float m_tensor = 5f;
  74. // private int body_autodisable_frames = 20;
  75. private IMesh primMesh = null;
  76. private bool m_usePID = false;
  77. private const CollisionCategories m_default_collisionFlags = (CollisionCategories.Geom
  78. | CollisionCategories.Space
  79. | CollisionCategories.Body
  80. | CollisionCategories.Character
  81. );
  82. private bool m_taintshape = false;
  83. private bool m_taintPhysics = false;
  84. // private bool m_collidesLand = true;
  85. private bool m_collidesWater = false;
  86. public bool m_returnCollisions = false;
  87. // Default we're a Geometry
  88. // private CollisionCategories m_collisionCategories = (CollisionCategories.Geom);
  89. // Default, Collide with Other Geometries, spaces and Bodies
  90. // private CollisionCategories m_collisionFlags = m_default_collisionFlags;
  91. public bool m_taintremove = false;
  92. public bool m_taintdisable = false;
  93. public bool m_disabled = false;
  94. public bool m_taintadd = false;
  95. public bool m_taintselected = false;
  96. public bool m_taintCollidesWater = false;
  97. public uint m_localID = 0;
  98. //public GCHandle gc;
  99. // private CollisionLocker ode;
  100. private bool m_taintforce = false;
  101. private bool m_taintaddangularforce = false;
  102. private PhysicsVector m_force = new PhysicsVector(0.0f, 0.0f, 0.0f);
  103. private List<PhysicsVector> m_forcelist = new List<PhysicsVector>();
  104. private List<PhysicsVector> m_angularforcelist = new List<PhysicsVector>();
  105. private IMesh _mesh;
  106. private PrimitiveBaseShape _pbs;
  107. private BulletDotNETScene _parent_scene;
  108. public btCollisionShape prim_geom;
  109. public IntPtr _triMeshData;
  110. private PhysicsActor _parent = null;
  111. private PhysicsActor m_taintparent = null;
  112. private List<BulletDotNETPrim> childrenPrim = new List<BulletDotNETPrim>();
  113. private bool iscolliding = false;
  114. private bool m_isphysical = false;
  115. private bool m_isSelected = false;
  116. internal bool m_isVolumeDetect = false; // If true, this prim only detects collisions but doesn't collide actively
  117. private bool m_throttleUpdates = false;
  118. // private int throttleCounter = 0;
  119. public int m_interpenetrationcount = 0;
  120. public float m_collisionscore = 0;
  121. public int m_roundsUnderMotionThreshold = 0;
  122. private int m_crossingfailures = 0;
  123. public float m_buoyancy = 0f;
  124. public bool outofBounds = false;
  125. private float m_density = 10.000006836f; // Aluminum g/cm3;
  126. public bool _zeroFlag = false;
  127. private bool m_lastUpdateSent = false;
  128. private String m_primName;
  129. private PhysicsVector _target_velocity;
  130. public int m_eventsubscription = 0;
  131. // private CollisionEventUpdate CollisionEventsThisFrame = null;
  132. public volatile bool childPrim = false;
  133. private btVector3 tempPosition1;
  134. private btVector3 tempPosition2;
  135. private btVector3 tempPosition3;
  136. private btVector3 tempSize1;
  137. private btVector3 tempSize2;
  138. private btVector3 tempLinearVelocity1;
  139. private btVector3 tempLinearVelocity2;
  140. private btVector3 tempAngularVelocity1;
  141. private btVector3 tempAngularVelocity2;
  142. private btVector3 tempInertia1;
  143. private btVector3 tempInertia2;
  144. private btVector3 tempAddForce;
  145. private btQuaternion tempOrientation1;
  146. private btQuaternion tempOrientation2;
  147. private btMotionState tempMotionState1;
  148. private btMotionState tempMotionState2;
  149. private btMotionState tempMotionState3;
  150. private btTransform tempTransform1;
  151. private btTransform tempTransform2;
  152. private btTransform tempTransform3;
  153. private btTransform tempTransform4;
  154. private btTriangleIndexVertexArray btshapeArray;
  155. private btVector3 AxisLockAngleHigh;
  156. private btVector3 AxisLockLinearLow;
  157. private btVector3 AxisLockLinearHigh;
  158. private bool forceenable = false;
  159. private btGeneric6DofConstraint m_aMotor;
  160. public btRigidBody Body;
  161. public BulletDotNETPrim(String primName, BulletDotNETScene parent_scene, PhysicsVector pos, PhysicsVector size,
  162. Quaternion rotation, IMesh mesh, PrimitiveBaseShape pbs, bool pisPhysical)
  163. {
  164. tempPosition1 = new btVector3(0, 0, 0);
  165. tempPosition2 = new btVector3(0, 0, 0);
  166. tempPosition3 = new btVector3(0, 0, 0);
  167. tempSize1 = new btVector3(0, 0, 0);
  168. tempSize2 = new btVector3(0, 0, 0);
  169. tempLinearVelocity1 = new btVector3(0, 0, 0);
  170. tempLinearVelocity2 = new btVector3(0, 0, 0);
  171. tempAngularVelocity1 = new btVector3(0, 0, 0);
  172. tempAngularVelocity2 = new btVector3(0, 0, 0);
  173. tempInertia1 = new btVector3(0, 0, 0);
  174. tempInertia2 = new btVector3(0, 0, 0);
  175. tempOrientation1 = new btQuaternion(0,0,0,1);
  176. tempOrientation2 = new btQuaternion(0, 0, 0, 1);
  177. _parent_scene = parent_scene;
  178. tempTransform1 = new btTransform(_parent_scene.QuatIdentity, _parent_scene.VectorZero);
  179. tempTransform2 = new btTransform(_parent_scene.QuatIdentity, _parent_scene.VectorZero); ;
  180. tempTransform3 = new btTransform(_parent_scene.QuatIdentity, _parent_scene.VectorZero); ;
  181. tempTransform4 = new btTransform(_parent_scene.QuatIdentity, _parent_scene.VectorZero); ;
  182. tempMotionState1 = new btDefaultMotionState(_parent_scene.TransZero);
  183. tempMotionState2 = new btDefaultMotionState(_parent_scene.TransZero);
  184. tempMotionState3 = new btDefaultMotionState(_parent_scene.TransZero);
  185. AxisLockLinearLow = new btVector3(-256,-256,-256);
  186. AxisLockLinearHigh = new btVector3(512, 512, 512);
  187. _target_velocity = new PhysicsVector(0, 0, 0);
  188. _velocity = new PhysicsVector();
  189. _position = pos;
  190. m_taintposition = pos;
  191. PID_D = parent_scene.bodyPIDD;
  192. PID_G = parent_scene.bodyPIDG;
  193. m_density = parent_scene.geomDefaultDensity;
  194. // m_tensor = parent_scene.bodyMotorJointMaxforceTensor;
  195. // body_autodisable_frames = parent_scene.bodyFramesAutoDisable;
  196. prim_geom = null;
  197. Body = null;
  198. if (size.X <= 0) size.X = 0.01f;
  199. if (size.Y <= 0) size.Y = 0.01f;
  200. if (size.Z <= 0) size.Z = 0.01f;
  201. _size = size;
  202. m_taintsize = _size;
  203. _acceleration = new PhysicsVector();
  204. m_rotationalVelocity = PhysicsVector.Zero;
  205. _orientation = rotation;
  206. m_taintrot = _orientation;
  207. _mesh = mesh;
  208. _pbs = pbs;
  209. _parent_scene = parent_scene;
  210. if (pos.Z < 0)
  211. m_isphysical = false;
  212. else
  213. {
  214. m_isphysical = pisPhysical;
  215. // If we're physical, we need to be in the master space for now.
  216. // linksets *should* be in a space together.. but are not currently
  217. }
  218. m_primName = primName;
  219. m_taintadd = true;
  220. _parent_scene.AddPhysicsActorTaint(this);
  221. }
  222. #region PhysicsActor overrides
  223. public override bool Stopped
  224. {
  225. get { return _zeroFlag; }
  226. }
  227. public override PhysicsVector Size
  228. {
  229. get { return _size; }
  230. set { _size = value; }
  231. }
  232. public override PrimitiveBaseShape Shape
  233. {
  234. set
  235. {
  236. _pbs = value;
  237. m_taintshape = true;
  238. }
  239. }
  240. public override uint LocalID
  241. {
  242. set
  243. {
  244. //m_log.Info("[PHYSICS]: Setting TrackerID: " + value);
  245. m_localID = value;
  246. }
  247. }
  248. public override bool Grabbed
  249. {
  250. set { return; }
  251. }
  252. public override bool Selected
  253. {
  254. set
  255. {
  256. // This only makes the object not collidable if the object
  257. // is physical or the object is modified somehow *IN THE FUTURE*
  258. // without this, if an avatar selects prim, they can walk right
  259. // through it while it's selected
  260. m_collisionscore = 0;
  261. if ((m_isphysical && !_zeroFlag) || !value)
  262. {
  263. m_taintselected = value;
  264. _parent_scene.AddPhysicsActorTaint(this);
  265. }
  266. else
  267. {
  268. m_taintselected = value;
  269. m_isSelected = value;
  270. }
  271. }
  272. }
  273. public override void CrossingFailure()
  274. {
  275. m_crossingfailures++;
  276. if (m_crossingfailures > _parent_scene.geomCrossingFailuresBeforeOutofbounds)
  277. {
  278. base.RaiseOutOfBounds(_position);
  279. return;
  280. }
  281. else if (m_crossingfailures == _parent_scene.geomCrossingFailuresBeforeOutofbounds)
  282. {
  283. m_log.Warn("[PHYSICS]: Too many crossing failures for: " + m_primName);
  284. }
  285. }
  286. public override void link(PhysicsActor obj)
  287. {
  288. m_taintparent = obj;
  289. }
  290. public override void delink()
  291. {
  292. m_taintparent = null;
  293. }
  294. public override void LockAngularMotion(PhysicsVector axis)
  295. {
  296. m_log.DebugFormat("[axislock]: <{0},{1},{2}>", axis.X, axis.Y, axis.Z);
  297. m_taintAngularLock = new PhysicsVector(axis.X, axis.Y, axis.Z);
  298. }
  299. public override PhysicsVector Position
  300. {
  301. get { return _position; }
  302. set
  303. {
  304. _position = value;
  305. //m_log.Info("[PHYSICS]: " + _position.ToString());
  306. }
  307. }
  308. public override float Mass
  309. {
  310. get { return CalculateMass(); }
  311. }
  312. public override PhysicsVector Force
  313. {
  314. //get { return PhysicsVector.Zero; }
  315. get { return m_force; }
  316. set { m_force = value; }
  317. }
  318. public override int VehicleType
  319. {
  320. get { return 0; }
  321. set { return; }
  322. }
  323. public override void VehicleFloatParam(int param, float value)
  324. {
  325. //TODO:
  326. }
  327. public override void VehicleVectorParam(int param, PhysicsVector value)
  328. {
  329. //TODO:
  330. }
  331. public override void VehicleRotationParam(int param, Quaternion rotation)
  332. {
  333. //TODO:
  334. }
  335. public override void SetVolumeDetect(int param)
  336. {
  337. //TODO: GhostObject
  338. m_isVolumeDetect = (param != 0);
  339. }
  340. public override PhysicsVector GeometricCenter
  341. {
  342. get { return PhysicsVector.Zero; }
  343. }
  344. public override PhysicsVector CenterOfMass
  345. {
  346. get { return PhysicsVector.Zero; }
  347. }
  348. public override PhysicsVector Velocity
  349. {
  350. get
  351. {
  352. // Averate previous velocity with the new one so
  353. // client object interpolation works a 'little' better
  354. PhysicsVector returnVelocity = new PhysicsVector();
  355. returnVelocity.X = (m_lastVelocity.X + _velocity.X) / 2;
  356. returnVelocity.Y = (m_lastVelocity.Y + _velocity.Y) / 2;
  357. returnVelocity.Z = (m_lastVelocity.Z + _velocity.Z) / 2;
  358. return returnVelocity;
  359. }
  360. set
  361. {
  362. _velocity = value;
  363. m_taintVelocity = value;
  364. _parent_scene.AddPhysicsActorTaint(this);
  365. }
  366. }
  367. public override PhysicsVector Torque
  368. {
  369. get
  370. {
  371. if (!m_isphysical || Body.Handle == IntPtr.Zero)
  372. return new PhysicsVector(0, 0, 0);
  373. return _torque;
  374. }
  375. set
  376. {
  377. m_taintTorque = value;
  378. _parent_scene.AddPhysicsActorTaint(this);
  379. }
  380. }
  381. public override float CollisionScore
  382. {
  383. get { return m_collisionscore; }
  384. set { m_collisionscore = value; }
  385. }
  386. public override PhysicsVector Acceleration
  387. {
  388. get { return _acceleration; }
  389. }
  390. public override Quaternion Orientation
  391. {
  392. get { return _orientation; }
  393. set { _orientation = value; }
  394. }
  395. public override int PhysicsActorType
  396. {
  397. get { return (int)ActorTypes.Prim; }
  398. set { return; }
  399. }
  400. public override bool IsPhysical
  401. {
  402. get { return m_isphysical; }
  403. set { m_isphysical = value; }
  404. }
  405. public override bool Flying
  406. {
  407. // no flying prims for you
  408. get { return false; }
  409. set { }
  410. }
  411. public override bool SetAlwaysRun
  412. {
  413. get { return false; }
  414. set { return; }
  415. }
  416. public override bool ThrottleUpdates
  417. {
  418. get { return m_throttleUpdates; }
  419. set { m_throttleUpdates = value; }
  420. }
  421. public override bool IsColliding
  422. {
  423. get { return iscolliding; }
  424. set { iscolliding = value; }
  425. }
  426. public override bool CollidingGround
  427. {
  428. get { return false; }
  429. set { return; }
  430. }
  431. public override bool CollidingObj
  432. {
  433. get { return false; }
  434. set { return; }
  435. }
  436. public override bool FloatOnWater
  437. {
  438. set
  439. {
  440. m_taintCollidesWater = value;
  441. _parent_scene.AddPhysicsActorTaint(this);
  442. }
  443. }
  444. public override PhysicsVector RotationalVelocity
  445. {
  446. get
  447. {
  448. PhysicsVector pv = new PhysicsVector(0, 0, 0);
  449. if (_zeroFlag)
  450. return pv;
  451. m_lastUpdateSent = false;
  452. if (m_rotationalVelocity.IsIdentical(pv, 0.2f))
  453. return pv;
  454. return m_rotationalVelocity;
  455. }
  456. set { m_rotationalVelocity = value; }
  457. }
  458. public override bool Kinematic
  459. {
  460. get { return false; }
  461. set { }
  462. }
  463. public override float Buoyancy
  464. {
  465. get { return m_buoyancy; }
  466. set { m_buoyancy = value; }
  467. }
  468. public override PhysicsVector PIDTarget { set { m_PIDTarget = value; ; } }
  469. public override bool PIDActive { set { m_usePID = value; } }
  470. public override float PIDTau { set { m_PIDTau = value; } }
  471. public override float PIDHoverHeight { set { m_PIDHoverHeight = value; ; } }
  472. public override bool PIDHoverActive { set { m_useHoverPID = value; } }
  473. public override PIDHoverType PIDHoverType { set { m_PIDHoverType = value; } }
  474. public override float PIDHoverTau { set { m_PIDHoverTau = value; } }
  475. public override void AddForce(PhysicsVector force, bool pushforce)
  476. {
  477. m_forcelist.Add(force);
  478. m_taintforce = true;
  479. //m_log.Info("[PHYSICS]: Added Force:" + force.ToString() + " to prim at " + Position.ToString());
  480. }
  481. public override void AddAngularForce(PhysicsVector force, bool pushforce)
  482. {
  483. m_angularforcelist.Add(force);
  484. m_taintaddangularforce = true;
  485. }
  486. public override void SetMomentum(PhysicsVector momentum)
  487. {
  488. }
  489. public override void SubscribeEvents(int ms)
  490. {
  491. m_eventsubscription = ms;
  492. _parent_scene.addCollisionEventReporting(this);
  493. }
  494. public override void UnSubscribeEvents()
  495. {
  496. _parent_scene.remCollisionEventReporting(this);
  497. m_eventsubscription = 0;
  498. }
  499. public override bool SubscribedEvents()
  500. {
  501. return (m_eventsubscription > 0);
  502. }
  503. #endregion
  504. internal void Dispose()
  505. {
  506. //TODO:
  507. DisableAxisMotor();
  508. DisposeOfBody();
  509. SetCollisionShape(null);
  510. if (tempMotionState3 != null && tempMotionState3.Handle != IntPtr.Zero)
  511. {
  512. tempMotionState3.Dispose();
  513. tempMotionState3 = null;
  514. }
  515. if (tempMotionState2 != null && tempMotionState2.Handle != IntPtr.Zero)
  516. {
  517. tempMotionState2.Dispose();
  518. tempMotionState2 = null;
  519. }
  520. if (tempMotionState1 != null && tempMotionState1.Handle != IntPtr.Zero)
  521. {
  522. tempMotionState1.Dispose();
  523. tempMotionState1 = null;
  524. }
  525. if (tempTransform4 != null && tempTransform4.Handle != IntPtr.Zero)
  526. {
  527. tempTransform4.Dispose();
  528. tempTransform4 = null;
  529. }
  530. if (tempTransform3 != null && tempTransform3.Handle != IntPtr.Zero)
  531. {
  532. tempTransform3.Dispose();
  533. tempTransform3 = null;
  534. }
  535. if (tempTransform2 != null && tempTransform2.Handle != IntPtr.Zero)
  536. {
  537. tempTransform2.Dispose();
  538. tempTransform2 = null;
  539. }
  540. if (tempTransform1 != null && tempTransform1.Handle != IntPtr.Zero)
  541. {
  542. tempTransform1.Dispose();
  543. tempTransform1 = null;
  544. }
  545. if (tempOrientation2 != null && tempOrientation2.Handle != IntPtr.Zero)
  546. {
  547. tempOrientation2.Dispose();
  548. tempOrientation2 = null;
  549. }
  550. if (tempOrientation1 != null && tempOrientation1.Handle != IntPtr.Zero)
  551. {
  552. tempOrientation1.Dispose();
  553. tempOrientation1 = null;
  554. }
  555. if (tempInertia1 != null && tempInertia1.Handle != IntPtr.Zero)
  556. {
  557. tempInertia1.Dispose();
  558. tempInertia1 = null;
  559. }
  560. if (tempInertia2 != null && tempInertia2.Handle != IntPtr.Zero)
  561. {
  562. tempInertia2.Dispose();
  563. tempInertia1 = null;
  564. }
  565. if (tempAngularVelocity2 != null && tempAngularVelocity2.Handle != IntPtr.Zero)
  566. {
  567. tempAngularVelocity2.Dispose();
  568. tempAngularVelocity2 = null;
  569. }
  570. if (tempAngularVelocity1 != null && tempAngularVelocity1.Handle != IntPtr.Zero)
  571. {
  572. tempAngularVelocity1.Dispose();
  573. tempAngularVelocity1 = null;
  574. }
  575. if (tempLinearVelocity2 != null && tempLinearVelocity2.Handle != IntPtr.Zero)
  576. {
  577. tempLinearVelocity2.Dispose();
  578. tempLinearVelocity2 = null;
  579. }
  580. if (tempLinearVelocity1 != null && tempLinearVelocity1.Handle != IntPtr.Zero)
  581. {
  582. tempLinearVelocity1.Dispose();
  583. tempLinearVelocity1 = null;
  584. }
  585. if (tempSize2 != null && tempSize2.Handle != IntPtr.Zero)
  586. {
  587. tempSize2.Dispose();
  588. tempSize2 = null;
  589. }
  590. if (tempSize1 != null && tempSize1.Handle != IntPtr.Zero)
  591. {
  592. tempSize1.Dispose();
  593. tempSize1 = null;
  594. }
  595. if (tempPosition3 != null && tempPosition3.Handle != IntPtr.Zero)
  596. {
  597. tempPosition3.Dispose();
  598. tempPosition3 = null;
  599. }
  600. if (tempPosition2 != null && tempPosition2.Handle != IntPtr.Zero)
  601. {
  602. tempPosition2.Dispose();
  603. tempPosition2 = null;
  604. }
  605. if (tempPosition1 != null && tempPosition1.Handle != IntPtr.Zero)
  606. {
  607. tempPosition1.Dispose();
  608. tempPosition1 = null;
  609. }
  610. if (AxisLockLinearLow != null && AxisLockLinearLow.Handle != IntPtr.Zero)
  611. {
  612. AxisLockLinearLow.Dispose();
  613. AxisLockLinearLow = null;
  614. }
  615. if (AxisLockLinearHigh != null && AxisLockLinearHigh.Handle != IntPtr.Zero)
  616. {
  617. AxisLockLinearHigh.Dispose();
  618. AxisLockLinearHigh = null;
  619. }
  620. }
  621. public void ProcessTaints(float timestep)
  622. {
  623. if (m_taintadd)
  624. {
  625. m_log.Debug("[PHYSICS]: TaintAdd");
  626. changeadd(timestep);
  627. }
  628. if (prim_geom == null)
  629. {
  630. CreateGeom(IntPtr.Zero, primMesh);
  631. if (IsPhysical)
  632. SetBody(Mass);
  633. else
  634. SetBody(0);
  635. m_log.Debug("[PHYSICS]: GEOM_DOESNT_EXSIT");
  636. }
  637. if (prim_geom.Handle == IntPtr.Zero)
  638. {
  639. CreateGeom(IntPtr.Zero, primMesh);
  640. if (IsPhysical)
  641. SetBody(Mass);
  642. else
  643. SetBody(0);
  644. m_log.Debug("[PHYSICS]: GEOM_DOESNT_EXSIT");
  645. }
  646. if (!_position.IsIdentical(m_taintposition, 0f))
  647. {
  648. m_log.Debug("[PHYSICS]: TaintMove");
  649. changemove(timestep);
  650. }
  651. if (m_taintrot != _orientation)
  652. {
  653. m_log.Debug("[PHYSICS]: TaintRotate");
  654. rotate(timestep);
  655. } //
  656. if (m_taintPhysics != m_isphysical && !(m_taintparent != _parent))
  657. {
  658. m_log.Debug("[PHYSICS]: TaintPhysics");
  659. changePhysicsStatus(timestep);
  660. }
  661. //
  662. if (!_size.IsIdentical(m_taintsize, 0))
  663. {
  664. m_log.Debug("[PHYSICS]: TaintSize");
  665. changesize(timestep);
  666. }
  667. //
  668. if (m_taintshape)
  669. {
  670. m_log.Debug("[PHYSICS]: TaintShape");
  671. changeshape(timestep);
  672. } //
  673. if (m_taintforce)
  674. {
  675. m_log.Debug("[PHYSICS]: TaintForce");
  676. changeAddForce(timestep);
  677. }
  678. if (m_taintaddangularforce)
  679. {
  680. m_log.Debug("[PHYSICS]: TaintAngularForce");
  681. changeAddAngularForce(timestep);
  682. }
  683. if (!m_taintTorque.IsIdentical(PhysicsVector.Zero, 0.001f))
  684. {
  685. m_log.Debug("[PHYSICS]: TaintTorque");
  686. changeSetTorque(timestep);
  687. }
  688. if (m_taintdisable)
  689. {
  690. m_log.Debug("[PHYSICS]: TaintDisable");
  691. changedisable(timestep);
  692. }
  693. if (m_taintselected != m_isSelected)
  694. {
  695. m_log.Debug("[PHYSICS]: TaintSelected");
  696. changeSelectedStatus(timestep);
  697. }
  698. if (!m_taintVelocity.IsIdentical(PhysicsVector.Zero, 0.001f))
  699. {
  700. m_log.Debug("[PHYSICS]: TaintVelocity");
  701. changevelocity(timestep);
  702. }
  703. if (m_taintparent != _parent)
  704. {
  705. m_log.Debug("[PHYSICS]: TaintLink");
  706. changelink(timestep);
  707. }
  708. if (m_taintCollidesWater != m_collidesWater)
  709. {
  710. changefloatonwater(timestep);
  711. }
  712. if (!m_angularlock.IsIdentical(m_taintAngularLock, 0))
  713. {
  714. m_log.Debug("[PHYSICS]: TaintAngularLock");
  715. changeAngularLock(timestep);
  716. }
  717. if (m_taintremove)
  718. {
  719. DisposeOfBody();
  720. Dispose();
  721. }
  722. }
  723. #region Physics Scene Change Action routines
  724. private void changeadd(float timestep)
  725. {
  726. //SetCollisionShape(null);
  727. // Construction of new prim
  728. if (Body != null)
  729. {
  730. if (Body.Handle != IntPtr.Zero)
  731. {
  732. DisableAxisMotor();
  733. _parent_scene.removeFromWorld(this, Body);
  734. //Body.Dispose();
  735. }
  736. //Body = null;
  737. // TODO: dispose parts that make up body
  738. }
  739. if (_parent_scene.needsMeshing(_pbs))
  740. {
  741. // Don't need to re-enable body.. it's done in SetMesh
  742. float meshlod = _parent_scene.meshSculptLOD;
  743. if (IsPhysical)
  744. meshlod = _parent_scene.MeshSculptphysicalLOD;
  745. IMesh mesh = _parent_scene.mesher.CreateMesh(SOPName, _pbs, _size, meshlod, IsPhysical);
  746. // createmesh returns null when it doesn't mesh.
  747. CreateGeom(IntPtr.Zero, mesh);
  748. }
  749. else
  750. {
  751. _mesh = null;
  752. CreateGeom(IntPtr.Zero, null);
  753. }
  754. if (IsPhysical)
  755. SetBody(Mass);
  756. else
  757. SetBody(0);
  758. //changeSelectedStatus(timestep);
  759. m_taintadd = false;
  760. }
  761. private void changemove(float timestep)
  762. {
  763. m_log.Debug("[PHYSICS]: _________ChangeMove");
  764. if (!m_isphysical)
  765. {
  766. tempTransform2 = Body.getWorldTransform();
  767. btQuaternion quat = tempTransform2.getRotation();
  768. tempPosition2.setValue(_position.X, _position.Y, _position.Z);
  769. tempTransform2.Dispose();
  770. tempTransform2 = new btTransform(quat, tempPosition2);
  771. Body.setWorldTransform(tempTransform2);
  772. changeSelectedStatus(timestep);
  773. resetCollisionAccounting();
  774. }
  775. else
  776. {
  777. if (Body != null)
  778. {
  779. if (Body.Handle != IntPtr.Zero)
  780. {
  781. DisableAxisMotor();
  782. _parent_scene.removeFromWorld(this, Body);
  783. //Body.Dispose();
  784. }
  785. //Body = null;
  786. // TODO: dispose parts that make up body
  787. }
  788. /*
  789. if (_parent_scene.needsMeshing(_pbs))
  790. {
  791. // Don't need to re-enable body.. it's done in SetMesh
  792. float meshlod = _parent_scene.meshSculptLOD;
  793. if (IsPhysical)
  794. meshlod = _parent_scene.MeshSculptphysicalLOD;
  795. IMesh mesh = _parent_scene.mesher.CreateMesh(SOPName, _pbs, _size, meshlod, IsPhysical);
  796. // createmesh returns null when it doesn't mesh.
  797. CreateGeom(IntPtr.Zero, mesh);
  798. }
  799. else
  800. {
  801. _mesh = null;
  802. CreateGeom(IntPtr.Zero, null);
  803. }
  804. SetCollisionShape(prim_geom);
  805. */
  806. if (m_isphysical)
  807. SetBody(Mass);
  808. else
  809. SetBody(0);
  810. changeSelectedStatus(timestep);
  811. resetCollisionAccounting();
  812. }
  813. m_taintposition = _position;
  814. }
  815. private void rotate(float timestep)
  816. {
  817. m_log.Debug("[PHYSICS]: _________ChangeRotate");
  818. tempTransform2 = Body.getWorldTransform();
  819. tempOrientation2 = new btQuaternion(_orientation.X, _orientation.Y, _orientation.Z, _orientation.W);
  820. tempTransform2.setRotation(tempOrientation2);
  821. Body.setWorldTransform(tempTransform2);
  822. resetCollisionAccounting();
  823. m_taintrot = _orientation;
  824. }
  825. private void changePhysicsStatus(float timestep)
  826. {
  827. if (Body != null)
  828. {
  829. if (Body.Handle != IntPtr.Zero)
  830. {
  831. DisableAxisMotor();
  832. _parent_scene.removeFromWorld(this, Body);
  833. //Body.Dispose();
  834. }
  835. //Body = null;
  836. // TODO: dispose parts that make up body
  837. }
  838. m_log.Debug("[PHYSICS]: _________ChangePhysics");
  839. ProcessGeomCreation();
  840. if (m_isphysical)
  841. SetBody(Mass);
  842. else
  843. SetBody(0);
  844. changeSelectedStatus(timestep);
  845. resetCollisionAccounting();
  846. m_taintPhysics = m_isphysical;
  847. }
  848. internal void ProcessGeomCreation()
  849. {
  850. if (_parent_scene.needsMeshing(_pbs))
  851. {
  852. ProcessGeomCreationAsTriMesh(PhysicsVector.Zero,Quaternion.Identity);
  853. // createmesh returns null when it doesn't mesh.
  854. CreateGeom(IntPtr.Zero, _mesh);
  855. }
  856. else
  857. {
  858. _mesh = null;
  859. CreateGeom(IntPtr.Zero, null);
  860. }
  861. SetCollisionShape(prim_geom);
  862. }
  863. internal bool NeedsMeshing()
  864. {
  865. return _parent_scene.needsMeshing(_pbs);
  866. }
  867. internal void ProcessGeomCreationAsTriMesh(PhysicsVector positionOffset, Quaternion orientation)
  868. {
  869. // Don't need to re-enable body.. it's done in SetMesh
  870. float meshlod = _parent_scene.meshSculptLOD;
  871. if (IsPhysical)
  872. meshlod = _parent_scene.MeshSculptphysicalLOD;
  873. IMesh mesh = _parent_scene.mesher.CreateMesh(SOPName, _pbs, _size, meshlod, IsPhysical);
  874. if (!positionOffset.IsIdentical(PhysicsVector.Zero,0.001f) || orientation != Quaternion.Identity)
  875. {
  876. float[] xyz = new float[3];
  877. xyz[0] = positionOffset.X;
  878. xyz[1] = positionOffset.Y;
  879. xyz[2] = positionOffset.Z;
  880. Matrix4 m4 = Matrix4.CreateFromQuaternion(orientation);
  881. float[,] matrix = new float[3,3];
  882. matrix[0, 0] = m4.M11;
  883. matrix[0, 1] = m4.M12;
  884. matrix[0, 2] = m4.M13;
  885. matrix[1, 0] = m4.M21;
  886. matrix[1, 1] = m4.M22;
  887. matrix[1, 2] = m4.M23;
  888. matrix[2, 0] = m4.M31;
  889. matrix[2, 1] = m4.M32;
  890. matrix[2, 2] = m4.M33;
  891. mesh.TransformLinear(matrix, xyz);
  892. }
  893. _mesh = mesh;
  894. }
  895. private void changesize(float timestep)
  896. {
  897. if (Body != null)
  898. {
  899. if (Body.Handle != IntPtr.Zero)
  900. {
  901. DisableAxisMotor();
  902. _parent_scene.removeFromWorld(this, Body);
  903. //Body.Dispose();
  904. }
  905. //Body = null;
  906. // TODO: dispose parts that make up body
  907. }
  908. m_log.Debug("[PHYSICS]: _________ChangeSize");
  909. SetCollisionShape(null);
  910. // Construction of new prim
  911. ProcessGeomCreation();
  912. if (IsPhysical)
  913. SetBody(Mass);
  914. else
  915. SetBody(0);
  916. m_taintsize = _size;
  917. }
  918. private void changeshape(float timestep)
  919. {
  920. if (Body != null)
  921. {
  922. if (Body.Handle != IntPtr.Zero)
  923. {
  924. DisableAxisMotor();
  925. _parent_scene.removeFromWorld(this, Body);
  926. //Body.Dispose();
  927. }
  928. //Body = null;
  929. // TODO: dispose parts that make up body
  930. }
  931. // Cleanup of old prim geometry and Bodies
  932. if (IsPhysical && Body != null && Body.Handle != IntPtr.Zero)
  933. {
  934. if (childPrim)
  935. {
  936. if (_parent != null)
  937. {
  938. BulletDotNETPrim parent = (BulletDotNETPrim)_parent;
  939. parent.ChildDelink(this);
  940. }
  941. }
  942. else
  943. {
  944. //disableBody();
  945. }
  946. }
  947. try
  948. {
  949. //SetCollisionShape(null);
  950. }
  951. catch (System.AccessViolationException)
  952. {
  953. //prim_geom = IntPtr.Zero;
  954. m_log.Error("[PHYSICS]: PrimGeom dead");
  955. }
  956. // we don't need to do space calculation because the client sends a position update also.
  957. if (_size.X <= 0) _size.X = 0.01f;
  958. if (_size.Y <= 0) _size.Y = 0.01f;
  959. if (_size.Z <= 0) _size.Z = 0.01f;
  960. // Construction of new prim
  961. ProcessGeomCreation();
  962. tempPosition1.setValue(_position.X, _position.Y, _position.Z);
  963. if (tempOrientation1.Handle != IntPtr.Zero)
  964. tempOrientation1.Dispose();
  965. tempOrientation1 = new btQuaternion(_orientation.X, Orientation.Y, _orientation.Z, _orientation.W);
  966. if (tempTransform1 != null && tempTransform1.Handle != IntPtr.Zero)
  967. tempTransform1.Dispose();
  968. tempTransform1 = new btTransform(tempOrientation1, tempPosition1);
  969. //d.GeomBoxSetLengths(prim_geom, _size.X, _size.Y, _size.Z);
  970. if (IsPhysical)
  971. {
  972. SetBody(Mass);
  973. // Re creates body on size.
  974. // EnableBody also does setMass()
  975. }
  976. else
  977. {
  978. SetBody(0);
  979. }
  980. changeSelectedStatus(timestep);
  981. if (childPrim)
  982. {
  983. if (_parent is BulletDotNETPrim)
  984. {
  985. BulletDotNETPrim parent = (BulletDotNETPrim)_parent;
  986. parent.ChildSetGeom(this);
  987. }
  988. }
  989. resetCollisionAccounting();
  990. m_taintshape = false;
  991. }
  992. private void resetCollisionAccounting()
  993. {
  994. m_collisionscore = 0;
  995. }
  996. private void ChildSetGeom(BulletDotNETPrim bulletDotNETPrim)
  997. {
  998. // TODO: throw new NotImplementedException();
  999. }
  1000. private void changeAddForce(float timestep)
  1001. {
  1002. if (!m_isSelected)
  1003. {
  1004. lock (m_forcelist)
  1005. {
  1006. //m_log.Info("[PHYSICS]: dequeing forcelist");
  1007. if (IsPhysical)
  1008. {
  1009. PhysicsVector iforce = new PhysicsVector();
  1010. for (int i = 0; i < m_forcelist.Count; i++)
  1011. {
  1012. iforce = iforce + m_forcelist[i];
  1013. }
  1014. if (Body != null && Body.Handle != IntPtr.Zero)
  1015. {
  1016. if (tempAddForce != null && tempAddForce.Handle != IntPtr.Zero)
  1017. tempAddForce.Dispose();
  1018. enableBodySoft();
  1019. tempAddForce = new btVector3(iforce.X, iforce.Y, iforce.Z);
  1020. Body.applyCentralImpulse(tempAddForce);
  1021. }
  1022. }
  1023. m_forcelist.Clear();
  1024. }
  1025. m_collisionscore = 0;
  1026. m_interpenetrationcount = 0;
  1027. }
  1028. m_taintforce = false;
  1029. }
  1030. private void changeAddAngularForce(float timestep)
  1031. {
  1032. if (!m_isSelected)
  1033. {
  1034. lock (m_angularforcelist)
  1035. {
  1036. //m_log.Info("[PHYSICS]: dequeing forcelist");
  1037. if (IsPhysical)
  1038. {
  1039. PhysicsVector iforce = new PhysicsVector();
  1040. for (int i = 0; i < m_angularforcelist.Count; i++)
  1041. {
  1042. iforce = iforce + m_angularforcelist[i];
  1043. }
  1044. if (Body != null && Body.Handle != IntPtr.Zero)
  1045. {
  1046. if (tempAddForce != null && tempAddForce.Handle != IntPtr.Zero)
  1047. tempAddForce.Dispose();
  1048. enableBodySoft();
  1049. tempAddForce = new btVector3(iforce.X, iforce.Y, iforce.Z);
  1050. Body.applyTorqueImpulse(tempAddForce);
  1051. }
  1052. }
  1053. m_angularforcelist.Clear();
  1054. }
  1055. m_collisionscore = 0;
  1056. m_interpenetrationcount = 0;
  1057. }
  1058. m_taintaddangularforce = false;
  1059. }
  1060. private void changeSetTorque(float timestep)
  1061. {
  1062. if (!m_isSelected)
  1063. {
  1064. if (IsPhysical)
  1065. {
  1066. if (Body != null && Body.Handle != IntPtr.Zero)
  1067. {
  1068. tempAngularVelocity2.setValue(m_taintTorque.X, m_taintTorque.Y, m_taintTorque.Z);
  1069. Body.applyTorque(tempAngularVelocity2);
  1070. }
  1071. }
  1072. }
  1073. m_taintTorque = new PhysicsVector(0, 0, 0);
  1074. }
  1075. private void changedisable(float timestep)
  1076. {
  1077. // TODO: throw new NotImplementedException();
  1078. }
  1079. private void changeSelectedStatus(float timestep)
  1080. {
  1081. // TODO: throw new NotImplementedException();
  1082. if (m_taintselected)
  1083. {
  1084. Body.setCollisionFlags((int)ContactFlags.CF_NO_CONTACT_RESPONSE);
  1085. disableBodySoft();
  1086. }
  1087. else
  1088. {
  1089. Body.setCollisionFlags(0 | (int)ContactFlags.CF_CUSTOM_MATERIAL_CALLBACK);
  1090. enableBodySoft();
  1091. }
  1092. m_isSelected = m_taintselected;
  1093. }
  1094. private void changevelocity(float timestep)
  1095. {
  1096. if (!m_isSelected)
  1097. {
  1098. if (IsPhysical)
  1099. {
  1100. if (Body != null && Body.Handle != IntPtr.Zero)
  1101. {
  1102. tempLinearVelocity2.setValue(m_taintVelocity.X, m_taintVelocity.Y, m_taintVelocity.Z);
  1103. Body.setLinearVelocity(tempLinearVelocity2);
  1104. }
  1105. }
  1106. //resetCollisionAccounting();
  1107. }
  1108. m_taintVelocity = PhysicsVector.Zero;
  1109. }
  1110. private void changelink(float timestep)
  1111. {
  1112. if (IsPhysical)
  1113. {
  1114. // Construction of new prim
  1115. if (Body != null)
  1116. {
  1117. if (Body.Handle != IntPtr.Zero)
  1118. {
  1119. DisableAxisMotor();
  1120. _parent_scene.removeFromWorld(this, Body);
  1121. //Body.Dispose();
  1122. }
  1123. //Body = null;
  1124. // TODO: dispose parts that make up body
  1125. }
  1126. if (_parent == null && m_taintparent != null)
  1127. {
  1128. if (m_taintparent is BulletDotNETPrim)
  1129. {
  1130. BulletDotNETPrim obj = (BulletDotNETPrim)m_taintparent;
  1131. obj.ParentPrim(this);
  1132. childPrim = true;
  1133. }
  1134. }
  1135. else if (_parent != null && m_taintparent == null)
  1136. {
  1137. if (_parent is BulletDotNETPrim)
  1138. {
  1139. BulletDotNETPrim obj = (BulletDotNETPrim)_parent;
  1140. obj.ChildDelink(obj);
  1141. childPrim = false;
  1142. }
  1143. }
  1144. if (m_taintparent != null)
  1145. {
  1146. m_taintparent.Position.Z = m_taintparent.Position.Z + 0.02f;
  1147. _parent_scene.AddPhysicsActorTaint(m_taintparent);
  1148. }
  1149. }
  1150. _parent = m_taintparent;
  1151. m_taintPhysics = m_isphysical;
  1152. }
  1153. private void changefloatonwater(float timestep)
  1154. {
  1155. // TODO: throw new NotImplementedException();
  1156. }
  1157. private void changeAngularLock(float timestep)
  1158. {
  1159. if (IsPhysical && Body != null && Body.Handle != IntPtr.Zero)
  1160. {
  1161. if (_parent == null)
  1162. {
  1163. if (!m_taintAngularLock.IsIdentical(new PhysicsVector(1f, 1f, 1f), 0))
  1164. {
  1165. //d.BodySetFiniteRotationMode(Body, 0);
  1166. //d.BodySetFiniteRotationAxis(Body,m_taintAngularLock.X,m_taintAngularLock.Y,m_taintAngularLock.Z);
  1167. EnableAxisMotor(m_taintAngularLock);
  1168. }
  1169. else
  1170. {
  1171. DisableAxisMotor();
  1172. }
  1173. }
  1174. }
  1175. m_angularlock = new PhysicsVector(m_taintAngularLock.X, m_taintAngularLock.Y, m_taintAngularLock.Z);
  1176. }
  1177. #endregion
  1178. internal void Move(float timestep)
  1179. {
  1180. //TODO:
  1181. float fx = 0;
  1182. float fy = 0;
  1183. float fz = 0;
  1184. if (IsPhysical && Body != null && Body.Handle != IntPtr.Zero && !m_isSelected)
  1185. {
  1186. float m_mass = CalculateMass();
  1187. fz = 0f;
  1188. //m_log.Info(m_collisionFlags.ToString());
  1189. if (m_buoyancy != 0)
  1190. {
  1191. if (m_buoyancy > 0)
  1192. {
  1193. fz = (((-1 * _parent_scene.gravityz) * m_buoyancy) * m_mass) * 0.035f;
  1194. //d.Vector3 l_velocity = d.BodyGetLinearVel(Body);
  1195. //m_log.Info("Using Buoyancy: " + buoyancy + " G: " + (_parent_scene.gravityz * m_buoyancy) + "mass:" + m_mass + " Pos: " + Position.ToString());
  1196. }
  1197. else
  1198. {
  1199. fz = (-1 * (((-1 * _parent_scene.gravityz) * (-1 * m_buoyancy)) * m_mass) * 0.035f);
  1200. }
  1201. }
  1202. if (m_usePID)
  1203. {
  1204. PID_D = 61f;
  1205. PID_G = 65f;
  1206. //if (!d.BodyIsEnabled(Body))
  1207. //d.BodySetForce(Body, 0f, 0f, 0f);
  1208. // If we're using the PID controller, then we have no gravity
  1209. fz = ((-1 * _parent_scene.gravityz) * m_mass) * 1.025f;
  1210. // no lock; for now it's only called from within Simulate()
  1211. // If the PID Controller isn't active then we set our force
  1212. // calculating base velocity to the current position
  1213. if ((m_PIDTau < 1) && (m_PIDTau != 0))
  1214. {
  1215. //PID_G = PID_G / m_PIDTau;
  1216. m_PIDTau = 1;
  1217. }
  1218. if ((PID_G - m_PIDTau) <= 0)
  1219. {
  1220. PID_G = m_PIDTau + 1;
  1221. }
  1222. // TODO: NEED btVector3 for Linear Velocity
  1223. // NEED btVector3 for Position
  1224. PhysicsVector pos = new PhysicsVector(_position.X, _position.Y, _position.Z); //TODO: Insert values gotten from bullet
  1225. PhysicsVector vel = new PhysicsVector(_velocity.X, _velocity.Y, _velocity.Z);
  1226. _target_velocity =
  1227. new PhysicsVector(
  1228. (m_PIDTarget.X - pos.X) * ((PID_G - m_PIDTau) * timestep),
  1229. (m_PIDTarget.Y - pos.Y) * ((PID_G - m_PIDTau) * timestep),
  1230. (m_PIDTarget.Z - pos.Z) * ((PID_G - m_PIDTau) * timestep)
  1231. );
  1232. if (_target_velocity.IsIdentical(PhysicsVector.Zero, 0.1f))
  1233. {
  1234. /* TODO: Do Bullet equiv
  1235. *
  1236. d.BodySetPosition(Body, m_PIDTarget.X, m_PIDTarget.Y, m_PIDTarget.Z);
  1237. d.BodySetLinearVel(Body, 0, 0, 0);
  1238. d.BodyAddForce(Body, 0, 0, fz);
  1239. return;
  1240. */
  1241. }
  1242. else
  1243. {
  1244. _zeroFlag = false;
  1245. fx = ((_target_velocity.X) - vel.X) * (PID_D);
  1246. fy = ((_target_velocity.Y) - vel.Y) * (PID_D);
  1247. fz = fz + ((_target_velocity.Z - vel.Z) * (PID_D) * m_mass);
  1248. }
  1249. }
  1250. if (m_useHoverPID && !m_usePID)
  1251. {
  1252. // If we're using the PID controller, then we have no gravity
  1253. fz = (-1 * _parent_scene.gravityz) * m_mass;
  1254. // no lock; for now it's only called from within Simulate()
  1255. // If the PID Controller isn't active then we set our force
  1256. // calculating base velocity to the current position
  1257. if ((m_PIDTau < 1))
  1258. {
  1259. PID_G = PID_G / m_PIDTau;
  1260. }
  1261. if ((PID_G - m_PIDTau) <= 0)
  1262. {
  1263. PID_G = m_PIDTau + 1;
  1264. }
  1265. PhysicsVector pos = new PhysicsVector(0, 0, 0); //TODO: Insert values gotten from bullet
  1266. PhysicsVector vel = new PhysicsVector(0, 0, 0);
  1267. // determine what our target height really is based on HoverType
  1268. switch (m_PIDHoverType)
  1269. {
  1270. case PIDHoverType.Absolute:
  1271. m_targetHoverHeight = m_PIDHoverHeight;
  1272. break;
  1273. case PIDHoverType.Ground:
  1274. m_groundHeight = _parent_scene.GetTerrainHeightAtXY(pos.X, pos.Y);
  1275. m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight;
  1276. break;
  1277. case PIDHoverType.GroundAndWater:
  1278. m_groundHeight = _parent_scene.GetTerrainHeightAtXY(pos.X, pos.Y);
  1279. m_waterHeight = _parent_scene.GetWaterLevel();
  1280. if (m_groundHeight > m_waterHeight)
  1281. {
  1282. m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight;
  1283. }
  1284. else
  1285. {
  1286. m_targetHoverHeight = m_waterHeight + m_PIDHoverHeight;
  1287. }
  1288. break;
  1289. case PIDHoverType.Water:
  1290. m_waterHeight = _parent_scene.GetWaterLevel();
  1291. m_targetHoverHeight = m_waterHeight + m_PIDHoverHeight;
  1292. break;
  1293. }
  1294. _target_velocity =
  1295. new PhysicsVector(0.0f, 0.0f,
  1296. (m_targetHoverHeight - pos.Z) * ((PID_G - m_PIDHoverTau) * timestep)
  1297. );
  1298. // if velocity is zero, use position control; otherwise, velocity control
  1299. if (_target_velocity.IsIdentical(PhysicsVector.Zero, 0.1f))
  1300. {
  1301. /* TODO: Do Bullet Equiv
  1302. d.BodySetPosition(Body, pos.X, pos.Y, m_targetHoverHeight);
  1303. d.BodySetLinearVel(Body, vel.X, vel.Y, 0);
  1304. d.BodyAddForce(Body, 0, 0, fz);
  1305. */
  1306. if (Body != null && Body.Handle != IntPtr.Zero)
  1307. {
  1308. Body.setLinearVelocity(_parent_scene.VectorZero);
  1309. Body.clearForces();
  1310. }
  1311. return;
  1312. }
  1313. else
  1314. {
  1315. _zeroFlag = false;
  1316. // We're flying and colliding with something
  1317. fz = fz + ((_target_velocity.Z - vel.Z) * (PID_D) * m_mass);
  1318. }
  1319. }
  1320. fx *= m_mass;
  1321. fy *= m_mass;
  1322. //fz *= m_mass;
  1323. fx += m_force.X;
  1324. fy += m_force.Y;
  1325. fz += m_force.Z;
  1326. //m_log.Info("[OBJPID]: X:" + fx.ToString() + " Y:" + fy.ToString() + " Z:" + fz.ToString());
  1327. if (fx != 0 || fy != 0 || fz != 0)
  1328. {
  1329. /*
  1330. * TODO: Do Bullet Equiv
  1331. if (!d.BodyIsEnabled(Body))
  1332. {
  1333. d.BodySetLinearVel(Body, 0f, 0f, 0f);
  1334. d.BodySetForce(Body, 0, 0, 0);
  1335. enableBodySoft();
  1336. }
  1337. */
  1338. // 35x10 = 350n times the mass per second applied maximum.
  1339. float nmax = 35f * m_mass;
  1340. float nmin = -35f * m_mass;
  1341. if (fx > nmax)
  1342. fx = nmax;
  1343. if (fx < nmin)
  1344. fx = nmin;
  1345. if (fy > nmax)
  1346. fy = nmax;
  1347. if (fy < nmin)
  1348. fy = nmin;
  1349. // TODO: Do Bullet Equiv
  1350. // d.BodyAddForce(Body, fx, fy, fz);
  1351. if (Body != null && Body.Handle != IntPtr.Zero)
  1352. {
  1353. Body.activate(true);
  1354. if (tempAddForce != null && tempAddForce.Handle != IntPtr.Zero)
  1355. tempAddForce.Dispose();
  1356. tempAddForce = new btVector3(fx * 0.01f, fy * 0.01f, fz * 0.01f);
  1357. Body.applyCentralImpulse(tempAddForce);
  1358. }
  1359. }
  1360. }
  1361. else
  1362. {
  1363. if (m_zeroPosition == null)
  1364. m_zeroPosition = new PhysicsVector(0, 0, 0);
  1365. m_zeroPosition.setValues(_position.X,_position.Y,_position.Z);
  1366. return;
  1367. }
  1368. }
  1369. #region Mass Calculation
  1370. private float CalculateMass()
  1371. {
  1372. float volume = 0;
  1373. // No material is passed to the physics engines yet.. soo..
  1374. // we're using the m_density constant in the class definition
  1375. float returnMass = 0;
  1376. switch (_pbs.ProfileShape)
  1377. {
  1378. case ProfileShape.Square:
  1379. // Profile Volume
  1380. volume = _size.X * _size.Y * _size.Z;
  1381. // If the user has 'hollowed out'
  1382. // ProfileHollow is one of those 0 to 50000 values :P
  1383. // we like percentages better.. so turning into a percentage
  1384. if (((float)_pbs.ProfileHollow / 50000f) > 0.0)
  1385. {
  1386. float hollowAmount = (float)_pbs.ProfileHollow / 50000f;
  1387. // calculate the hollow volume by it's shape compared to the prim shape
  1388. float hollowVolume = 0;
  1389. switch (_pbs.HollowShape)
  1390. {
  1391. case HollowShape.Square:
  1392. case HollowShape.Same:
  1393. // Cube Hollow volume calculation
  1394. float hollowsizex = _size.X * hollowAmount;
  1395. float hollowsizey = _size.Y * hollowAmount;
  1396. float hollowsizez = _size.Z * hollowAmount;
  1397. hollowVolume = hollowsizex * hollowsizey * hollowsizez;
  1398. break;
  1399. case HollowShape.Circle:
  1400. // Hollow shape is a perfect cyllinder in respect to the cube's scale
  1401. // Cyllinder hollow volume calculation
  1402. float hRadius = _size.X / 2;
  1403. float hLength = _size.Z;
  1404. // pi * r2 * h
  1405. hollowVolume = ((float)(Math.PI * Math.Pow(hRadius, 2) * hLength) * hollowAmount);
  1406. break;
  1407. case HollowShape.Triangle:
  1408. // Equilateral Triangular Prism volume hollow calculation
  1409. // Triangle is an Equilateral Triangular Prism with aLength = to _size.Y
  1410. float aLength = _size.Y;
  1411. // 1/2 abh
  1412. hollowVolume = (float)((0.5 * aLength * _size.X * _size.Z) * hollowAmount);
  1413. break;
  1414. default:
  1415. hollowVolume = 0;
  1416. break;
  1417. }
  1418. volume = volume - hollowVolume;
  1419. }
  1420. break;
  1421. case ProfileShape.Circle:
  1422. if (_pbs.PathCurve == (byte)Extrusion.Straight)
  1423. {
  1424. // Cylinder
  1425. float volume1 = (float)(Math.PI * Math.Pow(_size.X / 2, 2) * _size.Z);
  1426. float volume2 = (float)(Math.PI * Math.Pow(_size.Y / 2, 2) * _size.Z);
  1427. // Approximating the cylinder's irregularity.
  1428. if (volume1 > volume2)
  1429. {
  1430. volume = (float)volume1 - (volume1 - volume2);
  1431. }
  1432. else if (volume2 > volume1)
  1433. {
  1434. volume = (float)volume2 - (volume2 - volume1);
  1435. }
  1436. else
  1437. {
  1438. // Regular cylinder
  1439. volume = volume1;
  1440. }
  1441. }
  1442. else
  1443. {
  1444. // We don't know what the shape is yet, so use default
  1445. volume = _size.X * _size.Y * _size.Z;
  1446. }
  1447. // If the user has 'hollowed out'
  1448. // ProfileHollow is one of those 0 to 50000 values :P
  1449. // we like percentages better.. so turning into a percentage
  1450. if (((float)_pbs.ProfileHollow / 50000f) > 0.0)
  1451. {
  1452. float hollowAmount = (float)_pbs.ProfileHollow / 50000f;
  1453. // calculate the hollow volume by it's shape compared to the prim shape
  1454. float hollowVolume = 0;
  1455. switch (_pbs.HollowShape)
  1456. {
  1457. case HollowShape.Same:
  1458. case HollowShape.Circle:
  1459. // Hollow shape is a perfect cyllinder in respect to the cube's scale
  1460. // Cyllinder hollow volume calculation
  1461. float hRadius = _size.X / 2;
  1462. float hLength = _size.Z;
  1463. // pi * r2 * h
  1464. hollowVolume = ((float)(Math.PI * Math.Pow(hRadius, 2) * hLength) * hollowAmount);
  1465. break;
  1466. case HollowShape.Square:
  1467. // Cube Hollow volume calculation
  1468. float hollowsizex = _size.X * hollowAmount;
  1469. float hollowsizey = _size.Y * hollowAmount;
  1470. float hollowsizez = _size.Z * hollowAmount;
  1471. hollowVolume = hollowsizex * hollowsizey * hollowsizez;
  1472. break;
  1473. case HollowShape.Triangle:
  1474. // Equilateral Triangular Prism volume hollow calculation
  1475. // Triangle is an Equilateral Triangular Prism with aLength = to _size.Y
  1476. float aLength = _size.Y;
  1477. // 1/2 abh
  1478. hollowVolume = (float)((0.5 * aLength * _size.X * _size.Z) * hollowAmount);
  1479. break;
  1480. default:
  1481. hollowVolume = 0;
  1482. break;
  1483. }
  1484. volume = volume - hollowVolume;
  1485. }
  1486. break;
  1487. case ProfileShape.HalfCircle:
  1488. if (_pbs.PathCurve == (byte)Extrusion.Curve1)
  1489. {
  1490. if (_size.X == _size.Y && _size.Z == _size.X)
  1491. {
  1492. // regular sphere
  1493. // v = 4/3 * pi * r^3
  1494. float sradius3 = (float)Math.Pow((_size.X / 2), 3);
  1495. volume = (float)((4 / 3f) * Math.PI * sradius3);
  1496. }
  1497. else
  1498. {
  1499. // we treat this as a box currently
  1500. volume = _size.X * _size.Y * _size.Z;
  1501. }
  1502. }
  1503. else
  1504. {
  1505. // We don't know what the shape is yet, so use default
  1506. volume = _size.X * _size.Y * _size.Z;
  1507. }
  1508. break;
  1509. case ProfileShape.EquilateralTriangle:
  1510. /*
  1511. v = (abs((xB*yA-xA*yB)+(xC*yB-xB*yC)+(xA*yC-xC*yA))/2) * h
  1512. // seed mesh
  1513. Vertex MM = new Vertex(-0.25f, -0.45f, 0.0f);
  1514. Vertex PM = new Vertex(+0.5f, 0f, 0.0f);
  1515. Vertex PP = new Vertex(-0.25f, +0.45f, 0.0f);
  1516. */
  1517. float xA = -0.25f * _size.X;
  1518. float yA = -0.45f * _size.Y;
  1519. float xB = 0.5f * _size.X;
  1520. float yB = 0;
  1521. float xC = -0.25f * _size.X;
  1522. float yC = 0.45f * _size.Y;
  1523. volume = (float)((Math.Abs((xB * yA - xA * yB) + (xC * yB - xB * yC) + (xA * yC - xC * yA)) / 2) * _size.Z);
  1524. // If the user has 'hollowed out'
  1525. // ProfileHollow is one of those 0 to 50000 values :P
  1526. // we like percentages better.. so turning into a percentage
  1527. float fhollowFactor = ((float)_pbs.ProfileHollow / 1.9f);
  1528. if (((float)fhollowFactor / 50000f) > 0.0)
  1529. {
  1530. float hollowAmount = (float)fhollowFactor / 50000f;
  1531. // calculate the hollow volume by it's shape compared to the prim shape
  1532. float hollowVolume = 0;
  1533. switch (_pbs.HollowShape)
  1534. {
  1535. case HollowShape.Same:
  1536. case HollowShape.Triangle:
  1537. // Equilateral Triangular Prism volume hollow calculation
  1538. // Triangle is an Equilateral Triangular Prism with aLength = to _size.Y
  1539. float aLength = _size.Y;
  1540. // 1/2 abh
  1541. hollowVolume = (float)((0.5 * aLength * _size.X * _size.Z) * hollowAmount);
  1542. break;
  1543. case HollowShape.Square:
  1544. // Cube Hollow volume calculation
  1545. float hollowsizex = _size.X * hollowAmount;
  1546. float hollowsizey = _size.Y * hollowAmount;
  1547. float hollowsizez = _size.Z * hollowAmount;
  1548. hollowVolume = hollowsizex * hollowsizey * hollowsizez;
  1549. break;
  1550. case HollowShape.Circle:
  1551. // Hollow shape is a perfect cyllinder in respect to the cube's scale
  1552. // Cyllinder hollow volume calculation
  1553. float hRadius = _size.X / 2;
  1554. float hLength = _size.Z;
  1555. // pi * r2 * h
  1556. hollowVolume = ((float)((Math.PI * Math.Pow(hRadius, 2) * hLength) / 2) * hollowAmount);
  1557. break;
  1558. default:
  1559. hollowVolume = 0;
  1560. break;
  1561. }
  1562. volume = volume - hollowVolume;
  1563. }
  1564. break;
  1565. default:
  1566. // we don't have all of the volume formulas yet so
  1567. // use the common volume formula for all
  1568. volume = _size.X * _size.Y * _size.Z;
  1569. break;
  1570. }
  1571. // Calculate Path cut effect on volume
  1572. // Not exact, in the triangle hollow example
  1573. // They should never be zero or less then zero..
  1574. // we'll ignore it if it's less then zero
  1575. // ProfileEnd and ProfileBegin are values
  1576. // from 0 to 50000
  1577. // Turning them back into percentages so that I can cut that percentage off the volume
  1578. float PathCutEndAmount = _pbs.ProfileEnd;
  1579. float PathCutStartAmount = _pbs.ProfileBegin;
  1580. if (((PathCutStartAmount + PathCutEndAmount) / 50000f) > 0.0f)
  1581. {
  1582. float pathCutAmount = ((PathCutStartAmount + PathCutEndAmount) / 50000f);
  1583. // Check the return amount for sanity
  1584. if (pathCutAmount >= 0.99f)
  1585. pathCutAmount = 0.99f;
  1586. volume = volume - (volume * pathCutAmount);
  1587. }
  1588. UInt16 taperX = _pbs.PathScaleX;
  1589. UInt16 taperY = _pbs.PathScaleY;
  1590. float taperFactorX = 0;
  1591. float taperFactorY = 0;
  1592. // Mass = density * volume
  1593. if (taperX != 100)
  1594. {
  1595. if (taperX > 100)
  1596. {
  1597. taperFactorX = 1.0f - ((float)taperX / 200);
  1598. //m_log.Warn("taperTopFactorX: " + extr.taperTopFactorX.ToString());
  1599. }
  1600. else
  1601. {
  1602. taperFactorX = 1.0f - ((100 - (float)taperX) / 100);
  1603. //m_log.Warn("taperBotFactorX: " + extr.taperBotFactorX.ToString());
  1604. }
  1605. volume = (float)volume * ((taperFactorX / 3f) + 0.001f);
  1606. }
  1607. if (taperY != 100)
  1608. {
  1609. if (taperY > 100)
  1610. {
  1611. taperFactorY = 1.0f - ((float)taperY / 200);
  1612. //m_log.Warn("taperTopFactorY: " + extr.taperTopFactorY.ToString());
  1613. }
  1614. else
  1615. {
  1616. taperFactorY = 1.0f - ((100 - (float)taperY) / 100);
  1617. //m_log.Warn("taperBotFactorY: " + extr.taperBotFactorY.ToString());
  1618. }
  1619. volume = (float)volume * ((taperFactorY / 3f) + 0.001f);
  1620. }
  1621. returnMass = m_density * volume;
  1622. if (returnMass <= 0) returnMass = 0.0001f;//ckrinke: Mass must be greater then zero.
  1623. // Recursively calculate mass
  1624. bool HasChildPrim = false;
  1625. lock (childrenPrim)
  1626. {
  1627. if (childrenPrim.Count > 0)
  1628. {
  1629. HasChildPrim = true;
  1630. }
  1631. }
  1632. if (HasChildPrim)
  1633. {
  1634. BulletDotNETPrim[] childPrimArr = new BulletDotNETPrim[0];
  1635. lock (childrenPrim)
  1636. childPrimArr = childrenPrim.ToArray();
  1637. for (int i = 0; i < childPrimArr.Length; i++)
  1638. {
  1639. if (childPrimArr[i] != null && !childPrimArr[i].m_taintremove)
  1640. returnMass += childPrimArr[i].CalculateMass();
  1641. // failsafe, this shouldn't happen but with OpenSim, you never know :)
  1642. if (i > 256)
  1643. break;
  1644. }
  1645. }
  1646. return returnMass;
  1647. }
  1648. #endregion
  1649. public void CreateGeom(IntPtr m_targetSpace, IMesh p_mesh)
  1650. {
  1651. m_log.Debug("[PHYSICS]: _________CreateGeom");
  1652. if (p_mesh != null)
  1653. {
  1654. //_mesh = _parent_scene.mesher.CreateMesh(m_primName, _pbs, _size, _parent_scene.meshSculptLOD, IsPhysical);
  1655. _mesh = p_mesh;
  1656. setMesh(_parent_scene, _mesh);
  1657. }
  1658. else
  1659. {
  1660. if (_pbs.ProfileShape == ProfileShape.HalfCircle && _pbs.PathCurve == (byte)Extrusion.Curve1)
  1661. {
  1662. if (_size.X == _size.Y && _size.Y == _size.Z && _size.X == _size.Z)
  1663. {
  1664. if (((_size.X / 2f) > 0f))
  1665. {
  1666. //SetGeom to a Regular Sphere
  1667. if (tempSize1 == null)
  1668. tempSize1 = new btVector3(0, 0, 0);
  1669. tempSize1.setValue(_size.X * 0.5f,_size.Y * 0.5f, _size.Z * 0.5f);
  1670. SetCollisionShape(new btSphereShape(_size.X*0.5f));
  1671. }
  1672. else
  1673. {
  1674. // uses halfextents
  1675. if (tempSize1 == null)
  1676. tempSize1 = new btVector3(0, 0, 0);
  1677. tempSize1.setValue(_size.X*0.5f, _size.Y*0.5f, _size.Z*0.5f);
  1678. SetCollisionShape(new btBoxShape(tempSize1));
  1679. }
  1680. }
  1681. else
  1682. {
  1683. // uses halfextents
  1684. if (tempSize1 == null)
  1685. tempSize1 = new btVector3(0, 0, 0);
  1686. tempSize1.setValue(_size.X * 0.5f, _size.Y * 0.5f, _size.Z * 0.5f);
  1687. SetCollisionShape(new btBoxShape(tempSize1));
  1688. }
  1689. }
  1690. else
  1691. {
  1692. if (tempSize1 == null)
  1693. tempSize1 = new btVector3(0, 0, 0);
  1694. // uses halfextents
  1695. tempSize1.setValue(_size.X * 0.5f, _size.Y * 0.5f, _size.Z * 0.5f);
  1696. SetCollisionShape(new btBoxShape(tempSize1));
  1697. }
  1698. }
  1699. }
  1700. private void setMesh(BulletDotNETScene _parent_scene, IMesh mesh)
  1701. {
  1702. // TODO: Set Collision Body Mesh
  1703. // This sleeper is there to moderate how long it takes between
  1704. // setting up the mesh and pre-processing it when we get rapid fire mesh requests on a single object
  1705. m_log.Debug("_________SetMesh");
  1706. Thread.Sleep(10);
  1707. //Kill Body so that mesh can re-make the geom
  1708. if (IsPhysical && Body != null && Body.Handle != IntPtr.Zero)
  1709. {
  1710. if (childPrim)
  1711. {
  1712. if (_parent != null)
  1713. {
  1714. BulletDotNETPrim parent = (BulletDotNETPrim)_parent;
  1715. parent.ChildDelink(this);
  1716. }
  1717. }
  1718. else
  1719. {
  1720. //disableBody();
  1721. }
  1722. }
  1723. IMesh oldMesh = primMesh;
  1724. primMesh = mesh;
  1725. float[] vertexList = primMesh.getVertexListAsFloatLocked(); // Note, that vertextList is pinned in memory
  1726. int[] indexList = primMesh.getIndexListAsIntLocked(); // Also pinned, needs release after usage
  1727. //Array.Reverse(indexList);
  1728. primMesh.releaseSourceMeshData(); // free up the original mesh data to save memory
  1729. int VertexCount = vertexList.GetLength(0) / 3;
  1730. int IndexCount = indexList.GetLength(0);
  1731. if (btshapeArray != null && btshapeArray.Handle != IntPtr.Zero)
  1732. btshapeArray.Dispose();
  1733. //Array.Reverse(indexList);
  1734. btshapeArray = new btTriangleIndexVertexArray(IndexCount / 3, indexList, (3 * sizeof(int)),
  1735. VertexCount, vertexList, 3*sizeof (float));
  1736. SetCollisionShape(new btGImpactMeshShape(btshapeArray));
  1737. //((btGImpactMeshShape) prim_geom).updateBound();
  1738. ((btGImpactMeshShape)prim_geom).setLocalScaling(new btVector3(1,1, 1));
  1739. ((btGImpactMeshShape)prim_geom).updateBound();
  1740. _parent_scene.SetUsingGImpact();
  1741. if (oldMesh != null)
  1742. {
  1743. oldMesh.releasePinned();
  1744. oldMesh = null;
  1745. }
  1746. }
  1747. private void SetCollisionShape(btCollisionShape shape)
  1748. {
  1749. /*
  1750. if (shape == null)
  1751. m_log.Debug("[PHYSICS]:SetShape!Null");
  1752. else
  1753. m_log.Debug("[PHYSICS]:SetShape!");
  1754. if (Body != null)
  1755. {
  1756. DisposeOfBody();
  1757. }
  1758. if (prim_geom != null)
  1759. {
  1760. prim_geom.Dispose();
  1761. prim_geom = null;
  1762. }
  1763. */
  1764. prim_geom = shape;
  1765. //Body.set
  1766. }
  1767. public void SetBody(float mass)
  1768. {
  1769. if (!IsPhysical || childrenPrim.Count == 0)
  1770. {
  1771. if (tempMotionState1 != null && tempMotionState1.Handle != IntPtr.Zero)
  1772. tempMotionState1.Dispose();
  1773. if (tempTransform2 != null && tempTransform2.Handle != IntPtr.Zero)
  1774. tempTransform2.Dispose();
  1775. if (tempOrientation2 != null && tempOrientation2.Handle != IntPtr.Zero)
  1776. tempOrientation2.Dispose();
  1777. if (tempPosition2 != null && tempPosition2.Handle != IntPtr.Zero)
  1778. tempPosition2.Dispose();
  1779. tempOrientation2 = new btQuaternion(_orientation.X, _orientation.Y, _orientation.Z, _orientation.W);
  1780. tempPosition2 = new btVector3(_position.X, _position.Y, _position.Z);
  1781. tempTransform2 = new btTransform(tempOrientation2, tempPosition2);
  1782. tempMotionState1 = new btDefaultMotionState(tempTransform2, _parent_scene.TransZero);
  1783. if (tempInertia1 != null && tempInertia1.Handle != IntPtr.Zero)
  1784. tempInertia1.Dispose();
  1785. tempInertia1 = new btVector3(0, 0, 0);
  1786. prim_geom.calculateLocalInertia(mass, tempInertia1);
  1787. if (mass != 0)
  1788. _parent_scene.addActivePrim(this);
  1789. else
  1790. _parent_scene.remActivePrim(this);
  1791. // Body = new btRigidBody(mass, tempMotionState1, prim_geom);
  1792. //else
  1793. Body = new btRigidBody(mass, tempMotionState1, prim_geom, tempInertia1);
  1794. if (prim_geom is btGImpactMeshShape)
  1795. {
  1796. ((btGImpactMeshShape) prim_geom).setLocalScaling(new btVector3(1, 1, 1));
  1797. ((btGImpactMeshShape) prim_geom).updateBound();
  1798. }
  1799. //Body.setCollisionFlags(Body.getCollisionFlags() | (int)ContactFlags.CF_CUSTOM_MATERIAL_CALLBACK);
  1800. //Body.setUserPointer((IntPtr) (int)m_localID);
  1801. _parent_scene.AddPrimToScene(this);
  1802. }
  1803. else
  1804. {
  1805. // bool hasTrimesh = false;
  1806. lock (childrenPrim)
  1807. {
  1808. foreach (BulletDotNETPrim chld in childrenPrim)
  1809. {
  1810. if (chld == null)
  1811. continue;
  1812. // if (chld.NeedsMeshing())
  1813. // hasTrimesh = true;
  1814. }
  1815. }
  1816. //if (hasTrimesh)
  1817. //{
  1818. ProcessGeomCreationAsTriMesh(PhysicsVector.Zero, Quaternion.Identity);
  1819. // createmesh returns null when it doesn't mesh.
  1820. /*
  1821. if (_mesh is Mesh)
  1822. {
  1823. }
  1824. else
  1825. {
  1826. m_log.Warn("[PHYSICS]: Can't link a OpenSim.Region.Physics.Meshing.Mesh object");
  1827. return;
  1828. }
  1829. */
  1830. foreach (BulletDotNETPrim chld in childrenPrim)
  1831. {
  1832. if (chld == null)
  1833. continue;
  1834. PhysicsVector offset = chld.Position - Position;
  1835. Vector3 pos = new Vector3(offset.X, offset.Y, offset.Z);
  1836. pos *= Quaternion.Inverse(Orientation);
  1837. //pos *= Orientation;
  1838. offset.setValues(pos.X, pos.Y, pos.Z);
  1839. chld.ProcessGeomCreationAsTriMesh(offset, chld.Orientation);
  1840. _mesh.Append(chld._mesh);
  1841. }
  1842. setMesh(_parent_scene, _mesh);
  1843. //}
  1844. if (tempMotionState1 != null && tempMotionState1.Handle != IntPtr.Zero)
  1845. tempMotionState1.Dispose();
  1846. if (tempTransform2 != null && tempTransform2.Handle != IntPtr.Zero)
  1847. tempTransform2.Dispose();
  1848. if (tempOrientation2 != null && tempOrientation2.Handle != IntPtr.Zero)
  1849. tempOrientation2.Dispose();
  1850. if (tempPosition2 != null && tempPosition2.Handle != IntPtr.Zero)
  1851. tempPosition2.Dispose();
  1852. tempOrientation2 = new btQuaternion(_orientation.X, _orientation.Y, _orientation.Z, _orientation.W);
  1853. tempPosition2 = new btVector3(_position.X, _position.Y, _position.Z);
  1854. tempTransform2 = new btTransform(tempOrientation2, tempPosition2);
  1855. tempMotionState1 = new btDefaultMotionState(tempTransform2, _parent_scene.TransZero);
  1856. if (tempInertia1 != null && tempInertia1.Handle != IntPtr.Zero)
  1857. tempInertia1.Dispose();
  1858. tempInertia1 = new btVector3(0, 0, 0);
  1859. prim_geom.calculateLocalInertia(mass, tempInertia1);
  1860. if (mass != 0)
  1861. _parent_scene.addActivePrim(this);
  1862. else
  1863. _parent_scene.remActivePrim(this);
  1864. // Body = new btRigidBody(mass, tempMotionState1, prim_geom);
  1865. //else
  1866. Body = new btRigidBody(mass, tempMotionState1, prim_geom, tempInertia1);
  1867. if (prim_geom is btGImpactMeshShape)
  1868. {
  1869. ((btGImpactMeshShape)prim_geom).setLocalScaling(new btVector3(1, 1, 1));
  1870. ((btGImpactMeshShape)prim_geom).updateBound();
  1871. }
  1872. _parent_scene.AddPrimToScene(this);
  1873. }
  1874. if (IsPhysical)
  1875. changeAngularLock(0);
  1876. }
  1877. private void DisposeOfBody()
  1878. {
  1879. if (Body != null)
  1880. {
  1881. if (Body.Handle != IntPtr.Zero)
  1882. {
  1883. DisableAxisMotor();
  1884. _parent_scene.removeFromWorld(this,Body);
  1885. Body.Dispose();
  1886. }
  1887. Body = null;
  1888. // TODO: dispose parts that make up body
  1889. }
  1890. }
  1891. private void ChildDelink(BulletDotNETPrim pPrim)
  1892. {
  1893. // Okay, we have a delinked child.. need to rebuild the body.
  1894. lock (childrenPrim)
  1895. {
  1896. foreach (BulletDotNETPrim prm in childrenPrim)
  1897. {
  1898. prm.childPrim = true;
  1899. prm.disableBody();
  1900. }
  1901. }
  1902. disableBody();
  1903. lock (childrenPrim)
  1904. {
  1905. childrenPrim.Remove(pPrim);
  1906. }
  1907. if (Body != null && Body.Handle != IntPtr.Zero)
  1908. {
  1909. _parent_scene.remActivePrim(this);
  1910. }
  1911. lock (childrenPrim)
  1912. {
  1913. foreach (BulletDotNETPrim prm in childrenPrim)
  1914. {
  1915. ParentPrim(prm);
  1916. }
  1917. }
  1918. }
  1919. internal void ParentPrim(BulletDotNETPrim prm)
  1920. {
  1921. if (prm == null)
  1922. return;
  1923. lock (childrenPrim)
  1924. {
  1925. if (!childrenPrim.Contains(prm))
  1926. {
  1927. childrenPrim.Add(prm);
  1928. }
  1929. }
  1930. }
  1931. public void disableBody()
  1932. {
  1933. //this kills the body so things like 'mesh' can re-create it.
  1934. /*
  1935. lock (this)
  1936. {
  1937. if (!childPrim)
  1938. {
  1939. if (Body != null && Body.Handle != IntPtr.Zero)
  1940. {
  1941. _parent_scene.remActivePrim(this);
  1942. m_collisionCategories &= ~CollisionCategories.Body;
  1943. m_collisionFlags &= ~(CollisionCategories.Wind | CollisionCategories.Land);
  1944. if (prim_geom != null && prim_geom.Handle != IntPtr.Zero)
  1945. {
  1946. // TODO: Set Category bits and Flags
  1947. }
  1948. // TODO: destroy body
  1949. DisposeOfBody();
  1950. lock (childrenPrim)
  1951. {
  1952. if (childrenPrim.Count > 0)
  1953. {
  1954. foreach (BulletDotNETPrim prm in childrenPrim)
  1955. {
  1956. _parent_scene.remActivePrim(prm);
  1957. prm.DisposeOfBody();
  1958. prm.SetCollisionShape(null);
  1959. }
  1960. }
  1961. }
  1962. DisposeOfBody();
  1963. }
  1964. }
  1965. else
  1966. {
  1967. _parent_scene.remActivePrim(this);
  1968. m_collisionCategories &= ~CollisionCategories.Body;
  1969. m_collisionFlags &= ~(CollisionCategories.Wind | CollisionCategories.Land);
  1970. if (prim_geom != null && prim_geom.Handle != IntPtr.Zero)
  1971. {
  1972. // TODO: Set Category bits and Flags
  1973. }
  1974. DisposeOfBody();
  1975. }
  1976. }
  1977. */
  1978. DisableAxisMotor();
  1979. m_disabled = true;
  1980. m_collisionscore = 0;
  1981. }
  1982. public void disableBodySoft()
  1983. {
  1984. m_disabled = true;
  1985. if (m_isphysical && Body.Handle != IntPtr.Zero)
  1986. {
  1987. Body.clearForces();
  1988. Body.forceActivationState(0);
  1989. }
  1990. }
  1991. public void enableBodySoft()
  1992. {
  1993. if (!childPrim)
  1994. {
  1995. if (m_isphysical && Body.Handle != IntPtr.Zero)
  1996. {
  1997. Body.clearForces();
  1998. Body.forceActivationState(4);
  1999. forceenable = true;
  2000. }
  2001. m_disabled = false;
  2002. }
  2003. }
  2004. public void enableBody()
  2005. {
  2006. if (!childPrim)
  2007. {
  2008. //SetCollisionShape(prim_geom);
  2009. if (IsPhysical)
  2010. SetBody(Mass);
  2011. else
  2012. SetBody(0);
  2013. // TODO: Set Collision Category Bits and Flags
  2014. // TODO: Set Auto Disable data
  2015. m_interpenetrationcount = 0;
  2016. m_collisionscore = 0;
  2017. m_disabled = false;
  2018. // The body doesn't already have a finite rotation mode set here
  2019. if ((!m_angularlock.IsIdentical(PhysicsVector.Zero, 0)) && _parent == null)
  2020. {
  2021. // TODO: Create Angular Motor on Axis Lock!
  2022. }
  2023. _parent_scene.addActivePrim(this);
  2024. }
  2025. }
  2026. public void UpdatePositionAndVelocity()
  2027. {
  2028. if (!m_isSelected)
  2029. {
  2030. if (_parent == null)
  2031. {
  2032. PhysicsVector pv = new PhysicsVector(0, 0, 0);
  2033. bool lastZeroFlag = _zeroFlag;
  2034. if (tempPosition3 != null && tempPosition3.Handle != IntPtr.Zero)
  2035. tempPosition3.Dispose();
  2036. if (tempTransform3 != null && tempTransform3.Handle != IntPtr.Zero)
  2037. tempTransform3.Dispose();
  2038. if (tempOrientation2 != null && tempOrientation2.Handle != IntPtr.Zero)
  2039. tempOrientation2.Dispose();
  2040. if (tempAngularVelocity1 != null && tempAngularVelocity1.Handle != IntPtr.Zero)
  2041. tempAngularVelocity1.Dispose();
  2042. if (tempLinearVelocity1 != null && tempLinearVelocity1.Handle != IntPtr.Zero)
  2043. tempLinearVelocity1.Dispose();
  2044. tempTransform3 = Body.getInterpolationWorldTransform();
  2045. tempPosition3 = tempTransform3.getOrigin(); // vec
  2046. tempOrientation2 = tempTransform3.getRotation(); // ori
  2047. tempAngularVelocity1 = Body.getInterpolationAngularVelocity(); //rotvel
  2048. tempLinearVelocity1 = Body.getInterpolationLinearVelocity(); // vel
  2049. _torque.setValues(tempAngularVelocity1.getX(), tempAngularVelocity1.getX(),
  2050. tempAngularVelocity1.getZ());
  2051. PhysicsVector l_position = new PhysicsVector();
  2052. Quaternion l_orientation = new Quaternion();
  2053. m_lastposition = _position;
  2054. m_lastorientation = _orientation;
  2055. l_position.X = tempPosition3.getX();
  2056. l_position.Y = tempPosition3.getY();
  2057. l_position.Z = tempPosition3.getZ();
  2058. l_orientation.X = tempOrientation2.getX();
  2059. l_orientation.Y = tempOrientation2.getY();
  2060. l_orientation.Z = tempOrientation2.getZ();
  2061. l_orientation.W = tempOrientation2.getW();
  2062. if (l_position.X > 255.95f || l_position.X < 0f || l_position.Y > 255.95f || l_position.Y < 0f)
  2063. {
  2064. //base.RaiseOutOfBounds(l_position);
  2065. if (m_crossingfailures < _parent_scene.geomCrossingFailuresBeforeOutofbounds)
  2066. {
  2067. _position = l_position;
  2068. //_parent_scene.remActivePrim(this);
  2069. if (_parent == null)
  2070. base.RequestPhysicsterseUpdate();
  2071. return;
  2072. }
  2073. else
  2074. {
  2075. if (_parent == null)
  2076. base.RaiseOutOfBounds(l_position);
  2077. return;
  2078. }
  2079. }
  2080. if (l_position.Z < -200000f)
  2081. {
  2082. // This is so prim that get lost underground don't fall forever and suck up
  2083. //
  2084. // Sim resources and memory.
  2085. // Disables the prim's movement physics....
  2086. // It's a hack and will generate a console message if it fails.
  2087. //IsPhysical = false;
  2088. //if (_parent == null)
  2089. //base.RaiseOutOfBounds(_position);
  2090. _acceleration.X = 0;
  2091. _acceleration.Y = 0;
  2092. _acceleration.Z = 0;
  2093. _velocity.X = 0;
  2094. _velocity.Y = 0;
  2095. _velocity.Z = 0;
  2096. m_rotationalVelocity.X = 0;
  2097. m_rotationalVelocity.Y = 0;
  2098. m_rotationalVelocity.Z = 0;
  2099. if (_parent == null)
  2100. base.RequestPhysicsterseUpdate();
  2101. m_throttleUpdates = false;
  2102. // throttleCounter = 0;
  2103. _zeroFlag = true;
  2104. //outofBounds = true;
  2105. }
  2106. if ((Math.Abs(m_lastposition.X - l_position.X) < 0.02)
  2107. && (Math.Abs(m_lastposition.Y - l_position.Y) < 0.02)
  2108. && (Math.Abs(m_lastposition.Z - l_position.Z) < 0.02)
  2109. && (1.0 - Math.Abs(Quaternion.Dot(m_lastorientation, l_orientation)) < 0.01))
  2110. {
  2111. _zeroFlag = true;
  2112. m_throttleUpdates = false;
  2113. }
  2114. else
  2115. {
  2116. //m_log.Debug(Math.Abs(m_lastposition.X - l_position.X).ToString());
  2117. _zeroFlag = false;
  2118. }
  2119. if (_zeroFlag)
  2120. {
  2121. _velocity.X = 0.0f;
  2122. _velocity.Y = 0.0f;
  2123. _velocity.Z = 0.0f;
  2124. _acceleration.X = 0;
  2125. _acceleration.Y = 0;
  2126. _acceleration.Z = 0;
  2127. //_orientation.w = 0f;
  2128. //_orientation.X = 0f;
  2129. //_orientation.Y = 0f;
  2130. //_orientation.Z = 0f;
  2131. m_rotationalVelocity.X = 0;
  2132. m_rotationalVelocity.Y = 0;
  2133. m_rotationalVelocity.Z = 0;
  2134. if (!m_lastUpdateSent)
  2135. {
  2136. m_throttleUpdates = false;
  2137. // throttleCounter = 0;
  2138. m_rotationalVelocity = pv;
  2139. if (_parent == null)
  2140. base.RequestPhysicsterseUpdate();
  2141. m_lastUpdateSent = true;
  2142. }
  2143. }
  2144. else
  2145. {
  2146. if (lastZeroFlag != _zeroFlag)
  2147. {
  2148. if (_parent == null)
  2149. base.RequestPhysicsterseUpdate();
  2150. }
  2151. m_lastVelocity = _velocity;
  2152. _position = l_position;
  2153. _velocity.X = tempLinearVelocity1.getX();
  2154. _velocity.Y = tempLinearVelocity1.getY();
  2155. _velocity.Z = tempLinearVelocity1.getZ();
  2156. _acceleration = ((_velocity - m_lastVelocity)/0.1f);
  2157. _acceleration = new PhysicsVector(_velocity.X - m_lastVelocity.X/0.1f,
  2158. _velocity.Y - m_lastVelocity.Y/0.1f,
  2159. _velocity.Z - m_lastVelocity.Z/0.1f);
  2160. //m_log.Info("[PHYSICS]: V1: " + _velocity + " V2: " + m_lastVelocity + " Acceleration: " + _acceleration.ToString());
  2161. if (_velocity.IsIdentical(pv, 0.5f))
  2162. {
  2163. m_rotationalVelocity = pv;
  2164. }
  2165. else
  2166. {
  2167. m_rotationalVelocity.setValues(tempAngularVelocity1.getX(), tempAngularVelocity1.getY(),
  2168. tempAngularVelocity1.getZ());
  2169. }
  2170. //m_log.Debug("ODE: " + m_rotationalVelocity.ToString());
  2171. _orientation.X = l_orientation.X;
  2172. _orientation.Y = l_orientation.Y;
  2173. _orientation.Z = l_orientation.Z;
  2174. _orientation.W = l_orientation.W;
  2175. m_lastUpdateSent = false;
  2176. //if (!m_throttleUpdates || throttleCounter > _parent_scene.geomUpdatesPerThrottledUpdate)
  2177. //{
  2178. if (_parent == null)
  2179. base.RequestPhysicsterseUpdate();
  2180. // }
  2181. // else
  2182. // {
  2183. // throttleCounter++;
  2184. //}
  2185. }
  2186. m_lastposition = l_position;
  2187. if (forceenable)
  2188. {
  2189. Body.forceActivationState(1);
  2190. forceenable = false;
  2191. }
  2192. }
  2193. else
  2194. {
  2195. // Not a body.. so Make sure the client isn't interpolating
  2196. _velocity.X = 0;
  2197. _velocity.Y = 0;
  2198. _velocity.Z = 0;
  2199. _acceleration.X = 0;
  2200. _acceleration.Y = 0;
  2201. _acceleration.Z = 0;
  2202. m_rotationalVelocity.X = 0;
  2203. m_rotationalVelocity.Y = 0;
  2204. m_rotationalVelocity.Z = 0;
  2205. _zeroFlag = true;
  2206. }
  2207. }
  2208. }
  2209. internal void setPrimForRemoval()
  2210. {
  2211. m_taintremove = true;
  2212. }
  2213. internal void EnableAxisMotor(PhysicsVector axislock)
  2214. {
  2215. if (m_aMotor != null)
  2216. DisableAxisMotor();
  2217. if (Body == null)
  2218. return;
  2219. if (Body.Handle == IntPtr.Zero)
  2220. return;
  2221. if (AxisLockAngleHigh != null && AxisLockAngleHigh.Handle != IntPtr.Zero)
  2222. AxisLockAngleHigh.Dispose();
  2223. m_aMotor = new btGeneric6DofConstraint(Body, _parent_scene.TerrainBody, _parent_scene.TransZero,
  2224. _parent_scene.TransZero, false);
  2225. float endNoLock = (360 * Utils.DEG_TO_RAD);
  2226. AxisLockAngleHigh = new btVector3((axislock.X == 0) ? 0 : endNoLock, (axislock.Y == 0) ? 0 : endNoLock, (axislock.Z == 0) ? 0 : endNoLock);
  2227. m_aMotor.setAngularLowerLimit(_parent_scene.VectorZero);
  2228. m_aMotor.setAngularUpperLimit(AxisLockAngleHigh);
  2229. m_aMotor.setLinearLowerLimit(AxisLockLinearLow);
  2230. m_aMotor.setLinearUpperLimit(AxisLockLinearHigh);
  2231. _parent_scene.getBulletWorld().addConstraint((btTypedConstraint)m_aMotor);
  2232. //m_aMotor.
  2233. }
  2234. internal void DisableAxisMotor()
  2235. {
  2236. if (m_aMotor != null && m_aMotor.Handle != IntPtr.Zero)
  2237. {
  2238. _parent_scene.getBulletWorld().removeConstraint(m_aMotor);
  2239. m_aMotor.Dispose();
  2240. m_aMotor = null;
  2241. }
  2242. }
  2243. }
  2244. }