BSAPIXNA.cs 121 KB

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