PhysXScene.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. public class PhysXScene : PhysicsScene
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private List<PhysXCharacter> _characters = new List<PhysXCharacter>();
  43. private List<PhysXPrim> _prims = new List<PhysXPrim>();
  44. private float[] _heightMap = null;
  45. private NxPhysicsSDK mySdk;
  46. private NxScene scene;
  47. // protected internal string sceneIdentifier;
  48. public PhysXScene(string _sceneIdentifier)
  49. {
  50. //sceneIdentifier = _sceneIdentifier;
  51. mySdk = NxPhysicsSDK.CreateSDK();
  52. m_log.Info("Sdk created - now creating scene");
  53. scene = mySdk.CreateScene();
  54. }
  55. public override void Initialise(IMesher meshmerizer, IConfigSource config)
  56. {
  57. // Does nothing right now
  58. }
  59. public override void Dispose()
  60. {
  61. }
  62. public override void SetWaterLevel(float baseheight)
  63. {
  64. }
  65. public override PhysicsActor AddAvatar(string avName, Vector3 position, Vector3 size, bool isFlying)
  66. {
  67. Vec3 pos = new Vec3();
  68. pos.X = position.X;
  69. pos.Y = position.Y;
  70. pos.Z = position.Z;
  71. PhysXCharacter act = new PhysXCharacter(scene.AddCharacter(pos));
  72. act.Flying = isFlying;
  73. act.Position = position;
  74. _characters.Add(act);
  75. return act;
  76. }
  77. public override void RemovePrim(PhysicsActor prim)
  78. {
  79. }
  80. public override void RemoveAvatar(PhysicsActor actor)
  81. {
  82. }
  83. private PhysicsActor AddPrim(Vector3 position, Vector3 size, Quaternion rotation)
  84. {
  85. Vec3 pos = new Vec3();
  86. pos.X = position.X;
  87. pos.Y = position.Y;
  88. pos.Z = position.Z;
  89. Vec3 siz = new Vec3();
  90. siz.X = size.X;
  91. siz.Y = size.Y;
  92. siz.Z = size.Z;
  93. PhysXPrim act = new PhysXPrim(scene.AddNewBox(pos, siz));
  94. _prims.Add(act);
  95. return act;
  96. }
  97. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  98. Vector3 size, Quaternion rotation) //To be removed
  99. {
  100. return AddPrimShape(primName, pbs, position, size, rotation, false);
  101. }
  102. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  103. Vector3 size, Quaternion rotation, bool isPhysical)
  104. {
  105. return AddPrim(position, size, rotation);
  106. }
  107. public override void AddPhysicsActorTaint(PhysicsActor prim)
  108. {
  109. }
  110. public override float Simulate(float timeStep)
  111. {
  112. float fps = 0f;
  113. try
  114. {
  115. foreach (PhysXCharacter actor in _characters)
  116. {
  117. actor.Move(timeStep);
  118. }
  119. scene.Simulate(timeStep);
  120. scene.FetchResults();
  121. scene.UpdateControllers();
  122. foreach (PhysXCharacter actor in _characters)
  123. {
  124. actor.UpdatePosition();
  125. }
  126. }
  127. catch (Exception e)
  128. {
  129. m_log.Error(e.Message);
  130. }
  131. return fps;
  132. }
  133. public override void GetResults()
  134. {
  135. }
  136. public override bool IsThreaded
  137. {
  138. // for now we won't be multithreaded
  139. get { return (false); }
  140. }
  141. public override void SetTerrain(float[] heightMap)
  142. {
  143. if (_heightMap != null)
  144. {
  145. m_log.Debug("PhysX - deleting old terrain");
  146. scene.DeleteTerrain();
  147. }
  148. _heightMap = heightMap;
  149. scene.AddTerrain(heightMap);
  150. }
  151. public override void DeleteTerrain()
  152. {
  153. scene.DeleteTerrain();
  154. }
  155. public override Dictionary<uint, float> GetTopColliders()
  156. {
  157. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  158. return returncolliders;
  159. }
  160. }
  161. }