BSAPIXNA.cs 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  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. // Turn off Pooling since globals and pooling are bad for threading.
  1064. BulletGlobals.VoronoiSimplexSolverPool.SetPoolingEnabled(false);
  1065. BulletGlobals.SubSimplexConvexCastPool.SetPoolingEnabled(false);
  1066. BulletGlobals.ManifoldPointPool.SetPoolingEnabled(false);
  1067. BulletGlobals.CastResultPool.SetPoolingEnabled(false);
  1068. BulletGlobals.SphereShapePool.SetPoolingEnabled(false);
  1069. BulletGlobals.DbvtNodePool.SetPoolingEnabled(false);
  1070. BulletGlobals.SingleRayCallbackPool.SetPoolingEnabled(false);
  1071. BulletGlobals.SubSimplexClosestResultPool.SetPoolingEnabled(false);
  1072. BulletGlobals.GjkPairDetectorPool.SetPoolingEnabled(false);
  1073. BulletGlobals.DbvtTreeColliderPool.SetPoolingEnabled(false);
  1074. BulletGlobals.SingleSweepCallbackPool.SetPoolingEnabled(false);
  1075. BulletGlobals.BroadphaseRayTesterPool.SetPoolingEnabled(false);
  1076. BulletGlobals.ClosestNotMeConvexResultCallbackPool.SetPoolingEnabled(false);
  1077. BulletGlobals.GjkEpaPenetrationDepthSolverPool.SetPoolingEnabled(false);
  1078. BulletGlobals.ContinuousConvexCollisionPool.SetPoolingEnabled(false);
  1079. BulletGlobals.DbvtStackDataBlockPool.SetPoolingEnabled(false);
  1080. BulletGlobals.BoxBoxCollisionAlgorithmPool.SetPoolingEnabled(false);
  1081. BulletGlobals.CompoundCollisionAlgorithmPool.SetPoolingEnabled(false);
  1082. BulletGlobals.ConvexConcaveCollisionAlgorithmPool.SetPoolingEnabled(false);
  1083. BulletGlobals.ConvexConvexAlgorithmPool.SetPoolingEnabled(false);
  1084. BulletGlobals.ConvexPlaneAlgorithmPool.SetPoolingEnabled(false);
  1085. BulletGlobals.SphereBoxCollisionAlgorithmPool.SetPoolingEnabled(false);
  1086. BulletGlobals.SphereSphereCollisionAlgorithmPool.SetPoolingEnabled(false);
  1087. BulletGlobals.SphereTriangleCollisionAlgorithmPool.SetPoolingEnabled(false);
  1088. BulletGlobals.GImpactCollisionAlgorithmPool.SetPoolingEnabled(false);
  1089. BulletGlobals.GjkEpaSolver2MinkowskiDiffPool.SetPoolingEnabled(false);
  1090. BulletGlobals.PersistentManifoldPool.SetPoolingEnabled(false);
  1091. BulletGlobals.ManifoldResultPool.SetPoolingEnabled(false);
  1092. BulletGlobals.GJKPool.SetPoolingEnabled(false);
  1093. BulletGlobals.GIM_ShapeRetrieverPool.SetPoolingEnabled(false);
  1094. BulletGlobals.TriangleShapePool.SetPoolingEnabled(false);
  1095. BulletGlobals.SphereTriangleDetectorPool.SetPoolingEnabled(false);
  1096. BulletGlobals.CompoundLeafCallbackPool.SetPoolingEnabled(false);
  1097. BulletGlobals.GjkConvexCastPool.SetPoolingEnabled(false);
  1098. BulletGlobals.LocalTriangleSphereCastCallbackPool.SetPoolingEnabled(false);
  1099. BulletGlobals.BridgeTriangleRaycastCallbackPool.SetPoolingEnabled(false);
  1100. BulletGlobals.BridgeTriangleConcaveRaycastCallbackPool.SetPoolingEnabled(false);
  1101. BulletGlobals.BridgeTriangleConvexcastCallbackPool.SetPoolingEnabled(false);
  1102. BulletGlobals.MyNodeOverlapCallbackPool.SetPoolingEnabled(false);
  1103. BulletGlobals.ClosestRayResultCallbackPool.SetPoolingEnabled(false);
  1104. BulletGlobals.DebugDrawcallbackPool.SetPoolingEnabled(false);
  1105. return world;
  1106. }
  1107. //m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_STOP_CFM, cfm, ConstraintParamAxis.AXIS_ALL
  1108. public override bool SetConstraintParam(BulletConstraint pConstraint, ConstraintParams paramIndex, float paramvalue, ConstraintParamAxis axis)
  1109. {
  1110. Generic6DofConstraint constrain = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
  1111. if (axis == ConstraintParamAxis.AXIS_LINEAR_ALL || axis == ConstraintParamAxis.AXIS_ALL)
  1112. {
  1113. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams) (int) paramIndex, paramvalue, 0);
  1114. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams) (int) paramIndex, paramvalue, 1);
  1115. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams) (int) paramIndex, paramvalue, 2);
  1116. }
  1117. if (axis == ConstraintParamAxis.AXIS_ANGULAR_ALL || axis == ConstraintParamAxis.AXIS_ALL)
  1118. {
  1119. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams)(int)paramIndex, paramvalue, 3);
  1120. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams)(int)paramIndex, paramvalue, 4);
  1121. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams)(int)paramIndex, paramvalue, 5);
  1122. }
  1123. if (axis == ConstraintParamAxis.AXIS_LINEAR_ALL)
  1124. {
  1125. constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams)(int)paramIndex, paramvalue, (int)axis);
  1126. }
  1127. return true;
  1128. }
  1129. public override bool PushUpdate(BulletBody pCollisionObject)
  1130. {
  1131. bool ret = false;
  1132. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  1133. RigidBody rb = collisionObject as RigidBody;
  1134. if (rb != null)
  1135. {
  1136. SimMotionState sms = rb.GetMotionState() as SimMotionState;
  1137. if (sms != null)
  1138. {
  1139. IndexedMatrix wt = IndexedMatrix.Identity;
  1140. sms.GetWorldTransform(out wt);
  1141. sms.SetWorldTransform(ref wt, true);
  1142. ret = true;
  1143. }
  1144. }
  1145. return ret;
  1146. }
  1147. public override float GetAngularMotionDisc(BulletShape pShape)
  1148. {
  1149. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1150. return shape.GetAngularMotionDisc();
  1151. }
  1152. public override float GetContactBreakingThreshold(BulletShape pShape, float defaultFactor)
  1153. {
  1154. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1155. return shape.GetContactBreakingThreshold(defaultFactor);
  1156. }
  1157. public override bool IsCompound(BulletShape pShape)
  1158. {
  1159. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1160. return shape.IsCompound();
  1161. }
  1162. public override bool IsSoftBody(BulletShape pShape)
  1163. {
  1164. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1165. return shape.IsSoftBody();
  1166. }
  1167. public override bool IsPolyhedral(BulletShape pShape)
  1168. {
  1169. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1170. return shape.IsPolyhedral();
  1171. }
  1172. public override bool IsConvex2d(BulletShape pShape)
  1173. {
  1174. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1175. return shape.IsConvex2d();
  1176. }
  1177. public override bool IsConvex(BulletShape pShape)
  1178. {
  1179. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1180. return shape.IsConvex();
  1181. }
  1182. public override bool IsNonMoving(BulletShape pShape)
  1183. {
  1184. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1185. return shape.IsNonMoving();
  1186. }
  1187. public override bool IsConcave(BulletShape pShape)
  1188. {
  1189. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1190. return shape.IsConcave();
  1191. }
  1192. public override bool IsInfinite(BulletShape pShape)
  1193. {
  1194. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1195. return shape.IsInfinite();
  1196. }
  1197. public override bool IsNativeShape(BulletShape pShape)
  1198. {
  1199. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1200. bool ret;
  1201. switch (shape.GetShapeType())
  1202. {
  1203. case BroadphaseNativeTypes.BOX_SHAPE_PROXYTYPE:
  1204. case BroadphaseNativeTypes.CONE_SHAPE_PROXYTYPE:
  1205. case BroadphaseNativeTypes.SPHERE_SHAPE_PROXYTYPE:
  1206. case BroadphaseNativeTypes.CYLINDER_SHAPE_PROXYTYPE:
  1207. ret = true;
  1208. break;
  1209. default:
  1210. ret = false;
  1211. break;
  1212. }
  1213. return ret;
  1214. }
  1215. public override void SetShapeCollisionMargin(BulletShape pShape, float pMargin)
  1216. {
  1217. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1218. shape.SetMargin(pMargin);
  1219. }
  1220. //sim.ptr, shape.ptr,prim.LocalID, prim.RawPosition, prim.RawOrientation
  1221. public override BulletBody CreateGhostFromShape(BulletWorld pWorld, BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation)
  1222. {
  1223. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1224. IndexedMatrix bodyTransform = new IndexedMatrix();
  1225. bodyTransform._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z);
  1226. bodyTransform.SetRotation(new IndexedQuaternion(pRawOrientation.X,pRawOrientation.Y,pRawOrientation.Z,pRawOrientation.W));
  1227. GhostObject gObj = new PairCachingGhostObject();
  1228. gObj.SetWorldTransform(bodyTransform);
  1229. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1230. gObj.SetCollisionShape(shape);
  1231. gObj.SetUserPointer(pLocalID);
  1232. if (specialCollisionObjects.ContainsKey(pLocalID))
  1233. specialCollisionObjects[pLocalID] = gObj;
  1234. else
  1235. specialCollisionObjects.Add(pLocalID, gObj);
  1236. // TODO: Add to Special CollisionObjects!
  1237. return new BulletBodyXNA(pLocalID, gObj);
  1238. }
  1239. public override void SetCollisionShape(BulletWorld pWorld, BulletBody pCollisionObject, BulletShape pShape)
  1240. {
  1241. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1242. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
  1243. if (pShape == null)
  1244. {
  1245. collisionObject.SetCollisionShape(new EmptyShape());
  1246. }
  1247. else
  1248. {
  1249. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1250. collisionObject.SetCollisionShape(shape);
  1251. }
  1252. }
  1253. public override BulletShape GetCollisionShape(BulletBody pCollisionObject)
  1254. {
  1255. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  1256. CollisionShape shape = collisionObject.GetCollisionShape();
  1257. return new BulletShapeXNA(shape, BSShapeTypeFromBroadPhaseNativeType(shape.GetShapeType()));
  1258. }
  1259. //(PhysicsScene.World.ptr, nativeShapeData)
  1260. public override BulletShape BuildNativeShape(BulletWorld pWorld, ShapeData pShapeData)
  1261. {
  1262. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1263. CollisionShape shape = null;
  1264. switch (pShapeData.Type)
  1265. {
  1266. case BSPhysicsShapeType.SHAPE_BOX:
  1267. shape = new BoxShape(new IndexedVector3(0.5f,0.5f,0.5f));
  1268. break;
  1269. case BSPhysicsShapeType.SHAPE_CONE:
  1270. shape = new ConeShapeZ(0.5f, 1.0f);
  1271. break;
  1272. case BSPhysicsShapeType.SHAPE_CYLINDER:
  1273. shape = new CylinderShapeZ(new IndexedVector3(0.5f, 0.5f, 0.5f));
  1274. break;
  1275. case BSPhysicsShapeType.SHAPE_SPHERE:
  1276. shape = new SphereShape(0.5f);
  1277. break;
  1278. }
  1279. if (shape != null)
  1280. {
  1281. IndexedVector3 scaling = new IndexedVector3(pShapeData.Scale.X, pShapeData.Scale.Y, pShapeData.Scale.Z);
  1282. shape.SetMargin(world.WorldSettings.Params.collisionMargin);
  1283. shape.SetLocalScaling(ref scaling);
  1284. }
  1285. return new BulletShapeXNA(shape, pShapeData.Type);
  1286. }
  1287. //PhysicsScene.World.ptr, false
  1288. public override BulletShape CreateCompoundShape(BulletWorld pWorld, bool enableDynamicAabbTree)
  1289. {
  1290. return new BulletShapeXNA(new CompoundShape(enableDynamicAabbTree), BSPhysicsShapeType.SHAPE_COMPOUND);
  1291. }
  1292. public override int GetNumberOfCompoundChildren(BulletShape pCompoundShape)
  1293. {
  1294. CompoundShape compoundshape = (pCompoundShape as BulletShapeXNA).shape as CompoundShape;
  1295. return compoundshape.GetNumChildShapes();
  1296. }
  1297. //LinksetRoot.PhysShape.ptr, newShape.ptr, displacementPos, displacementRot
  1298. public override void AddChildShapeToCompoundShape(BulletShape pCShape, BulletShape paddShape, Vector3 displacementPos, Quaternion displacementRot)
  1299. {
  1300. IndexedMatrix relativeTransform = new IndexedMatrix();
  1301. CompoundShape compoundshape = (pCShape as BulletShapeXNA).shape as CompoundShape;
  1302. CollisionShape addshape = (paddShape as BulletShapeXNA).shape;
  1303. relativeTransform._origin = new IndexedVector3(displacementPos.X, displacementPos.Y, displacementPos.Z);
  1304. relativeTransform.SetRotation(new IndexedQuaternion(displacementRot.X,displacementRot.Y,displacementRot.Z,displacementRot.W));
  1305. compoundshape.AddChildShape(ref relativeTransform, addshape);
  1306. }
  1307. public override BulletShape RemoveChildShapeFromCompoundShapeIndex(BulletShape pCShape, int pii)
  1308. {
  1309. CompoundShape compoundshape = (pCShape as BulletShapeXNA).shape as CompoundShape;
  1310. CollisionShape ret = null;
  1311. ret = compoundshape.GetChildShape(pii);
  1312. compoundshape.RemoveChildShapeByIndex(pii);
  1313. return new BulletShapeXNA(ret, BSShapeTypeFromBroadPhaseNativeType(ret.GetShapeType()));
  1314. }
  1315. public override BulletShape GetChildShapeFromCompoundShapeIndex(BulletShape cShape, int indx) {
  1316. if (cShape == null)
  1317. return null;
  1318. CompoundShape compoundShape = (cShape as BulletShapeXNA).shape as CompoundShape;
  1319. CollisionShape shape = compoundShape.GetChildShape(indx);
  1320. BulletShape retShape = new BulletShapeXNA(shape, BSShapeTypeFromBroadPhaseNativeType(shape.GetShapeType()));
  1321. return retShape;
  1322. }
  1323. public BSPhysicsShapeType BSShapeTypeFromBroadPhaseNativeType(BroadphaseNativeTypes pin)
  1324. {
  1325. BSPhysicsShapeType ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1326. switch (pin)
  1327. {
  1328. case BroadphaseNativeTypes.BOX_SHAPE_PROXYTYPE:
  1329. ret = BSPhysicsShapeType.SHAPE_BOX;
  1330. break;
  1331. case BroadphaseNativeTypes.TRIANGLE_SHAPE_PROXYTYPE:
  1332. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1333. break;
  1334. case BroadphaseNativeTypes.TETRAHEDRAL_SHAPE_PROXYTYPE:
  1335. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1336. break;
  1337. case BroadphaseNativeTypes.CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE:
  1338. ret = BSPhysicsShapeType.SHAPE_CONVEXHULL;
  1339. break;
  1340. case BroadphaseNativeTypes.CONVEX_HULL_SHAPE_PROXYTYPE:
  1341. ret = BSPhysicsShapeType.SHAPE_HULL;
  1342. break;
  1343. case BroadphaseNativeTypes.CONVEX_POINT_CLOUD_SHAPE_PROXYTYPE:
  1344. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1345. break;
  1346. case BroadphaseNativeTypes.CUSTOM_POLYHEDRAL_SHAPE_TYPE:
  1347. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1348. break;
  1349. //implicit convex shapes
  1350. case BroadphaseNativeTypes.IMPLICIT_CONVEX_SHAPES_START_HERE:
  1351. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1352. break;
  1353. case BroadphaseNativeTypes.SPHERE_SHAPE_PROXYTYPE:
  1354. ret = BSPhysicsShapeType.SHAPE_SPHERE;
  1355. break;
  1356. case BroadphaseNativeTypes.MULTI_SPHERE_SHAPE_PROXYTYPE:
  1357. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1358. break;
  1359. case BroadphaseNativeTypes.CAPSULE_SHAPE_PROXYTYPE:
  1360. ret = BSPhysicsShapeType.SHAPE_CAPSULE;
  1361. break;
  1362. case BroadphaseNativeTypes.CONE_SHAPE_PROXYTYPE:
  1363. ret = BSPhysicsShapeType.SHAPE_CONE;
  1364. break;
  1365. case BroadphaseNativeTypes.CONVEX_SHAPE_PROXYTYPE:
  1366. ret = BSPhysicsShapeType.SHAPE_CONVEXHULL;
  1367. break;
  1368. case BroadphaseNativeTypes.CYLINDER_SHAPE_PROXYTYPE:
  1369. ret = BSPhysicsShapeType.SHAPE_CYLINDER;
  1370. break;
  1371. case BroadphaseNativeTypes.UNIFORM_SCALING_SHAPE_PROXYTYPE:
  1372. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1373. break;
  1374. case BroadphaseNativeTypes.MINKOWSKI_SUM_SHAPE_PROXYTYPE:
  1375. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1376. break;
  1377. case BroadphaseNativeTypes.MINKOWSKI_DIFFERENCE_SHAPE_PROXYTYPE:
  1378. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1379. break;
  1380. case BroadphaseNativeTypes.BOX_2D_SHAPE_PROXYTYPE:
  1381. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1382. break;
  1383. case BroadphaseNativeTypes.CONVEX_2D_SHAPE_PROXYTYPE:
  1384. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1385. break;
  1386. case BroadphaseNativeTypes.CUSTOM_CONVEX_SHAPE_TYPE:
  1387. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1388. break;
  1389. //concave shape
  1390. case BroadphaseNativeTypes.CONCAVE_SHAPES_START_HERE:
  1391. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1392. break;
  1393. //keep all the convex shapetype below here, for the check IsConvexShape in broadphase proxy!
  1394. case BroadphaseNativeTypes.TRIANGLE_MESH_SHAPE_PROXYTYPE:
  1395. ret = BSPhysicsShapeType.SHAPE_MESH;
  1396. break;
  1397. case BroadphaseNativeTypes.SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE:
  1398. ret = BSPhysicsShapeType.SHAPE_MESH;
  1399. break;
  1400. ///used for demo integration FAST/Swift collision library and Bullet
  1401. case BroadphaseNativeTypes.FAST_CONCAVE_MESH_PROXYTYPE:
  1402. ret = BSPhysicsShapeType.SHAPE_MESH;
  1403. break;
  1404. //terrain
  1405. case BroadphaseNativeTypes.TERRAIN_SHAPE_PROXYTYPE:
  1406. ret = BSPhysicsShapeType.SHAPE_HEIGHTMAP;
  1407. break;
  1408. ///Used for GIMPACT Trimesh integration
  1409. case BroadphaseNativeTypes.GIMPACT_SHAPE_PROXYTYPE:
  1410. ret = BSPhysicsShapeType.SHAPE_GIMPACT;
  1411. break;
  1412. ///Multimaterial mesh
  1413. case BroadphaseNativeTypes.MULTIMATERIAL_TRIANGLE_MESH_PROXYTYPE:
  1414. ret = BSPhysicsShapeType.SHAPE_MESH;
  1415. break;
  1416. case BroadphaseNativeTypes.EMPTY_SHAPE_PROXYTYPE:
  1417. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1418. break;
  1419. case BroadphaseNativeTypes.STATIC_PLANE_PROXYTYPE:
  1420. ret = BSPhysicsShapeType.SHAPE_GROUNDPLANE;
  1421. break;
  1422. case BroadphaseNativeTypes.CUSTOM_CONCAVE_SHAPE_TYPE:
  1423. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1424. break;
  1425. case BroadphaseNativeTypes.CONCAVE_SHAPES_END_HERE:
  1426. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1427. break;
  1428. case BroadphaseNativeTypes.COMPOUND_SHAPE_PROXYTYPE:
  1429. ret = BSPhysicsShapeType.SHAPE_COMPOUND;
  1430. break;
  1431. case BroadphaseNativeTypes.SOFTBODY_SHAPE_PROXYTYPE:
  1432. ret = BSPhysicsShapeType.SHAPE_MESH;
  1433. break;
  1434. case BroadphaseNativeTypes.HFFLUID_SHAPE_PROXYTYPE:
  1435. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1436. break;
  1437. case BroadphaseNativeTypes.HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE:
  1438. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1439. break;
  1440. case BroadphaseNativeTypes.INVALID_SHAPE_PROXYTYPE:
  1441. ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  1442. break;
  1443. }
  1444. return ret;
  1445. }
  1446. public override void RemoveChildShapeFromCompoundShape(BulletShape cShape, BulletShape removeShape) { /* TODO */ }
  1447. public override void UpdateChildTransform(BulletShape pShape, int childIndex, Vector3 pos, Quaternion rot, bool shouldRecalculateLocalAabb) { /* TODO */ }
  1448. public override BulletShape CreateGroundPlaneShape(uint pLocalId, float pheight, float pcollisionMargin)
  1449. {
  1450. StaticPlaneShape m_planeshape = new StaticPlaneShape(new IndexedVector3(0,0,1),(int)pheight );
  1451. m_planeshape.SetMargin(pcollisionMargin);
  1452. m_planeshape.SetUserPointer(pLocalId);
  1453. return new BulletShapeXNA(m_planeshape, BSPhysicsShapeType.SHAPE_GROUNDPLANE);
  1454. }
  1455. public override BulletConstraint Create6DofSpringConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  1456. Vector3 pframe1, Quaternion pframe1rot, Vector3 pframe2, Quaternion pframe2rot,
  1457. bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
  1458. {
  1459. Generic6DofSpringConstraint constrain = null;
  1460. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1461. RigidBody body1 = (pBody1 as BulletBodyXNA).rigidBody;
  1462. RigidBody body2 = (pBody2 as BulletBodyXNA).rigidBody;
  1463. if (body1 != null && body2 != null)
  1464. {
  1465. IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
  1466. IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
  1467. IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
  1468. frame1._origin = frame1v;
  1469. IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
  1470. IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
  1471. IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
  1472. frame2._origin = frame1v;
  1473. constrain = new Generic6DofSpringConstraint(body1, body2, ref frame1, ref frame2, puseLinearReferenceFrameA);
  1474. world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
  1475. constrain.CalculateTransforms();
  1476. }
  1477. return new BulletConstraintXNA(constrain);
  1478. }
  1479. public override BulletConstraint CreateHingeConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  1480. Vector3 ppivotInA, Vector3 ppivotInB, Vector3 paxisInA, Vector3 paxisInB,
  1481. bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
  1482. {
  1483. HingeConstraint constrain = null;
  1484. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1485. RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
  1486. RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
  1487. if (rb1 != null && rb2 != null)
  1488. {
  1489. IndexedVector3 pivotInA = new IndexedVector3(ppivotInA.X, ppivotInA.Y, ppivotInA.Z);
  1490. IndexedVector3 pivotInB = new IndexedVector3(ppivotInB.X, ppivotInB.Y, ppivotInB.Z);
  1491. IndexedVector3 axisInA = new IndexedVector3(paxisInA.X, paxisInA.Y, paxisInA.Z);
  1492. IndexedVector3 axisInB = new IndexedVector3(paxisInB.X, paxisInB.Y, paxisInB.Z);
  1493. constrain = new HingeConstraint(rb1, rb2, ref pivotInA, ref pivotInB, ref axisInA, ref axisInB, puseLinearReferenceFrameA);
  1494. world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
  1495. }
  1496. return new BulletConstraintXNA(constrain);
  1497. }
  1498. public override BulletConstraint CreateSliderConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  1499. Vector3 pframe1, Quaternion pframe1rot,
  1500. Vector3 pframe2, Quaternion pframe2rot,
  1501. bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
  1502. {
  1503. SliderConstraint constrain = null;
  1504. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1505. RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
  1506. RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
  1507. if (rb1 != null && rb2 != null)
  1508. {
  1509. IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
  1510. IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
  1511. IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
  1512. frame1._origin = frame1v;
  1513. IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
  1514. IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
  1515. IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
  1516. frame2._origin = frame1v;
  1517. constrain = new SliderConstraint(rb1, rb2, ref frame1, ref frame2, puseLinearReferenceFrameA);
  1518. world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
  1519. }
  1520. return new BulletConstraintXNA(constrain);
  1521. }
  1522. public override BulletConstraint CreateConeTwistConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  1523. Vector3 pframe1, Quaternion pframe1rot,
  1524. Vector3 pframe2, Quaternion pframe2rot,
  1525. bool pdisableCollisionsBetweenLinkedBodies)
  1526. {
  1527. ConeTwistConstraint constrain = null;
  1528. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1529. RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
  1530. RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
  1531. if (rb1 != null && rb2 != null)
  1532. {
  1533. IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
  1534. IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
  1535. IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
  1536. frame1._origin = frame1v;
  1537. IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
  1538. IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
  1539. IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
  1540. frame2._origin = frame1v;
  1541. constrain = new ConeTwistConstraint(rb1, rb2, ref frame1, ref frame2);
  1542. world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
  1543. }
  1544. return new BulletConstraintXNA(constrain);
  1545. }
  1546. public override BulletConstraint CreateGearConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  1547. Vector3 paxisInA, Vector3 paxisInB,
  1548. float pratio, bool pdisableCollisionsBetweenLinkedBodies)
  1549. {
  1550. Generic6DofConstraint constrain = null;
  1551. /* BulletXNA does not have a gear constraint
  1552. GearConstraint constrain = null;
  1553. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1554. RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
  1555. RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
  1556. if (rb1 != null && rb2 != null)
  1557. {
  1558. IndexedVector3 axis1 = new IndexedVector3(paxisInA.X, paxisInA.Y, paxisInA.Z);
  1559. IndexedVector3 axis2 = new IndexedVector3(paxisInB.X, paxisInB.Y, paxisInB.Z);
  1560. constrain = new GearConstraint(rb1, rb2, ref axis1, ref axis2, pratio);
  1561. world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
  1562. }
  1563. */
  1564. return new BulletConstraintXNA(constrain);
  1565. }
  1566. public override BulletConstraint CreatePoint2PointConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
  1567. Vector3 ppivotInA, Vector3 ppivotInB,
  1568. bool pdisableCollisionsBetweenLinkedBodies)
  1569. {
  1570. Point2PointConstraint constrain = null;
  1571. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1572. RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
  1573. RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
  1574. if (rb1 != null && rb2 != null)
  1575. {
  1576. IndexedVector3 pivotInA = new IndexedVector3(ppivotInA.X, ppivotInA.Y, ppivotInA.Z);
  1577. IndexedVector3 pivotInB = new IndexedVector3(ppivotInB.X, ppivotInB.Y, ppivotInB.Z);
  1578. constrain = new Point2PointConstraint(rb1, rb2, ref pivotInA, ref pivotInB);
  1579. world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
  1580. }
  1581. return new BulletConstraintXNA(constrain);
  1582. }
  1583. public override BulletShape CreateHullShape(BulletWorld pWorld, int pHullCount, float[] pConvHulls)
  1584. {
  1585. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1586. CompoundShape compoundshape = new CompoundShape(false);
  1587. compoundshape.SetMargin(world.WorldSettings.Params.collisionMargin);
  1588. int ii = 1;
  1589. for (int i = 0; i < pHullCount; i++)
  1590. {
  1591. int vertexCount = (int) pConvHulls[ii];
  1592. IndexedVector3 centroid = new IndexedVector3(pConvHulls[ii + 1], pConvHulls[ii + 2], pConvHulls[ii + 3]);
  1593. IndexedMatrix childTrans = IndexedMatrix.Identity;
  1594. childTrans._origin = centroid;
  1595. List<IndexedVector3> virts = new List<IndexedVector3>();
  1596. int ender = ((ii + 4) + (vertexCount*3));
  1597. for (int iii = ii + 4; iii < ender; iii+=3)
  1598. {
  1599. virts.Add(new IndexedVector3(pConvHulls[iii], pConvHulls[iii + 1], pConvHulls[iii +2]));
  1600. }
  1601. ConvexHullShape convexShape = new ConvexHullShape(virts, vertexCount);
  1602. convexShape.SetMargin(world.WorldSettings.Params.collisionMargin);
  1603. compoundshape.AddChildShape(ref childTrans, convexShape);
  1604. ii += (vertexCount*3 + 4);
  1605. }
  1606. return new BulletShapeXNA(compoundshape, BSPhysicsShapeType.SHAPE_HULL);
  1607. }
  1608. public override BulletShape BuildHullShapeFromMesh(BulletWorld world, BulletShape meshShape, HACDParams parms)
  1609. {
  1610. /* TODO */ return null;
  1611. }
  1612. public override BulletShape BuildConvexHullShapeFromMesh(BulletWorld world, BulletShape meshShape)
  1613. {
  1614. /* TODO */ return null;
  1615. }
  1616. public override BulletShape CreateConvexHullShape(BulletWorld pWorld, int pIndicesCount, int[] indices, int pVerticesCount, float[] verticesAsFloats)
  1617. {
  1618. /* TODO */ return null;
  1619. }
  1620. public override BulletShape CreateMeshShape(BulletWorld pWorld, int pIndicesCount, int[] indices, int pVerticesCount, float[] verticesAsFloats)
  1621. {
  1622. //DumpRaw(indices,verticesAsFloats,pIndicesCount,pVerticesCount);
  1623. for (int iter = 0; iter < pVerticesCount; iter++)
  1624. {
  1625. if (verticesAsFloats[iter] > 0 && verticesAsFloats[iter] < 0.0001) verticesAsFloats[iter] = 0;
  1626. if (verticesAsFloats[iter] < 0 && verticesAsFloats[iter] > -0.0001) verticesAsFloats[iter] = 0;
  1627. }
  1628. ObjectArray<int> indicesarr = new ObjectArray<int>(indices);
  1629. ObjectArray<float> vertices = new ObjectArray<float>(verticesAsFloats);
  1630. DumpRaw(indicesarr,vertices,pIndicesCount,pVerticesCount);
  1631. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1632. IndexedMesh mesh = new IndexedMesh();
  1633. mesh.m_indexType = PHY_ScalarType.PHY_INTEGER;
  1634. mesh.m_numTriangles = pIndicesCount/3;
  1635. mesh.m_numVertices = pVerticesCount;
  1636. mesh.m_triangleIndexBase = indicesarr;
  1637. mesh.m_vertexBase = vertices;
  1638. mesh.m_vertexStride = 3;
  1639. mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT;
  1640. mesh.m_triangleIndexStride = 3;
  1641. TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray();
  1642. tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER);
  1643. BvhTriangleMeshShape meshShape = new BvhTriangleMeshShape(tribuilder, true,true);
  1644. meshShape.SetMargin(world.WorldSettings.Params.collisionMargin);
  1645. // world.UpdateSingleAabb(meshShape);
  1646. return new BulletShapeXNA(meshShape, BSPhysicsShapeType.SHAPE_MESH);
  1647. }
  1648. public override BulletShape CreateGImpactShape(BulletWorld pWorld, int pIndicesCount, int[] indices, int pVerticesCount, float[] verticesAsFloats)
  1649. {
  1650. // TODO:
  1651. return null;
  1652. }
  1653. public static void DumpRaw(ObjectArray<int>indices, ObjectArray<float> vertices, int pIndicesCount,int pVerticesCount )
  1654. {
  1655. String fileName = "objTest3.raw";
  1656. String completePath = System.IO.Path.Combine(Util.configDir(), fileName);
  1657. StreamWriter sw = new StreamWriter(completePath);
  1658. IndexedMesh mesh = new IndexedMesh();
  1659. mesh.m_indexType = PHY_ScalarType.PHY_INTEGER;
  1660. mesh.m_numTriangles = pIndicesCount / 3;
  1661. mesh.m_numVertices = pVerticesCount;
  1662. mesh.m_triangleIndexBase = indices;
  1663. mesh.m_vertexBase = vertices;
  1664. mesh.m_vertexStride = 3;
  1665. mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT;
  1666. mesh.m_triangleIndexStride = 3;
  1667. TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray();
  1668. tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER);
  1669. for (int i = 0; i < pVerticesCount; i++)
  1670. {
  1671. string s = vertices[indices[i * 3]].ToString("0.0000");
  1672. s += " " + vertices[indices[i * 3 + 1]].ToString("0.0000");
  1673. s += " " + vertices[indices[i * 3 + 2]].ToString("0.0000");
  1674. sw.Write(s + "\n");
  1675. }
  1676. sw.Close();
  1677. }
  1678. public static void DumpRaw(int[] indices, float[] vertices, int pIndicesCount, int pVerticesCount)
  1679. {
  1680. String fileName = "objTest6.raw";
  1681. String completePath = System.IO.Path.Combine(Util.configDir(), fileName);
  1682. StreamWriter sw = new StreamWriter(completePath);
  1683. IndexedMesh mesh = new IndexedMesh();
  1684. mesh.m_indexType = PHY_ScalarType.PHY_INTEGER;
  1685. mesh.m_numTriangles = pIndicesCount / 3;
  1686. mesh.m_numVertices = pVerticesCount;
  1687. mesh.m_triangleIndexBase = indices;
  1688. mesh.m_vertexBase = vertices;
  1689. mesh.m_vertexStride = 3;
  1690. mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT;
  1691. mesh.m_triangleIndexStride = 3;
  1692. TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray();
  1693. tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER);
  1694. sw.WriteLine("Indices");
  1695. sw.WriteLine(string.Format("int[] indices = new int[{0}];",pIndicesCount));
  1696. for (int iter = 0; iter < indices.Length; iter++)
  1697. {
  1698. sw.WriteLine(string.Format("indices[{0}]={1};",iter,indices[iter]));
  1699. }
  1700. sw.WriteLine("VerticesFloats");
  1701. sw.WriteLine(string.Format("float[] vertices = new float[{0}];", pVerticesCount));
  1702. for (int iter = 0; iter < vertices.Length; iter++)
  1703. {
  1704. sw.WriteLine(string.Format("Vertices[{0}]={1};", iter, vertices[iter].ToString("0.0000")));
  1705. }
  1706. // for (int i = 0; i < pVerticesCount; i++)
  1707. // {
  1708. //
  1709. // string s = vertices[indices[i * 3]].ToString("0.0000");
  1710. // s += " " + vertices[indices[i * 3 + 1]].ToString("0.0000");
  1711. // s += " " + vertices[indices[i * 3 + 2]].ToString("0.0000");
  1712. //
  1713. // sw.Write(s + "\n");
  1714. //}
  1715. sw.Close();
  1716. }
  1717. public override BulletShape CreateTerrainShape(uint id, Vector3 size, float minHeight, float maxHeight, float[] heightMap,
  1718. float scaleFactor, float collisionMargin)
  1719. {
  1720. const int upAxis = 2;
  1721. HeightfieldTerrainShape terrainShape = new HeightfieldTerrainShape((int)size.X, (int)size.Y,
  1722. heightMap, scaleFactor,
  1723. minHeight, maxHeight, upAxis,
  1724. false);
  1725. terrainShape.SetMargin(collisionMargin);
  1726. terrainShape.SetUseDiamondSubdivision(true);
  1727. terrainShape.SetUserPointer(id);
  1728. return new BulletShapeXNA(terrainShape, BSPhysicsShapeType.SHAPE_TERRAIN);
  1729. }
  1730. public override bool TranslationalLimitMotor(BulletConstraint pConstraint, float ponOff, float targetVelocity, float maxMotorForce)
  1731. {
  1732. TypedConstraint tconstrain = (pConstraint as BulletConstraintXNA).constrain;
  1733. bool onOff = ponOff != 0;
  1734. bool ret = false;
  1735. switch (tconstrain.GetConstraintType())
  1736. {
  1737. case TypedConstraintType.D6_CONSTRAINT_TYPE:
  1738. Generic6DofConstraint constrain = tconstrain as Generic6DofConstraint;
  1739. constrain.GetTranslationalLimitMotor().m_enableMotor[0] = onOff;
  1740. constrain.GetTranslationalLimitMotor().m_targetVelocity[0] = targetVelocity;
  1741. constrain.GetTranslationalLimitMotor().m_maxMotorForce[0] = maxMotorForce;
  1742. ret = true;
  1743. break;
  1744. }
  1745. return ret;
  1746. }
  1747. public override int PhysicsStep(BulletWorld world, float timeStep, int maxSubSteps, float fixedTimeStep,
  1748. out int updatedEntityCount, out int collidersCount)
  1749. {
  1750. /* TODO */
  1751. updatedEntityCount = 0;
  1752. collidersCount = 0;
  1753. int ret = PhysicsStep2(world,timeStep,maxSubSteps,fixedTimeStep,out updatedEntityCount,out world.physicsScene.m_updateArray, out collidersCount, out world.physicsScene.m_collisionArray);
  1754. return ret;
  1755. }
  1756. private int PhysicsStep2(BulletWorld pWorld, float timeStep, int m_maxSubSteps, float m_fixedTimeStep,
  1757. out int updatedEntityCount, out EntityProperties[] updatedEntities,
  1758. out int collidersCount, out CollisionDesc[] colliders)
  1759. {
  1760. int epic = PhysicsStepint(pWorld, timeStep, m_maxSubSteps, m_fixedTimeStep, out updatedEntityCount, out updatedEntities,
  1761. out collidersCount, out colliders, m_maxCollisions, m_maxUpdatesPerFrame);
  1762. return epic;
  1763. }
  1764. private int PhysicsStepint(BulletWorld pWorld,float timeStep, int m_maxSubSteps, float m_fixedTimeStep, out int updatedEntityCount,
  1765. out EntityProperties[] updatedEntities, out int collidersCount, out CollisionDesc[] colliders, int maxCollisions, int maxUpdates)
  1766. {
  1767. int numSimSteps = 0;
  1768. Array.Clear(UpdatedObjects, 0, UpdatedObjects.Length);
  1769. Array.Clear(UpdatedCollisions, 0, UpdatedCollisions.Length);
  1770. LastEntityProperty=0;
  1771. LastCollisionDesc=0;
  1772. updatedEntityCount = 0;
  1773. collidersCount = 0;
  1774. if (pWorld is BulletWorldXNA)
  1775. {
  1776. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1777. world.LastCollisionDesc = 0;
  1778. world.LastEntityProperty = 0;
  1779. numSimSteps = world.StepSimulation(timeStep, m_maxSubSteps, m_fixedTimeStep);
  1780. int updates = 0;
  1781. PersistentManifold contactManifold;
  1782. CollisionObject objA;
  1783. CollisionObject objB;
  1784. ManifoldPoint manifoldPoint;
  1785. PairCachingGhostObject pairCachingGhostObject;
  1786. m_collisionsThisFrame = 0;
  1787. int numManifolds = world.GetDispatcher().GetNumManifolds();
  1788. for (int j = 0; j < numManifolds; j++)
  1789. {
  1790. contactManifold = world.GetDispatcher().GetManifoldByIndexInternal(j);
  1791. int numContacts = contactManifold.GetNumContacts();
  1792. if (numContacts == 0)
  1793. continue;
  1794. objA = contactManifold.GetBody0() as CollisionObject;
  1795. objB = contactManifold.GetBody1() as CollisionObject;
  1796. manifoldPoint = contactManifold.GetContactPoint(0);
  1797. //IndexedVector3 contactPoint = manifoldPoint.GetPositionWorldOnB();
  1798. // IndexedVector3 contactNormal = -manifoldPoint.m_normalWorldOnB; // make relative to A
  1799. RecordCollision(this, objA, objB, manifoldPoint.GetPositionWorldOnB(), -manifoldPoint.m_normalWorldOnB, manifoldPoint.GetDistance());
  1800. m_collisionsThisFrame ++;
  1801. if (m_collisionsThisFrame >= 9999999)
  1802. break;
  1803. }
  1804. foreach (GhostObject ghostObject in specialCollisionObjects.Values)
  1805. {
  1806. pairCachingGhostObject = ghostObject as PairCachingGhostObject;
  1807. if (pairCachingGhostObject != null)
  1808. {
  1809. RecordGhostCollisions(pairCachingGhostObject);
  1810. }
  1811. }
  1812. updatedEntityCount = LastEntityProperty;
  1813. updatedEntities = UpdatedObjects;
  1814. collidersCount = LastCollisionDesc;
  1815. colliders = UpdatedCollisions;
  1816. }
  1817. else
  1818. {
  1819. //if (updatedEntities is null)
  1820. //updatedEntities = new List<BulletXNA.EntityProperties>();
  1821. //updatedEntityCount = 0;
  1822. //collidersCount = 0;
  1823. updatedEntities = new EntityProperties[0];
  1824. colliders = new CollisionDesc[0];
  1825. }
  1826. return numSimSteps;
  1827. }
  1828. public void RecordGhostCollisions(PairCachingGhostObject obj)
  1829. {
  1830. IOverlappingPairCache cache = obj.GetOverlappingPairCache();
  1831. ObjectArray<BroadphasePair> pairs = cache.GetOverlappingPairArray();
  1832. DiscreteDynamicsWorld world = (PhysicsScene.World as BulletWorldXNA).world;
  1833. PersistentManifoldArray manifoldArray = new PersistentManifoldArray();
  1834. BroadphasePair collisionPair;
  1835. PersistentManifold contactManifold;
  1836. CollisionObject objA;
  1837. CollisionObject objB;
  1838. ManifoldPoint pt;
  1839. int numPairs = pairs.Count;
  1840. for (int i = 0; i < numPairs; i++)
  1841. {
  1842. manifoldArray.Clear();
  1843. if (LastCollisionDesc < UpdatedCollisions.Length)
  1844. break;
  1845. collisionPair = world.GetPairCache().FindPair(pairs[i].m_pProxy0, pairs[i].m_pProxy1);
  1846. if (collisionPair == null)
  1847. continue;
  1848. collisionPair.m_algorithm.GetAllContactManifolds(manifoldArray);
  1849. for (int j = 0; j < manifoldArray.Count; j++)
  1850. {
  1851. contactManifold = manifoldArray[j];
  1852. int numContacts = contactManifold.GetNumContacts();
  1853. objA = contactManifold.GetBody0() as CollisionObject;
  1854. objB = contactManifold.GetBody1() as CollisionObject;
  1855. for (int p = 0; p < numContacts; p++)
  1856. {
  1857. pt = contactManifold.GetContactPoint(p);
  1858. if (pt.GetDistance() < 0.0f)
  1859. {
  1860. RecordCollision(this, objA, objB, pt.GetPositionWorldOnA(), -pt.m_normalWorldOnB,pt.GetDistance());
  1861. break;
  1862. }
  1863. }
  1864. }
  1865. }
  1866. }
  1867. private static void RecordCollision(BSAPIXNA world, CollisionObject objA, CollisionObject objB, IndexedVector3 contact, IndexedVector3 norm, float penetration)
  1868. {
  1869. IndexedVector3 contactNormal = norm;
  1870. if ((objA.GetCollisionFlags() & BulletXNA.BulletCollision.CollisionFlags.BS_WANTS_COLLISIONS) == 0 &&
  1871. (objB.GetCollisionFlags() & BulletXNA.BulletCollision.CollisionFlags.BS_WANTS_COLLISIONS) == 0)
  1872. {
  1873. return;
  1874. }
  1875. uint idA = (uint)objA.GetUserPointer();
  1876. uint idB = (uint)objB.GetUserPointer();
  1877. if (idA > idB)
  1878. {
  1879. uint temp = idA;
  1880. idA = idB;
  1881. idB = temp;
  1882. contactNormal = -contactNormal;
  1883. }
  1884. //ulong collisionID = ((ulong) idA << 32) | idB;
  1885. CollisionDesc cDesc = new CollisionDesc()
  1886. {
  1887. aID = idA,
  1888. bID = idB,
  1889. point = new Vector3(contact.X,contact.Y,contact.Z),
  1890. normal = new Vector3(contactNormal.X,contactNormal.Y,contactNormal.Z),
  1891. penetration = penetration
  1892. };
  1893. if (world.LastCollisionDesc < world.UpdatedCollisions.Length)
  1894. world.UpdatedCollisions[world.LastCollisionDesc++] = (cDesc);
  1895. m_collisionsThisFrame++;
  1896. }
  1897. private static EntityProperties GetDebugProperties(BulletWorld pWorld, BulletBody pCollisionObject)
  1898. {
  1899. EntityProperties ent = new EntityProperties();
  1900. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1901. CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
  1902. IndexedMatrix transform = collisionObject.GetWorldTransform();
  1903. IndexedVector3 LinearVelocity = collisionObject.GetInterpolationLinearVelocity();
  1904. IndexedVector3 AngularVelocity = collisionObject.GetInterpolationAngularVelocity();
  1905. IndexedQuaternion rotation = transform.GetRotation();
  1906. ent.Acceleration = Vector3.Zero;
  1907. ent.ID = (uint)collisionObject.GetUserPointer();
  1908. ent.Position = new Vector3(transform._origin.X,transform._origin.Y,transform._origin.Z);
  1909. ent.Rotation = new Quaternion(rotation.X,rotation.Y,rotation.Z,rotation.W);
  1910. ent.Velocity = new Vector3(LinearVelocity.X, LinearVelocity.Y, LinearVelocity.Z);
  1911. ent.RotationalVelocity = new Vector3(AngularVelocity.X, AngularVelocity.Y, AngularVelocity.Z);
  1912. return ent;
  1913. }
  1914. public override bool UpdateParameter(BulletWorld world, uint localID, String parm, float value) { /* TODO */
  1915. return false; }
  1916. public override Vector3 GetLocalScaling(BulletShape pShape)
  1917. {
  1918. CollisionShape shape = (pShape as BulletShapeXNA).shape;
  1919. IndexedVector3 scale = shape.GetLocalScaling();
  1920. return new Vector3(scale.X,scale.Y,scale.Z);
  1921. }
  1922. public bool RayCastGround(BulletWorld pWorld, Vector3 _RayOrigin, float pRayHeight, BulletBody NotMe)
  1923. {
  1924. DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
  1925. if (world != null)
  1926. {
  1927. if (NotMe is BulletBodyXNA && NotMe.HasPhysicalBody)
  1928. {
  1929. CollisionObject AvoidBody = (NotMe as BulletBodyXNA).body;
  1930. IndexedVector3 rOrigin = new IndexedVector3(_RayOrigin.X, _RayOrigin.Y, _RayOrigin.Z);
  1931. IndexedVector3 rEnd = new IndexedVector3(_RayOrigin.X, _RayOrigin.Y, _RayOrigin.Z - pRayHeight);
  1932. using (
  1933. ClosestNotMeRayResultCallback rayCallback =
  1934. new ClosestNotMeRayResultCallback(rOrigin, rEnd, AvoidBody)
  1935. )
  1936. {
  1937. world.RayTest(ref rOrigin, ref rEnd, rayCallback);
  1938. if (rayCallback.HasHit())
  1939. {
  1940. IndexedVector3 hitLocation = rayCallback.m_hitPointWorld;
  1941. }
  1942. return rayCallback.HasHit();
  1943. }
  1944. }
  1945. }
  1946. return false;
  1947. }
  1948. }
  1949. public class SimMotionState : DefaultMotionState
  1950. {
  1951. public RigidBody Rigidbody;
  1952. public Vector3 ZeroVect;
  1953. private IndexedMatrix m_xform;
  1954. private EntityProperties m_properties;
  1955. private EntityProperties m_lastProperties;
  1956. private BSAPIXNA m_world;
  1957. const float POSITION_TOLERANCE = 0.05f;
  1958. const float VELOCITY_TOLERANCE = 0.001f;
  1959. const float ROTATION_TOLERANCE = 0.01f;
  1960. const float ANGULARVELOCITY_TOLERANCE = 0.01f;
  1961. public SimMotionState(BSAPIXNA pWorld, uint id, IndexedMatrix starTransform, object frameUpdates)
  1962. {
  1963. IndexedQuaternion OrientationQuaterion = starTransform.GetRotation();
  1964. m_properties = new EntityProperties()
  1965. {
  1966. ID = id,
  1967. Position = new Vector3(starTransform._origin.X, starTransform._origin.Y,starTransform._origin.Z),
  1968. Rotation = new Quaternion(OrientationQuaterion.X,OrientationQuaterion.Y,OrientationQuaterion.Z,OrientationQuaterion.W)
  1969. };
  1970. m_lastProperties = new EntityProperties()
  1971. {
  1972. ID = id,
  1973. Position = new Vector3(starTransform._origin.X, starTransform._origin.Y, starTransform._origin.Z),
  1974. Rotation = new Quaternion(OrientationQuaterion.X, OrientationQuaterion.Y, OrientationQuaterion.Z, OrientationQuaterion.W)
  1975. };
  1976. m_world = pWorld;
  1977. m_xform = starTransform;
  1978. }
  1979. public override void GetWorldTransform(out IndexedMatrix worldTrans)
  1980. {
  1981. worldTrans = m_xform;
  1982. }
  1983. public override void SetWorldTransform(IndexedMatrix worldTrans)
  1984. {
  1985. SetWorldTransform(ref worldTrans);
  1986. }
  1987. public override void SetWorldTransform(ref IndexedMatrix worldTrans)
  1988. {
  1989. SetWorldTransform(ref worldTrans, false);
  1990. }
  1991. public void SetWorldTransform(ref IndexedMatrix worldTrans, bool force)
  1992. {
  1993. m_xform = worldTrans;
  1994. // Put the new transform into m_properties
  1995. IndexedQuaternion OrientationQuaternion = m_xform.GetRotation();
  1996. IndexedVector3 LinearVelocityVector = Rigidbody.GetLinearVelocity();
  1997. IndexedVector3 AngularVelocityVector = Rigidbody.GetAngularVelocity();
  1998. m_properties.Position = new Vector3(m_xform._origin.X, m_xform._origin.Y, m_xform._origin.Z);
  1999. m_properties.Rotation = new Quaternion(OrientationQuaternion.X, OrientationQuaternion.Y,
  2000. OrientationQuaternion.Z, OrientationQuaternion.W);
  2001. // A problem with stock Bullet is that we don't get an event when an object is deactivated.
  2002. // This means that the last non-zero values for linear and angular velocity
  2003. // are left in the viewer who does dead reconning and the objects look like
  2004. // they float off.
  2005. // BulletSim ships with a patch to Bullet which creates such an event.
  2006. m_properties.Velocity = new Vector3(LinearVelocityVector.X, LinearVelocityVector.Y, LinearVelocityVector.Z);
  2007. m_properties.RotationalVelocity = new Vector3(AngularVelocityVector.X, AngularVelocityVector.Y, AngularVelocityVector.Z);
  2008. if (force
  2009. || !AlmostEqual(ref m_lastProperties.Position, ref m_properties.Position, POSITION_TOLERANCE)
  2010. || !AlmostEqual(ref m_properties.Rotation, ref m_lastProperties.Rotation, ROTATION_TOLERANCE)
  2011. // If the Velocity and AngularVelocity are zero, most likely the object has
  2012. // been deactivated. If they both are zero and they have become zero recently,
  2013. // make sure a property update is sent so the zeros make it to the viewer.
  2014. || ((m_properties.Velocity == ZeroVect && m_properties.RotationalVelocity == ZeroVect)
  2015. &&
  2016. (m_properties.Velocity != m_lastProperties.Velocity ||
  2017. m_properties.RotationalVelocity != m_lastProperties.RotationalVelocity))
  2018. // If Velocity and AngularVelocity are non-zero but have changed, send an update.
  2019. || !AlmostEqual(ref m_properties.Velocity, ref m_lastProperties.Velocity, VELOCITY_TOLERANCE)
  2020. ||
  2021. !AlmostEqual(ref m_properties.RotationalVelocity, ref m_lastProperties.RotationalVelocity,
  2022. ANGULARVELOCITY_TOLERANCE)
  2023. )
  2024. {
  2025. // Add this update to the list of updates for this frame.
  2026. m_lastProperties = m_properties;
  2027. if (m_world.LastEntityProperty < m_world.UpdatedObjects.Length)
  2028. m_world.UpdatedObjects[m_world.LastEntityProperty++]=(m_properties);
  2029. //(*m_updatesThisFrame)[m_properties.ID] = &m_properties;
  2030. }
  2031. }
  2032. public override void SetRigidBody(RigidBody body)
  2033. {
  2034. Rigidbody = body;
  2035. }
  2036. internal static bool AlmostEqual(ref Vector3 v1, ref Vector3 v2, float nEpsilon)
  2037. {
  2038. return
  2039. (((v1.X - nEpsilon) < v2.X) && (v2.X < (v1.X + nEpsilon))) &&
  2040. (((v1.Y - nEpsilon) < v2.Y) && (v2.Y < (v1.Y + nEpsilon))) &&
  2041. (((v1.Z - nEpsilon) < v2.Z) && (v2.Z < (v1.Z + nEpsilon)));
  2042. }
  2043. internal static bool AlmostEqual(ref Quaternion v1, ref Quaternion v2, float nEpsilon)
  2044. {
  2045. return
  2046. (((v1.X - nEpsilon) < v2.X) && (v2.X < (v1.X + nEpsilon))) &&
  2047. (((v1.Y - nEpsilon) < v2.Y) && (v2.Y < (v1.Y + nEpsilon))) &&
  2048. (((v1.Z - nEpsilon) < v2.Z) && (v2.Z < (v1.Z + nEpsilon))) &&
  2049. (((v1.W - nEpsilon) < v2.W) && (v2.W < (v1.W + nEpsilon)));
  2050. }
  2051. }
  2052. }