BasicPhysicsPlugin.cs 13 KB

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