1
0

POSScene.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 float Simulate(float timeStep)
  162. {
  163. float fps = 0;
  164. for (int i = 0; i < _characters.Count; ++i)
  165. {
  166. fps++;
  167. POSCharacter character = _characters[i];
  168. float oldposX = character.Position.X;
  169. float oldposY = character.Position.Y;
  170. float oldposZ = character.Position.Z;
  171. if (!character.Flying)
  172. {
  173. character._target_velocity.Z += gravity * timeStep;
  174. }
  175. Vector3 characterPosition = character.Position;
  176. characterPosition.X += character._target_velocity.X * timeStep;
  177. characterPosition.Y += character._target_velocity.Y * timeStep;
  178. characterPosition.X = Util.Clamp(character.Position.X, 0.01f, Constants.RegionSize - 0.01f);
  179. characterPosition.Y = Util.Clamp(character.Position.Y, 0.01f, Constants.RegionSize - 0.01f);
  180. bool forcedZ = false;
  181. float terrainheight = _heightMap[(int)character.Position.Y * Constants.RegionSize + (int)character.Position.X];
  182. if (character.Position.Z + (character._target_velocity.Z * timeStep) < terrainheight + 2)
  183. {
  184. characterPosition.Z = terrainheight + character.Size.Z;
  185. forcedZ = true;
  186. }
  187. else
  188. {
  189. characterPosition.Z += character._target_velocity.Z*timeStep;
  190. }
  191. /// this is it -- the magic you've all been waiting for! Ladies and gentlemen --
  192. /// Completely Bogus Collision Detection!!!
  193. /// better known as the CBCD algorithm
  194. if (isCollidingWithPrim(character))
  195. {
  196. characterPosition.Z = oldposZ; // first try Z axis
  197. if (isCollidingWithPrim(character))
  198. {
  199. characterPosition.Z = oldposZ + character.Size.Z / 4.4f; // try harder
  200. if (isCollidingWithPrim(character))
  201. {
  202. characterPosition.Z = oldposZ + character.Size.Z / 2.2f; // try very hard
  203. if (isCollidingWithPrim(character))
  204. {
  205. characterPosition.X = oldposX;
  206. characterPosition.Y = oldposY;
  207. characterPosition.Z = oldposZ;
  208. characterPosition.X += character._target_velocity.X * timeStep;
  209. if (isCollidingWithPrim(character))
  210. {
  211. characterPosition.X = oldposX;
  212. }
  213. characterPosition.Y += character._target_velocity.Y * timeStep;
  214. if (isCollidingWithPrim(character))
  215. {
  216. characterPosition.Y = oldposY;
  217. }
  218. }
  219. else
  220. {
  221. forcedZ = true;
  222. }
  223. }
  224. else
  225. {
  226. forcedZ = true;
  227. }
  228. }
  229. else
  230. {
  231. forcedZ = true;
  232. }
  233. }
  234. characterPosition.X = Util.Clamp(character.Position.X, 0.01f, Constants.RegionSize - 0.01f);
  235. characterPosition.Y = Util.Clamp(character.Position.Y, 0.01f, Constants.RegionSize - 0.01f);
  236. character.Position = characterPosition;
  237. character._velocity.X = (character.Position.X - oldposX)/timeStep;
  238. character._velocity.Y = (character.Position.Y - oldposY)/timeStep;
  239. if (forcedZ)
  240. {
  241. character._velocity.Z = 0;
  242. character._target_velocity.Z = 0;
  243. ((PhysicsActor)character).IsColliding = true;
  244. character.RequestPhysicsterseUpdate();
  245. }
  246. else
  247. {
  248. ((PhysicsActor)character).IsColliding = false;
  249. character._velocity.Z = (character.Position.Z - oldposZ)/timeStep;
  250. }
  251. }
  252. return 1.0f;
  253. }
  254. public override void SetTerrain(float[] heightMap)
  255. {
  256. _heightMap = heightMap;
  257. }
  258. public override void DeleteTerrain()
  259. {
  260. }
  261. public override void SetWaterLevel(float baseheight)
  262. {
  263. }
  264. public override Dictionary<uint, float> GetTopColliders()
  265. {
  266. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  267. return returncolliders;
  268. }
  269. }
  270. }