PhysXPlugin.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*/*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim 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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Physics.Manager;
  32. using PhysXWrapper;
  33. using Quaternion=Axiom.Math.Quaternion;
  34. namespace OpenSim.Region.Physics.PhysXPlugin
  35. {
  36. /// <summary>
  37. /// Will be the PhysX plugin but for now will be a very basic physics engine
  38. /// </summary>
  39. public class PhysXPlugin : IPhysicsPlugin
  40. {
  41. private PhysXScene _mScene;
  42. public PhysXPlugin()
  43. {
  44. }
  45. public bool Init()
  46. {
  47. return true;
  48. }
  49. public PhysicsScene GetScene()
  50. {
  51. if (_mScene == null)
  52. {
  53. _mScene = new PhysXScene();
  54. }
  55. return (_mScene);
  56. }
  57. public string GetName()
  58. {
  59. return ("RealPhysX");
  60. }
  61. public void Dispose()
  62. {
  63. }
  64. }
  65. public class PhysXScene : PhysicsScene
  66. {
  67. private List<PhysXCharacter> _characters = new List<PhysXCharacter>();
  68. private List<PhysXPrim> _prims = new List<PhysXPrim>();
  69. private float[] _heightMap = null;
  70. private NxPhysicsSDK mySdk;
  71. private NxScene scene;
  72. public PhysXScene()
  73. {
  74. mySdk = NxPhysicsSDK.CreateSDK();
  75. Console.WriteLine("Sdk created - now creating scene");
  76. scene = mySdk.CreateScene();
  77. }
  78. public override void Initialise(IMesher meshmerizer)
  79. {
  80. // Does nothing right now
  81. }
  82. public override void Dispose()
  83. {
  84. }
  85. public override PhysicsActor AddAvatar(string avName, PhysicsVector position, PhysicsVector size)
  86. {
  87. Vec3 pos = new Vec3();
  88. pos.X = position.X;
  89. pos.Y = position.Y;
  90. pos.Z = position.Z;
  91. PhysXCharacter act = new PhysXCharacter(scene.AddCharacter(pos));
  92. act.Position = position;
  93. _characters.Add(act);
  94. return act;
  95. }
  96. public override void RemovePrim(PhysicsActor prim)
  97. {
  98. }
  99. public override void RemoveAvatar(PhysicsActor actor)
  100. {
  101. }
  102. private PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size, Quaternion rotation)
  103. {
  104. Vec3 pos = new Vec3();
  105. pos.X = position.X;
  106. pos.Y = position.Y;
  107. pos.Z = position.Z;
  108. Vec3 siz = new Vec3();
  109. siz.X = size.X;
  110. siz.Y = size.Y;
  111. siz.Z = size.Z;
  112. PhysXPrim act = new PhysXPrim(scene.AddNewBox(pos, siz));
  113. _prims.Add(act);
  114. return act;
  115. }
  116. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  117. PhysicsVector size, Quaternion rotation) //To be removed
  118. {
  119. return AddPrimShape(primName, pbs, position, size, rotation, false);
  120. }
  121. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  122. PhysicsVector size, Quaternion rotation, bool isPhysical)
  123. {
  124. return AddPrim(position, size, rotation);
  125. }
  126. public override void AddPhysicsActorTaint(PhysicsActor prim)
  127. {
  128. }
  129. public override float Simulate(float timeStep)
  130. {
  131. float fps = 0f;
  132. try
  133. {
  134. foreach (PhysXCharacter actor in _characters)
  135. {
  136. actor.Move(timeStep);
  137. }
  138. scene.Simulate(timeStep);
  139. scene.FetchResults();
  140. scene.UpdateControllers();
  141. foreach (PhysXCharacter actor in _characters)
  142. {
  143. actor.UpdatePosition();
  144. }
  145. }
  146. catch (Exception e)
  147. {
  148. Console.WriteLine(e.Message);
  149. }
  150. return fps;
  151. }
  152. public override void GetResults()
  153. {
  154. }
  155. public override bool IsThreaded
  156. {
  157. get { return (false); // for now we won't be multithreaded
  158. }
  159. }
  160. public override void SetTerrain(float[] heightMap)
  161. {
  162. if (_heightMap != null)
  163. {
  164. Console.WriteLine("PhysX - deleting old terrain");
  165. scene.DeleteTerrain();
  166. }
  167. _heightMap = heightMap;
  168. scene.AddTerrain(heightMap);
  169. }
  170. public override void DeleteTerrain()
  171. {
  172. scene.DeleteTerrain();
  173. }
  174. }
  175. public class PhysXCharacter : PhysicsActor
  176. {
  177. private PhysicsVector _position;
  178. private PhysicsVector _velocity;
  179. private PhysicsVector m_rotationalVelocity = PhysicsVector.Zero;
  180. private PhysicsVector _acceleration;
  181. private NxCharacter _character;
  182. private bool flying;
  183. private bool iscolliding = false;
  184. private float gravityAccel;
  185. public PhysXCharacter(NxCharacter character)
  186. {
  187. _velocity = new PhysicsVector();
  188. _position = new PhysicsVector();
  189. _acceleration = new PhysicsVector();
  190. _character = character;
  191. }
  192. public override int PhysicsActorType
  193. {
  194. get { return (int) ActorTypes.Agent; }
  195. set { return; }
  196. }
  197. public override bool SetAlwaysRun
  198. {
  199. get { return false; }
  200. set { return; }
  201. }
  202. public override bool Grabbed
  203. {
  204. set { return; }
  205. }
  206. public override bool Selected
  207. {
  208. set { return; }
  209. }
  210. public override bool IsPhysical
  211. {
  212. get { return false; }
  213. set { return; }
  214. }
  215. public override bool ThrottleUpdates
  216. {
  217. get { return false; }
  218. set { return; }
  219. }
  220. public override bool Flying
  221. {
  222. get { return flying; }
  223. set { flying = value; }
  224. }
  225. public override bool IsColliding
  226. {
  227. get { return iscolliding; }
  228. set { iscolliding = value; }
  229. }
  230. public override bool CollidingGround
  231. {
  232. get { return false; }
  233. set { return; }
  234. }
  235. public override bool CollidingObj
  236. {
  237. get { return false; }
  238. set { return; }
  239. }
  240. public override PhysicsVector RotationalVelocity
  241. {
  242. get { return m_rotationalVelocity; }
  243. set { m_rotationalVelocity = value; }
  244. }
  245. public override bool Stopped
  246. {
  247. get { return false; }
  248. }
  249. public override PhysicsVector Position
  250. {
  251. get { return _position; }
  252. set
  253. {
  254. _position = value;
  255. Vec3 ps = new Vec3();
  256. ps.X = value.X;
  257. ps.Y = value.Y;
  258. ps.Z = value.Z;
  259. _character.Position = ps;
  260. }
  261. }
  262. public override PhysicsVector Size
  263. {
  264. get { return PhysicsVector.Zero; }
  265. set { }
  266. }
  267. public override float Mass
  268. {
  269. get { return 0f; }
  270. }
  271. public override PhysicsVector Force
  272. {
  273. get { return PhysicsVector.Zero; }
  274. }
  275. public override PhysicsVector CenterOfMass
  276. {
  277. get { return PhysicsVector.Zero; }
  278. }
  279. public override PhysicsVector GeometricCenter
  280. {
  281. get { return PhysicsVector.Zero; }
  282. }
  283. public override PhysicsVector Velocity
  284. {
  285. get { return _velocity; }
  286. set { _velocity = value; }
  287. }
  288. public override float CollisionScore
  289. {
  290. get { return 0f; }
  291. }
  292. public override bool Kinematic
  293. {
  294. get { return false; }
  295. set { }
  296. }
  297. public override Quaternion Orientation
  298. {
  299. get { return Quaternion.Identity; }
  300. set { }
  301. }
  302. public override PhysicsVector Acceleration
  303. {
  304. get { return _acceleration; }
  305. }
  306. public void SetAcceleration(PhysicsVector accel)
  307. {
  308. _acceleration = accel;
  309. }
  310. public override void AddForce(PhysicsVector force)
  311. {
  312. }
  313. public override void SetMomentum(PhysicsVector momentum)
  314. {
  315. }
  316. public void Move(float timeStep)
  317. {
  318. Vec3 vec = new Vec3();
  319. vec.X = _velocity.X*timeStep;
  320. vec.Y = _velocity.Y*timeStep;
  321. if (flying)
  322. {
  323. vec.Z = (_velocity.Z)*timeStep;
  324. }
  325. else
  326. {
  327. gravityAccel += -9.8f;
  328. vec.Z = (gravityAccel + _velocity.Z)*timeStep;
  329. }
  330. int res = _character.Move(vec);
  331. if (res == 1)
  332. {
  333. gravityAccel = 0;
  334. }
  335. }
  336. public override PrimitiveBaseShape Shape
  337. {
  338. set { return; }
  339. }
  340. public void UpdatePosition()
  341. {
  342. Vec3 vec = _character.Position;
  343. _position.X = vec.X;
  344. _position.Y = vec.Y;
  345. _position.Z = vec.Z;
  346. }
  347. public override void CrossingFailure()
  348. {
  349. }
  350. }
  351. public class PhysXPrim : PhysicsActor
  352. {
  353. private PhysicsVector _velocity;
  354. private PhysicsVector _acceleration;
  355. private PhysicsVector m_rotationalVelocity;
  356. private NxActor _prim;
  357. public PhysXPrim(NxActor prim)
  358. {
  359. _velocity = new PhysicsVector();
  360. _acceleration = new PhysicsVector();
  361. _prim = prim;
  362. }
  363. public override int PhysicsActorType
  364. {
  365. get { return (int) ActorTypes.Prim; }
  366. set { return; }
  367. }
  368. public override bool IsPhysical
  369. {
  370. get { return false; }
  371. set { return; }
  372. }
  373. public override bool SetAlwaysRun
  374. {
  375. get { return false; }
  376. set { return; }
  377. }
  378. public override bool Grabbed
  379. {
  380. set { return; }
  381. }
  382. public override bool Selected
  383. {
  384. set { return; }
  385. }
  386. public override bool ThrottleUpdates
  387. {
  388. get { return false; }
  389. set { return; }
  390. }
  391. public override PhysicsVector RotationalVelocity
  392. {
  393. get { return m_rotationalVelocity; }
  394. set { m_rotationalVelocity = value; }
  395. }
  396. public override bool Flying
  397. {
  398. get { return false; //no flying prims for you
  399. }
  400. set { }
  401. }
  402. public override bool IsColliding
  403. {
  404. get { return false; }
  405. set { }
  406. }
  407. public override bool CollidingGround
  408. {
  409. get { return false; }
  410. set { return; }
  411. }
  412. public override bool CollidingObj
  413. {
  414. get { return false; }
  415. set { return; }
  416. }
  417. public override bool Stopped
  418. {
  419. get { return false; }
  420. }
  421. public override PhysicsVector Position
  422. {
  423. get
  424. {
  425. PhysicsVector pos = new PhysicsVector();
  426. Vec3 vec = _prim.Position;
  427. pos.X = vec.X;
  428. pos.Y = vec.Y;
  429. pos.Z = vec.Z;
  430. return pos;
  431. }
  432. set
  433. {
  434. PhysicsVector vec = value;
  435. Vec3 pos = new Vec3();
  436. pos.X = vec.X;
  437. pos.Y = vec.Y;
  438. pos.Z = vec.Z;
  439. _prim.Position = pos;
  440. }
  441. }
  442. public override PrimitiveBaseShape Shape
  443. {
  444. set { return; }
  445. }
  446. public override PhysicsVector Velocity
  447. {
  448. get { return _velocity; }
  449. set { _velocity = value; }
  450. }
  451. public override float CollisionScore
  452. {
  453. get { return 0f; }
  454. }
  455. public override bool Kinematic
  456. {
  457. get { return _prim.Kinematic; }
  458. set { _prim.Kinematic = value; }
  459. }
  460. public override Quaternion Orientation
  461. {
  462. get
  463. {
  464. Quaternion res = new Quaternion();
  465. PhysXWrapper.Quaternion quat = _prim.GetOrientation();
  466. res.w = quat.W;
  467. res.x = quat.X;
  468. res.y = quat.Y;
  469. res.z = quat.Z;
  470. return res;
  471. }
  472. set { }
  473. }
  474. public override PhysicsVector Acceleration
  475. {
  476. get { return _acceleration; }
  477. }
  478. public void SetAcceleration(PhysicsVector accel)
  479. {
  480. _acceleration = accel;
  481. }
  482. public override void AddForce(PhysicsVector force)
  483. {
  484. }
  485. public override void SetMomentum(PhysicsVector momentum)
  486. {
  487. }
  488. public override PhysicsVector Size
  489. {
  490. get { return PhysicsVector.Zero; }
  491. set { }
  492. }
  493. public override float Mass
  494. {
  495. get { return 0f; }
  496. }
  497. public override PhysicsVector Force
  498. {
  499. get { return PhysicsVector.Zero; }
  500. }
  501. public override PhysicsVector CenterOfMass
  502. {
  503. get { return PhysicsVector.Zero; }
  504. }
  505. public override PhysicsVector GeometricCenter
  506. {
  507. get { return PhysicsVector.Zero; }
  508. }
  509. public override void CrossingFailure()
  510. {
  511. }
  512. }
  513. }