POSScene.cs 12 KB

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