PhysXPlugin.cs 20 KB

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