BasicPhysicsPlugin.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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, 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. }
  145. else
  146. {
  147. actor.Position.Z += actor.Velocity.Z*timeStep;
  148. }
  149. }
  150. else
  151. {
  152. actor.Position.Z = height;
  153. actor.Velocity.Z = 0;
  154. }
  155. }
  156. return fps;
  157. }
  158. public override void GetResults()
  159. {
  160. }
  161. public override bool IsThreaded
  162. {
  163. get { return (false); // for now we won't be multithreaded
  164. }
  165. }
  166. public override void SetTerrain(float[] heightMap)
  167. {
  168. _heightMap = heightMap;
  169. }
  170. public override void SetWaterLevel(float baseheight)
  171. {
  172. }
  173. public override void DeleteTerrain()
  174. {
  175. }
  176. public override Dictionary<uint, float> GetTopColliders()
  177. {
  178. Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
  179. return returncolliders;
  180. }
  181. }
  182. public class BasicActor : PhysicsActor
  183. {
  184. private PhysicsVector _position;
  185. private PhysicsVector _velocity;
  186. private PhysicsVector _acceleration;
  187. private PhysicsVector _size;
  188. private PhysicsVector m_rotationalVelocity = PhysicsVector.Zero;
  189. private bool flying;
  190. private bool iscolliding;
  191. public BasicActor()
  192. {
  193. _velocity = new PhysicsVector();
  194. _position = new PhysicsVector();
  195. _acceleration = new PhysicsVector();
  196. _size = new PhysicsVector();
  197. }
  198. public override int PhysicsActorType
  199. {
  200. get { return (int) ActorTypes.Agent; }
  201. set { return; }
  202. }
  203. public override PhysicsVector RotationalVelocity
  204. {
  205. get { return m_rotationalVelocity; }
  206. set { m_rotationalVelocity = value; }
  207. }
  208. public override bool SetAlwaysRun
  209. {
  210. get { return false; }
  211. set { return; }
  212. }
  213. public override uint LocalID
  214. {
  215. set { return; }
  216. }
  217. public override bool Grabbed
  218. {
  219. set { return; }
  220. }
  221. public override bool Selected
  222. {
  223. set { return; }
  224. }
  225. public override float Buoyancy
  226. {
  227. get { return 0f; }
  228. set { return; }
  229. }
  230. public override bool FloatOnWater
  231. {
  232. set { return; }
  233. }
  234. public override bool IsPhysical
  235. {
  236. get { return false; }
  237. set { return; }
  238. }
  239. public override bool ThrottleUpdates
  240. {
  241. get { return false; }
  242. set { return; }
  243. }
  244. public override bool Flying
  245. {
  246. get { return flying; }
  247. set { flying = value; }
  248. }
  249. public override bool IsColliding
  250. {
  251. get { return iscolliding; }
  252. set { iscolliding = value; }
  253. }
  254. public override bool CollidingGround
  255. {
  256. get { return false; }
  257. set { return; }
  258. }
  259. public override bool CollidingObj
  260. {
  261. get { return false; }
  262. set { return; }
  263. }
  264. public override bool Stopped
  265. {
  266. get { return false; }
  267. }
  268. public override PhysicsVector Position
  269. {
  270. get { return _position; }
  271. set { _position = value; }
  272. }
  273. public override PhysicsVector Size
  274. {
  275. get { return _size; }
  276. set {
  277. _size = value;
  278. _size.Z = _size.Z / 2.0f;
  279. }
  280. }
  281. public override PrimitiveBaseShape Shape
  282. {
  283. set { return; }
  284. }
  285. public override float Mass
  286. {
  287. get { return 0f; }
  288. }
  289. public override PhysicsVector Force
  290. {
  291. get { return PhysicsVector.Zero; }
  292. set { return; }
  293. }
  294. public override int VehicleType
  295. {
  296. get { return 0; }
  297. set { return; }
  298. }
  299. public override void VehicleFloatParam(int param, float value)
  300. {
  301. }
  302. public override void VehicleVectorParam(int param, PhysicsVector value)
  303. {
  304. }
  305. public override void VehicleRotationParam(int param, Quaternion rotation)
  306. {
  307. }
  308. public override void SetVolumeDetect(int param)
  309. {
  310. }
  311. public override PhysicsVector CenterOfMass
  312. {
  313. get { return PhysicsVector.Zero; }
  314. }
  315. public override PhysicsVector GeometricCenter
  316. {
  317. get { return PhysicsVector.Zero; }
  318. }
  319. public override PhysicsVector Velocity
  320. {
  321. get { return _velocity; }
  322. set { _velocity = value; }
  323. }
  324. public override PhysicsVector Torque
  325. {
  326. get { return PhysicsVector.Zero; }
  327. set { return; }
  328. }
  329. public override float CollisionScore
  330. {
  331. get { return 0f; }
  332. set { }
  333. }
  334. public override Quaternion Orientation
  335. {
  336. get { return Quaternion.Identity; }
  337. set { }
  338. }
  339. public override PhysicsVector Acceleration
  340. {
  341. get { return _acceleration; }
  342. }
  343. public override bool Kinematic
  344. {
  345. get { return true; }
  346. set { }
  347. }
  348. public override void link(PhysicsActor obj)
  349. {
  350. }
  351. public override void delink()
  352. {
  353. }
  354. public override void LockAngularMotion(PhysicsVector axis)
  355. {
  356. }
  357. public void SetAcceleration(PhysicsVector accel)
  358. {
  359. _acceleration = accel;
  360. }
  361. public override void AddForce(PhysicsVector force, bool pushforce)
  362. {
  363. }
  364. public override void AddAngularForce(PhysicsVector force, bool pushforce)
  365. {
  366. }
  367. public override void SetMomentum(PhysicsVector momentum)
  368. {
  369. }
  370. public override void CrossingFailure()
  371. {
  372. }
  373. public override PhysicsVector PIDTarget { set { return; } }
  374. public override bool PIDActive { set { return; } }
  375. public override float PIDTau { set { return; } }
  376. public override float PIDHoverHeight { set { return; } }
  377. public override bool PIDHoverActive { set { return; } }
  378. public override PIDHoverType PIDHoverType { set { return; } }
  379. public override float PIDHoverTau { set { return; } }
  380. public override void SubscribeEvents(int ms)
  381. {
  382. }
  383. public override void UnSubscribeEvents()
  384. {
  385. }
  386. public override bool SubscribedEvents()
  387. {
  388. return false;
  389. }
  390. }
  391. }