PhysXPlugin.cs 17 KB

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