BasicPhysicsScene.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Physics.Manager;
  33. namespace OpenSim.Region.Physics.BasicPhysicsPlugin
  34. {
  35. /// <summary>
  36. /// This is an incomplete extremely basic physics implementation
  37. /// </summary>
  38. /// <remarks>
  39. /// Not useful for anything at the moment apart from some regression testing in other components where some form
  40. /// of physics plugin is needed.
  41. /// </remarks>
  42. public class BasicScene : PhysicsScene
  43. {
  44. private List<BasicActor> _actors = new List<BasicActor>();
  45. private List<BasicPhysicsPrim> _prims = new List<BasicPhysicsPrim>();
  46. private float[] _heightMap;
  47. private Vector3 m_regionExtent;
  48. //protected internal string sceneIdentifier;
  49. public BasicScene(string engineType, string _sceneIdentifier)
  50. {
  51. EngineType = engineType;
  52. Name = EngineType + "/" + _sceneIdentifier;
  53. //sceneIdentifier = _sceneIdentifier;
  54. }
  55. public override void Initialise(IMesher meshmerizer, IConfigSource config)
  56. {
  57. throw new Exception("Should not be called.");
  58. }
  59. public override void Initialise(IMesher meshmerizer, IConfigSource config, Vector3 regionExtent)
  60. {
  61. m_regionExtent = regionExtent;
  62. }
  63. public override void Dispose() {}
  64. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  65. Vector3 size, Quaternion rotation, bool isPhysical, uint localid)
  66. {
  67. BasicPhysicsPrim prim = new BasicPhysicsPrim(primName, localid, position, size, rotation, pbs);
  68. prim.IsPhysical = isPhysical;
  69. _prims.Add(prim);
  70. return prim;
  71. }
  72. public override PhysicsActor AddAvatar(string avName, Vector3 position, Vector3 velocity, Vector3 size, bool isFlying)
  73. {
  74. BasicActor act = new BasicActor(size);
  75. act.Position = position;
  76. act.Velocity = velocity;
  77. act.Flying = isFlying;
  78. _actors.Add(act);
  79. return act;
  80. }
  81. public override void RemovePrim(PhysicsActor actor)
  82. {
  83. BasicPhysicsPrim prim = (BasicPhysicsPrim)actor;
  84. if (_prims.Contains(prim))
  85. _prims.Remove(prim);
  86. }
  87. public override void RemoveAvatar(PhysicsActor actor)
  88. {
  89. BasicActor act = (BasicActor)actor;
  90. if (_actors.Contains(act))
  91. _actors.Remove(act);
  92. }
  93. public override void AddPhysicsActorTaint(PhysicsActor prim)
  94. {
  95. }
  96. public override float Simulate(float timeStep)
  97. {
  98. // Console.WriteLine("Simulating");
  99. float fps = 0;
  100. for (int i = 0; i < _actors.Count; ++i)
  101. {
  102. BasicActor actor = _actors[i];
  103. Vector3 actorPosition = actor.Position;
  104. Vector3 actorVelocity = actor.Velocity;
  105. // Console.WriteLine(
  106. // "Processing actor {0}, starting pos {1}, starting vel {2}", i, actorPosition, actorVelocity);
  107. actorPosition.X += actor.Velocity.X * timeStep;
  108. actorPosition.Y += actor.Velocity.Y * timeStep;
  109. if (actor.Position.Y < 0)
  110. {
  111. actorPosition.Y = 0.1F;
  112. }
  113. else if (actor.Position.Y >= m_regionExtent.Y)
  114. {
  115. actorPosition.Y = (m_regionExtent.Y - 0.1f);
  116. }
  117. if (actor.Position.X < 0)
  118. {
  119. actorPosition.X = 0.1F;
  120. }
  121. else if (actor.Position.X >= m_regionExtent.X)
  122. {
  123. actorPosition.X = (m_regionExtent.X - 0.1f);
  124. }
  125. float terrainHeight = 0;
  126. if (_heightMap != null)
  127. terrainHeight = _heightMap[(int)actor.Position.Y * (int)m_regionExtent.Y + (int)actor.Position.X];
  128. float height = terrainHeight + actor.Size.Z;
  129. // Console.WriteLine("height {0}, actorPosition {1}", height, actorPosition);
  130. if (actor.Flying)
  131. {
  132. if (actor.Position.Z + (actor.Velocity.Z * timeStep) < terrainHeight + 2)
  133. {
  134. actorPosition.Z = height;
  135. actorVelocity.Z = 0;
  136. actor.IsColliding = true;
  137. }
  138. else
  139. {
  140. actorPosition.Z += actor.Velocity.Z * timeStep;
  141. actor.IsColliding = false;
  142. }
  143. }
  144. else
  145. {
  146. actorPosition.Z = height;
  147. actorVelocity.Z = 0;
  148. actor.IsColliding = true;
  149. }
  150. actor.Position = actorPosition;
  151. actor.Velocity = actorVelocity;
  152. }
  153. return fps;
  154. }
  155. public override void GetResults()
  156. {
  157. }
  158. public override bool IsThreaded
  159. {
  160. get { return (false); // for now we won't be multithreaded
  161. }
  162. }
  163. public override void SetTerrain(float[] heightMap)
  164. {
  165. _heightMap = heightMap;
  166. }
  167. public override void DeleteTerrain()
  168. {
  169. }
  170. public override void SetWaterLevel(float baseheight)
  171. {
  172. }
  173. public override Dictionary<uint, float> GetTopColliders()
  174. {
  175. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  176. return returncolliders;
  177. }
  178. }
  179. }