PhysXPlugin.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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 Nini.Config;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Physics.Manager;
  32. using PhysXWrapper;
  33. using Quaternion=OpenMetaverse.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(string sceneIdentifier)
  50. {
  51. if (_mScene == null)
  52. {
  53. _mScene = new PhysXScene(sceneIdentifier);
  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. // protected internal string sceneIdentifier;
  73. public PhysXScene(string _sceneIdentifier)
  74. {
  75. //sceneIdentifier = _sceneIdentifier;
  76. mySdk = NxPhysicsSDK.CreateSDK();
  77. Console.WriteLine("Sdk created - now creating scene");
  78. scene = mySdk.CreateScene();
  79. }
  80. public override void Initialise(IMesher meshmerizer, IConfigSource config)
  81. {
  82. // Does nothing right now
  83. }
  84. public override void Dispose()
  85. {
  86. }
  87. public override void SetWaterLevel(float baseheight)
  88. {
  89. }
  90. public override PhysicsActor AddAvatar(string avName, PhysicsVector position, PhysicsVector size)
  91. {
  92. Vec3 pos = new Vec3();
  93. pos.X = position.X;
  94. pos.Y = position.Y;
  95. pos.Z = position.Z;
  96. PhysXCharacter act = new PhysXCharacter(scene.AddCharacter(pos));
  97. act.Position = position;
  98. _characters.Add(act);
  99. return act;
  100. }
  101. public override void RemovePrim(PhysicsActor prim)
  102. {
  103. }
  104. public override void RemoveAvatar(PhysicsActor actor)
  105. {
  106. }
  107. private PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size, Quaternion rotation)
  108. {
  109. Vec3 pos = new Vec3();
  110. pos.X = position.X;
  111. pos.Y = position.Y;
  112. pos.Z = position.Z;
  113. Vec3 siz = new Vec3();
  114. siz.X = size.X;
  115. siz.Y = size.Y;
  116. siz.Z = size.Z;
  117. PhysXPrim act = new PhysXPrim(scene.AddNewBox(pos, siz));
  118. _prims.Add(act);
  119. return act;
  120. }
  121. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  122. PhysicsVector size, Quaternion rotation) //To be removed
  123. {
  124. return AddPrimShape(primName, pbs, position, size, rotation, false);
  125. }
  126. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  127. PhysicsVector size, Quaternion rotation, bool isPhysical)
  128. {
  129. return AddPrim(position, size, rotation);
  130. }
  131. public override void AddPhysicsActorTaint(PhysicsActor prim)
  132. {
  133. }
  134. public override float Simulate(float timeStep)
  135. {
  136. float fps = 0f;
  137. try
  138. {
  139. foreach (PhysXCharacter actor in _characters)
  140. {
  141. actor.Move(timeStep);
  142. }
  143. scene.Simulate(timeStep);
  144. scene.FetchResults();
  145. scene.UpdateControllers();
  146. foreach (PhysXCharacter actor in _characters)
  147. {
  148. actor.UpdatePosition();
  149. }
  150. }
  151. catch (Exception e)
  152. {
  153. Console.WriteLine(e.Message);
  154. }
  155. return fps;
  156. }
  157. public override void GetResults()
  158. {
  159. }
  160. public override bool IsThreaded
  161. {
  162. get { return (false); // for now we won't be multithreaded
  163. }
  164. }
  165. public override void SetTerrain(float[] heightMap)
  166. {
  167. if (_heightMap != null)
  168. {
  169. Console.WriteLine("PhysX - deleting old terrain");
  170. scene.DeleteTerrain();
  171. }
  172. _heightMap = heightMap;
  173. scene.AddTerrain(heightMap);
  174. }
  175. public override void DeleteTerrain()
  176. {
  177. scene.DeleteTerrain();
  178. }
  179. public override Dictionary<uint, float> GetTopColliders()
  180. {
  181. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  182. return returncolliders;
  183. }
  184. }
  185. public class PhysXCharacter : PhysicsActor
  186. {
  187. private PhysicsVector _position;
  188. private PhysicsVector _velocity;
  189. private PhysicsVector m_rotationalVelocity = PhysicsVector.Zero;
  190. private PhysicsVector _acceleration;
  191. private NxCharacter _character;
  192. private bool flying;
  193. private bool iscolliding = false;
  194. private float gravityAccel;
  195. public PhysXCharacter(NxCharacter character)
  196. {
  197. _velocity = new PhysicsVector();
  198. _position = new PhysicsVector();
  199. _acceleration = new PhysicsVector();
  200. _character = character;
  201. }
  202. public override int PhysicsActorType
  203. {
  204. get { return (int) ActorTypes.Agent; }
  205. set { return; }
  206. }
  207. public override bool SetAlwaysRun
  208. {
  209. get { return false; }
  210. set { return; }
  211. }
  212. public override uint LocalID
  213. {
  214. set { return; }
  215. }
  216. public override bool Grabbed
  217. {
  218. set { return; }
  219. }
  220. public override bool Selected
  221. {
  222. set { return; }
  223. }
  224. public override float Buoyancy
  225. {
  226. get { return 0f; }
  227. set { return; }
  228. }
  229. public override bool FloatOnWater
  230. {
  231. set { return; }
  232. }
  233. public override bool IsPhysical
  234. {
  235. get { return false; }
  236. set { return; }
  237. }
  238. public override bool ThrottleUpdates
  239. {
  240. get { return false; }
  241. set { return; }
  242. }
  243. public override bool Flying
  244. {
  245. get { return flying; }
  246. set { flying = value; }
  247. }
  248. public override bool IsColliding
  249. {
  250. get { return iscolliding; }
  251. set { iscolliding = value; }
  252. }
  253. public override bool CollidingGround
  254. {
  255. get { return false; }
  256. set { return; }
  257. }
  258. public override bool CollidingObj
  259. {
  260. get { return false; }
  261. set { return; }
  262. }
  263. public override PhysicsVector RotationalVelocity
  264. {
  265. get { return m_rotationalVelocity; }
  266. set { m_rotationalVelocity = value; }
  267. }
  268. public override bool Stopped
  269. {
  270. get { return false; }
  271. }
  272. public override PhysicsVector Position
  273. {
  274. get { return _position; }
  275. set
  276. {
  277. _position = value;
  278. Vec3 ps = new Vec3();
  279. ps.X = value.X;
  280. ps.Y = value.Y;
  281. ps.Z = value.Z;
  282. _character.Position = ps;
  283. }
  284. }
  285. public override PhysicsVector Size
  286. {
  287. get { return PhysicsVector.Zero; }
  288. set { }
  289. }
  290. public override float Mass
  291. {
  292. get { return 0f; }
  293. }
  294. public override PhysicsVector Force
  295. {
  296. get { return PhysicsVector.Zero; }
  297. set { return; }
  298. }
  299. public override int VehicleType
  300. {
  301. get { return 0; }
  302. set { return; }
  303. }
  304. public override void VehicleFloatParam(int param, float value)
  305. {
  306. }
  307. public override void VehicleVectorParam(int param, PhysicsVector value)
  308. {
  309. }
  310. public override void VehicleRotationParam(int param, Quaternion rotation)
  311. {
  312. }
  313. public override void SetVolumeDetect(int param)
  314. {
  315. }
  316. public override PhysicsVector CenterOfMass
  317. {
  318. get { return PhysicsVector.Zero; }
  319. }
  320. public override PhysicsVector GeometricCenter
  321. {
  322. get { return PhysicsVector.Zero; }
  323. }
  324. public override PhysicsVector Velocity
  325. {
  326. get { return _velocity; }
  327. set { _velocity = value; }
  328. }
  329. public override float CollisionScore
  330. {
  331. get { return 0f; }
  332. set { }
  333. }
  334. public override bool Kinematic
  335. {
  336. get { return false; }
  337. set { }
  338. }
  339. public override Quaternion Orientation
  340. {
  341. get { return Quaternion.Identity; }
  342. set { }
  343. }
  344. public override PhysicsVector Acceleration
  345. {
  346. get { return _acceleration; }
  347. }
  348. public void SetAcceleration(PhysicsVector accel)
  349. {
  350. _acceleration = accel;
  351. }
  352. public override void AddForce(PhysicsVector force, bool pushforce)
  353. {
  354. }
  355. public override PhysicsVector Torque
  356. {
  357. get { return PhysicsVector.Zero; }
  358. set { return; }
  359. }
  360. public override void AddAngularForce(PhysicsVector force, bool pushforce)
  361. {
  362. }
  363. public override void link(PhysicsActor obj)
  364. {
  365. }
  366. public override void delink()
  367. {
  368. }
  369. public override void LockAngularMotion(PhysicsVector axis)
  370. {
  371. }
  372. public override void SetMomentum(PhysicsVector momentum)
  373. {
  374. }
  375. public void Move(float timeStep)
  376. {
  377. Vec3 vec = new Vec3();
  378. vec.X = _velocity.X*timeStep;
  379. vec.Y = _velocity.Y*timeStep;
  380. if (flying)
  381. {
  382. vec.Z = (_velocity.Z)*timeStep;
  383. }
  384. else
  385. {
  386. gravityAccel += -9.8f;
  387. vec.Z = (gravityAccel + _velocity.Z)*timeStep;
  388. }
  389. int res = _character.Move(vec);
  390. if (res == 1)
  391. {
  392. gravityAccel = 0;
  393. }
  394. }
  395. public override PrimitiveBaseShape Shape
  396. {
  397. set { return; }
  398. }
  399. public void UpdatePosition()
  400. {
  401. Vec3 vec = _character.Position;
  402. _position.X = vec.X;
  403. _position.Y = vec.Y;
  404. _position.Z = vec.Z;
  405. }
  406. public override void CrossingFailure()
  407. {
  408. }
  409. public override PhysicsVector PIDTarget { set { return; } }
  410. public override bool PIDActive { set { return; } }
  411. public override float PIDTau { set { return; } }
  412. public override void SubscribeEvents(int ms)
  413. {
  414. }
  415. public override void UnSubscribeEvents()
  416. {
  417. }
  418. public override bool SubscribedEvents()
  419. {
  420. return false;
  421. }
  422. }
  423. public class PhysXPrim : PhysicsActor
  424. {
  425. private PhysicsVector _velocity;
  426. private PhysicsVector _acceleration;
  427. private PhysicsVector m_rotationalVelocity;
  428. private NxActor _prim;
  429. public PhysXPrim(NxActor prim)
  430. {
  431. _velocity = new PhysicsVector();
  432. _acceleration = new PhysicsVector();
  433. _prim = prim;
  434. }
  435. public override int PhysicsActorType
  436. {
  437. get { return (int) ActorTypes.Prim; }
  438. set { return; }
  439. }
  440. public override bool IsPhysical
  441. {
  442. get { return false; }
  443. set { return; }
  444. }
  445. public override bool SetAlwaysRun
  446. {
  447. get { return false; }
  448. set { return; }
  449. }
  450. public override uint LocalID
  451. {
  452. set { return; }
  453. }
  454. public override bool Grabbed
  455. {
  456. set { return; }
  457. }
  458. public override bool Selected
  459. {
  460. set { return; }
  461. }
  462. public override float Buoyancy
  463. {
  464. get { return 0f; }
  465. set { return; }
  466. }
  467. public override bool FloatOnWater
  468. {
  469. set { return; }
  470. }
  471. public override bool ThrottleUpdates
  472. {
  473. get { return false; }
  474. set { return; }
  475. }
  476. public override PhysicsVector RotationalVelocity
  477. {
  478. get { return m_rotationalVelocity; }
  479. set { m_rotationalVelocity = value; }
  480. }
  481. public override bool Flying
  482. {
  483. get { return false; //no flying prims for you
  484. }
  485. set { }
  486. }
  487. public override bool IsColliding
  488. {
  489. get { return false; }
  490. set { }
  491. }
  492. public override bool CollidingGround
  493. {
  494. get { return false; }
  495. set { return; }
  496. }
  497. public override bool CollidingObj
  498. {
  499. get { return false; }
  500. set { return; }
  501. }
  502. public override bool Stopped
  503. {
  504. get { return false; }
  505. }
  506. public override PhysicsVector Position
  507. {
  508. get
  509. {
  510. PhysicsVector pos = new PhysicsVector();
  511. Vec3 vec = _prim.Position;
  512. pos.X = vec.X;
  513. pos.Y = vec.Y;
  514. pos.Z = vec.Z;
  515. return pos;
  516. }
  517. set
  518. {
  519. PhysicsVector vec = value;
  520. Vec3 pos = new Vec3();
  521. pos.X = vec.X;
  522. pos.Y = vec.Y;
  523. pos.Z = vec.Z;
  524. _prim.Position = pos;
  525. }
  526. }
  527. public override PrimitiveBaseShape Shape
  528. {
  529. set { return; }
  530. }
  531. public override PhysicsVector Velocity
  532. {
  533. get { return _velocity; }
  534. set { _velocity = value; }
  535. }
  536. public override PhysicsVector Torque
  537. {
  538. get { return PhysicsVector.Zero; }
  539. set { return; }
  540. }
  541. public override float CollisionScore
  542. {
  543. get { return 0f; }
  544. set { }
  545. }
  546. public override bool Kinematic
  547. {
  548. get { return _prim.Kinematic; }
  549. set { _prim.Kinematic = value; }
  550. }
  551. public override Quaternion Orientation
  552. {
  553. get
  554. {
  555. Quaternion res;
  556. PhysXWrapper.Quaternion quat = _prim.GetOrientation();
  557. res.W = quat.W;
  558. res.X = quat.X;
  559. res.Y = quat.Y;
  560. res.Z = quat.Z;
  561. return res;
  562. }
  563. set { }
  564. }
  565. public override PhysicsVector Acceleration
  566. {
  567. get { return _acceleration; }
  568. }
  569. public void SetAcceleration(PhysicsVector accel)
  570. {
  571. _acceleration = accel;
  572. }
  573. public override void AddForce(PhysicsVector force, bool pushforce)
  574. {
  575. }
  576. public override void AddAngularForce(PhysicsVector force, bool pushforce)
  577. {
  578. }
  579. public override void SetMomentum(PhysicsVector momentum)
  580. {
  581. }
  582. public override PhysicsVector Size
  583. {
  584. get { return PhysicsVector.Zero; }
  585. set { }
  586. }
  587. public override void link(PhysicsActor obj)
  588. {
  589. }
  590. public override void delink()
  591. {
  592. }
  593. public override void LockAngularMotion(PhysicsVector axis)
  594. {
  595. }
  596. public override float Mass
  597. {
  598. get { return 0f; }
  599. }
  600. public override PhysicsVector Force
  601. {
  602. get { return PhysicsVector.Zero; }
  603. set { return; }
  604. }
  605. public override int VehicleType
  606. {
  607. get { return 0; }
  608. set { return; }
  609. }
  610. public override void VehicleFloatParam(int param, float value)
  611. {
  612. }
  613. public override void VehicleVectorParam(int param, PhysicsVector value)
  614. {
  615. }
  616. public override void VehicleRotationParam(int param, Quaternion rotation)
  617. {
  618. }
  619. public override void SetVolumeDetect(int param)
  620. {
  621. }
  622. public override PhysicsVector CenterOfMass
  623. {
  624. get { return PhysicsVector.Zero; }
  625. }
  626. public override PhysicsVector GeometricCenter
  627. {
  628. get { return PhysicsVector.Zero; }
  629. }
  630. public override void CrossingFailure()
  631. {
  632. }
  633. public override PhysicsVector PIDTarget { set { return; } }
  634. public override bool PIDActive { set { return; } }
  635. public override float PIDTau { set { return; } }
  636. public override void SubscribeEvents(int ms)
  637. {
  638. }
  639. public override void UnSubscribeEvents()
  640. {
  641. }
  642. public override bool SubscribedEvents()
  643. {
  644. return false;
  645. }
  646. }
  647. }