BasicPhysicsPlugin.cs 10 KB

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