BasicPhysicsPlugin.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 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 DeleteTerrain()
  168. {
  169. }
  170. }
  171. public class BasicActor : PhysicsActor
  172. {
  173. private PhysicsVector _position;
  174. private PhysicsVector _velocity;
  175. private PhysicsVector _acceleration;
  176. private PhysicsVector m_rotationalVelocity = PhysicsVector.Zero;
  177. private bool flying;
  178. private bool iscolliding;
  179. public BasicActor()
  180. {
  181. _velocity = new PhysicsVector();
  182. _position = new PhysicsVector();
  183. _acceleration = new PhysicsVector();
  184. }
  185. public override int PhysicsActorType
  186. {
  187. get { return (int) ActorTypes.Agent; }
  188. set { return; }
  189. }
  190. public override PhysicsVector RotationalVelocity
  191. {
  192. get { return m_rotationalVelocity; }
  193. set { m_rotationalVelocity = value; }
  194. }
  195. public override bool SetAlwaysRun
  196. {
  197. get { return false; }
  198. set { return; }
  199. }
  200. public override bool Grabbed
  201. {
  202. set { return; }
  203. }
  204. public override bool Selected
  205. {
  206. set { return; }
  207. }
  208. public override bool IsPhysical
  209. {
  210. get { return false; }
  211. set { return; }
  212. }
  213. public override bool ThrottleUpdates
  214. {
  215. get { return false; }
  216. set { return; }
  217. }
  218. public override bool Flying
  219. {
  220. get { return flying; }
  221. set { flying = value; }
  222. }
  223. public override bool IsColliding
  224. {
  225. get { return iscolliding; }
  226. set { iscolliding = value; }
  227. }
  228. public override bool CollidingGround
  229. {
  230. get { return false; }
  231. set { return; }
  232. }
  233. public override bool CollidingObj
  234. {
  235. get { return false; }
  236. set { return; }
  237. }
  238. public override bool Stopped
  239. {
  240. get { return false; }
  241. }
  242. public override PhysicsVector Position
  243. {
  244. get { return _position; }
  245. set { _position = value; }
  246. }
  247. public override PhysicsVector Size
  248. {
  249. get { return PhysicsVector.Zero; }
  250. set { }
  251. }
  252. public override PrimitiveBaseShape Shape
  253. {
  254. set { return; }
  255. }
  256. public override float Mass
  257. {
  258. get { return 0f; }
  259. }
  260. public override PhysicsVector Force
  261. {
  262. get { return PhysicsVector.Zero; }
  263. }
  264. public override PhysicsVector CenterOfMass
  265. {
  266. get { return PhysicsVector.Zero; }
  267. }
  268. public override PhysicsVector GeometricCenter
  269. {
  270. get { return PhysicsVector.Zero; }
  271. }
  272. public override PhysicsVector Velocity
  273. {
  274. get { return _velocity; }
  275. set { _velocity = value; }
  276. }
  277. public override float CollisionScore
  278. {
  279. get { return 0f; }
  280. }
  281. public override Quaternion Orientation
  282. {
  283. get { return Quaternion.Identity; }
  284. set { }
  285. }
  286. public override PhysicsVector Acceleration
  287. {
  288. get { return _acceleration; }
  289. }
  290. public override bool Kinematic
  291. {
  292. get { return true; }
  293. set { }
  294. }
  295. public void SetAcceleration(PhysicsVector accel)
  296. {
  297. _acceleration = accel;
  298. }
  299. public override void AddForce(PhysicsVector force)
  300. {
  301. }
  302. public override void SetMomentum(PhysicsVector momentum)
  303. {
  304. }
  305. public override void CrossingFailure()
  306. {
  307. }
  308. }
  309. }