POSScene.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 Mono.Addins;
  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.POS
  37. {
  38. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "POSPhysicsScene")]
  39. public class POSScene : PhysicsScene
  40. {
  41. private List<POSCharacter> _characters = new List<POSCharacter>();
  42. private List<POSPrim> _prims = new List<POSPrim>();
  43. private float[] _heightMap;
  44. private const float gravity = -9.8f;
  45. private bool m_Enabled = false;
  46. //protected internal string sceneIdentifier;
  47. #region INonSharedRegionModule
  48. public string Name
  49. {
  50. get { return "POS"; }
  51. }
  52. public Type ReplaceableInterface
  53. {
  54. get { return null; }
  55. }
  56. public void Initialise(IConfigSource source)
  57. {
  58. // TODO: Move this out of Startup
  59. IConfig config = source.Configs["Startup"];
  60. if (config != null)
  61. {
  62. string physics = config.GetString("physics", string.Empty);
  63. if (physics == Name)
  64. m_Enabled = true;
  65. }
  66. }
  67. public void Close()
  68. {
  69. }
  70. public void AddRegion(Scene scene)
  71. {
  72. if (!m_Enabled)
  73. return;
  74. EngineType = Name;
  75. PhysicsSceneName = EngineType + "/" + scene.RegionInfo.RegionName;
  76. scene.RegisterModuleInterface<PhysicsScene>(this);
  77. base.Initialise(scene.PhysicsRequestAsset, scene.Heightmap.GetFloatsSerialised(), (float)scene.RegionInfo.RegionSettings.WaterHeight);
  78. }
  79. public void RemoveRegion(Scene scene)
  80. {
  81. if (!m_Enabled)
  82. return;
  83. }
  84. public void RegionLoaded(Scene scene)
  85. {
  86. if (!m_Enabled)
  87. return;
  88. }
  89. #endregion
  90. public override void Dispose()
  91. {
  92. }
  93. public override PhysicsActor AddAvatar(
  94. string avName, Vector3 position, Vector3 velocity, Vector3 size, bool isFlying)
  95. {
  96. POSCharacter act = new POSCharacter();
  97. act.Position = position;
  98. act.Flying = isFlying;
  99. _characters.Add(act);
  100. return act;
  101. }
  102. public override void RemovePrim(PhysicsActor prim)
  103. {
  104. POSPrim p = (POSPrim) prim;
  105. if (_prims.Contains(p))
  106. {
  107. _prims.Remove(p);
  108. }
  109. }
  110. public override void RemoveAvatar(PhysicsActor character)
  111. {
  112. POSCharacter act = (POSCharacter) character;
  113. if (_characters.Contains(act))
  114. {
  115. _characters.Remove(act);
  116. }
  117. }
  118. /*
  119. public override PhysicsActor AddPrim(Vector3 position, Vector3 size, Quaternion rotation)
  120. {
  121. return null;
  122. }
  123. */
  124. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  125. Vector3 size, Quaternion rotation, bool isPhysical, uint localid)
  126. {
  127. POSPrim prim = new POSPrim();
  128. prim.Position = position;
  129. prim.Orientation = rotation;
  130. prim.Size = size;
  131. _prims.Add(prim);
  132. return prim;
  133. }
  134. private bool isColliding(POSCharacter c, POSPrim p)
  135. {
  136. Vector3 rotatedPos = new Vector3(c.Position.X - p.Position.X, c.Position.Y - p.Position.Y,
  137. c.Position.Z - p.Position.Z) * Quaternion.Inverse(p.Orientation);
  138. Vector3 avatarSize = new Vector3(c.Size.X, c.Size.Y, c.Size.Z) * Quaternion.Inverse(p.Orientation);
  139. return (Math.Abs(rotatedPos.X) < (p.Size.X*0.5 + Math.Abs(avatarSize.X)) &&
  140. Math.Abs(rotatedPos.Y) < (p.Size.Y*0.5 + Math.Abs(avatarSize.Y)) &&
  141. Math.Abs(rotatedPos.Z) < (p.Size.Z*0.5 + Math.Abs(avatarSize.Z)));
  142. }
  143. private bool isCollidingWithPrim(POSCharacter c)
  144. {
  145. foreach (POSPrim p in _prims)
  146. {
  147. if (isColliding(c, p))
  148. {
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. public override void AddPhysicsActorTaint(PhysicsActor prim)
  155. {
  156. }
  157. public override float Simulate(float timeStep)
  158. {
  159. float fps = 0;
  160. for (int i = 0; i < _characters.Count; ++i)
  161. {
  162. fps++;
  163. POSCharacter character = _characters[i];
  164. float oldposX = character.Position.X;
  165. float oldposY = character.Position.Y;
  166. float oldposZ = character.Position.Z;
  167. if (!character.Flying)
  168. {
  169. character._target_velocity.Z += gravity * timeStep;
  170. }
  171. Vector3 characterPosition = character.Position;
  172. characterPosition.X += character._target_velocity.X * timeStep;
  173. characterPosition.Y += character._target_velocity.Y * timeStep;
  174. characterPosition.X = Util.Clamp(character.Position.X, 0.01f, Constants.RegionSize - 0.01f);
  175. characterPosition.Y = Util.Clamp(character.Position.Y, 0.01f, Constants.RegionSize - 0.01f);
  176. bool forcedZ = false;
  177. float terrainheight = _heightMap[(int)character.Position.Y * Constants.RegionSize + (int)character.Position.X];
  178. if (character.Position.Z + (character._target_velocity.Z * timeStep) < terrainheight + 2)
  179. {
  180. characterPosition.Z = terrainheight + character.Size.Z;
  181. forcedZ = true;
  182. }
  183. else
  184. {
  185. characterPosition.Z += character._target_velocity.Z*timeStep;
  186. }
  187. /// this is it -- the magic you've all been waiting for! Ladies and gentlemen --
  188. /// Completely Bogus Collision Detection!!!
  189. /// better known as the CBCD algorithm
  190. if (isCollidingWithPrim(character))
  191. {
  192. characterPosition.Z = oldposZ; // first try Z axis
  193. if (isCollidingWithPrim(character))
  194. {
  195. characterPosition.Z = oldposZ + character.Size.Z / 4.4f; // try harder
  196. if (isCollidingWithPrim(character))
  197. {
  198. characterPosition.Z = oldposZ + character.Size.Z / 2.2f; // try very hard
  199. if (isCollidingWithPrim(character))
  200. {
  201. characterPosition.X = oldposX;
  202. characterPosition.Y = oldposY;
  203. characterPosition.Z = oldposZ;
  204. characterPosition.X += character._target_velocity.X * timeStep;
  205. if (isCollidingWithPrim(character))
  206. {
  207. characterPosition.X = oldposX;
  208. }
  209. characterPosition.Y += character._target_velocity.Y * timeStep;
  210. if (isCollidingWithPrim(character))
  211. {
  212. characterPosition.Y = oldposY;
  213. }
  214. }
  215. else
  216. {
  217. forcedZ = true;
  218. }
  219. }
  220. else
  221. {
  222. forcedZ = true;
  223. }
  224. }
  225. else
  226. {
  227. forcedZ = true;
  228. }
  229. }
  230. characterPosition.X = Util.Clamp(character.Position.X, 0.01f, Constants.RegionSize - 0.01f);
  231. characterPosition.Y = Util.Clamp(character.Position.Y, 0.01f, Constants.RegionSize - 0.01f);
  232. character.Position = characterPosition;
  233. character._velocity.X = (character.Position.X - oldposX)/timeStep;
  234. character._velocity.Y = (character.Position.Y - oldposY)/timeStep;
  235. if (forcedZ)
  236. {
  237. character._velocity.Z = 0;
  238. character._target_velocity.Z = 0;
  239. ((PhysicsActor)character).IsColliding = true;
  240. character.RequestPhysicsterseUpdate();
  241. }
  242. else
  243. {
  244. ((PhysicsActor)character).IsColliding = false;
  245. character._velocity.Z = (character.Position.Z - oldposZ)/timeStep;
  246. }
  247. }
  248. return fps;
  249. }
  250. public override void GetResults()
  251. {
  252. }
  253. public override bool IsThreaded
  254. {
  255. // for now we won't be multithreaded
  256. get { return (false); }
  257. }
  258. public override void SetTerrain(float[] heightMap)
  259. {
  260. _heightMap = heightMap;
  261. }
  262. public override void DeleteTerrain()
  263. {
  264. }
  265. public override void SetWaterLevel(float baseheight)
  266. {
  267. }
  268. public override Dictionary<uint, float> GetTopColliders()
  269. {
  270. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  271. return returncolliders;
  272. }
  273. }
  274. }