BasicPhysicsPlugin.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 OpenSim 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.Collections.Generic;
  28. using Nini.Config;
  29. using OpenMetaverse;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Physics.Manager;
  32. namespace OpenSim.Region.Physics.BasicPhysicsPlugin
  33. {
  34. /// <summary>
  35. /// Effectively a physics plugin that simulates no physics at all.
  36. /// </summary>
  37. public class BasicPhysicsPlugin : IPhysicsPlugin
  38. {
  39. public BasicPhysicsPlugin()
  40. {
  41. }
  42. public bool Init()
  43. {
  44. return true;
  45. }
  46. public PhysicsScene GetScene(string sceneIdentifier)
  47. {
  48. return new BasicScene(sceneIdentifier);
  49. }
  50. public string GetName()
  51. {
  52. return ("basicphysics");
  53. }
  54. public void Dispose()
  55. {
  56. }
  57. }
  58. public class BasicScene : PhysicsScene
  59. {
  60. private List<BasicActor> _actors = new List<BasicActor>();
  61. private float[] _heightMap;
  62. //protected internal string sceneIdentifier;
  63. public BasicScene(string _sceneIdentifier)
  64. {
  65. //sceneIdentifier = _sceneIdentifier;
  66. }
  67. public override void Initialise(IMesher meshmerizer, IConfigSource config)
  68. {
  69. // Does nothing right now
  70. }
  71. public override void Dispose()
  72. {
  73. }
  74. public override PhysicsActor AddAvatar(string avName, PhysicsVector position, PhysicsVector size)
  75. {
  76. BasicActor act = new BasicActor();
  77. act.Position = position;
  78. _actors.Add(act);
  79. return act;
  80. }
  81. public override void RemovePrim(PhysicsActor prim)
  82. {
  83. }
  84. public override void RemoveAvatar(PhysicsActor actor)
  85. {
  86. BasicActor act = (BasicActor) actor;
  87. if (_actors.Contains(act))
  88. {
  89. _actors.Remove(act);
  90. }
  91. }
  92. /*
  93. public override PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size, Quaternion rotation)
  94. {
  95. return null;
  96. }
  97. */
  98. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  99. PhysicsVector size, Quaternion rotation)
  100. {
  101. return AddPrimShape(primName, pbs, position, size, rotation, false);
  102. }
  103. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  104. PhysicsVector size, Quaternion rotation, bool isPhysical)
  105. {
  106. return null;
  107. }
  108. public override void AddPhysicsActorTaint(PhysicsActor prim)
  109. {
  110. }
  111. public override float Simulate(float timeStep)
  112. {
  113. float fps = 0;
  114. for (int i = 0; i < _actors.Count; ++i)
  115. {
  116. BasicActor actor = _actors[i];
  117. actor.Position.X += actor.Velocity.X*timeStep;
  118. actor.Position.Y += actor.Velocity.Y*timeStep;
  119. if (actor.Position.Y < 0)
  120. {
  121. actor.Position.Y = 0.1F;
  122. }
  123. else if (actor.Position.Y >= Constants.RegionSize)
  124. {
  125. actor.Position.Y = 255.9F;
  126. }
  127. if (actor.Position.X < 0)
  128. {
  129. actor.Position.X = 0.1F;
  130. }
  131. else if (actor.Position.X >= Constants.RegionSize)
  132. {
  133. actor.Position.X = 255.9F;
  134. }
  135. float height = _heightMap[(int)actor.Position.Y * Constants.RegionSize + (int)actor.Position.X] + actor.Size.Z;
  136. if (actor.Flying)
  137. {
  138. if (actor.Position.Z + (actor.Velocity.Z*timeStep) <
  139. _heightMap[(int)actor.Position.Y * Constants.RegionSize + (int)actor.Position.X] + 2)
  140. {
  141. actor.Position.Z = height;
  142. actor.Velocity.Z = 0;
  143. }
  144. else
  145. {
  146. actor.Position.Z += actor.Velocity.Z*timeStep;
  147. }
  148. }
  149. else
  150. {
  151. actor.Position.Z = height;
  152. actor.Velocity.Z = 0;
  153. }
  154. }
  155. return fps;
  156. }
  157. public override void GetResults()
  158. {
  159. }
  160. public override bool IsThreaded
  161. {
  162. get { return (false); // for now we won't be multithreaded
  163. }
  164. }
  165. public override void SetTerrain(float[] heightMap)
  166. {
  167. _heightMap = heightMap;
  168. }
  169. public override void SetWaterLevel(float baseheight)
  170. {
  171. }
  172. public override void DeleteTerrain()
  173. {
  174. }
  175. public override Dictionary<uint, float> GetTopColliders()
  176. {
  177. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  178. return returncolliders;
  179. }
  180. }
  181. public class BasicActor : PhysicsActor
  182. {
  183. private PhysicsVector _position;
  184. private PhysicsVector _velocity;
  185. private PhysicsVector _acceleration;
  186. private PhysicsVector _size;
  187. private PhysicsVector m_rotationalVelocity = PhysicsVector.Zero;
  188. private bool flying;
  189. private bool iscolliding;
  190. public BasicActor()
  191. {
  192. _velocity = new PhysicsVector();
  193. _position = new PhysicsVector();
  194. _acceleration = new PhysicsVector();
  195. _size = new PhysicsVector();
  196. }
  197. public override int PhysicsActorType
  198. {
  199. get { return (int) ActorTypes.Agent; }
  200. set { return; }
  201. }
  202. public override PhysicsVector RotationalVelocity
  203. {
  204. get { return m_rotationalVelocity; }
  205. set { m_rotationalVelocity = value; }
  206. }
  207. public override bool SetAlwaysRun
  208. {
  209. get { return false; }
  210. set { return; }
  211. }
  212. public override uint LocalID
  213. {
  214. set { return; }
  215. }
  216. public override bool Grabbed
  217. {
  218. set { return; }
  219. }
  220. public override bool Selected
  221. {
  222. set { return; }
  223. }
  224. public override float Buoyancy
  225. {
  226. get { return 0f; }
  227. set { return; }
  228. }
  229. public override bool FloatOnWater
  230. {
  231. set { return; }
  232. }
  233. public override bool IsPhysical
  234. {
  235. get { return false; }
  236. set { return; }
  237. }
  238. public override bool ThrottleUpdates
  239. {
  240. get { return false; }
  241. set { return; }
  242. }
  243. public override bool Flying
  244. {
  245. get { return flying; }
  246. set { flying = value; }
  247. }
  248. public override bool IsColliding
  249. {
  250. get { return iscolliding; }
  251. set { iscolliding = value; }
  252. }
  253. public override bool CollidingGround
  254. {
  255. get { return false; }
  256. set { return; }
  257. }
  258. public override bool CollidingObj
  259. {
  260. get { return false; }
  261. set { return; }
  262. }
  263. public override bool Stopped
  264. {
  265. get { return false; }
  266. }
  267. public override PhysicsVector Position
  268. {
  269. get { return _position; }
  270. set { _position = value; }
  271. }
  272. public override PhysicsVector Size
  273. {
  274. get { return _size; }
  275. set {
  276. _size = value;
  277. _size.Z = _size.Z / 2.0f;
  278. }
  279. }
  280. public override PrimitiveBaseShape Shape
  281. {
  282. set { return; }
  283. }
  284. public override float Mass
  285. {
  286. get { return 0f; }
  287. }
  288. public override PhysicsVector Force
  289. {
  290. get { return PhysicsVector.Zero; }
  291. set { return; }
  292. }
  293. public override int VehicleType
  294. {
  295. get { return 0; }
  296. set { return; }
  297. }
  298. public override void VehicleFloatParam(int param, float value)
  299. {
  300. }
  301. public override void VehicleVectorParam(int param, PhysicsVector value)
  302. {
  303. }
  304. public override void VehicleRotationParam(int param, Quaternion rotation)
  305. {
  306. }
  307. public override void SetVolumeDetect(int param)
  308. {
  309. }
  310. public override PhysicsVector CenterOfMass
  311. {
  312. get { return PhysicsVector.Zero; }
  313. }
  314. public override PhysicsVector GeometricCenter
  315. {
  316. get { return PhysicsVector.Zero; }
  317. }
  318. public override PhysicsVector Velocity
  319. {
  320. get { return _velocity; }
  321. set { _velocity = value; }
  322. }
  323. public override PhysicsVector Torque
  324. {
  325. get { return PhysicsVector.Zero; }
  326. set { return; }
  327. }
  328. public override float CollisionScore
  329. {
  330. get { return 0f; }
  331. set { }
  332. }
  333. public override Quaternion Orientation
  334. {
  335. get { return Quaternion.Identity; }
  336. set { }
  337. }
  338. public override PhysicsVector Acceleration
  339. {
  340. get { return _acceleration; }
  341. }
  342. public override bool Kinematic
  343. {
  344. get { return true; }
  345. set { }
  346. }
  347. public override void link(PhysicsActor obj)
  348. {
  349. }
  350. public override void delink()
  351. {
  352. }
  353. public override void LockAngularMotion(PhysicsVector axis)
  354. {
  355. }
  356. public void SetAcceleration(PhysicsVector accel)
  357. {
  358. _acceleration = accel;
  359. }
  360. public override void AddForce(PhysicsVector force, bool pushforce)
  361. {
  362. }
  363. public override void AddAngularForce(PhysicsVector force, bool pushforce)
  364. {
  365. }
  366. public override void SetMomentum(PhysicsVector momentum)
  367. {
  368. }
  369. public override void CrossingFailure()
  370. {
  371. }
  372. public override PhysicsVector PIDTarget { set { return; } }
  373. public override bool PIDActive { set { return; } }
  374. public override float PIDTau { set { return; } }
  375. public override void SubscribeEvents(int ms)
  376. {
  377. }
  378. public override void UnSubscribeEvents()
  379. {
  380. }
  381. public override bool SubscribedEvents()
  382. {
  383. return false;
  384. }
  385. }
  386. }