1
0

PhysXPlugin.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c) OpenSim project, http://sim.opensecondlife.org/
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the <organization> nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. /*
  28. * Copyright (c) OpenSim project, http://sim.opensecondlife.org/
  29. *
  30. * Redistribution and use in source and binary forms, with or without
  31. * modification, are permitted provided that the following conditions are met:
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. * * Redistributions in binary form must reproduce the above copyright
  35. * notice, this list of conditions and the following disclaimer in the
  36. * documentation and/or other materials provided with the distribution.
  37. * * Neither the name of the <organization> nor the
  38. * names of its contributors may be used to endorse or promote products
  39. * derived from this software without specific prior written permission.
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
  42. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  43. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  45. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  46. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  48. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  49. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  50. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. *
  52. */
  53. using System;
  54. using System.Collections.Generic;
  55. using OpenSim.Physics.Manager;
  56. using PhysXWrapper;
  57. namespace OpenSim.Physics.PhysXPlugin
  58. {
  59. /// <summary>
  60. /// Will be the PhysX plugin but for now will be a very basic physics engine
  61. /// </summary>
  62. public class PhysXPlugin : IPhysicsPlugin
  63. {
  64. private PhysXScene _mScene;
  65. public PhysXPlugin()
  66. {
  67. }
  68. public bool Init()
  69. {
  70. return true;
  71. }
  72. public PhysicsScene GetScene()
  73. {
  74. if(_mScene == null)
  75. {
  76. _mScene = new PhysXScene();
  77. }
  78. return(_mScene);
  79. }
  80. public string GetName()
  81. {
  82. return("RealPhysX");
  83. }
  84. public void Dispose()
  85. {
  86. }
  87. }
  88. public class PhysXScene :PhysicsScene
  89. {
  90. private List<PhysXCharacter> _characters = new List<PhysXCharacter>();
  91. private List<PhysXPrim> _prims = new List<PhysXPrim>();
  92. private float[] _heightMap = null;
  93. private NxPhysicsSDK mySdk;
  94. private NxScene scene;
  95. public PhysXScene()
  96. {
  97. mySdk = NxPhysicsSDK.CreateSDK();
  98. Console.WriteLine("Sdk created - now creating scene");
  99. scene = mySdk.CreateScene();
  100. }
  101. public override PhysicsActor AddAvatar(PhysicsVector position)
  102. {
  103. Vec3 pos = new Vec3();
  104. pos.X = position.X;
  105. pos.Y = position.Y;
  106. pos.Z = position.Z;
  107. PhysXCharacter act = new PhysXCharacter( scene.AddCharacter(pos));
  108. act.Position = position;
  109. _characters.Add(act);
  110. return act;
  111. }
  112. public override PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size)
  113. {
  114. Vec3 pos = new Vec3();
  115. pos.X = position.X;
  116. pos.Y = position.Y;
  117. pos.Z = position.Z;
  118. Vec3 siz = new Vec3();
  119. siz.X = size.X;
  120. siz.Y = size.Y;
  121. siz.Z = size.Z;
  122. PhysXPrim act = new PhysXPrim( scene.AddNewBox(pos, siz));
  123. _prims.Add(act);
  124. return act;
  125. }
  126. public override void Simulate(float timeStep)
  127. {
  128. try
  129. {
  130. foreach (PhysXCharacter actor in _characters)
  131. {
  132. actor.Move(timeStep);
  133. }
  134. scene.Simulate(timeStep);
  135. scene.FetchResults();
  136. scene.UpdateControllers();
  137. foreach (PhysXCharacter actor in _characters)
  138. {
  139. actor.UpdatePosition();
  140. }
  141. }
  142. catch (Exception e)
  143. {
  144. Console.WriteLine(e.Message);
  145. }
  146. }
  147. public override void GetResults()
  148. {
  149. }
  150. public override bool IsThreaded
  151. {
  152. get
  153. {
  154. return(false); // for now we won't be multithreaded
  155. }
  156. }
  157. public override void SetTerrain(float[] heightMap)
  158. {
  159. if (this._heightMap != null)
  160. {
  161. Console.WriteLine("PhysX - deleting old terrain");
  162. this.scene.DeleteTerrain();
  163. }
  164. this._heightMap = heightMap;
  165. this.scene.AddTerrain(heightMap);
  166. }
  167. public override void DeleteTerrain()
  168. {
  169. this.scene.DeleteTerrain();
  170. }
  171. }
  172. public class PhysXCharacter : PhysicsActor
  173. {
  174. private PhysicsVector _position;
  175. private PhysicsVector _velocity;
  176. private PhysicsVector _acceleration;
  177. private NxCharacter _character;
  178. private bool flying;
  179. private float gravityAccel;
  180. public PhysXCharacter(NxCharacter character)
  181. {
  182. _velocity = new PhysicsVector();
  183. _position = new PhysicsVector();
  184. _acceleration = new PhysicsVector();
  185. _character = character;
  186. }
  187. public override bool Flying
  188. {
  189. get
  190. {
  191. return flying;
  192. }
  193. set
  194. {
  195. flying = value;
  196. }
  197. }
  198. public override PhysicsVector Position
  199. {
  200. get
  201. {
  202. return _position;
  203. }
  204. set
  205. {
  206. _position = value;
  207. Vec3 ps = new Vec3();
  208. ps.X = value.X;
  209. ps.Y = value.Y;
  210. ps.Z = value.Z;
  211. this._character.Position = ps;
  212. }
  213. }
  214. public override PhysicsVector Velocity
  215. {
  216. get
  217. {
  218. return _velocity;
  219. }
  220. set
  221. {
  222. _velocity = value;
  223. }
  224. }
  225. public override bool Kinematic
  226. {
  227. get
  228. {
  229. return false;
  230. }
  231. set
  232. {
  233. }
  234. }
  235. public override Axiom.MathLib.Quaternion Orientation
  236. {
  237. get
  238. {
  239. return Axiom.MathLib.Quaternion.Identity;
  240. }
  241. set
  242. {
  243. }
  244. }
  245. public override PhysicsVector Acceleration
  246. {
  247. get
  248. {
  249. return _acceleration;
  250. }
  251. }
  252. public void SetAcceleration (PhysicsVector accel)
  253. {
  254. this._acceleration = accel;
  255. }
  256. public override void AddForce(PhysicsVector force)
  257. {
  258. }
  259. public override void SetMomentum(PhysicsVector momentum)
  260. {
  261. }
  262. public void Move(float timeStep)
  263. {
  264. Vec3 vec = new Vec3();
  265. vec.X = this._velocity.X * timeStep;
  266. vec.Y = this._velocity.Y * timeStep;
  267. if(flying)
  268. {
  269. vec.Z = ( this._velocity.Z) * timeStep;
  270. }
  271. else
  272. {
  273. gravityAccel+= -9.8f;
  274. vec.Z = (gravityAccel + this._velocity.Z) * timeStep;
  275. }
  276. int res = this._character.Move(vec);
  277. if(res == 1)
  278. {
  279. gravityAccel = 0;
  280. }
  281. }
  282. public void UpdatePosition()
  283. {
  284. Vec3 vec = this._character.Position;
  285. this._position.X = vec.X;
  286. this._position.Y = vec.Y;
  287. this._position.Z = vec.Z;
  288. }
  289. }
  290. public class PhysXPrim : PhysicsActor
  291. {
  292. private PhysicsVector _position;
  293. private PhysicsVector _velocity;
  294. private PhysicsVector _acceleration;
  295. private NxActor _prim;
  296. public PhysXPrim(NxActor prim)
  297. {
  298. _velocity = new PhysicsVector();
  299. _position = new PhysicsVector();
  300. _acceleration = new PhysicsVector();
  301. _prim = prim;
  302. }
  303. public override bool Flying
  304. {
  305. get
  306. {
  307. return false; //no flying prims for you
  308. }
  309. set
  310. {
  311. }
  312. }
  313. public override PhysicsVector Position
  314. {
  315. get
  316. {
  317. PhysicsVector pos = new PhysicsVector();
  318. Vec3 vec = this._prim.Position;
  319. pos.X = vec.X;
  320. pos.Y = vec.Y;
  321. pos.Z = vec.Z;
  322. return pos;
  323. }
  324. set
  325. {
  326. PhysicsVector vec = value;
  327. Vec3 pos = new Vec3();
  328. pos.X = vec.X;
  329. pos.Y = vec.Y;
  330. pos.Z = vec.Z;
  331. this._prim.Position = pos;
  332. }
  333. }
  334. public override PhysicsVector Velocity
  335. {
  336. get
  337. {
  338. return _velocity;
  339. }
  340. set
  341. {
  342. _velocity = value;
  343. }
  344. }
  345. public override bool Kinematic
  346. {
  347. get
  348. {
  349. return this._prim.Kinematic;
  350. }
  351. set
  352. {
  353. this._prim.Kinematic = value;
  354. }
  355. }
  356. public override Axiom.MathLib.Quaternion Orientation
  357. {
  358. get
  359. {
  360. Axiom.MathLib.Quaternion res = new Axiom.MathLib.Quaternion();
  361. PhysXWrapper.Quaternion quat = this._prim.GetOrientation();
  362. res.w = quat.W;
  363. res.x = quat.X;
  364. res.y = quat.Y;
  365. res.z = quat.Z;
  366. return res;
  367. }
  368. set
  369. {
  370. }
  371. }
  372. public override PhysicsVector Acceleration
  373. {
  374. get
  375. {
  376. return _acceleration;
  377. }
  378. }
  379. public void SetAcceleration (PhysicsVector accel)
  380. {
  381. this._acceleration = accel;
  382. }
  383. public override void AddForce(PhysicsVector force)
  384. {
  385. }
  386. public override void SetMomentum(PhysicsVector momentum)
  387. {
  388. }
  389. }
  390. }