BasicPhysicsScene.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 Mono.Addins;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.PhysicsModules.SharedBase;
  34. using OpenSim.Region.Framework.Scenes;
  35. using OpenSim.Region.Framework.Interfaces;
  36. namespace OpenSim.Region.PhysicsModule.BasicPhysics
  37. {
  38. /// <summary>
  39. /// This is an incomplete extremely basic physics implementation
  40. /// </summary>
  41. /// <remarks>
  42. /// Not useful for anything at the moment apart from some regression testing in other components where some form
  43. /// of physics plugin is needed.
  44. /// </remarks>
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "BasicPhysicsScene")]
  46. public class BasicScene : PhysicsScene, INonSharedRegionModule
  47. {
  48. private List<BasicActor> _actors = new List<BasicActor>();
  49. private List<BasicPhysicsPrim> _prims = new List<BasicPhysicsPrim>();
  50. private float[] _heightMap;
  51. private Vector3 m_regionExtent;
  52. private bool m_Enabled = false;
  53. //protected internal string sceneIdentifier;
  54. #region INonSharedRegionModule
  55. public string Name
  56. {
  57. get { return "basicphysics"; }
  58. }
  59. public Type ReplaceableInterface
  60. {
  61. get { return null; }
  62. }
  63. public void Initialise(IConfigSource source)
  64. {
  65. // TODO: Move this out of Startup
  66. IConfig config = source.Configs["Startup"];
  67. if (config != null)
  68. {
  69. string physics = config.GetString("physics", string.Empty);
  70. if (physics == Name)
  71. m_Enabled = true;
  72. }
  73. }
  74. public void Close()
  75. {
  76. }
  77. public void AddRegion(Scene scene)
  78. {
  79. if (!m_Enabled)
  80. return;
  81. EngineType = Name;
  82. PhysicsSceneName = EngineType + "/" + scene.RegionInfo.RegionName;
  83. scene.RegisterModuleInterface<PhysicsScene>(this);
  84. m_regionExtent = new Vector3(scene.RegionInfo.RegionSizeX, scene.RegionInfo.RegionSizeY, scene.RegionInfo.RegionSizeZ);
  85. base.Initialise(scene.PhysicsRequestAsset,
  86. (scene.Heightmap != null ? scene.Heightmap.GetFloatsSerialised() : new float[scene.RegionInfo.RegionSizeX * scene.RegionInfo.RegionSizeY]),
  87. (float)scene.RegionInfo.RegionSettings.WaterHeight);
  88. }
  89. public void RemoveRegion(Scene scene)
  90. {
  91. if (!m_Enabled)
  92. return;
  93. }
  94. public void RegionLoaded(Scene scene)
  95. {
  96. if (!m_Enabled)
  97. return;
  98. }
  99. #endregion
  100. public override void Dispose() {}
  101. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  102. Vector3 size, Quaternion rotation, bool isPhysical, uint localid)
  103. {
  104. BasicPhysicsPrim prim = new BasicPhysicsPrim(primName, localid, position, size, rotation, pbs);
  105. prim.IsPhysical = isPhysical;
  106. _prims.Add(prim);
  107. return prim;
  108. }
  109. public override PhysicsActor AddAvatar(string avName, Vector3 position, Vector3 velocity, Vector3 size, bool isFlying)
  110. {
  111. BasicActor act = new BasicActor(size);
  112. act.Position = position;
  113. act.Velocity = velocity;
  114. act.Flying = isFlying;
  115. _actors.Add(act);
  116. return act;
  117. }
  118. public override void RemovePrim(PhysicsActor actor)
  119. {
  120. BasicPhysicsPrim prim = (BasicPhysicsPrim)actor;
  121. if (_prims.Contains(prim))
  122. _prims.Remove(prim);
  123. }
  124. public override void RemoveAvatar(PhysicsActor actor)
  125. {
  126. BasicActor act = (BasicActor)actor;
  127. if (_actors.Contains(act))
  128. _actors.Remove(act);
  129. }
  130. public override void AddPhysicsActorTaint(PhysicsActor prim)
  131. {
  132. }
  133. public override float Simulate(float timeStep)
  134. {
  135. // Console.WriteLine("Simulating");
  136. float fps = 0;
  137. for (int i = 0; i < _actors.Count; ++i)
  138. {
  139. BasicActor actor = _actors[i];
  140. Vector3 actorPosition = actor.Position;
  141. Vector3 actorVelocity = actor.Velocity;
  142. //Console.WriteLine(
  143. // "Processing actor {0}, starting pos {1}, starting vel {2}", i, actorPosition, actorVelocity);
  144. actorPosition.X += actor.Velocity.X * timeStep;
  145. actorPosition.Y += actor.Velocity.Y * timeStep;
  146. if (actor.Position.Y < 0)
  147. {
  148. actorPosition.Y = 0.1F;
  149. }
  150. else if (actor.Position.Y >= m_regionExtent.Y)
  151. {
  152. actorPosition.Y = (m_regionExtent.Y - 0.1f);
  153. }
  154. if (actor.Position.X < 0)
  155. {
  156. actorPosition.X = 0.1F;
  157. }
  158. else if (actor.Position.X >= m_regionExtent.X)
  159. {
  160. actorPosition.X = (m_regionExtent.X - 0.1f);
  161. }
  162. float terrainHeight = 0;
  163. if (_heightMap != null)
  164. terrainHeight = _heightMap[(int)actor.Position.Y * (int)m_regionExtent.Y + (int)actor.Position.X];
  165. float height = terrainHeight + actor.Size.Z;
  166. // Console.WriteLine("height {0}, actorPosition {1}", height, actorPosition);
  167. if (actor.Flying)
  168. {
  169. if (actor.Position.Z + (actor.Velocity.Z * timeStep) < terrainHeight + 2)
  170. {
  171. actorPosition.Z = height;
  172. actorVelocity.Z = 0;
  173. actor.IsColliding = true;
  174. }
  175. else
  176. {
  177. actorPosition.Z += actor.Velocity.Z * timeStep;
  178. actor.IsColliding = false;
  179. }
  180. }
  181. else
  182. {
  183. actorPosition.Z = height;
  184. actorVelocity.Z = 0;
  185. actor.IsColliding = true;
  186. }
  187. actor.Position = actorPosition;
  188. actor.Velocity = actorVelocity;
  189. }
  190. return 1.0f;
  191. }
  192. public override void GetResults()
  193. {
  194. }
  195. public override bool IsThreaded
  196. {
  197. get { return (false); // for now we won't be multithreaded
  198. }
  199. }
  200. public override void SetTerrain(float[] heightMap)
  201. {
  202. _heightMap = heightMap;
  203. }
  204. public override void DeleteTerrain()
  205. {
  206. }
  207. public override void SetWaterLevel(float baseheight)
  208. {
  209. }
  210. public override Dictionary<uint, float> GetTopColliders()
  211. {
  212. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  213. return returncolliders;
  214. }
  215. }
  216. }