PhysXPlugin.cs 20 KB

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