PhysXPlugin.cs 17 KB

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