BulletDotNETPrim.cs 94 KB

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