BulletDotNETPrim.cs 96 KB

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