1
0

BasicPhysicsScene.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 float Simulate(float timeStep)
  136. {
  137. // Console.WriteLine("Simulating");
  138. float fps = 1.0f / timeStep;
  139. for (int i = 0; i < _actors.Count; ++i)
  140. {
  141. BasicActor actor = _actors[i];
  142. Vector3 actorPosition = actor.Position;
  143. Vector3 actorVelocity = actor.Velocity;
  144. //Console.WriteLine(
  145. // "Processing actor {0}, starting pos {1}, starting vel {2}", i, actorPosition, actorVelocity);
  146. actorPosition.X += actor.Velocity.X * timeStep;
  147. actorPosition.Y += actor.Velocity.Y * timeStep;
  148. if (actor.Position.Y < 0)
  149. {
  150. actorPosition.Y = 0.1F;
  151. }
  152. else if (actor.Position.Y >= m_regionExtent.Y)
  153. {
  154. actorPosition.Y = (m_regionExtent.Y - 0.1f);
  155. }
  156. if (actor.Position.X < 0)
  157. {
  158. actorPosition.X = 0.1F;
  159. }
  160. else if (actor.Position.X >= m_regionExtent.X)
  161. {
  162. actorPosition.X = (m_regionExtent.X - 0.1f);
  163. }
  164. float terrainHeight = 0;
  165. if (_heightMap != null)
  166. terrainHeight = _heightMap[(int)actor.Position.Y * (int)m_regionExtent.Y + (int)actor.Position.X];
  167. float height = terrainHeight + actor.Size.Z;
  168. // Console.WriteLine("height {0}, actorPosition {1}", height, actorPosition);
  169. if (actor.Flying)
  170. {
  171. if (actor.Position.Z + (actor.Velocity.Z * timeStep) < terrainHeight + 2)
  172. {
  173. actorPosition.Z = height;
  174. actorVelocity.Z = 0;
  175. actor.IsColliding = true;
  176. }
  177. else
  178. {
  179. actorPosition.Z += actor.Velocity.Z * timeStep;
  180. actor.IsColliding = false;
  181. }
  182. }
  183. else
  184. {
  185. actorPosition.Z = height;
  186. actorVelocity.Z = 0;
  187. actor.IsColliding = true;
  188. }
  189. actor.Position = actorPosition;
  190. actor.Velocity = actorVelocity;
  191. }
  192. return fps;
  193. }
  194. public override void SetTerrain(float[] heightMap)
  195. {
  196. _heightMap = heightMap;
  197. }
  198. public override void DeleteTerrain()
  199. {
  200. }
  201. public override void SetWaterLevel(float baseheight)
  202. {
  203. }
  204. public override Dictionary<uint, float> GetTopColliders()
  205. {
  206. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  207. return returncolliders;
  208. }
  209. }
  210. }