1
0

PhysXPlugin.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 LockAngularMotion(PhysicsVector axis)
  335. {
  336. }
  337. public override void SetMomentum(PhysicsVector momentum)
  338. {
  339. }
  340. public void Move(float timeStep)
  341. {
  342. Vec3 vec = new Vec3();
  343. vec.X = _velocity.X*timeStep;
  344. vec.Y = _velocity.Y*timeStep;
  345. if (flying)
  346. {
  347. vec.Z = (_velocity.Z)*timeStep;
  348. }
  349. else
  350. {
  351. gravityAccel += -9.8f;
  352. vec.Z = (gravityAccel + _velocity.Z)*timeStep;
  353. }
  354. int res = _character.Move(vec);
  355. if (res == 1)
  356. {
  357. gravityAccel = 0;
  358. }
  359. }
  360. public override PrimitiveBaseShape Shape
  361. {
  362. set { return; }
  363. }
  364. public void UpdatePosition()
  365. {
  366. Vec3 vec = _character.Position;
  367. _position.X = vec.X;
  368. _position.Y = vec.Y;
  369. _position.Z = vec.Z;
  370. }
  371. public override void CrossingFailure()
  372. {
  373. }
  374. public override PhysicsVector PIDTarget { set { return; } }
  375. public override bool PIDActive { set { return; } }
  376. public override float PIDTau { set { return; } }
  377. }
  378. public class PhysXPrim : PhysicsActor
  379. {
  380. private PhysicsVector _velocity;
  381. private PhysicsVector _acceleration;
  382. private PhysicsVector m_rotationalVelocity;
  383. private NxActor _prim;
  384. public PhysXPrim(NxActor prim)
  385. {
  386. _velocity = new PhysicsVector();
  387. _acceleration = new PhysicsVector();
  388. _prim = prim;
  389. }
  390. public override int PhysicsActorType
  391. {
  392. get { return (int) ActorTypes.Prim; }
  393. set { return; }
  394. }
  395. public override bool IsPhysical
  396. {
  397. get { return false; }
  398. set { return; }
  399. }
  400. public override bool SetAlwaysRun
  401. {
  402. get { return false; }
  403. set { return; }
  404. }
  405. public override uint LocalID
  406. {
  407. set { return; }
  408. }
  409. public override bool Grabbed
  410. {
  411. set { return; }
  412. }
  413. public override bool Selected
  414. {
  415. set { return; }
  416. }
  417. public override float Buoyancy
  418. {
  419. get { return 0f; }
  420. set { return; }
  421. }
  422. public override bool FloatOnWater
  423. {
  424. set { return; }
  425. }
  426. public override bool ThrottleUpdates
  427. {
  428. get { return false; }
  429. set { return; }
  430. }
  431. public override PhysicsVector RotationalVelocity
  432. {
  433. get { return m_rotationalVelocity; }
  434. set { m_rotationalVelocity = value; }
  435. }
  436. public override bool Flying
  437. {
  438. get { return false; //no flying prims for you
  439. }
  440. set { }
  441. }
  442. public override bool IsColliding
  443. {
  444. get { return false; }
  445. set { }
  446. }
  447. public override bool CollidingGround
  448. {
  449. get { return false; }
  450. set { return; }
  451. }
  452. public override bool CollidingObj
  453. {
  454. get { return false; }
  455. set { return; }
  456. }
  457. public override bool Stopped
  458. {
  459. get { return false; }
  460. }
  461. public override PhysicsVector Position
  462. {
  463. get
  464. {
  465. PhysicsVector pos = new PhysicsVector();
  466. Vec3 vec = _prim.Position;
  467. pos.X = vec.X;
  468. pos.Y = vec.Y;
  469. pos.Z = vec.Z;
  470. return pos;
  471. }
  472. set
  473. {
  474. PhysicsVector vec = value;
  475. Vec3 pos = new Vec3();
  476. pos.X = vec.X;
  477. pos.Y = vec.Y;
  478. pos.Z = vec.Z;
  479. _prim.Position = pos;
  480. }
  481. }
  482. public override PrimitiveBaseShape Shape
  483. {
  484. set { return; }
  485. }
  486. public override PhysicsVector Velocity
  487. {
  488. get { return _velocity; }
  489. set { _velocity = value; }
  490. }
  491. public override float CollisionScore
  492. {
  493. get { return 0f; }
  494. }
  495. public override bool Kinematic
  496. {
  497. get { return _prim.Kinematic; }
  498. set { _prim.Kinematic = value; }
  499. }
  500. public override Quaternion Orientation
  501. {
  502. get
  503. {
  504. Quaternion res = new Quaternion();
  505. PhysXWrapper.Quaternion quat = _prim.GetOrientation();
  506. res.w = quat.W;
  507. res.x = quat.X;
  508. res.y = quat.Y;
  509. res.z = quat.Z;
  510. return res;
  511. }
  512. set { }
  513. }
  514. public override PhysicsVector Acceleration
  515. {
  516. get { return _acceleration; }
  517. }
  518. public void SetAcceleration(PhysicsVector accel)
  519. {
  520. _acceleration = accel;
  521. }
  522. public override void AddForce(PhysicsVector force)
  523. {
  524. }
  525. public override void SetMomentum(PhysicsVector momentum)
  526. {
  527. }
  528. public override PhysicsVector Size
  529. {
  530. get { return PhysicsVector.Zero; }
  531. set { }
  532. }
  533. public override void link(PhysicsActor obj)
  534. {
  535. }
  536. public override void delink()
  537. {
  538. }
  539. public override void LockAngularMotion(PhysicsVector axis)
  540. {
  541. }
  542. public override float Mass
  543. {
  544. get { return 0f; }
  545. }
  546. public override PhysicsVector Force
  547. {
  548. get { return PhysicsVector.Zero; }
  549. }
  550. public override PhysicsVector CenterOfMass
  551. {
  552. get { return PhysicsVector.Zero; }
  553. }
  554. public override PhysicsVector GeometricCenter
  555. {
  556. get { return PhysicsVector.Zero; }
  557. }
  558. public override void CrossingFailure()
  559. {
  560. }
  561. public override PhysicsVector PIDTarget { set { return; } }
  562. public override bool PIDActive { set { return; } }
  563. public override float PIDTau { set { return; } }
  564. }
  565. }