BSAPIXNA.cs 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325
  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 copyrightD
  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.IO;
  30. using System.Runtime.InteropServices;
  31. using System.Text;
  32. using OpenSim.Framework;
  33. using OpenMetaverse;
  34. using BulletXNA;
  35. using BulletXNA.LinearMath;
  36. using BulletXNA.BulletCollision;
  37. using BulletXNA.BulletDynamics;
  38. using BulletXNA.BulletCollision.CollisionDispatch;
  39. namespace OpenSim.Region.Physics.BulletSPlugin
  40. {
  41. public sealed class BSAPIXNA : BSAPITemplate
  42. {
  43. private sealed class BulletWorldXNA : BulletWorld
  44. {
  45. public DiscreteDynamicsWorld world;
  46. public BulletWorldXNA(uint id, BSScene physScene, DiscreteDynamicsWorld xx)
  47. : base(id, physScene)
  48. {
  49. world = xx;
  50. }
  51. }
  52. private sealed class BulletBodyXNA : BulletBody
  53. {
  54. public CollisionObject body;
  55. public RigidBody rigidBody { get { return RigidBody.Upcast(body); } }
  56. public BulletBodyXNA(uint id, CollisionObject xx)
  57. : base(id)
  58. {
  59. body = xx;
  60. }
  61. public override bool HasPhysicalBody
  62. {
  63. get { return body != null; }
  64. }
  65. public override void Clear()
  66. {
  67. body = null;
  68. }
  69. public override string AddrString
  70. {
  71. get { return "XNARigidBody"; }
  72. }
  73. }
  74. private sealed class BulletShapeXNA : BulletShape
  75. {
  76. public CollisionShape shape;
  77. public BulletShapeXNA(CollisionShape xx, BSPhysicsShapeType typ)
  78. : base()
  79. {
  80. shape = xx;
  81. shapeType = typ;
  82. }
  83. public override bool HasPhysicalShape
  84. {
  85. get { return shape != null; }
  86. }
  87. public override void Clear()
  88. {
  89. shape = null;
  90. }
  91. public override BulletShape Clone()
  92. {
  93. return new BulletShapeXNA(shape, shapeType);
  94. }
  95. public override bool ReferenceSame(BulletShape other)
  96. {
  97. BulletShapeXNA otheru = other as BulletShapeXNA;
  98. return (otheru != null) && (this.shape == otheru.shape);
  99. }
  100. public override string AddrString
  101. {
  102. get { return "XNACollisionShape"; }
  103. }
  104. }
  105. private sealed class BulletConstraintXNA : BulletConstraint
  106. {
  107. public TypedConstraint constrain;
  108. public BulletConstraintXNA(TypedConstraint xx) : base()
  109. {
  110. constrain = xx;
  111. }
  112. public override void Clear()
  113. {
  114. constrain = null;
  115. }
  116. public override bool HasPhysicalConstraint { get { return constrain != null; } }
  117. // Used for log messages for a unique display of the memory/object allocated to this instance
  118. public override string AddrString
  119. {
  120. get { return "XNAConstraint"; }
  121. }
  122. }
  123. internal int m_maxCollisions;
  124. internal CollisionDesc[] UpdatedCollisions;
  125. internal int LastCollisionDesc = 0;
  126. internal int m_maxUpdatesPerFrame;
  127. internal int LastEntityProperty = 0;
  128. internal EntityProperties[] UpdatedObjects;
  129. internal Dictionary<uint, GhostObject> specialCollisionObjects;
  130. private static int m_collisionsThisFrame;
  131. private BSScene PhysicsScene { get; set; }
  132. public override string BulletEngineName { get { return "BulletXNA"; } }
  133. public override string BulletEngineVersion { get; protected set; }
  134. public BSAPIXNA(string paramName, BSScene physScene)
  135. {
  136. PhysicsScene = physScene;
  137. }
  138. /// <summary>
  139. ///
  140. /// </summary>
  141. /// <param name="p"></param>
  142. /// <param name="p_2"></param>
  143. public override bool RemoveObjectFromWorld(BulletWorld pWorld, BulletBody pBody)
  144. {
  145. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  146. RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
  147. CollisionObject collisionObject = ((BulletBodyXNA)pBody).body;
  148. if (body != null)
  149. world.RemoveRigidBody(body);
  150. else if (collisionObject != null)
  151. world.RemoveCollisionObject(collisionObject);
  152. else
  153. return false;
  154. return true;
  155. }
  156. public override bool AddConstraintToWorld(BulletWorld pWorld, BulletConstraint pConstraint, bool pDisableCollisionsBetweenLinkedObjects)
  157. {
  158. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  159. TypedConstraint constraint = (pConstraint as BulletConstraintXNA).constrain;
  160. world.AddConstraint(constraint, pDisableCollisionsBetweenLinkedObjects);
  161. return true;
  162. }
  163. public override bool RemoveConstraintFromWorld(BulletWorld pWorld, BulletConstraint pConstraint)
  164. {
  165. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  166. TypedConstraint constraint = (pConstraint as BulletConstraintXNA).constrain;
  167. world.RemoveConstraint(constraint);
  168. return true;
  169. }
  170. public override void SetRestitution(BulletBody pCollisionObject, float pRestitution)
  171. {
  172. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  173. collisionObject.SetRestitution(pRestitution);
  174. }
  175. public override int GetShapeType(BulletShape pShape)
  176. {
  177. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  178. return (int)shape.GetShapeType();
  179. }
  180. public override void SetMargin(BulletShape pShape, float pMargin)
  181. {
  182. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  183. shape.SetMargin(pMargin);
  184. }
  185. public override float GetMargin(BulletShape pShape)
  186. {
  187. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  188. return shape.GetMargin();
  189. }
  190. public override void SetLocalScaling(BulletShape pShape, Vector3 pScale)
  191. {
  192. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  193. IndexedVector3 vec = new IndexedVector3(pScale.X, pScale.Y, pScale.Z);
  194. shape.SetLocalScaling(ref vec);
  195. }
  196. public override void SetContactProcessingThreshold(BulletBody pCollisionObject, float contactprocessingthreshold)
  197. {
  198. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  199. collisionObject.SetContactProcessingThreshold(contactprocessingthreshold);
  200. }
  201. public override void SetCcdMotionThreshold(BulletBody pCollisionObject, float pccdMotionThreashold)
  202. {
  203. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  204. collisionObject.SetCcdMotionThreshold(pccdMotionThreashold);
  205. }
  206. public override void SetCcdSweptSphereRadius(BulletBody pCollisionObject, float pCcdSweptSphereRadius)
  207. {
  208. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  209. collisionObject.SetCcdSweptSphereRadius(pCcdSweptSphereRadius);
  210. }
  211. public override void SetAngularFactorV(BulletBody pBody, Vector3 pAngularFactor)
  212. {
  213. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  214. body.SetAngularFactor(new IndexedVector3(pAngularFactor.X, pAngularFactor.Y, pAngularFactor.Z));
  215. }
  216. public override CollisionFlags AddToCollisionFlags(BulletBody pCollisionObject, CollisionFlags pcollisionFlags)
  217. {
  218. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  219. CollisionFlags existingcollisionFlags = (CollisionFlags)(uint)collisionObject.GetCollisionFlags();
  220. existingcollisionFlags |= pcollisionFlags;
  221. collisionObject.SetCollisionFlags((BulletXNA.BulletCollision.CollisionFlags)(uint)existingcollisionFlags);
  222. return (CollisionFlags) (uint) existingcollisionFlags;
  223. }
  224. public override bool AddObjectToWorld(BulletWorld pWorld, BulletBody pBody)
  225. {
  226. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  227. CollisionObject cbody = (pBody as BulletBodyXNA).body;
  228. RigidBody rbody = cbody as RigidBody;
  229. // Bullet resets several variables when an object is added to the world. In particular,
  230. // BulletXNA resets position and rotation. Gravity is also reset depending on the static/dynamic
  231. // type. Of course, the collision flags in the broadphase proxy are initialized to default.
  232. IndexedMatrix origPos = cbody.GetWorldTransform();
  233. if (rbody != null)
  234. {
  235. IndexedVector3 origGrav = rbody.GetGravity();
  236. world.AddRigidBody(rbody);
  237. rbody.SetGravity(origGrav);
  238. }
  239. else
  240. {
  241. world.AddCollisionObject(cbody);
  242. }
  243. cbody.SetWorldTransform(origPos);
  244. pBody.ApplyCollisionMask(pWorld.physicsScene);
  245. //if (body.GetBroadphaseHandle() != null)
  246. // world.UpdateSingleAabb(body);
  247. return true;
  248. }
  249. public override void ForceActivationState(BulletBody pCollisionObject, ActivationState pActivationState)
  250. {
  251. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  252. collisionObject.ForceActivationState((BulletXNA.BulletCollision.ActivationState)(uint)pActivationState);
  253. }
  254. public override void UpdateSingleAabb(BulletWorld pWorld, BulletBody pCollisionObject)
  255. {
  256. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  257. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  258. world.UpdateSingleAabb(collisionObject);
  259. }
  260. public override void UpdateAabbs(BulletWorld pWorld) {
  261. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  262. world.UpdateAabbs();
  263. }
  264. public override bool GetForceUpdateAllAabbs(BulletWorld pWorld) {
  265. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  266. return world.GetForceUpdateAllAabbs();
  267. }
  268. public override void SetForceUpdateAllAabbs(BulletWorld pWorld, bool pForce)
  269. {
  270. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  271. world.SetForceUpdateAllAabbs(pForce);
  272. }
  273. public override bool SetCollisionGroupMask(BulletBody pCollisionObject, uint pGroup, uint pMask)
  274. {
  275. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  276. collisionObject.GetBroadphaseHandle().m_collisionFilterGroup = (BulletXNA.BulletCollision.CollisionFilterGroups) pGroup;
  277. collisionObject.GetBroadphaseHandle().m_collisionFilterGroup = (BulletXNA.BulletCollision.CollisionFilterGroups) pGroup;
  278. if ((uint) collisionObject.GetBroadphaseHandle().m_collisionFilterGroup == 0)
  279. return false;
  280. return true;
  281. }
  282. public override void ClearAllForces(BulletBody pCollisionObject)
  283. {
  284. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  285. IndexedVector3 zeroVector = new IndexedVector3(0, 0, 0);
  286. collisionObject.SetInterpolationLinearVelocity(ref zeroVector);
  287. collisionObject.SetInterpolationAngularVelocity(ref zeroVector);
  288. IndexedMatrix bodytransform = collisionObject.GetWorldTransform();
  289. collisionObject.SetInterpolationWorldTransform(ref bodytransform);
  290. if (collisionObject is RigidBody)
  291. {
  292. RigidBody rigidbody = collisionObject as RigidBody;
  293. rigidbody.SetLinearVelocity(zeroVector);
  294. rigidbody.SetAngularVelocity(zeroVector);
  295. rigidbody.ClearForces();
  296. }
  297. }
  298. public override void SetInterpolationAngularVelocity(BulletBody pCollisionObject, Vector3 pVector3)
  299. {
  300. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  301. IndexedVector3 vec = new IndexedVector3(pVector3.X, pVector3.Y, pVector3.Z);
  302. collisionObject.SetInterpolationAngularVelocity(ref vec);
  303. }
  304. public override void SetAngularVelocity(BulletBody pBody, Vector3 pVector3)
  305. {
  306. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  307. IndexedVector3 vec = new IndexedVector3(pVector3.X, pVector3.Y, pVector3.Z);
  308. body.SetAngularVelocity(ref vec);
  309. }
  310. public override Vector3 GetTotalForce(BulletBody pBody)
  311. {
  312. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  313. IndexedVector3 iv3 = body.GetTotalForce();
  314. return new Vector3(iv3.X, iv3.Y, iv3.Z);
  315. }
  316. public override Vector3 GetTotalTorque(BulletBody pBody)
  317. {
  318. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  319. IndexedVector3 iv3 = body.GetTotalTorque();
  320. return new Vector3(iv3.X, iv3.Y, iv3.Z);
  321. }
  322. public override Vector3 GetInvInertiaDiagLocal(BulletBody pBody)
  323. {
  324. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  325. IndexedVector3 iv3 = body.GetInvInertiaDiagLocal();
  326. return new Vector3(iv3.X, iv3.Y, iv3.Z);
  327. }
  328. public override void SetInvInertiaDiagLocal(BulletBody pBody, Vector3 inert)
  329. {
  330. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  331. IndexedVector3 iv3 = new IndexedVector3(inert.X, inert.Y, inert.Z);
  332. body.SetInvInertiaDiagLocal(ref iv3);
  333. }
  334. public override void ApplyForce(BulletBody pBody, Vector3 force, Vector3 pos)
  335. {
  336. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  337. IndexedVector3 forceiv3 = new IndexedVector3(force.X, force.Y, force.Z);
  338. IndexedVector3 posiv3 = new IndexedVector3(pos.X, pos.Y, pos.Z);
  339. body.ApplyForce(ref forceiv3, ref posiv3);
  340. }
  341. public override void ApplyImpulse(BulletBody pBody, Vector3 imp, Vector3 pos)
  342. {
  343. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  344. IndexedVector3 impiv3 = new IndexedVector3(imp.X, imp.Y, imp.Z);
  345. IndexedVector3 posiv3 = new IndexedVector3(pos.X, pos.Y, pos.Z);
  346. body.ApplyImpulse(ref impiv3, ref posiv3);
  347. }
  348. public override void ClearForces(BulletBody pBody)
  349. {
  350. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  351. body.ClearForces();
  352. }
  353. public override void SetTranslation(BulletBody pCollisionObject, Vector3 _position, Quaternion _orientation)
  354. {
  355. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  356. IndexedVector3 vposition = new IndexedVector3(_position.X, _position.Y, _position.Z);
  357. IndexedQuaternion vquaternion = new IndexedQuaternion(_orientation.X, _orientation.Y, _orientation.Z,
  358. _orientation.W);
  359. IndexedMatrix mat = IndexedMatrix.CreateFromQuaternion(vquaternion);
  360. mat._origin = vposition;
  361. collisionObject.SetWorldTransform(mat);
  362. }
  363. public override Vector3 GetPosition(BulletBody pCollisionObject)
  364. {
  365. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  366. IndexedVector3 pos = collisionObject.GetInterpolationWorldTransform()._origin;
  367. return new Vector3(pos.X, pos.Y, pos.Z);
  368. }
  369. public override Vector3 CalculateLocalInertia(BulletShape pShape, float pphysMass)
  370. {
  371. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  372. IndexedVector3 inertia = IndexedVector3.Zero;
  373. shape.CalculateLocalInertia(pphysMass, out inertia);
  374. return new Vector3(inertia.X, inertia.Y, inertia.Z);
  375. }
  376. public override void SetMassProps(BulletBody pBody, float pphysMass, Vector3 plocalInertia)
  377. {
  378. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  379. if (body != null) // Can't set mass props on collision object.
  380. {
  381. IndexedVector3 inertia = new IndexedVector3(plocalInertia.X, plocalInertia.Y, plocalInertia.Z);
  382. body.SetMassProps(pphysMass, inertia);
  383. }
  384. }
  385. public override void SetObjectForce(BulletBody pBody, Vector3 _force)
  386. {
  387. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  388. IndexedVector3 force = new IndexedVector3(_force.X, _force.Y, _force.Z);
  389. body.SetTotalForce(ref force);
  390. }
  391. public override void SetFriction(BulletBody pCollisionObject, float _currentFriction)
  392. {
  393. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  394. collisionObject.SetFriction(_currentFriction);
  395. }
  396. public override void SetLinearVelocity(BulletBody pBody, Vector3 _velocity)
  397. {
  398. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  399. IndexedVector3 velocity = new IndexedVector3(_velocity.X, _velocity.Y, _velocity.Z);
  400. body.SetLinearVelocity(velocity);
  401. }
  402. public override void Activate(BulletBody pCollisionObject, bool pforceactivation)
  403. {
  404. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  405. collisionObject.Activate(pforceactivation);
  406. }
  407. public override Quaternion GetOrientation(BulletBody pCollisionObject)
  408. {
  409. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  410. IndexedQuaternion mat = collisionObject.GetInterpolationWorldTransform().GetRotation();
  411. return new Quaternion(mat.X, mat.Y, mat.Z, mat.W);
  412. }
  413. public override CollisionFlags RemoveFromCollisionFlags(BulletBody pCollisionObject, CollisionFlags pcollisionFlags)
  414. {
  415. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  416. CollisionFlags existingcollisionFlags = (CollisionFlags)(uint)collisionObject.GetCollisionFlags();
  417. existingcollisionFlags &= ~pcollisionFlags;
  418. collisionObject.SetCollisionFlags((BulletXNA.BulletCollision.CollisionFlags)(uint)existingcollisionFlags);
  419. return (CollisionFlags)(uint)existingcollisionFlags;
  420. }
  421. public override float GetCcdMotionThreshold(BulletBody pCollisionObject)
  422. {
  423. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  424. return collisionObject.GetCcdSquareMotionThreshold();
  425. }
  426. public override float GetCcdSweptSphereRadius(BulletBody pCollisionObject)
  427. {
  428. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  429. return collisionObject.GetCcdSweptSphereRadius();
  430. }
  431. public override IntPtr GetUserPointer(BulletBody pCollisionObject)
  432. {
  433. CollisionObject shape = (pCollisionObject as BulletBodyXNA).body;
  434. return (IntPtr)shape.GetUserPointer();
  435. }
  436. public override void SetUserPointer(BulletBody pCollisionObject, IntPtr val)
  437. {
  438. CollisionObject shape = (pCollisionObject as BulletBodyXNA).body;
  439. shape.SetUserPointer(val);
  440. }
  441. public override void SetGravity(BulletBody pBody, Vector3 pGravity)
  442. {
  443. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  444. if (body != null) // Can't set collisionobject.set gravity
  445. {
  446. IndexedVector3 gravity = new IndexedVector3(pGravity.X, pGravity.Y, pGravity.Z);
  447. body.SetGravity(gravity);
  448. }
  449. }
  450. public override bool DestroyConstraint(BulletWorld pWorld, BulletConstraint pConstraint)
  451. {
  452. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  453. TypedConstraint constraint = (pConstraint as BulletConstraintXNA).constrain;
  454. world.RemoveConstraint(constraint);
  455. return true;
  456. }
  457. public override bool SetLinearLimits(BulletConstraint pConstraint, Vector3 low, Vector3 high)
  458. {
  459. Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
  460. IndexedVector3 lowlimit = new IndexedVector3(low.X, low.Y, low.Z);
  461. IndexedVector3 highlimit = new IndexedVector3(high.X, high.Y, high.Z);
  462. constraint.SetLinearLowerLimit(lowlimit);
  463. constraint.SetLinearUpperLimit(highlimit);
  464. return true;
  465. }
  466. public override bool SetAngularLimits(BulletConstraint pConstraint, Vector3 low, Vector3 high)
  467. {
  468. Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
  469. IndexedVector3 lowlimit = new IndexedVector3(low.X, low.Y, low.Z);
  470. IndexedVector3 highlimit = new IndexedVector3(high.X, high.Y, high.Z);
  471. constraint.SetAngularLowerLimit(lowlimit);
  472. constraint.SetAngularUpperLimit(highlimit);
  473. return true;
  474. }
  475. public override void SetConstraintNumSolverIterations(BulletConstraint pConstraint, float cnt)
  476. {
  477. Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
  478. constraint.SetOverrideNumSolverIterations((int)cnt);
  479. }
  480. public override bool CalculateTransforms(BulletConstraint pConstraint)
  481. {
  482. Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
  483. constraint.CalculateTransforms();
  484. return true;
  485. }
  486. public override void SetConstraintEnable(BulletConstraint pConstraint, float p_2)
  487. {
  488. Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
  489. constraint.SetEnabled((p_2 == 0) ? false : true);
  490. }
  491. public override BulletConstraint Create6DofConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  492. Vector3 pframe1, Quaternion pframe1rot, Vector3 pframe2, Quaternion pframe2rot,
  493. bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
  494. {
  495. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  496. RigidBody body1 = (pBody1 as BulletBodyXNA).rigidBody;
  497. RigidBody body2 = (pBody2 as BulletBodyXNA).rigidBody;
  498. IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
  499. IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
  500. IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
  501. frame1._origin = frame1v;
  502. IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
  503. IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
  504. IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
  505. frame2._origin = frame1v;
  506. Generic6DofConstraint consttr = new Generic6DofConstraint(body1, body2, ref frame1, ref frame2,
  507. puseLinearReferenceFrameA);
  508. consttr.CalculateTransforms();
  509. world.AddConstraint(consttr,pdisableCollisionsBetweenLinkedBodies);
  510. return new BulletConstraintXNA(consttr);
  511. }
  512. public override BulletConstraint Create6DofConstraintFixed(BulletWorld pWorld, BulletBody pBody1,
  513. Vector3 pframe1, Quaternion pframe1rot,
  514. bool pUseLinearReferenceFrameB, bool pdisableCollisionsBetweenLinkedBodies)
  515. {
  516. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  517. RigidBody body1 = (pBody1 as BulletBodyXNA).rigidBody;
  518. IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
  519. IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
  520. IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
  521. frame1._origin = frame1v;
  522. Generic6DofConstraint consttr = new Generic6DofConstraint(body1, ref frame1, pUseLinearReferenceFrameB);
  523. consttr.CalculateTransforms();
  524. world.AddConstraint(consttr,pdisableCollisionsBetweenLinkedBodies);
  525. return new BulletConstraintXNA(consttr);
  526. }
  527. /// <summary>
  528. ///
  529. /// </summary>
  530. /// <param name="pWorld"></param>
  531. /// <param name="pBody1"></param>
  532. /// <param name="pBody2"></param>
  533. /// <param name="pjoinPoint"></param>
  534. /// <param name="puseLinearReferenceFrameA"></param>
  535. /// <param name="pdisableCollisionsBetweenLinkedBodies"></param>
  536. /// <returns></returns>
  537. public override BulletConstraint Create6DofConstraintToPoint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2, Vector3 pjoinPoint, bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
  538. {
  539. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  540. RigidBody body1 = (pBody1 as BulletBodyXNA).rigidBody;
  541. RigidBody body2 = (pBody2 as BulletBodyXNA).rigidBody;
  542. IndexedMatrix frame1 = new IndexedMatrix(IndexedBasisMatrix.Identity, new IndexedVector3(0, 0, 0));
  543. IndexedMatrix frame2 = new IndexedMatrix(IndexedBasisMatrix.Identity, new IndexedVector3(0, 0, 0));
  544. IndexedVector3 joinPoint = new IndexedVector3(pjoinPoint.X, pjoinPoint.Y, pjoinPoint.Z);
  545. IndexedMatrix mat = IndexedMatrix.Identity;
  546. mat._origin = new IndexedVector3(pjoinPoint.X, pjoinPoint.Y, pjoinPoint.Z);
  547. frame1._origin = body1.GetWorldTransform().Inverse()*joinPoint;
  548. frame2._origin = body2.GetWorldTransform().Inverse()*joinPoint;
  549. Generic6DofConstraint consttr = new Generic6DofConstraint(body1, body2, ref frame1, ref frame2, puseLinearReferenceFrameA);
  550. consttr.CalculateTransforms();
  551. world.AddConstraint(consttr, pdisableCollisionsBetweenLinkedBodies);
  552. return new BulletConstraintXNA(consttr);
  553. }
  554. //SetFrames(m_constraint.ptr, frameA, frameArot, frameB, frameBrot);
  555. public override bool SetFrames(BulletConstraint pConstraint, Vector3 pframe1, Quaternion pframe1rot, Vector3 pframe2, Quaternion pframe2rot)
  556. {
  557. Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
  558. IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
  559. IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
  560. IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
  561. frame1._origin = frame1v;
  562. IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
  563. IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
  564. IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
  565. frame2._origin = frame1v;
  566. constraint.SetFrames(ref frame1, ref frame2);
  567. return true;
  568. }
  569. public override Vector3 GetLinearVelocity(BulletBody pBody)
  570. {
  571. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  572. IndexedVector3 iv3 = body.GetLinearVelocity();
  573. return new Vector3(iv3.X, iv3.Y, iv3.Z);
  574. }
  575. public override Vector3 GetAngularVelocity(BulletBody pBody)
  576. {
  577. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  578. IndexedVector3 iv3 = body.GetAngularVelocity();
  579. return new Vector3(iv3.X, iv3.Y, iv3.Z);
  580. }
  581. public override Vector3 GetVelocityInLocalPoint(BulletBody pBody, Vector3 pos)
  582. {
  583. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  584. IndexedVector3 posiv3 = new IndexedVector3(pos.X, pos.Y, pos.Z);
  585. IndexedVector3 iv3 = body.GetVelocityInLocalPoint(ref posiv3);
  586. return new Vector3(iv3.X, iv3.Y, iv3.Z);
  587. }
  588. public override void Translate(BulletBody pCollisionObject, Vector3 trans)
  589. {
  590. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  591. collisionObject.Translate(new IndexedVector3(trans.X,trans.Y,trans.Z));
  592. }
  593. public override void UpdateDeactivation(BulletBody pBody, float timeStep)
  594. {
  595. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  596. body.UpdateDeactivation(timeStep);
  597. }
  598. public override bool WantsSleeping(BulletBody pBody)
  599. {
  600. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  601. return body.WantsSleeping();
  602. }
  603. public override void SetAngularFactor(BulletBody pBody, float factor)
  604. {
  605. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  606. body.SetAngularFactor(factor);
  607. }
  608. public override Vector3 GetAngularFactor(BulletBody pBody)
  609. {
  610. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  611. IndexedVector3 iv3 = body.GetAngularFactor();
  612. return new Vector3(iv3.X, iv3.Y, iv3.Z);
  613. }
  614. public override bool IsInWorld(BulletWorld pWorld, BulletBody pCollisionObject)
  615. {
  616. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  617. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  618. return world.IsInWorld(collisionObject);
  619. }
  620. public override void AddConstraintRef(BulletBody pBody, BulletConstraint pConstraint)
  621. {
  622. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  623. TypedConstraint constrain = (pConstraint as BulletConstraintXNA).constrain;
  624. body.AddConstraintRef(constrain);
  625. }
  626. public override void RemoveConstraintRef(BulletBody pBody, BulletConstraint pConstraint)
  627. {
  628. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  629. TypedConstraint constrain = (pConstraint as BulletConstraintXNA).constrain;
  630. body.RemoveConstraintRef(constrain);
  631. }
  632. public override BulletConstraint GetConstraintRef(BulletBody pBody, int index)
  633. {
  634. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  635. return new BulletConstraintXNA(body.GetConstraintRef(index));
  636. }
  637. public override int GetNumConstraintRefs(BulletBody pBody)
  638. {
  639. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  640. return body.GetNumConstraintRefs();
  641. }
  642. public override void SetInterpolationLinearVelocity(BulletBody pCollisionObject, Vector3 VehicleVelocity)
  643. {
  644. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  645. IndexedVector3 velocity = new IndexedVector3(VehicleVelocity.X, VehicleVelocity.Y, VehicleVelocity.Z);
  646. collisionObject.SetInterpolationLinearVelocity(ref velocity);
  647. }
  648. public override bool UseFrameOffset(BulletConstraint pConstraint, float onOff)
  649. {
  650. Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
  651. constraint.SetUseFrameOffset((onOff == 0) ? false : true);
  652. return true;
  653. }
  654. //SetBreakingImpulseThreshold(m_constraint.ptr, threshold);
  655. public override bool SetBreakingImpulseThreshold(BulletConstraint pConstraint, float threshold)
  656. {
  657. Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
  658. constraint.SetBreakingImpulseThreshold(threshold);
  659. return true;
  660. }
  661. //BulletSimAPI.SetAngularDamping(Prim.PhysBody.ptr, angularDamping);
  662. public override void SetAngularDamping(BulletBody pBody, float angularDamping)
  663. {
  664. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  665. float lineardamping = body.GetLinearDamping();
  666. body.SetDamping(lineardamping, angularDamping);
  667. }
  668. public override void UpdateInertiaTensor(BulletBody pBody)
  669. {
  670. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  671. if (body != null) // can't update inertia tensor on CollisionObject
  672. body.UpdateInertiaTensor();
  673. }
  674. public override void RecalculateCompoundShapeLocalAabb(BulletShape pCompoundShape)
  675. {
  676. CompoundShape shape = (pCompoundShape as BulletShapeXNA).shape as CompoundShape;
  677. shape.RecalculateLocalAabb();
  678. }
  679. //BulletSimAPI.GetCollisionFlags(PhysBody.ptr)
  680. public override CollisionFlags GetCollisionFlags(BulletBody pCollisionObject)
  681. {
  682. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  683. uint flags = (uint)collisionObject.GetCollisionFlags();
  684. return (CollisionFlags) flags;
  685. }
  686. public override void SetDamping(BulletBody pBody, float pLinear, float pAngular)
  687. {
  688. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  689. body.SetDamping(pLinear, pAngular);
  690. }
  691. //PhysBody.ptr, PhysicsScene.Params.deactivationTime);
  692. public override void SetDeactivationTime(BulletBody pCollisionObject, float pDeactivationTime)
  693. {
  694. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  695. collisionObject.SetDeactivationTime(pDeactivationTime);
  696. }
  697. //SetSleepingThresholds(PhysBody.ptr, PhysicsScene.Params.linearSleepingThreshold, PhysicsScene.Params.angularSleepingThreshold);
  698. public override void SetSleepingThresholds(BulletBody pBody, float plinearSleepingThreshold, float pangularSleepingThreshold)
  699. {
  700. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  701. body.SetSleepingThresholds(plinearSleepingThreshold, pangularSleepingThreshold);
  702. }
  703. public override CollisionObjectTypes GetBodyType(BulletBody pCollisionObject)
  704. {
  705. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  706. return (CollisionObjectTypes)(int) collisionObject.GetInternalType();
  707. }
  708. public override void ApplyGravity(BulletBody pBody)
  709. {
  710. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  711. body.ApplyGravity();
  712. }
  713. public override Vector3 GetGravity(BulletBody pBody)
  714. {
  715. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  716. IndexedVector3 gravity = body.GetGravity();
  717. return new Vector3(gravity.X, gravity.Y, gravity.Z);
  718. }
  719. public override void SetLinearDamping(BulletBody pBody, float lin_damping)
  720. {
  721. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  722. float angularDamping = body.GetAngularDamping();
  723. body.SetDamping(lin_damping, angularDamping);
  724. }
  725. public override float GetLinearDamping(BulletBody pBody)
  726. {
  727. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  728. return body.GetLinearDamping();
  729. }
  730. public override float GetAngularDamping(BulletBody pBody)
  731. {
  732. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  733. return body.GetAngularDamping();
  734. }
  735. public override float GetLinearSleepingThreshold(BulletBody pBody)
  736. {
  737. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  738. return body.GetLinearSleepingThreshold();
  739. }
  740. public override void ApplyDamping(BulletBody pBody, float timeStep)
  741. {
  742. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  743. body.ApplyDamping(timeStep);
  744. }
  745. public override Vector3 GetLinearFactor(BulletBody pBody)
  746. {
  747. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  748. IndexedVector3 linearFactor = body.GetLinearFactor();
  749. return new Vector3(linearFactor.X, linearFactor.Y, linearFactor.Z);
  750. }
  751. public override void SetLinearFactor(BulletBody pBody, Vector3 factor)
  752. {
  753. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  754. body.SetLinearFactor(new IndexedVector3(factor.X, factor.Y, factor.Z));
  755. }
  756. public override void SetCenterOfMassByPosRot(BulletBody pBody, Vector3 pos, Quaternion rot)
  757. {
  758. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  759. IndexedQuaternion quat = new IndexedQuaternion(rot.X, rot.Y, rot.Z,rot.W);
  760. IndexedMatrix mat = IndexedMatrix.CreateFromQuaternion(quat);
  761. mat._origin = new IndexedVector3(pos.X, pos.Y, pos.Z);
  762. body.SetCenterOfMassTransform( ref mat);
  763. /* TODO: double check this */
  764. }
  765. //BulletSimAPI.ApplyCentralForce(PhysBody.ptr, fSum);
  766. public override void ApplyCentralForce(BulletBody pBody, Vector3 pfSum)
  767. {
  768. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  769. IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z);
  770. body.ApplyCentralForce(ref fSum);
  771. }
  772. public override void ApplyCentralImpulse(BulletBody pBody, Vector3 pfSum)
  773. {
  774. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  775. IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z);
  776. body.ApplyCentralImpulse(ref fSum);
  777. }
  778. public override void ApplyTorque(BulletBody pBody, Vector3 pfSum)
  779. {
  780. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  781. IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z);
  782. body.ApplyTorque(ref fSum);
  783. }
  784. public override void ApplyTorqueImpulse(BulletBody pBody, Vector3 pfSum)
  785. {
  786. RigidBody body = (pBody as BulletBodyXNA).rigidBody;
  787. IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z);
  788. body.ApplyTorqueImpulse(ref fSum);
  789. }
  790. public override void DestroyObject(BulletWorld pWorld, BulletBody pBody)
  791. {
  792. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  793. CollisionObject co = (pBody as BulletBodyXNA).rigidBody;
  794. RigidBody bo = co as RigidBody;
  795. if (bo == null)
  796. {
  797. if (world.IsInWorld(co))
  798. {
  799. world.RemoveCollisionObject(co);
  800. }
  801. }
  802. else
  803. {
  804. if (world.IsInWorld(bo))
  805. {
  806. world.RemoveRigidBody(bo);
  807. }
  808. }
  809. if (co != null)
  810. {
  811. if (co.GetUserPointer() != null)
  812. {
  813. uint localId = (uint) co.GetUserPointer();
  814. if (specialCollisionObjects.ContainsKey(localId))
  815. {
  816. specialCollisionObjects.Remove(localId);
  817. }
  818. }
  819. }
  820. }
  821. public override void Shutdown(BulletWorld pWorld)
  822. {
  823. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  824. world.Cleanup();
  825. }
  826. public override BulletShape DuplicateCollisionShape(BulletWorld pWorld, BulletShape pShape, uint id)
  827. {
  828. CollisionShape shape1 = (pShape as BulletShapeXNA).shape;
  829. // TODO: Turn this from a reference copy to a Value Copy.
  830. BulletShapeXNA shape2 = new BulletShapeXNA(shape1, BSShapeTypeFromBroadPhaseNativeType(shape1.GetShapeType()));
  831. return shape2;
  832. }
  833. public override bool DeleteCollisionShape(BulletWorld pWorld, BulletShape pShape)
  834. {
  835. //TODO:
  836. return false;
  837. }
  838. //(sim.ptr, shape.ptr, prim.LocalID, prim.RawPosition, prim.RawOrientation);
  839. public override BulletBody CreateBodyFromShape(BulletWorld pWorld, BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation)
  840. {
  841. CollisionWorld world = (pWorld as BulletWorldXNA).world;
  842. IndexedMatrix mat =
  843. IndexedMatrix.CreateFromQuaternion(new IndexedQuaternion(pRawOrientation.X, pRawOrientation.Y,
  844. pRawOrientation.Z, pRawOrientation.W));
  845. mat._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z);
  846. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  847. //UpdateSingleAabb(world, shape);
  848. // TODO: Feed Update array into null
  849. SimMotionState motionState = new SimMotionState(this, pLocalID, mat, null);
  850. RigidBody body = new RigidBody(0,motionState,shape,IndexedVector3.Zero);
  851. RigidBodyConstructionInfo constructionInfo = new RigidBodyConstructionInfo(0, motionState, shape, IndexedVector3.Zero)
  852. {
  853. m_mass = 0
  854. };
  855. /*
  856. m_mass = mass;
  857. m_motionState =motionState;
  858. m_collisionShape = collisionShape;
  859. m_localInertia = localInertia;
  860. m_linearDamping = 0f;
  861. m_angularDamping = 0f;
  862. m_friction = 0.5f;
  863. m_restitution = 0f;
  864. m_linearSleepingThreshold = 0.8f;
  865. m_angularSleepingThreshold = 1f;
  866. m_additionalDamping = false;
  867. m_additionalDampingFactor = 0.005f;
  868. m_additionalLinearDampingThresholdSqr = 0.01f;
  869. m_additionalAngularDampingThresholdSqr = 0.01f;
  870. m_additionalAngularDampingFactor = 0.01f;
  871. m_startWorldTransform = IndexedMatrix.Identity;
  872. */
  873. body.SetUserPointer(pLocalID);
  874. return new BulletBodyXNA(pLocalID, body);
  875. }
  876. public override BulletBody CreateBodyWithDefaultMotionState( BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation)
  877. {
  878. IndexedMatrix mat =
  879. IndexedMatrix.CreateFromQuaternion(new IndexedQuaternion(pRawOrientation.X, pRawOrientation.Y,
  880. pRawOrientation.Z, pRawOrientation.W));
  881. mat._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z);
  882. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  883. // TODO: Feed Update array into null
  884. RigidBody body = new RigidBody(0, new DefaultMotionState( mat, IndexedMatrix.Identity), shape, IndexedVector3.Zero);
  885. body.SetWorldTransform(mat);
  886. body.SetUserPointer(pLocalID);
  887. return new BulletBodyXNA(pLocalID, body);
  888. }
  889. //(m_mapInfo.terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT);
  890. public override CollisionFlags SetCollisionFlags(BulletBody pCollisionObject, CollisionFlags collisionFlags)
  891. {
  892. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  893. collisionObject.SetCollisionFlags((BulletXNA.BulletCollision.CollisionFlags) (uint) collisionFlags);
  894. return (CollisionFlags)collisionObject.GetCollisionFlags();
  895. }
  896. public override Vector3 GetAnisotripicFriction(BulletConstraint pconstrain)
  897. {
  898. /* TODO */
  899. return Vector3.Zero;
  900. }
  901. public override Vector3 SetAnisotripicFriction(BulletConstraint pconstrain, Vector3 frict) { /* TODO */ return Vector3.Zero; }
  902. public override bool HasAnisotripicFriction(BulletConstraint pconstrain) { /* TODO */ return false; }
  903. public override float GetContactProcessingThreshold(BulletBody pBody) { /* TODO */ return 0f; }
  904. public override bool IsStaticObject(BulletBody pCollisionObject)
  905. {
  906. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  907. return collisionObject.IsStaticObject();
  908. }
  909. public override bool IsKinematicObject(BulletBody pCollisionObject)
  910. {
  911. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  912. return collisionObject.IsKinematicObject();
  913. }
  914. public override bool IsStaticOrKinematicObject(BulletBody pCollisionObject)
  915. {
  916. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  917. return collisionObject.IsStaticOrKinematicObject();
  918. }
  919. public override bool HasContactResponse(BulletBody pCollisionObject)
  920. {
  921. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  922. return collisionObject.HasContactResponse();
  923. }
  924. public override int GetActivationState(BulletBody pBody) { /* TODO */ return 0; }
  925. public override void SetActivationState(BulletBody pBody, int state) { /* TODO */ }
  926. public override float GetDeactivationTime(BulletBody pBody) { /* TODO */ return 0f; }
  927. public override bool IsActive(BulletBody pBody) { /* TODO */ return false; }
  928. public override float GetRestitution(BulletBody pBody) { /* TODO */ return 0f; }
  929. public override float GetFriction(BulletBody pBody) { /* TODO */ return 0f; }
  930. public override void SetInterpolationVelocity(BulletBody pBody, Vector3 linearVel, Vector3 angularVel) { /* TODO */ }
  931. public override float GetHitFraction(BulletBody pBody) { /* TODO */ return 0f; }
  932. //(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainHitFraction);
  933. public override void SetHitFraction(BulletBody pCollisionObject, float pHitFraction)
  934. {
  935. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  936. collisionObject.SetHitFraction(pHitFraction);
  937. }
  938. //BuildCapsuleShape(physicsScene.World.ptr, 1f, 1f, prim.Scale);
  939. public override BulletShape BuildCapsuleShape(BulletWorld pWorld, float pRadius, float pHeight, Vector3 pScale)
  940. {
  941. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  942. IndexedVector3 scale = new IndexedVector3(pScale.X, pScale.Y, pScale.Z);
  943. CapsuleShapeZ capsuleShapeZ = new CapsuleShapeZ(pRadius, pHeight);
  944. capsuleShapeZ.SetMargin(world.WorldSettings.Params.collisionMargin);
  945. capsuleShapeZ.SetLocalScaling(ref scale);
  946. return new BulletShapeXNA(capsuleShapeZ, BSPhysicsShapeType.SHAPE_CAPSULE); ;
  947. }
  948. public override BulletWorld Initialize(Vector3 maxPosition, ConfigurationParameters parms,
  949. int maxCollisions, ref CollisionDesc[] collisionArray,
  950. int maxUpdates, ref EntityProperties[] updateArray
  951. )
  952. {
  953. UpdatedObjects = updateArray;
  954. UpdatedCollisions = collisionArray;
  955. /* TODO */
  956. ConfigurationParameters[] configparms = new ConfigurationParameters[1];
  957. configparms[0] = parms;
  958. Vector3 worldExtent = new Vector3(Constants.RegionSize, Constants.RegionSize, Constants.RegionHeight);
  959. m_maxCollisions = maxCollisions;
  960. m_maxUpdatesPerFrame = maxUpdates;
  961. specialCollisionObjects = new Dictionary<uint, GhostObject>();
  962. return new BulletWorldXNA(1, PhysicsScene, BSAPIXNA.Initialize2(worldExtent, configparms, maxCollisions, ref collisionArray, maxUpdates, ref updateArray, null));
  963. }
  964. private static DiscreteDynamicsWorld Initialize2(Vector3 worldExtent,
  965. ConfigurationParameters[] o,
  966. int mMaxCollisionsPerFrame, ref CollisionDesc[] collisionArray,
  967. int mMaxUpdatesPerFrame, ref EntityProperties[] updateArray,
  968. object mDebugLogCallbackHandle)
  969. {
  970. CollisionWorld.WorldData.ParamData p = new CollisionWorld.WorldData.ParamData();
  971. p.angularDamping = BSParam.AngularDamping;
  972. p.defaultFriction = o[0].defaultFriction;
  973. p.defaultFriction = o[0].defaultFriction;
  974. p.defaultDensity = o[0].defaultDensity;
  975. p.defaultRestitution = o[0].defaultRestitution;
  976. p.collisionMargin = o[0].collisionMargin;
  977. p.gravity = o[0].gravity;
  978. p.linearDamping = BSParam.LinearDamping;
  979. p.angularDamping = BSParam.AngularDamping;
  980. p.deactivationTime = BSParam.DeactivationTime;
  981. p.linearSleepingThreshold = BSParam.LinearSleepingThreshold;
  982. p.angularSleepingThreshold = BSParam.AngularSleepingThreshold;
  983. p.ccdMotionThreshold = BSParam.CcdMotionThreshold;
  984. p.ccdSweptSphereRadius = BSParam.CcdSweptSphereRadius;
  985. p.contactProcessingThreshold = BSParam.ContactProcessingThreshold;
  986. p.terrainImplementation = BSParam.TerrainImplementation;
  987. p.terrainFriction = BSParam.TerrainFriction;
  988. p.terrainHitFraction = BSParam.TerrainHitFraction;
  989. p.terrainRestitution = BSParam.TerrainRestitution;
  990. p.terrainCollisionMargin = BSParam.TerrainCollisionMargin;
  991. p.avatarFriction = BSParam.AvatarFriction;
  992. p.avatarStandingFriction = BSParam.AvatarStandingFriction;
  993. p.avatarDensity = BSParam.AvatarDensity;
  994. p.avatarRestitution = BSParam.AvatarRestitution;
  995. p.avatarCapsuleWidth = BSParam.AvatarCapsuleWidth;
  996. p.avatarCapsuleDepth = BSParam.AvatarCapsuleDepth;
  997. p.avatarCapsuleHeight = BSParam.AvatarCapsuleHeight;
  998. p.avatarContactProcessingThreshold = BSParam.AvatarContactProcessingThreshold;
  999. p.vehicleAngularDamping = BSParam.VehicleAngularDamping;
  1000. p.maxPersistantManifoldPoolSize = o[0].maxPersistantManifoldPoolSize;
  1001. p.maxCollisionAlgorithmPoolSize = o[0].maxCollisionAlgorithmPoolSize;
  1002. p.shouldDisableContactPoolDynamicAllocation = o[0].shouldDisableContactPoolDynamicAllocation;
  1003. p.shouldForceUpdateAllAabbs = o[0].shouldForceUpdateAllAabbs;
  1004. p.shouldRandomizeSolverOrder = o[0].shouldRandomizeSolverOrder;
  1005. p.shouldSplitSimulationIslands = o[0].shouldSplitSimulationIslands;
  1006. p.shouldEnableFrictionCaching = o[0].shouldEnableFrictionCaching;
  1007. p.numberOfSolverIterations = o[0].numberOfSolverIterations;
  1008. p.linksetImplementation = BSParam.LinksetImplementation;
  1009. p.linkConstraintUseFrameOffset = BSParam.NumericBool(BSParam.LinkConstraintUseFrameOffset);
  1010. p.linkConstraintEnableTransMotor = BSParam.NumericBool(BSParam.LinkConstraintEnableTransMotor);
  1011. p.linkConstraintTransMotorMaxVel = BSParam.LinkConstraintTransMotorMaxVel;
  1012. p.linkConstraintTransMotorMaxForce = BSParam.LinkConstraintTransMotorMaxForce;
  1013. p.linkConstraintERP = BSParam.LinkConstraintERP;
  1014. p.linkConstraintCFM = BSParam.LinkConstraintCFM;
  1015. p.linkConstraintSolverIterations = BSParam.LinkConstraintSolverIterations;
  1016. p.physicsLoggingFrames = o[0].physicsLoggingFrames;
  1017. DefaultCollisionConstructionInfo ccci = new DefaultCollisionConstructionInfo();
  1018. DefaultCollisionConfiguration cci = new DefaultCollisionConfiguration();
  1019. CollisionDispatcher m_dispatcher = new CollisionDispatcher(cci);
  1020. if (p.maxPersistantManifoldPoolSize > 0)
  1021. cci.m_persistentManifoldPoolSize = (int)p.maxPersistantManifoldPoolSize;
  1022. if (p.shouldDisableContactPoolDynamicAllocation !=0)
  1023. m_dispatcher.SetDispatcherFlags(DispatcherFlags.CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION);
  1024. //if (p.maxCollisionAlgorithmPoolSize >0 )
  1025. DbvtBroadphase m_broadphase = new DbvtBroadphase();
  1026. //IndexedVector3 aabbMin = new IndexedVector3(0, 0, 0);
  1027. //IndexedVector3 aabbMax = new IndexedVector3(256, 256, 256);
  1028. //AxisSweep3Internal m_broadphase2 = new AxisSweep3Internal(ref aabbMin, ref aabbMax, Convert.ToInt32(0xfffe), 0xffff, ushort.MaxValue/2, null, true);
  1029. m_broadphase.GetOverlappingPairCache().SetInternalGhostPairCallback(new GhostPairCallback());
  1030. SequentialImpulseConstraintSolver m_solver = new SequentialImpulseConstraintSolver();
  1031. DiscreteDynamicsWorld world = new DiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, cci);
  1032. world.LastCollisionDesc = 0;
  1033. world.LastEntityProperty = 0;
  1034. world.WorldSettings.Params = p;
  1035. world.SetForceUpdateAllAabbs(p.shouldForceUpdateAllAabbs != 0);
  1036. world.GetSolverInfo().m_solverMode = SolverMode.SOLVER_USE_WARMSTARTING | SolverMode.SOLVER_SIMD;
  1037. if (p.shouldRandomizeSolverOrder != 0)
  1038. world.GetSolverInfo().m_solverMode |= SolverMode.SOLVER_RANDMIZE_ORDER;
  1039. world.GetSimulationIslandManager().SetSplitIslands(p.shouldSplitSimulationIslands != 0);
  1040. //world.GetDispatchInfo().m_enableSatConvex Not implemented in C# port
  1041. if (p.shouldEnableFrictionCaching != 0)
  1042. world.GetSolverInfo().m_solverMode |= SolverMode.SOLVER_ENABLE_FRICTION_DIRECTION_CACHING;
  1043. if (p.numberOfSolverIterations > 0)
  1044. world.GetSolverInfo().m_numIterations = (int) p.numberOfSolverIterations;
  1045. world.GetSolverInfo().m_damping = world.WorldSettings.Params.linearDamping;
  1046. world.GetSolverInfo().m_restitution = world.WorldSettings.Params.defaultRestitution;
  1047. world.GetSolverInfo().m_globalCfm = 0.0f;
  1048. world.GetSolverInfo().m_tau = 0.6f;
  1049. world.GetSolverInfo().m_friction = 0.3f;
  1050. world.GetSolverInfo().m_maxErrorReduction = 20f;
  1051. world.GetSolverInfo().m_numIterations = 10;
  1052. world.GetSolverInfo().m_erp = 0.2f;
  1053. world.GetSolverInfo().m_erp2 = 0.1f;
  1054. world.GetSolverInfo().m_sor = 1.0f;
  1055. world.GetSolverInfo().m_splitImpulse = false;
  1056. world.GetSolverInfo().m_splitImpulsePenetrationThreshold = -0.02f;
  1057. world.GetSolverInfo().m_linearSlop = 0.0f;
  1058. world.GetSolverInfo().m_warmstartingFactor = 0.85f;
  1059. world.GetSolverInfo().m_restingContactRestitutionThreshold = 2;
  1060. world.SetForceUpdateAllAabbs(true);
  1061. //BSParam.TerrainImplementation = 0;
  1062. world.SetGravity(new IndexedVector3(0,0,p.gravity));
  1063. return world;
  1064. }
  1065. //m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_STOP_CFM, cfm, ConstraintParamAxis.AXIS_ALL
  1066. public override bool SetConstraintParam(BulletConstraint pConstraint, ConstraintParams paramIndex, float paramvalue, ConstraintParamAxis axis)
  1067. {
  1068. Generic6DofConstraint constrain = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
  1069. if (axis == ConstraintParamAxis.AXIS_LINEAR_ALL || axis == ConstraintParamAxis.AXIS_ALL)
  1070. {
  1071. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams) (int) paramIndex, paramvalue, 0);
  1072. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams) (int) paramIndex, paramvalue, 1);
  1073. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams) (int) paramIndex, paramvalue, 2);
  1074. }
  1075. if (axis == ConstraintParamAxis.AXIS_ANGULAR_ALL || axis == ConstraintParamAxis.AXIS_ALL)
  1076. {
  1077. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams)(int)paramIndex, paramvalue, 3);
  1078. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams)(int)paramIndex, paramvalue, 4);
  1079. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams)(int)paramIndex, paramvalue, 5);
  1080. }
  1081. if (axis == ConstraintParamAxis.AXIS_LINEAR_ALL)
  1082. {
  1083. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams)(int)paramIndex, paramvalue, (int)axis);
  1084. }
  1085. return true;
  1086. }
  1087. public override bool PushUpdate(BulletBody pCollisionObject)
  1088. {
  1089. bool ret = false;
  1090. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  1091. RigidBody rb = collisionObject as RigidBody;
  1092. if (rb != null)
  1093. {
  1094. SimMotionState sms = rb.GetMotionState() as SimMotionState;
  1095. if (sms != null)
  1096. {
  1097. IndexedMatrix wt = IndexedMatrix.Identity;
  1098. sms.GetWorldTransform(out wt);
  1099. sms.SetWorldTransform(ref wt, true);
  1100. ret = true;
  1101. }
  1102. }
  1103. return ret;
  1104. }
  1105. public override float GetAngularMotionDisc(BulletShape pShape)
  1106. {
  1107. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1108. return shape.GetAngularMotionDisc();
  1109. }
  1110. public override float GetContactBreakingThreshold(BulletShape pShape, float defaultFactor)
  1111. {
  1112. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1113. return shape.GetContactBreakingThreshold(defaultFactor);
  1114. }
  1115. public override bool IsCompound(BulletShape pShape)
  1116. {
  1117. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1118. return shape.IsCompound();
  1119. }
  1120. public override bool IsSoftBody(BulletShape pShape)
  1121. {
  1122. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1123. return shape.IsSoftBody();
  1124. }
  1125. public override bool IsPolyhedral(BulletShape pShape)
  1126. {
  1127. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1128. return shape.IsPolyhedral();
  1129. }
  1130. public override bool IsConvex2d(BulletShape pShape)
  1131. {
  1132. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1133. return shape.IsConvex2d();
  1134. }
  1135. public override bool IsConvex(BulletShape pShape)
  1136. {
  1137. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1138. return shape.IsConvex();
  1139. }
  1140. public override bool IsNonMoving(BulletShape pShape)
  1141. {
  1142. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1143. return shape.IsNonMoving();
  1144. }
  1145. public override bool IsConcave(BulletShape pShape)
  1146. {
  1147. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1148. return shape.IsConcave();
  1149. }
  1150. public override bool IsInfinite(BulletShape pShape)
  1151. {
  1152. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1153. return shape.IsInfinite();
  1154. }
  1155. public override bool IsNativeShape(BulletShape pShape)
  1156. {
  1157. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1158. bool ret;
  1159. switch (shape.GetShapeType())
  1160. {
  1161. case BroadphaseNativeTypes.BOX_SHAPE_PROXYTYPE:
  1162. case BroadphaseNativeTypes.CONE_SHAPE_PROXYTYPE:
  1163. case BroadphaseNativeTypes.SPHERE_SHAPE_PROXYTYPE:
  1164. case BroadphaseNativeTypes.CYLINDER_SHAPE_PROXYTYPE:
  1165. ret = true;
  1166. break;
  1167. default:
  1168. ret = false;
  1169. break;
  1170. }
  1171. return ret;
  1172. }
  1173. public override void SetShapeCollisionMargin(BulletShape pShape, float pMargin)
  1174. {
  1175. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1176. shape.SetMargin(pMargin);
  1177. }
  1178. //sim.ptr, shape.ptr,prim.LocalID, prim.RawPosition, prim.RawOrientation
  1179. public override BulletBody CreateGhostFromShape(BulletWorld pWorld, BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation)
  1180. {
  1181. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1182. IndexedMatrix bodyTransform = new IndexedMatrix();
  1183. bodyTransform._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z);
  1184. bodyTransform.SetRotation(new IndexedQuaternion(pRawOrientation.X,pRawOrientation.Y,pRawOrientation.Z,pRawOrientation.W));
  1185. GhostObject gObj = new PairCachingGhostObject();
  1186. gObj.SetWorldTransform(bodyTransform);
  1187. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1188. gObj.SetCollisionShape(shape);
  1189. gObj.SetUserPointer(pLocalID);
  1190. if (specialCollisionObjects.ContainsKey(pLocalID))
  1191. specialCollisionObjects[pLocalID] = gObj;
  1192. else
  1193. specialCollisionObjects.Add(pLocalID, gObj);
  1194. // TODO: Add to Special CollisionObjects!
  1195. return new BulletBodyXNA(pLocalID, gObj);
  1196. }
  1197. public override void SetCollisionShape(BulletWorld pWorld, BulletBody pCollisionObject, BulletShape pShape)
  1198. {
  1199. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1200. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  1201. if (pShape == null)
  1202. {
  1203. collisionObject.SetCollisionShape(new EmptyShape());
  1204. }
  1205. else
  1206. {
  1207. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1208. collisionObject.SetCollisionShape(shape);
  1209. }
  1210. }
  1211. public override BulletShape GetCollisionShape(BulletBody pCollisionObject)
  1212. {
  1213. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  1214. CollisionShape shape = collisionObject.GetCollisionShape();
  1215. return new BulletShapeXNA(shape, BSShapeTypeFromBroadPhaseNativeType(shape.GetShapeType()));
  1216. }
  1217. //(PhysicsScene.World.ptr, nativeShapeData)
  1218. public override BulletShape BuildNativeShape(BulletWorld pWorld, ShapeData pShapeData)
  1219. {
  1220. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1221. CollisionShape shape = null;
  1222. switch (pShapeData.Type)
  1223. {
  1224. case BSPhysicsShapeType.SHAPE_BOX:
  1225. shape = new BoxShape(new IndexedVector3(0.5f,0.5f,0.5f));
  1226. break;
  1227. case BSPhysicsShapeType.SHAPE_CONE:
  1228. shape = new ConeShapeZ(0.5f, 1.0f);
  1229. break;
  1230. case BSPhysicsShapeType.SHAPE_CYLINDER:
  1231. shape = new CylinderShapeZ(new IndexedVector3(0.5f, 0.5f, 0.5f));
  1232. break;
  1233. case BSPhysicsShapeType.SHAPE_SPHERE:
  1234. shape = new SphereShape(0.5f);
  1235. break;
  1236. }
  1237. if (shape != null)
  1238. {
  1239. IndexedVector3 scaling = new IndexedVector3(pShapeData.Scale.X, pShapeData.Scale.Y, pShapeData.Scale.Z);
  1240. shape.SetMargin(world.WorldSettings.Params.collisionMargin);
  1241. shape.SetLocalScaling(ref scaling);
  1242. }
  1243. return new BulletShapeXNA(shape, pShapeData.Type);
  1244. }
  1245. //PhysicsScene.World.ptr, false
  1246. public override BulletShape CreateCompoundShape(BulletWorld pWorld, bool enableDynamicAabbTree)
  1247. {
  1248. return new BulletShapeXNA(new CompoundShape(enableDynamicAabbTree), BSPhysicsShapeType.SHAPE_COMPOUND);
  1249. }
  1250. public override int GetNumberOfCompoundChildren(BulletShape pCompoundShape)
  1251. {
  1252. CompoundShape compoundshape = (pCompoundShape as BulletShapeXNA).shape as CompoundShape;
  1253. return compoundshape.GetNumChildShapes();
  1254. }
  1255. //LinksetRoot.PhysShape.ptr, newShape.ptr, displacementPos, displacementRot
  1256. public override void AddChildShapeToCompoundShape(BulletShape pCShape, BulletShape paddShape, Vector3 displacementPos, Quaternion displacementRot)
  1257. {
  1258. IndexedMatrix relativeTransform = new IndexedMatrix();
  1259. CompoundShape compoundshape = (pCShape as BulletShapeXNA).shape as CompoundShape;
  1260. CollisionShape addshape = (paddShape as BulletShapeXNA).shape;
  1261. relativeTransform._origin = new IndexedVector3(displacementPos.X, displacementPos.Y, displacementPos.Z);
  1262. relativeTransform.SetRotation(new IndexedQuaternion(displacementRot.X,displacementRot.Y,displacementRot.Z,displacementRot.W));
  1263. compoundshape.AddChildShape(ref relativeTransform, addshape);
  1264. }
  1265. public override BulletShape RemoveChildShapeFromCompoundShapeIndex(BulletShape pCShape, int pii)
  1266. {
  1267. CompoundShape compoundshape = (pCShape as BulletShapeXNA).shape as CompoundShape;
  1268. CollisionShape ret = null;
  1269. ret = compoundshape.GetChildShape(pii);
  1270. compoundshape.RemoveChildShapeByIndex(pii);
  1271. return new BulletShapeXNA(ret, BSShapeTypeFromBroadPhaseNativeType(ret.GetShapeType()));
  1272. }
  1273. public override BulletShape GetChildShapeFromCompoundShapeIndex(BulletShape cShape, int indx) {
  1274. if (cShape == null)
  1275. return null;
  1276. CompoundShape compoundShape = (cShape as BulletShapeXNA).shape as CompoundShape;
  1277. CollisionShape shape = compoundShape.GetChildShape(indx);
  1278. BulletShape retShape = new BulletShapeXNA(shape, BSShapeTypeFromBroadPhaseNativeType(shape.GetShapeType()));
  1279. return retShape;
  1280. }
  1281. public BSPhysicsShapeType BSShapeTypeFromBroadPhaseNativeType(BroadphaseNativeTypes pin)
  1282. {
  1283. BSPhysicsShapeType ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1284. switch (pin)
  1285. {
  1286. case BroadphaseNativeTypes.BOX_SHAPE_PROXYTYPE:
  1287. ret = BSPhysicsShapeType.SHAPE_BOX;
  1288. break;
  1289. case BroadphaseNativeTypes.TRIANGLE_SHAPE_PROXYTYPE:
  1290. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1291. break;
  1292. case BroadphaseNativeTypes.TETRAHEDRAL_SHAPE_PROXYTYPE:
  1293. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1294. break;
  1295. case BroadphaseNativeTypes.CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE:
  1296. ret = BSPhysicsShapeType.SHAPE_CONVEXHULL;
  1297. break;
  1298. case BroadphaseNativeTypes.CONVEX_HULL_SHAPE_PROXYTYPE:
  1299. ret = BSPhysicsShapeType.SHAPE_HULL;
  1300. break;
  1301. case BroadphaseNativeTypes.CONVEX_POINT_CLOUD_SHAPE_PROXYTYPE:
  1302. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1303. break;
  1304. case BroadphaseNativeTypes.CUSTOM_POLYHEDRAL_SHAPE_TYPE:
  1305. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1306. break;
  1307. //implicit convex shapes
  1308. case BroadphaseNativeTypes.IMPLICIT_CONVEX_SHAPES_START_HERE:
  1309. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1310. break;
  1311. case BroadphaseNativeTypes.SPHERE_SHAPE_PROXYTYPE:
  1312. ret = BSPhysicsShapeType.SHAPE_SPHERE;
  1313. break;
  1314. case BroadphaseNativeTypes.MULTI_SPHERE_SHAPE_PROXYTYPE:
  1315. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1316. break;
  1317. case BroadphaseNativeTypes.CAPSULE_SHAPE_PROXYTYPE:
  1318. ret = BSPhysicsShapeType.SHAPE_CAPSULE;
  1319. break;
  1320. case BroadphaseNativeTypes.CONE_SHAPE_PROXYTYPE:
  1321. ret = BSPhysicsShapeType.SHAPE_CONE;
  1322. break;
  1323. case BroadphaseNativeTypes.CONVEX_SHAPE_PROXYTYPE:
  1324. ret = BSPhysicsShapeType.SHAPE_CONVEXHULL;
  1325. break;
  1326. case BroadphaseNativeTypes.CYLINDER_SHAPE_PROXYTYPE:
  1327. ret = BSPhysicsShapeType.SHAPE_CYLINDER;
  1328. break;
  1329. case BroadphaseNativeTypes.UNIFORM_SCALING_SHAPE_PROXYTYPE:
  1330. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1331. break;
  1332. case BroadphaseNativeTypes.MINKOWSKI_SUM_SHAPE_PROXYTYPE:
  1333. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1334. break;
  1335. case BroadphaseNativeTypes.MINKOWSKI_DIFFERENCE_SHAPE_PROXYTYPE:
  1336. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1337. break;
  1338. case BroadphaseNativeTypes.BOX_2D_SHAPE_PROXYTYPE:
  1339. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1340. break;
  1341. case BroadphaseNativeTypes.CONVEX_2D_SHAPE_PROXYTYPE:
  1342. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1343. break;
  1344. case BroadphaseNativeTypes.CUSTOM_CONVEX_SHAPE_TYPE:
  1345. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1346. break;
  1347. //concave shape
  1348. case BroadphaseNativeTypes.CONCAVE_SHAPES_START_HERE:
  1349. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1350. break;
  1351. //keep all the convex shapetype below here, for the check IsConvexShape in broadphase proxy!
  1352. case BroadphaseNativeTypes.TRIANGLE_MESH_SHAPE_PROXYTYPE:
  1353. ret = BSPhysicsShapeType.SHAPE_MESH;
  1354. break;
  1355. case BroadphaseNativeTypes.SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE:
  1356. ret = BSPhysicsShapeType.SHAPE_MESH;
  1357. break;
  1358. ///used for demo integration FAST/Swift collision library and Bullet
  1359. case BroadphaseNativeTypes.FAST_CONCAVE_MESH_PROXYTYPE:
  1360. ret = BSPhysicsShapeType.SHAPE_MESH;
  1361. break;
  1362. //terrain
  1363. case BroadphaseNativeTypes.TERRAIN_SHAPE_PROXYTYPE:
  1364. ret = BSPhysicsShapeType.SHAPE_HEIGHTMAP;
  1365. break;
  1366. ///Used for GIMPACT Trimesh integration
  1367. case BroadphaseNativeTypes.GIMPACT_SHAPE_PROXYTYPE:
  1368. ret = BSPhysicsShapeType.SHAPE_GIMPACT;
  1369. break;
  1370. ///Multimaterial mesh
  1371. case BroadphaseNativeTypes.MULTIMATERIAL_TRIANGLE_MESH_PROXYTYPE:
  1372. ret = BSPhysicsShapeType.SHAPE_MESH;
  1373. break;
  1374. case BroadphaseNativeTypes.EMPTY_SHAPE_PROXYTYPE:
  1375. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1376. break;
  1377. case BroadphaseNativeTypes.STATIC_PLANE_PROXYTYPE:
  1378. ret = BSPhysicsShapeType.SHAPE_GROUNDPLANE;
  1379. break;
  1380. case BroadphaseNativeTypes.CUSTOM_CONCAVE_SHAPE_TYPE:
  1381. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1382. break;
  1383. case BroadphaseNativeTypes.CONCAVE_SHAPES_END_HERE:
  1384. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1385. break;
  1386. case BroadphaseNativeTypes.COMPOUND_SHAPE_PROXYTYPE:
  1387. ret = BSPhysicsShapeType.SHAPE_COMPOUND;
  1388. break;
  1389. case BroadphaseNativeTypes.SOFTBODY_SHAPE_PROXYTYPE:
  1390. ret = BSPhysicsShapeType.SHAPE_MESH;
  1391. break;
  1392. case BroadphaseNativeTypes.HFFLUID_SHAPE_PROXYTYPE:
  1393. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1394. break;
  1395. case BroadphaseNativeTypes.HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE:
  1396. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1397. break;
  1398. case BroadphaseNativeTypes.INVALID_SHAPE_PROXYTYPE:
  1399. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1400. break;
  1401. }
  1402. return ret;
  1403. }
  1404. public override void RemoveChildShapeFromCompoundShape(BulletShape cShape, BulletShape removeShape) { /* TODO */ }
  1405. public override void UpdateChildTransform(BulletShape pShape, int childIndex, Vector3 pos, Quaternion rot, bool shouldRecalculateLocalAabb) { /* TODO */ }
  1406. public override BulletShape CreateGroundPlaneShape(uint pLocalId, float pheight, float pcollisionMargin)
  1407. {
  1408. StaticPlaneShape m_planeshape = new StaticPlaneShape(new IndexedVector3(0,0,1),(int)pheight );
  1409. m_planeshape.SetMargin(pcollisionMargin);
  1410. m_planeshape.SetUserPointer(pLocalId);
  1411. return new BulletShapeXNA(m_planeshape, BSPhysicsShapeType.SHAPE_GROUNDPLANE);
  1412. }
  1413. public override BulletConstraint Create6DofSpringConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  1414. Vector3 pframe1, Quaternion pframe1rot, Vector3 pframe2, Quaternion pframe2rot,
  1415. bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
  1416. {
  1417. Generic6DofSpringConstraint constrain = null;
  1418. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1419. RigidBody body1 = (pBody1 as BulletBodyXNA).rigidBody;
  1420. RigidBody body2 = (pBody2 as BulletBodyXNA).rigidBody;
  1421. if (body1 != null && body2 != null)
  1422. {
  1423. IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
  1424. IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
  1425. IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
  1426. frame1._origin = frame1v;
  1427. IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
  1428. IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
  1429. IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
  1430. frame2._origin = frame1v;
  1431. constrain = new Generic6DofSpringConstraint(body1, body2, ref frame1, ref frame2, puseLinearReferenceFrameA);
  1432. world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
  1433. constrain.CalculateTransforms();
  1434. }
  1435. return new BulletConstraintXNA(constrain);
  1436. }
  1437. public override BulletConstraint CreateHingeConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  1438. Vector3 ppivotInA, Vector3 ppivotInB, Vector3 paxisInA, Vector3 paxisInB,
  1439. bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
  1440. {
  1441. HingeConstraint constrain = null;
  1442. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1443. RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
  1444. RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
  1445. if (rb1 != null && rb2 != null)
  1446. {
  1447. IndexedVector3 pivotInA = new IndexedVector3(ppivotInA.X, ppivotInA.Y, ppivotInA.Z);
  1448. IndexedVector3 pivotInB = new IndexedVector3(ppivotInB.X, ppivotInB.Y, ppivotInB.Z);
  1449. IndexedVector3 axisInA = new IndexedVector3(paxisInA.X, paxisInA.Y, paxisInA.Z);
  1450. IndexedVector3 axisInB = new IndexedVector3(paxisInB.X, paxisInB.Y, paxisInB.Z);
  1451. constrain = new HingeConstraint(rb1, rb2, ref pivotInA, ref pivotInB, ref axisInA, ref axisInB, puseLinearReferenceFrameA);
  1452. world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
  1453. }
  1454. return new BulletConstraintXNA(constrain);
  1455. }
  1456. public override BulletConstraint CreateSliderConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  1457. Vector3 pframe1, Quaternion pframe1rot,
  1458. Vector3 pframe2, Quaternion pframe2rot,
  1459. bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
  1460. {
  1461. SliderConstraint constrain = null;
  1462. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1463. RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
  1464. RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
  1465. if (rb1 != null && rb2 != null)
  1466. {
  1467. IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
  1468. IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
  1469. IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
  1470. frame1._origin = frame1v;
  1471. IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
  1472. IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
  1473. IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
  1474. frame2._origin = frame1v;
  1475. constrain = new SliderConstraint(rb1, rb2, ref frame1, ref frame2, puseLinearReferenceFrameA);
  1476. world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
  1477. }
  1478. return new BulletConstraintXNA(constrain);
  1479. }
  1480. public override BulletConstraint CreateConeTwistConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  1481. Vector3 pframe1, Quaternion pframe1rot,
  1482. Vector3 pframe2, Quaternion pframe2rot,
  1483. bool pdisableCollisionsBetweenLinkedBodies)
  1484. {
  1485. ConeTwistConstraint constrain = null;
  1486. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1487. RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
  1488. RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
  1489. if (rb1 != null && rb2 != null)
  1490. {
  1491. IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
  1492. IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
  1493. IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
  1494. frame1._origin = frame1v;
  1495. IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
  1496. IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
  1497. IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
  1498. frame2._origin = frame1v;
  1499. constrain = new ConeTwistConstraint(rb1, rb2, ref frame1, ref frame2);
  1500. world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
  1501. }
  1502. return new BulletConstraintXNA(constrain);
  1503. }
  1504. public override BulletConstraint CreateGearConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  1505. Vector3 paxisInA, Vector3 paxisInB,
  1506. float pratio, bool pdisableCollisionsBetweenLinkedBodies)
  1507. {
  1508. Generic6DofConstraint constrain = null;
  1509. /* BulletXNA does not have a gear constraint
  1510. GearConstraint constrain = null;
  1511. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1512. RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
  1513. RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
  1514. if (rb1 != null && rb2 != null)
  1515. {
  1516. IndexedVector3 axis1 = new IndexedVector3(paxisInA.X, paxisInA.Y, paxisInA.Z);
  1517. IndexedVector3 axis2 = new IndexedVector3(paxisInB.X, paxisInB.Y, paxisInB.Z);
  1518. constrain = new GearConstraint(rb1, rb2, ref axis1, ref axis2, pratio);
  1519. world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
  1520. }
  1521. */
  1522. return new BulletConstraintXNA(constrain);
  1523. }
  1524. public override BulletConstraint CreatePoint2PointConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  1525. Vector3 ppivotInA, Vector3 ppivotInB,
  1526. bool pdisableCollisionsBetweenLinkedBodies)
  1527. {
  1528. Point2PointConstraint constrain = null;
  1529. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1530. RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
  1531. RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
  1532. if (rb1 != null && rb2 != null)
  1533. {
  1534. IndexedVector3 pivotInA = new IndexedVector3(ppivotInA.X, ppivotInA.Y, ppivotInA.Z);
  1535. IndexedVector3 pivotInB = new IndexedVector3(ppivotInB.X, ppivotInB.Y, ppivotInB.Z);
  1536. constrain = new Point2PointConstraint(rb1, rb2, ref pivotInA, ref pivotInB);
  1537. world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
  1538. }
  1539. return new BulletConstraintXNA(constrain);
  1540. }
  1541. public override BulletShape CreateHullShape(BulletWorld pWorld, int pHullCount, float[] pConvHulls)
  1542. {
  1543. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1544. CompoundShape compoundshape = new CompoundShape(false);
  1545. compoundshape.SetMargin(world.WorldSettings.Params.collisionMargin);
  1546. int ii = 1;
  1547. for (int i = 0; i < pHullCount; i++)
  1548. {
  1549. int vertexCount = (int) pConvHulls[ii];
  1550. IndexedVector3 centroid = new IndexedVector3(pConvHulls[ii + 1], pConvHulls[ii + 2], pConvHulls[ii + 3]);
  1551. IndexedMatrix childTrans = IndexedMatrix.Identity;
  1552. childTrans._origin = centroid;
  1553. List<IndexedVector3> virts = new List<IndexedVector3>();
  1554. int ender = ((ii + 4) + (vertexCount*3));
  1555. for (int iii = ii + 4; iii < ender; iii+=3)
  1556. {
  1557. virts.Add(new IndexedVector3(pConvHulls[iii], pConvHulls[iii + 1], pConvHulls[iii +2]));
  1558. }
  1559. ConvexHullShape convexShape = new ConvexHullShape(virts, vertexCount);
  1560. convexShape.SetMargin(world.WorldSettings.Params.collisionMargin);
  1561. compoundshape.AddChildShape(ref childTrans, convexShape);
  1562. ii += (vertexCount*3 + 4);
  1563. }
  1564. return new BulletShapeXNA(compoundshape, BSPhysicsShapeType.SHAPE_HULL);
  1565. }
  1566. public override BulletShape BuildHullShapeFromMesh(BulletWorld world, BulletShape meshShape, HACDParams parms)
  1567. {
  1568. /* TODO */ return null;
  1569. }
  1570. public override BulletShape BuildConvexHullShapeFromMesh(BulletWorld world, BulletShape meshShape)
  1571. {
  1572. /* TODO */ return null;
  1573. }
  1574. public override BulletShape CreateConvexHullShape(BulletWorld pWorld, int pIndicesCount, int[] indices, int pVerticesCount, float[] verticesAsFloats)
  1575. {
  1576. /* TODO */ return null;
  1577. }
  1578. public override BulletShape CreateMeshShape(BulletWorld pWorld, int pIndicesCount, int[] indices, int pVerticesCount, float[] verticesAsFloats)
  1579. {
  1580. //DumpRaw(indices,verticesAsFloats,pIndicesCount,pVerticesCount);
  1581. for (int iter = 0; iter < pVerticesCount; iter++)
  1582. {
  1583. if (verticesAsFloats[iter] > 0 && verticesAsFloats[iter] < 0.0001) verticesAsFloats[iter] = 0;
  1584. if (verticesAsFloats[iter] < 0 && verticesAsFloats[iter] > -0.0001) verticesAsFloats[iter] = 0;
  1585. }
  1586. ObjectArray<int> indicesarr = new ObjectArray<int>(indices);
  1587. ObjectArray<float> vertices = new ObjectArray<float>(verticesAsFloats);
  1588. DumpRaw(indicesarr,vertices,pIndicesCount,pVerticesCount);
  1589. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1590. IndexedMesh mesh = new IndexedMesh();
  1591. mesh.m_indexType = PHY_ScalarType.PHY_INTEGER;
  1592. mesh.m_numTriangles = pIndicesCount/3;
  1593. mesh.m_numVertices = pVerticesCount;
  1594. mesh.m_triangleIndexBase = indicesarr;
  1595. mesh.m_vertexBase = vertices;
  1596. mesh.m_vertexStride = 3;
  1597. mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT;
  1598. mesh.m_triangleIndexStride = 3;
  1599. TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray();
  1600. tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER);
  1601. BvhTriangleMeshShape meshShape = new BvhTriangleMeshShape(tribuilder, true,true);
  1602. meshShape.SetMargin(world.WorldSettings.Params.collisionMargin);
  1603. // world.UpdateSingleAabb(meshShape);
  1604. return new BulletShapeXNA(meshShape, BSPhysicsShapeType.SHAPE_MESH);
  1605. }
  1606. public override BulletShape CreateGImpactShape(BulletWorld pWorld, int pIndicesCount, int[] indices, int pVerticesCount, float[] verticesAsFloats)
  1607. {
  1608. // TODO:
  1609. return null;
  1610. }
  1611. public static void DumpRaw(ObjectArray<int>indices, ObjectArray<float> vertices, int pIndicesCount,int pVerticesCount )
  1612. {
  1613. String fileName = "objTest3.raw";
  1614. String completePath = System.IO.Path.Combine(Util.configDir(), fileName);
  1615. StreamWriter sw = new StreamWriter(completePath);
  1616. IndexedMesh mesh = new IndexedMesh();
  1617. mesh.m_indexType = PHY_ScalarType.PHY_INTEGER;
  1618. mesh.m_numTriangles = pIndicesCount / 3;
  1619. mesh.m_numVertices = pVerticesCount;
  1620. mesh.m_triangleIndexBase = indices;
  1621. mesh.m_vertexBase = vertices;
  1622. mesh.m_vertexStride = 3;
  1623. mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT;
  1624. mesh.m_triangleIndexStride = 3;
  1625. TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray();
  1626. tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER);
  1627. for (int i = 0; i < pVerticesCount; i++)
  1628. {
  1629. string s = vertices[indices[i * 3]].ToString("0.0000");
  1630. s += " " + vertices[indices[i * 3 + 1]].ToString("0.0000");
  1631. s += " " + vertices[indices[i * 3 + 2]].ToString("0.0000");
  1632. sw.Write(s + "\n");
  1633. }
  1634. sw.Close();
  1635. }
  1636. public static void DumpRaw(int[] indices, float[] vertices, int pIndicesCount, int pVerticesCount)
  1637. {
  1638. String fileName = "objTest6.raw";
  1639. String completePath = System.IO.Path.Combine(Util.configDir(), fileName);
  1640. StreamWriter sw = new StreamWriter(completePath);
  1641. IndexedMesh mesh = new IndexedMesh();
  1642. mesh.m_indexType = PHY_ScalarType.PHY_INTEGER;
  1643. mesh.m_numTriangles = pIndicesCount / 3;
  1644. mesh.m_numVertices = pVerticesCount;
  1645. mesh.m_triangleIndexBase = indices;
  1646. mesh.m_vertexBase = vertices;
  1647. mesh.m_vertexStride = 3;
  1648. mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT;
  1649. mesh.m_triangleIndexStride = 3;
  1650. TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray();
  1651. tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER);
  1652. sw.WriteLine("Indices");
  1653. sw.WriteLine(string.Format("int[] indices = new int[{0}];",pIndicesCount));
  1654. for (int iter = 0; iter < indices.Length; iter++)
  1655. {
  1656. sw.WriteLine(string.Format("indices[{0}]={1};",iter,indices[iter]));
  1657. }
  1658. sw.WriteLine("VerticesFloats");
  1659. sw.WriteLine(string.Format("float[] vertices = new float[{0}];", pVerticesCount));
  1660. for (int iter = 0; iter < vertices.Length; iter++)
  1661. {
  1662. sw.WriteLine(string.Format("Vertices[{0}]={1};", iter, vertices[iter].ToString("0.0000")));
  1663. }
  1664. // for (int i = 0; i < pVerticesCount; i++)
  1665. // {
  1666. //
  1667. // string s = vertices[indices[i * 3]].ToString("0.0000");
  1668. // s += " " + vertices[indices[i * 3 + 1]].ToString("0.0000");
  1669. // s += " " + vertices[indices[i * 3 + 2]].ToString("0.0000");
  1670. //
  1671. // sw.Write(s + "\n");
  1672. //}
  1673. sw.Close();
  1674. }
  1675. public override BulletShape CreateTerrainShape(uint id, Vector3 size, float minHeight, float maxHeight, float[] heightMap,
  1676. float scaleFactor, float collisionMargin)
  1677. {
  1678. const int upAxis = 2;
  1679. HeightfieldTerrainShape terrainShape = new HeightfieldTerrainShape((int)size.X, (int)size.Y,
  1680. heightMap, scaleFactor,
  1681. minHeight, maxHeight, upAxis,
  1682. false);
  1683. terrainShape.SetMargin(collisionMargin + 0.5f);
  1684. terrainShape.SetUseDiamondSubdivision(true);
  1685. terrainShape.SetUserPointer(id);
  1686. return new BulletShapeXNA(terrainShape, BSPhysicsShapeType.SHAPE_TERRAIN);
  1687. }
  1688. public override bool TranslationalLimitMotor(BulletConstraint pConstraint, float ponOff, float targetVelocity, float maxMotorForce)
  1689. {
  1690. TypedConstraint tconstrain = (pConstraint as BulletConstraintXNA).constrain;
  1691. bool onOff = ponOff != 0;
  1692. bool ret = false;
  1693. switch (tconstrain.GetConstraintType())
  1694. {
  1695. case TypedConstraintType.D6_CONSTRAINT_TYPE:
  1696. Generic6DofConstraint constrain = tconstrain as Generic6DofConstraint;
  1697. constrain.GetTranslationalLimitMotor().m_enableMotor[0] = onOff;
  1698. constrain.GetTranslationalLimitMotor().m_targetVelocity[0] = targetVelocity;
  1699. constrain.GetTranslationalLimitMotor().m_maxMotorForce[0] = maxMotorForce;
  1700. ret = true;
  1701. break;
  1702. }
  1703. return ret;
  1704. }
  1705. public override int PhysicsStep(BulletWorld world, float timeStep, int maxSubSteps, float fixedTimeStep,
  1706. out int updatedEntityCount, out int collidersCount)
  1707. {
  1708. /* TODO */
  1709. updatedEntityCount = 0;
  1710. collidersCount = 0;
  1711. int ret = PhysicsStep2(world,timeStep,maxSubSteps,fixedTimeStep,out updatedEntityCount,out world.physicsScene.m_updateArray, out collidersCount, out world.physicsScene.m_collisionArray);
  1712. return ret;
  1713. }
  1714. private int PhysicsStep2(BulletWorld pWorld, float timeStep, int m_maxSubSteps, float m_fixedTimeStep,
  1715. out int updatedEntityCount, out EntityProperties[] updatedEntities,
  1716. out int collidersCount, out CollisionDesc[] colliders)
  1717. {
  1718. int epic = PhysicsStepint(pWorld, timeStep, m_maxSubSteps, m_fixedTimeStep, out updatedEntityCount, out updatedEntities,
  1719. out collidersCount, out colliders, m_maxCollisions, m_maxUpdatesPerFrame);
  1720. return epic;
  1721. }
  1722. private int PhysicsStepint(BulletWorld pWorld,float timeStep, int m_maxSubSteps, float m_fixedTimeStep, out int updatedEntityCount,
  1723. out EntityProperties[] updatedEntities, out int collidersCount, out CollisionDesc[] colliders, int maxCollisions, int maxUpdates)
  1724. {
  1725. int numSimSteps = 0;
  1726. Array.Clear(UpdatedObjects, 0, UpdatedObjects.Length);
  1727. Array.Clear(UpdatedCollisions, 0, UpdatedCollisions.Length);
  1728. LastEntityProperty=0;
  1729. LastCollisionDesc=0;
  1730. updatedEntityCount = 0;
  1731. collidersCount = 0;
  1732. if (pWorld is BulletWorldXNA)
  1733. {
  1734. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1735. world.LastCollisionDesc = 0;
  1736. world.LastEntityProperty = 0;
  1737. numSimSteps = world.StepSimulation(timeStep, m_maxSubSteps, m_fixedTimeStep);
  1738. int updates = 0;
  1739. PersistentManifold contactManifold;
  1740. CollisionObject objA;
  1741. CollisionObject objB;
  1742. ManifoldPoint manifoldPoint;
  1743. PairCachingGhostObject pairCachingGhostObject;
  1744. m_collisionsThisFrame = 0;
  1745. int numManifolds = world.GetDispatcher().GetNumManifolds();
  1746. for (int j = 0; j < numManifolds; j++)
  1747. {
  1748. contactManifold = world.GetDispatcher().GetManifoldByIndexInternal(j);
  1749. int numContacts = contactManifold.GetNumContacts();
  1750. if (numContacts == 0)
  1751. continue;
  1752. objA = contactManifold.GetBody0() as CollisionObject;
  1753. objB = contactManifold.GetBody1() as CollisionObject;
  1754. manifoldPoint = contactManifold.GetContactPoint(0);
  1755. //IndexedVector3 contactPoint = manifoldPoint.GetPositionWorldOnB();
  1756. // IndexedVector3 contactNormal = -manifoldPoint.m_normalWorldOnB; // make relative to A
  1757. RecordCollision(this, objA, objB, manifoldPoint.GetPositionWorldOnB(), -manifoldPoint.m_normalWorldOnB, manifoldPoint.GetDistance());
  1758. m_collisionsThisFrame ++;
  1759. if (m_collisionsThisFrame >= 9999999)
  1760. break;
  1761. }
  1762. foreach (GhostObject ghostObject in specialCollisionObjects.Values)
  1763. {
  1764. pairCachingGhostObject = ghostObject as PairCachingGhostObject;
  1765. if (pairCachingGhostObject != null)
  1766. {
  1767. RecordGhostCollisions(pairCachingGhostObject);
  1768. }
  1769. }
  1770. updatedEntityCount = LastEntityProperty;
  1771. updatedEntities = UpdatedObjects;
  1772. collidersCount = LastCollisionDesc;
  1773. colliders = UpdatedCollisions;
  1774. }
  1775. else
  1776. {
  1777. //if (updatedEntities is null)
  1778. //updatedEntities = new List<BulletXNA.EntityProperties>();
  1779. //updatedEntityCount = 0;
  1780. //collidersCount = 0;
  1781. updatedEntities = new EntityProperties[0];
  1782. colliders = new CollisionDesc[0];
  1783. }
  1784. return numSimSteps;
  1785. }
  1786. public void RecordGhostCollisions(PairCachingGhostObject obj)
  1787. {
  1788. IOverlappingPairCache cache = obj.GetOverlappingPairCache();
  1789. ObjectArray<BroadphasePair> pairs = cache.GetOverlappingPairArray();
  1790. DiscreteDynamicsWorld world = (PhysicsScene.World as BulletWorldXNA).world;
  1791. PersistentManifoldArray manifoldArray = new PersistentManifoldArray();
  1792. BroadphasePair collisionPair;
  1793. PersistentManifold contactManifold;
  1794. CollisionObject objA;
  1795. CollisionObject objB;
  1796. ManifoldPoint pt;
  1797. int numPairs = pairs.Count;
  1798. for (int i = 0; i < numPairs; i++)
  1799. {
  1800. manifoldArray.Clear();
  1801. if (LastCollisionDesc < UpdatedCollisions.Length)
  1802. break;
  1803. collisionPair = world.GetPairCache().FindPair(pairs[i].m_pProxy0, pairs[i].m_pProxy1);
  1804. if (collisionPair == null)
  1805. continue;
  1806. collisionPair.m_algorithm.GetAllContactManifolds(manifoldArray);
  1807. for (int j = 0; j < manifoldArray.Count; j++)
  1808. {
  1809. contactManifold = manifoldArray[j];
  1810. int numContacts = contactManifold.GetNumContacts();
  1811. objA = contactManifold.GetBody0() as CollisionObject;
  1812. objB = contactManifold.GetBody1() as CollisionObject;
  1813. for (int p = 0; p < numContacts; p++)
  1814. {
  1815. pt = contactManifold.GetContactPoint(p);
  1816. if (pt.GetDistance() < 0.0f)
  1817. {
  1818. RecordCollision(this, objA, objB, pt.GetPositionWorldOnA(), -pt.m_normalWorldOnB,pt.GetDistance());
  1819. break;
  1820. }
  1821. }
  1822. }
  1823. }
  1824. }
  1825. private static void RecordCollision(BSAPIXNA world, CollisionObject objA, CollisionObject objB, IndexedVector3 contact, IndexedVector3 norm, float penetration)
  1826. {
  1827. IndexedVector3 contactNormal = norm;
  1828. if ((objA.GetCollisionFlags() & BulletXNA.BulletCollision.CollisionFlags.BS_WANTS_COLLISIONS) == 0 &&
  1829. (objB.GetCollisionFlags() & BulletXNA.BulletCollision.CollisionFlags.BS_WANTS_COLLISIONS) == 0)
  1830. {
  1831. return;
  1832. }
  1833. uint idA = (uint)objA.GetUserPointer();
  1834. uint idB = (uint)objB.GetUserPointer();
  1835. if (idA > idB)
  1836. {
  1837. uint temp = idA;
  1838. idA = idB;
  1839. idB = temp;
  1840. contactNormal = -contactNormal;
  1841. }
  1842. //ulong collisionID = ((ulong) idA << 32) | idB;
  1843. CollisionDesc cDesc = new CollisionDesc()
  1844. {
  1845. aID = idA,
  1846. bID = idB,
  1847. point = new Vector3(contact.X,contact.Y,contact.Z),
  1848. normal = new Vector3(contactNormal.X,contactNormal.Y,contactNormal.Z),
  1849. penetration = penetration
  1850. };
  1851. if (world.LastCollisionDesc < world.UpdatedCollisions.Length)
  1852. world.UpdatedCollisions[world.LastCollisionDesc++] = (cDesc);
  1853. m_collisionsThisFrame++;
  1854. }
  1855. private static EntityProperties GetDebugProperties(BulletWorld pWorld, BulletBody pCollisionObject)
  1856. {
  1857. EntityProperties ent = new EntityProperties();
  1858. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1859. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  1860. IndexedMatrix transform = collisionObject.GetWorldTransform();
  1861. IndexedVector3 LinearVelocity = collisionObject.GetInterpolationLinearVelocity();
  1862. IndexedVector3 AngularVelocity = collisionObject.GetInterpolationAngularVelocity();
  1863. IndexedQuaternion rotation = transform.GetRotation();
  1864. ent.Acceleration = Vector3.Zero;
  1865. ent.ID = (uint)collisionObject.GetUserPointer();
  1866. ent.Position = new Vector3(transform._origin.X,transform._origin.Y,transform._origin.Z);
  1867. ent.Rotation = new Quaternion(rotation.X,rotation.Y,rotation.Z,rotation.W);
  1868. ent.Velocity = new Vector3(LinearVelocity.X, LinearVelocity.Y, LinearVelocity.Z);
  1869. ent.RotationalVelocity = new Vector3(AngularVelocity.X, AngularVelocity.Y, AngularVelocity.Z);
  1870. return ent;
  1871. }
  1872. public override bool UpdateParameter(BulletWorld world, uint localID, String parm, float value) { /* TODO */
  1873. return false; }
  1874. public override Vector3 GetLocalScaling(BulletShape pShape)
  1875. {
  1876. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1877. IndexedVector3 scale = shape.GetLocalScaling();
  1878. return new Vector3(scale.X,scale.Y,scale.Z);
  1879. }
  1880. public bool RayCastGround(BulletWorld pWorld, Vector3 _RayOrigin, float pRayHeight, BulletBody NotMe)
  1881. {
  1882. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1883. if (world != null)
  1884. {
  1885. if (NotMe is BulletBodyXNA && NotMe.HasPhysicalBody)
  1886. {
  1887. CollisionObject AvoidBody = (NotMe as BulletBodyXNA).body;
  1888. IndexedVector3 rOrigin = new IndexedVector3(_RayOrigin.X, _RayOrigin.Y, _RayOrigin.Z);
  1889. IndexedVector3 rEnd = new IndexedVector3(_RayOrigin.X, _RayOrigin.Y, _RayOrigin.Z - pRayHeight);
  1890. using (
  1891. ClosestNotMeRayResultCallback rayCallback =
  1892. new ClosestNotMeRayResultCallback(rOrigin, rEnd, AvoidBody)
  1893. )
  1894. {
  1895. world.RayTest(ref rOrigin, ref rEnd, rayCallback);
  1896. if (rayCallback.HasHit())
  1897. {
  1898. IndexedVector3 hitLocation = rayCallback.m_hitPointWorld;
  1899. }
  1900. return rayCallback.HasHit();
  1901. }
  1902. }
  1903. }
  1904. return false;
  1905. }
  1906. }
  1907. public class SimMotionState : DefaultMotionState
  1908. {
  1909. public RigidBody Rigidbody;
  1910. public Vector3 ZeroVect;
  1911. private IndexedMatrix m_xform;
  1912. private EntityProperties m_properties;
  1913. private EntityProperties m_lastProperties;
  1914. private BSAPIXNA m_world;
  1915. const float POSITION_TOLERANCE = 0.05f;
  1916. const float VELOCITY_TOLERANCE = 0.001f;
  1917. const float ROTATION_TOLERANCE = 0.01f;
  1918. const float ANGULARVELOCITY_TOLERANCE = 0.01f;
  1919. public SimMotionState(BSAPIXNA pWorld, uint id, IndexedMatrix starTransform, object frameUpdates)
  1920. {
  1921. IndexedQuaternion OrientationQuaterion = starTransform.GetRotation();
  1922. m_properties = new EntityProperties()
  1923. {
  1924. ID = id,
  1925. Position = new Vector3(starTransform._origin.X, starTransform._origin.Y,starTransform._origin.Z),
  1926. Rotation = new Quaternion(OrientationQuaterion.X,OrientationQuaterion.Y,OrientationQuaterion.Z,OrientationQuaterion.W)
  1927. };
  1928. m_lastProperties = new EntityProperties()
  1929. {
  1930. ID = id,
  1931. Position = new Vector3(starTransform._origin.X, starTransform._origin.Y, starTransform._origin.Z),
  1932. Rotation = new Quaternion(OrientationQuaterion.X, OrientationQuaterion.Y, OrientationQuaterion.Z, OrientationQuaterion.W)
  1933. };
  1934. m_world = pWorld;
  1935. m_xform = starTransform;
  1936. }
  1937. public override void GetWorldTransform(out IndexedMatrix worldTrans)
  1938. {
  1939. worldTrans = m_xform;
  1940. }
  1941. public override void SetWorldTransform(IndexedMatrix worldTrans)
  1942. {
  1943. SetWorldTransform(ref worldTrans);
  1944. }
  1945. public override void SetWorldTransform(ref IndexedMatrix worldTrans)
  1946. {
  1947. SetWorldTransform(ref worldTrans, false);
  1948. }
  1949. public void SetWorldTransform(ref IndexedMatrix worldTrans, bool force)
  1950. {
  1951. m_xform = worldTrans;
  1952. // Put the new transform into m_properties
  1953. IndexedQuaternion OrientationQuaternion = m_xform.GetRotation();
  1954. IndexedVector3 LinearVelocityVector = Rigidbody.GetLinearVelocity();
  1955. IndexedVector3 AngularVelocityVector = Rigidbody.GetAngularVelocity();
  1956. m_properties.Position = new Vector3(m_xform._origin.X, m_xform._origin.Y, m_xform._origin.Z);
  1957. m_properties.Rotation = new Quaternion(OrientationQuaternion.X, OrientationQuaternion.Y,
  1958. OrientationQuaternion.Z, OrientationQuaternion.W);
  1959. // A problem with stock Bullet is that we don't get an event when an object is deactivated.
  1960. // This means that the last non-zero values for linear and angular velocity
  1961. // are left in the viewer who does dead reconning and the objects look like
  1962. // they float off.
  1963. // BulletSim ships with a patch to Bullet which creates such an event.
  1964. m_properties.Velocity = new Vector3(LinearVelocityVector.X, LinearVelocityVector.Y, LinearVelocityVector.Z);
  1965. m_properties.RotationalVelocity = new Vector3(AngularVelocityVector.X, AngularVelocityVector.Y, AngularVelocityVector.Z);
  1966. if (force
  1967. || !AlmostEqual(ref m_lastProperties.Position, ref m_properties.Position, POSITION_TOLERANCE)
  1968. || !AlmostEqual(ref m_properties.Rotation, ref m_lastProperties.Rotation, ROTATION_TOLERANCE)
  1969. // If the Velocity and AngularVelocity are zero, most likely the object has
  1970. // been deactivated. If they both are zero and they have become zero recently,
  1971. // make sure a property update is sent so the zeros make it to the viewer.
  1972. || ((m_properties.Velocity == ZeroVect && m_properties.RotationalVelocity == ZeroVect)
  1973. &&
  1974. (m_properties.Velocity != m_lastProperties.Velocity ||
  1975. m_properties.RotationalVelocity != m_lastProperties.RotationalVelocity))
  1976. // If Velocity and AngularVelocity are non-zero but have changed, send an update.
  1977. || !AlmostEqual(ref m_properties.Velocity, ref m_lastProperties.Velocity, VELOCITY_TOLERANCE)
  1978. ||
  1979. !AlmostEqual(ref m_properties.RotationalVelocity, ref m_lastProperties.RotationalVelocity,
  1980. ANGULARVELOCITY_TOLERANCE)
  1981. )
  1982. {
  1983. // Add this update to the list of updates for this frame.
  1984. m_lastProperties = m_properties;
  1985. if (m_world.LastEntityProperty < m_world.UpdatedObjects.Length)
  1986. m_world.UpdatedObjects[m_world.LastEntityProperty++]=(m_properties);
  1987. //(*m_updatesThisFrame)[m_properties.ID] = &m_properties;
  1988. }
  1989. }
  1990. public override void SetRigidBody(RigidBody body)
  1991. {
  1992. Rigidbody = body;
  1993. }
  1994. internal static bool AlmostEqual(ref Vector3 v1, ref Vector3 v2, float nEpsilon)
  1995. {
  1996. return
  1997. (((v1.X - nEpsilon) < v2.X) && (v2.X < (v1.X + nEpsilon))) &&
  1998. (((v1.Y - nEpsilon) < v2.Y) && (v2.Y < (v1.Y + nEpsilon))) &&
  1999. (((v1.Z - nEpsilon) < v2.Z) && (v2.Z < (v1.Z + nEpsilon)));
  2000. }
  2001. internal static bool AlmostEqual(ref Quaternion v1, ref Quaternion v2, float nEpsilon)
  2002. {
  2003. return
  2004. (((v1.X - nEpsilon) < v2.X) && (v2.X < (v1.X + nEpsilon))) &&
  2005. (((v1.Y - nEpsilon) < v2.Y) && (v2.Y < (v1.Y + nEpsilon))) &&
  2006. (((v1.Z - nEpsilon) < v2.Z) && (v2.Z < (v1.Z + nEpsilon))) &&
  2007. (((v1.W - nEpsilon) < v2.W) && (v2.W < (v1.W + nEpsilon)));
  2008. }
  2009. }
  2010. }