POSScene.cs 11 KB

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