BasicPhysicsPlugin.cs 11 KB

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