PhysXPlugin.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. */
  28. using System;
  29. using System.Collections.Generic;
  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)
  79. {
  80. // Does nothing right now
  81. }
  82. public override PhysicsActor AddAvatar(string avName, PhysicsVector position, uint localID)
  83. {
  84. Vec3 pos = new Vec3();
  85. pos.X = position.X;
  86. pos.Y = position.Y;
  87. pos.Z = position.Z;
  88. PhysXCharacter act = new PhysXCharacter(scene.AddCharacter(pos));
  89. act.Position = position;
  90. _characters.Add(act);
  91. return act;
  92. }
  93. public override void RemovePrim(PhysicsActor prim)
  94. {
  95. }
  96. public override void RemoveAvatar(PhysicsActor actor)
  97. {
  98. }
  99. private PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size, Quaternion rotation, uint localID)
  100. {
  101. Vec3 pos = new Vec3();
  102. pos.X = position.X;
  103. pos.Y = position.Y;
  104. pos.Z = position.Z;
  105. Vec3 siz = new Vec3();
  106. siz.X = size.X;
  107. siz.Y = size.Y;
  108. siz.Z = size.Z;
  109. PhysXPrim act = new PhysXPrim(scene.AddNewBox(pos, siz));
  110. _prims.Add(act);
  111. return act;
  112. }
  113. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  114. PhysicsVector size, Quaternion rotation, uint localID) //To be removed
  115. {
  116. return AddPrimShape(primName, pbs, position, size, rotation, false,localID);
  117. }
  118. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  119. PhysicsVector size, Quaternion rotation, bool isPhysical, uint localID)
  120. {
  121. return AddPrim(position, size, rotation,localID);
  122. }
  123. public override void AddPhysicsActorTaint(PhysicsActor prim)
  124. {
  125. }
  126. public override float Simulate(float timeStep)
  127. {
  128. float fps = 0f;
  129. try
  130. {
  131. foreach (PhysXCharacter actor in _characters)
  132. {
  133. actor.Move(timeStep);
  134. }
  135. scene.Simulate(timeStep);
  136. scene.FetchResults();
  137. scene.UpdateControllers();
  138. foreach (PhysXCharacter actor in _characters)
  139. {
  140. actor.UpdatePosition();
  141. }
  142. }
  143. catch (Exception e)
  144. {
  145. Console.WriteLine(e.Message);
  146. }
  147. return fps;
  148. }
  149. public override void GetResults()
  150. {
  151. }
  152. public override bool IsThreaded
  153. {
  154. get { return (false); // for now we won't be multithreaded
  155. }
  156. }
  157. public override void SetTerrain(float[] heightMap)
  158. {
  159. if (_heightMap != null)
  160. {
  161. Console.WriteLine("PhysX - deleting old terrain");
  162. scene.DeleteTerrain();
  163. }
  164. _heightMap = heightMap;
  165. scene.AddTerrain(heightMap);
  166. }
  167. public override void DeleteTerrain()
  168. {
  169. scene.DeleteTerrain();
  170. }
  171. }
  172. public class PhysXCharacter : PhysicsActor
  173. {
  174. private PhysicsVector _position;
  175. private PhysicsVector _velocity;
  176. private PhysicsVector m_rotationalVelocity = PhysicsVector.Zero;
  177. private PhysicsVector _acceleration;
  178. private NxCharacter _character;
  179. private bool flying;
  180. private bool iscolliding = false;
  181. private float gravityAccel;
  182. public PhysXCharacter(NxCharacter character)
  183. {
  184. _velocity = new PhysicsVector();
  185. _position = new PhysicsVector();
  186. _acceleration = new PhysicsVector();
  187. _character = character;
  188. }
  189. public override int PhysicsActorType
  190. {
  191. get { return (int) ActorTypes.Agent; }
  192. set { return; }
  193. }
  194. public override bool SetAlwaysRun
  195. {
  196. get { return false; }
  197. set { return; }
  198. }
  199. public override bool IsPhysical
  200. {
  201. get { return false; }
  202. set { return; }
  203. }
  204. public override bool ThrottleUpdates
  205. {
  206. get { return false; }
  207. set { return; }
  208. }
  209. public override bool Flying
  210. {
  211. get { return flying; }
  212. set { flying = value; }
  213. }
  214. public override bool IsColliding
  215. {
  216. get { return iscolliding; }
  217. set { iscolliding = value; }
  218. }
  219. public override bool CollidingGround
  220. {
  221. get { return false; }
  222. set { return; }
  223. }
  224. public override bool CollidingObj
  225. {
  226. get { return false; }
  227. set { return; }
  228. }
  229. public override PhysicsVector RotationalVelocity
  230. {
  231. get { return m_rotationalVelocity; }
  232. set { m_rotationalVelocity = value; }
  233. }
  234. public override PhysicsVector Position
  235. {
  236. get { return _position; }
  237. set
  238. {
  239. _position = value;
  240. Vec3 ps = new Vec3();
  241. ps.X = value.X;
  242. ps.Y = value.Y;
  243. ps.Z = value.Z;
  244. _character.Position = ps;
  245. }
  246. }
  247. public override PhysicsVector Size
  248. {
  249. get { return PhysicsVector.Zero; }
  250. set { }
  251. }
  252. public override float Mass
  253. {
  254. get { return 0f; }
  255. }
  256. public override PhysicsVector Force
  257. {
  258. get { return PhysicsVector.Zero; }
  259. }
  260. public override PhysicsVector CenterOfMass
  261. {
  262. get { return PhysicsVector.Zero; }
  263. }
  264. public override PhysicsVector GeometricCenter
  265. {
  266. get { return PhysicsVector.Zero; }
  267. }
  268. public override PhysicsVector Velocity
  269. {
  270. get { return _velocity; }
  271. set { _velocity = value; }
  272. }
  273. public override bool Kinematic
  274. {
  275. get { return false; }
  276. set { }
  277. }
  278. public override Quaternion Orientation
  279. {
  280. get { return Quaternion.Identity; }
  281. set { }
  282. }
  283. public override PhysicsVector Acceleration
  284. {
  285. get { return _acceleration; }
  286. }
  287. public void SetAcceleration(PhysicsVector accel)
  288. {
  289. _acceleration = accel;
  290. }
  291. public override void AddForce(PhysicsVector force)
  292. {
  293. }
  294. public override void SetMomentum(PhysicsVector momentum)
  295. {
  296. }
  297. public void Move(float timeStep)
  298. {
  299. Vec3 vec = new Vec3();
  300. vec.X = _velocity.X*timeStep;
  301. vec.Y = _velocity.Y*timeStep;
  302. if (flying)
  303. {
  304. vec.Z = (_velocity.Z)*timeStep;
  305. }
  306. else
  307. {
  308. gravityAccel += -9.8f;
  309. vec.Z = (gravityAccel + _velocity.Z)*timeStep;
  310. }
  311. int res = _character.Move(vec);
  312. if (res == 1)
  313. {
  314. gravityAccel = 0;
  315. }
  316. }
  317. public override PrimitiveBaseShape Shape
  318. {
  319. set { return; }
  320. }
  321. public void UpdatePosition()
  322. {
  323. Vec3 vec = _character.Position;
  324. _position.X = vec.X;
  325. _position.Y = vec.Y;
  326. _position.Z = vec.Z;
  327. }
  328. }
  329. public class PhysXPrim : PhysicsActor
  330. {
  331. private PhysicsVector _velocity;
  332. private PhysicsVector _acceleration;
  333. private PhysicsVector m_rotationalVelocity;
  334. private NxActor _prim;
  335. public PhysXPrim(NxActor prim)
  336. {
  337. _velocity = new PhysicsVector();
  338. _acceleration = new PhysicsVector();
  339. _prim = prim;
  340. }
  341. public override int PhysicsActorType
  342. {
  343. get { return (int) ActorTypes.Prim; }
  344. set { return; }
  345. }
  346. public override bool IsPhysical
  347. {
  348. get { return false; }
  349. set { return; }
  350. }
  351. public override bool SetAlwaysRun
  352. {
  353. get { return false; }
  354. set { return; }
  355. }
  356. public override bool ThrottleUpdates
  357. {
  358. get { return false; }
  359. set { return; }
  360. }
  361. public override PhysicsVector RotationalVelocity
  362. {
  363. get { return m_rotationalVelocity; }
  364. set { m_rotationalVelocity = value; }
  365. }
  366. public override bool Flying
  367. {
  368. get { return false; //no flying prims for you
  369. }
  370. set { }
  371. }
  372. public override bool IsColliding
  373. {
  374. get { return false; }
  375. set { }
  376. }
  377. public override bool CollidingGround
  378. {
  379. get { return false; }
  380. set { return; }
  381. }
  382. public override bool CollidingObj
  383. {
  384. get { return false; }
  385. set { return; }
  386. }
  387. public override PhysicsVector Position
  388. {
  389. get
  390. {
  391. PhysicsVector pos = new PhysicsVector();
  392. Vec3 vec = _prim.Position;
  393. pos.X = vec.X;
  394. pos.Y = vec.Y;
  395. pos.Z = vec.Z;
  396. return pos;
  397. }
  398. set
  399. {
  400. PhysicsVector vec = value;
  401. Vec3 pos = new Vec3();
  402. pos.X = vec.X;
  403. pos.Y = vec.Y;
  404. pos.Z = vec.Z;
  405. _prim.Position = pos;
  406. }
  407. }
  408. public override PrimitiveBaseShape Shape
  409. {
  410. set { return; }
  411. }
  412. public override PhysicsVector Velocity
  413. {
  414. get { return _velocity; }
  415. set { _velocity = value; }
  416. }
  417. public override bool Kinematic
  418. {
  419. get { return _prim.Kinematic; }
  420. set { _prim.Kinematic = value; }
  421. }
  422. public override Quaternion Orientation
  423. {
  424. get
  425. {
  426. Quaternion res = new Quaternion();
  427. PhysXWrapper.Quaternion quat = _prim.GetOrientation();
  428. res.w = quat.W;
  429. res.x = quat.X;
  430. res.y = quat.Y;
  431. res.z = quat.Z;
  432. return res;
  433. }
  434. set { }
  435. }
  436. public override PhysicsVector Acceleration
  437. {
  438. get { return _acceleration; }
  439. }
  440. public void SetAcceleration(PhysicsVector accel)
  441. {
  442. _acceleration = accel;
  443. }
  444. public override void AddForce(PhysicsVector force)
  445. {
  446. }
  447. public override void SetMomentum(PhysicsVector momentum)
  448. {
  449. }
  450. public override PhysicsVector Size
  451. {
  452. get { return PhysicsVector.Zero; }
  453. set { }
  454. }
  455. public override float Mass
  456. {
  457. get { return 0f; }
  458. }
  459. public override PhysicsVector Force
  460. {
  461. get { return PhysicsVector.Zero; }
  462. }
  463. public override PhysicsVector CenterOfMass
  464. {
  465. get { return PhysicsVector.Zero; }
  466. }
  467. public override PhysicsVector GeometricCenter
  468. {
  469. get { return PhysicsVector.Zero; }
  470. }
  471. }
  472. }