BasicPhysicsPlugin.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. */
  28. using System.Collections.Generic;
  29. using Axiom.Math;
  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)
  66. {
  67. // Does nothing right now
  68. }
  69. public override PhysicsActor AddAvatar(string avName, PhysicsVector position, uint localID)
  70. {
  71. BasicActor act = new BasicActor();
  72. act.Position = position;
  73. _actors.Add(act);
  74. return act;
  75. }
  76. public override void RemovePrim(PhysicsActor prim)
  77. {
  78. }
  79. public override void RemoveAvatar(PhysicsActor actor)
  80. {
  81. BasicActor act = (BasicActor) actor;
  82. if (_actors.Contains(act))
  83. {
  84. _actors.Remove(act);
  85. }
  86. }
  87. /*
  88. public override PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size, Quaternion rotation)
  89. {
  90. return null;
  91. }
  92. */
  93. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  94. PhysicsVector size, Quaternion rotation, uint LocalID)
  95. {
  96. return AddPrimShape(primName, pbs, position, size, rotation, false, LocalID);
  97. }
  98. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  99. PhysicsVector size, Quaternion rotation, bool isPhysical, uint localID) // rex, modified
  100. {
  101. return null;
  102. }
  103. public override void AddPhysicsActorTaint(PhysicsActor prim)
  104. {
  105. }
  106. public override float Simulate(float timeStep)
  107. {
  108. float fps = 0;
  109. for (int i = 0; i < _actors.Count; ++i)
  110. {
  111. BasicActor actor = _actors[i];
  112. actor.Position.X += actor.Velocity.X*timeStep;
  113. actor.Position.Y += actor.Velocity.Y*timeStep;
  114. if (actor.Position.Y < 0)
  115. {
  116. actor.Position.Y = 0.1F;
  117. }
  118. else if (actor.Position.Y >= 256)
  119. {
  120. actor.Position.Y = 255.9F;
  121. }
  122. if (actor.Position.X < 0)
  123. {
  124. actor.Position.X = 0.1F;
  125. }
  126. else if (actor.Position.X >= 256)
  127. {
  128. actor.Position.X = 255.9F;
  129. }
  130. float height = _heightMap[(int) actor.Position.Y*256 + (int) actor.Position.X] + 1.0f;
  131. if (actor.Flying)
  132. {
  133. if (actor.Position.Z + (actor.Velocity.Z*timeStep) <
  134. _heightMap[(int) actor.Position.Y*256 + (int) actor.Position.X] + 2)
  135. {
  136. actor.Position.Z = height;
  137. actor.Velocity.Z = 0;
  138. }
  139. else
  140. {
  141. actor.Position.Z += actor.Velocity.Z*timeStep;
  142. }
  143. }
  144. else
  145. {
  146. actor.Position.Z = height;
  147. actor.Velocity.Z = 0;
  148. }
  149. }
  150. return fps;
  151. }
  152. public override void GetResults()
  153. {
  154. }
  155. public override bool IsThreaded
  156. {
  157. get { return (false); // for now we won't be multithreaded
  158. }
  159. }
  160. public override void SetTerrain(float[] heightMap)
  161. {
  162. _heightMap = heightMap;
  163. }
  164. public override void DeleteTerrain()
  165. {
  166. }
  167. }
  168. public class BasicActor : PhysicsActor
  169. {
  170. private PhysicsVector _position;
  171. private PhysicsVector _velocity;
  172. private PhysicsVector _acceleration;
  173. private PhysicsVector m_rotationalVelocity = PhysicsVector.Zero;
  174. private bool flying;
  175. private bool iscolliding;
  176. public BasicActor()
  177. {
  178. _velocity = new PhysicsVector();
  179. _position = new PhysicsVector();
  180. _acceleration = new PhysicsVector();
  181. }
  182. public override int PhysicsActorType
  183. {
  184. get { return (int) ActorTypes.Agent; }
  185. set { return; }
  186. }
  187. public override PhysicsVector RotationalVelocity
  188. {
  189. get { return m_rotationalVelocity; }
  190. set { m_rotationalVelocity = value; }
  191. }
  192. public override bool SetAlwaysRun
  193. {
  194. get { return false; }
  195. set { return; }
  196. }
  197. public override bool IsPhysical
  198. {
  199. get { return false; }
  200. set { return; }
  201. }
  202. public override bool ThrottleUpdates
  203. {
  204. get { return false; }
  205. set { return; }
  206. }
  207. public override bool Flying
  208. {
  209. get { return flying; }
  210. set { flying = value; }
  211. }
  212. public override bool IsColliding
  213. {
  214. get { return iscolliding; }
  215. set { iscolliding = value; }
  216. }
  217. public override bool CollidingGround
  218. {
  219. get { return false; }
  220. set { return; }
  221. }
  222. public override bool CollidingObj
  223. {
  224. get { return false; }
  225. set { return; }
  226. }
  227. public override PhysicsVector Position
  228. {
  229. get { return _position; }
  230. set { _position = value; }
  231. }
  232. public override PhysicsVector Size
  233. {
  234. get { return PhysicsVector.Zero; }
  235. set { }
  236. }
  237. public override PrimitiveBaseShape Shape
  238. {
  239. set { return; }
  240. }
  241. public override float Mass
  242. {
  243. get { return 0f; }
  244. }
  245. public override PhysicsVector Force
  246. {
  247. get { return PhysicsVector.Zero; }
  248. }
  249. public override PhysicsVector CenterOfMass
  250. {
  251. get { return PhysicsVector.Zero; }
  252. }
  253. public override PhysicsVector GeometricCenter
  254. {
  255. get { return PhysicsVector.Zero; }
  256. }
  257. public override PhysicsVector Velocity
  258. {
  259. get { return _velocity; }
  260. set { _velocity = value; }
  261. }
  262. public override Quaternion Orientation
  263. {
  264. get { return Quaternion.Identity; }
  265. set { }
  266. }
  267. public override PhysicsVector Acceleration
  268. {
  269. get { return _acceleration; }
  270. }
  271. public override bool Kinematic
  272. {
  273. get { return true; }
  274. set { }
  275. }
  276. public void SetAcceleration(PhysicsVector accel)
  277. {
  278. _acceleration = accel;
  279. }
  280. public override void AddForce(PhysicsVector force)
  281. {
  282. }
  283. public override void SetMomentum(PhysicsVector momentum)
  284. {
  285. }
  286. }
  287. }