POSScene.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.POSPlugin
  34. {
  35. public class POSScene : PhysicsScene
  36. {
  37. private List<POSCharacter> _characters = new List<POSCharacter>();
  38. private List<POSPrim> _prims = new List<POSPrim>();
  39. private float[] _heightMap;
  40. private const float gravity = -9.8f;
  41. //protected internal string sceneIdentifier;
  42. public POSScene(String _sceneIdentifier)
  43. {
  44. //sceneIdentifier = _sceneIdentifier;
  45. }
  46. public override void Initialise(IMesher meshmerizer, IConfigSource config)
  47. {
  48. }
  49. public override void Dispose()
  50. {
  51. }
  52. public override PhysicsActor AddAvatar(string avName, PhysicsVector position, PhysicsVector size, bool isFlying)
  53. {
  54. POSCharacter act = new POSCharacter();
  55. act.Position = position;
  56. act.Flying = isFlying;
  57. _characters.Add(act);
  58. return act;
  59. }
  60. public override void SetWaterLevel(float baseheight)
  61. {
  62. }
  63. public override void RemovePrim(PhysicsActor prim)
  64. {
  65. POSPrim p = (POSPrim) prim;
  66. if (_prims.Contains(p))
  67. {
  68. _prims.Remove(p);
  69. }
  70. }
  71. public override void RemoveAvatar(PhysicsActor character)
  72. {
  73. POSCharacter act = (POSCharacter) character;
  74. if (_characters.Contains(act))
  75. {
  76. _characters.Remove(act);
  77. }
  78. }
  79. /*
  80. public override PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size, Quaternion rotation)
  81. {
  82. return null;
  83. }
  84. */
  85. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  86. PhysicsVector size, Quaternion rotation)
  87. {
  88. return AddPrimShape(primName, pbs, position, size, rotation, false);
  89. }
  90. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  91. PhysicsVector size, Quaternion rotation, bool isPhysical)
  92. {
  93. POSPrim prim = new POSPrim();
  94. prim.Position = position;
  95. prim.Orientation = rotation;
  96. prim.Size = size;
  97. _prims.Add(prim);
  98. return prim;
  99. }
  100. private bool isColliding(POSCharacter c, POSPrim p)
  101. {
  102. Vector3 rotatedPos = new Vector3(c.Position.X - p.Position.X, c.Position.Y - p.Position.Y,
  103. c.Position.Z - p.Position.Z) * Quaternion.Inverse(p.Orientation);
  104. Vector3 avatarSize = new Vector3(c.Size.X, c.Size.Y, c.Size.Z) * Quaternion.Inverse(p.Orientation);
  105. if (Math.Abs(rotatedPos.X) >= (p.Size.X*0.5 + Math.Abs(avatarSize.X)) ||
  106. Math.Abs(rotatedPos.Y) >= (p.Size.Y*0.5 + Math.Abs(avatarSize.Y)) ||
  107. Math.Abs(rotatedPos.Z) >= (p.Size.Z*0.5 + Math.Abs(avatarSize.Z)))
  108. {
  109. return false;
  110. }
  111. return true;
  112. }
  113. private bool isCollidingWithPrim(POSCharacter c)
  114. {
  115. for (int i = 0; i < _prims.Count; ++i)
  116. {
  117. if (isColliding(c, _prims[i]))
  118. {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. public override void AddPhysicsActorTaint(PhysicsActor prim)
  125. {
  126. }
  127. public override float Simulate(float timeStep)
  128. {
  129. float fps = 0;
  130. for (int i = 0; i < _characters.Count; ++i)
  131. {
  132. fps++;
  133. POSCharacter character = _characters[i];
  134. float oldposX = character.Position.X;
  135. float oldposY = character.Position.Y;
  136. float oldposZ = character.Position.Z;
  137. if (!character.Flying)
  138. {
  139. character._target_velocity.Z += gravity * timeStep;
  140. }
  141. character.Position.X += character._target_velocity.X * timeStep;
  142. character.Position.Y += character._target_velocity.Y * timeStep;
  143. character.Position.X = Util.Clamp(character.Position.X, 0.01f, Constants.RegionSize - 0.01f);
  144. character.Position.Y = Util.Clamp(character.Position.Y, 0.01f, Constants.RegionSize - 0.01f);
  145. bool forcedZ = false;
  146. float terrainheight = _heightMap[(int)character.Position.Y * Constants.RegionSize + (int)character.Position.X];
  147. if (character.Position.Z + (character._target_velocity.Z * timeStep) < terrainheight + 2)
  148. {
  149. character.Position.Z = terrainheight + character.Size.Z;
  150. forcedZ = true;
  151. }
  152. else
  153. {
  154. character.Position.Z += character._target_velocity.Z*timeStep;
  155. }
  156. /// this is it -- the magic you've all been waiting for! Ladies and gentlemen --
  157. /// Completely Bogus Collision Detection!!!
  158. /// better known as the CBCD algorithm
  159. if (isCollidingWithPrim(character))
  160. {
  161. character.Position.Z = oldposZ; // first try Z axis
  162. if (isCollidingWithPrim(character))
  163. {
  164. character.Position.Z = oldposZ + character.Size.Z / 4.4f; // try harder
  165. if (isCollidingWithPrim(character))
  166. {
  167. character.Position.Z = oldposZ + character.Size.Z / 2.2f; // try very hard
  168. if (isCollidingWithPrim(character))
  169. {
  170. character.Position.X = oldposX;
  171. character.Position.Y = oldposY;
  172. character.Position.Z = oldposZ;
  173. character.Position.X += character._target_velocity.X * timeStep;
  174. if (isCollidingWithPrim(character))
  175. {
  176. character.Position.X = oldposX;
  177. }
  178. character.Position.Y += character._target_velocity.Y * timeStep;
  179. if (isCollidingWithPrim(character))
  180. {
  181. character.Position.Y = oldposY;
  182. }
  183. }
  184. else
  185. {
  186. forcedZ = true;
  187. }
  188. }
  189. else
  190. {
  191. forcedZ = true;
  192. }
  193. }
  194. else
  195. {
  196. forcedZ = true;
  197. }
  198. }
  199. character.Position.X = Util.Clamp(character.Position.X, 0.01f, Constants.RegionSize - 0.01f);
  200. character.Position.Y = Util.Clamp(character.Position.Y, 0.01f, Constants.RegionSize - 0.01f);
  201. character._velocity.X = (character.Position.X - oldposX)/timeStep;
  202. character._velocity.Y = (character.Position.Y - oldposY)/timeStep;
  203. if (forcedZ)
  204. {
  205. character._velocity.Z = 0;
  206. character._target_velocity.Z = 0;
  207. ((PhysicsActor)character).IsColliding = true;
  208. character.RequestPhysicsterseUpdate();
  209. }
  210. else
  211. {
  212. ((PhysicsActor)character).IsColliding = false;
  213. character._velocity.Z = (character.Position.Z - oldposZ)/timeStep;
  214. }
  215. }
  216. return fps;
  217. }
  218. public override void GetResults()
  219. {
  220. }
  221. public override bool IsThreaded
  222. {
  223. // for now we won't be multithreaded
  224. get { return (false); }
  225. }
  226. public override void SetTerrain(float[] heightMap)
  227. {
  228. _heightMap = heightMap;
  229. }
  230. public override void DeleteTerrain()
  231. {
  232. }
  233. public override Dictionary<uint, float> GetTopColliders()
  234. {
  235. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  236. return returncolliders;
  237. }
  238. }
  239. }