BasicPhysicsScene.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 string Version
  60. {
  61. get { return "1.0"; }
  62. }
  63. public Type ReplaceableInterface
  64. {
  65. get { return null; }
  66. }
  67. public void Initialise(IConfigSource source)
  68. {
  69. // TODO: Move this out of Startup
  70. IConfig config = source.Configs["Startup"];
  71. if (config != null)
  72. {
  73. string physics = config.GetString("physics", string.Empty);
  74. if (physics == Name)
  75. m_Enabled = true;
  76. }
  77. }
  78. public void Close()
  79. {
  80. }
  81. public void AddRegion(Scene scene)
  82. {
  83. if (!m_Enabled)
  84. return;
  85. EngineType = Name;
  86. PhysicsSceneName = EngineType + "/" + scene.RegionInfo.RegionName;
  87. EngineName = Name + " " + Version;
  88. scene.RegisterModuleInterface<PhysicsScene>(this);
  89. m_regionExtent = new Vector3(scene.RegionInfo.RegionSizeX, scene.RegionInfo.RegionSizeY, scene.RegionInfo.RegionSizeZ);
  90. base.Initialise(scene.PhysicsRequestAsset,
  91. (scene.Heightmap != null ? scene.Heightmap.GetFloatsSerialised() : new float[scene.RegionInfo.RegionSizeX * scene.RegionInfo.RegionSizeY]),
  92. (float)scene.RegionInfo.RegionSettings.WaterHeight);
  93. }
  94. public void RemoveRegion(Scene scene)
  95. {
  96. if (!m_Enabled)
  97. return;
  98. }
  99. public void RegionLoaded(Scene scene)
  100. {
  101. if (!m_Enabled)
  102. return;
  103. }
  104. #endregion
  105. public override void Dispose() {}
  106. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  107. Vector3 size, Quaternion rotation, bool isPhysical, uint localid)
  108. {
  109. BasicPhysicsPrim prim = new BasicPhysicsPrim(primName, localid, position, size, rotation, pbs);
  110. prim.IsPhysical = isPhysical;
  111. _prims.Add(prim);
  112. return prim;
  113. }
  114. public override PhysicsActor AddAvatar(string avName, Vector3 position, Vector3 velocity, Vector3 size, bool isFlying)
  115. {
  116. BasicActor act = new BasicActor(size);
  117. act.Position = position;
  118. act.Velocity = velocity;
  119. act.Flying = isFlying;
  120. _actors.Add(act);
  121. return act;
  122. }
  123. public override void RemovePrim(PhysicsActor actor)
  124. {
  125. BasicPhysicsPrim prim = (BasicPhysicsPrim)actor;
  126. if (_prims.Contains(prim))
  127. _prims.Remove(prim);
  128. }
  129. public override void RemoveAvatar(PhysicsActor actor)
  130. {
  131. BasicActor act = (BasicActor)actor;
  132. if (_actors.Contains(act))
  133. _actors.Remove(act);
  134. }
  135. public override void AddPhysicsActorTaint(PhysicsActor prim)
  136. {
  137. }
  138. public override float Simulate(float timeStep)
  139. {
  140. // Console.WriteLine("Simulating");
  141. float fps = 1.0f / timeStep;
  142. for (int i = 0; i < _actors.Count; ++i)
  143. {
  144. BasicActor actor = _actors[i];
  145. Vector3 actorPosition = actor.Position;
  146. Vector3 actorVelocity = actor.Velocity;
  147. //Console.WriteLine(
  148. // "Processing actor {0}, starting pos {1}, starting vel {2}", i, actorPosition, actorVelocity);
  149. actorPosition.X += actor.Velocity.X * timeStep;
  150. actorPosition.Y += actor.Velocity.Y * timeStep;
  151. if (actor.Position.Y < 0)
  152. {
  153. actorPosition.Y = 0.1F;
  154. }
  155. else if (actor.Position.Y >= m_regionExtent.Y)
  156. {
  157. actorPosition.Y = (m_regionExtent.Y - 0.1f);
  158. }
  159. if (actor.Position.X < 0)
  160. {
  161. actorPosition.X = 0.1F;
  162. }
  163. else if (actor.Position.X >= m_regionExtent.X)
  164. {
  165. actorPosition.X = (m_regionExtent.X - 0.1f);
  166. }
  167. float terrainHeight = 0;
  168. if (_heightMap != null)
  169. terrainHeight = _heightMap[(int)actor.Position.Y * (int)m_regionExtent.Y + (int)actor.Position.X];
  170. float height = terrainHeight + actor.Size.Z;
  171. // Console.WriteLine("height {0}, actorPosition {1}", height, actorPosition);
  172. if (actor.Flying)
  173. {
  174. if (actor.Position.Z + (actor.Velocity.Z * timeStep) < terrainHeight + 2)
  175. {
  176. actorPosition.Z = height;
  177. actorVelocity.Z = 0;
  178. actor.IsColliding = true;
  179. }
  180. else
  181. {
  182. actorPosition.Z += actor.Velocity.Z * timeStep;
  183. actor.IsColliding = false;
  184. }
  185. }
  186. else
  187. {
  188. actorPosition.Z = height;
  189. actorVelocity.Z = 0;
  190. actor.IsColliding = true;
  191. }
  192. actor.Position = actorPosition;
  193. actor.Velocity = actorVelocity;
  194. }
  195. return fps;
  196. }
  197. public override void SetTerrain(float[] heightMap)
  198. {
  199. _heightMap = heightMap;
  200. }
  201. public override void DeleteTerrain()
  202. {
  203. }
  204. public override void SetWaterLevel(float baseheight)
  205. {
  206. }
  207. public override Dictionary<uint, float> GetTopColliders()
  208. {
  209. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  210. return returncolliders;
  211. }
  212. }
  213. }