BasicPhysicsPlugin.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) OpenSim project, http://sim.opensecondlife.org/
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the <organization> nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using OpenSim.Physics.Manager;
  30. namespace OpenSim.Physics.BasicPhysicsPlugin
  31. {
  32. /// <summary>
  33. /// Will be the PhysX plugin but for now will be a very basic physics engine
  34. /// </summary>
  35. public class BasicPhysicsPlugin : IPhysicsPlugin
  36. {
  37. private BasicScene _mScene;
  38. public BasicPhysicsPlugin()
  39. {
  40. }
  41. public bool Init()
  42. {
  43. return true;
  44. }
  45. public PhysicsScene GetScene()
  46. {
  47. if(_mScene == null)
  48. {
  49. _mScene = new BasicScene();
  50. }
  51. return(_mScene);
  52. }
  53. public string GetName()
  54. {
  55. return("basicphysics");
  56. }
  57. public void Dispose()
  58. {
  59. }
  60. }
  61. public class BasicScene :PhysicsScene
  62. {
  63. private List<BasicActor> _actors = new List<BasicActor>();
  64. private float[] _heightMap;
  65. public BasicScene()
  66. {
  67. }
  68. public override PhysicsActor AddAvatar(PhysicsVector position)
  69. {
  70. BasicActor act = new BasicActor();
  71. act.Position = position;
  72. _actors.Add(act);
  73. return act;
  74. }
  75. public override PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size)
  76. {
  77. return null;
  78. }
  79. public override void Simulate(float timeStep)
  80. {
  81. foreach (BasicActor actor in _actors)
  82. {
  83. actor.Position.X = actor.Position.X + (actor.Velocity.X * timeStep);
  84. actor.Position.Y = actor.Position.Y + (actor.Velocity.Y * timeStep);
  85. actor.Position.Z = actor.Position.Z + (actor.Velocity.Z * timeStep);
  86. /*if(actor.Flying)
  87. {
  88. actor.Position.Z = actor.Position.Z + (actor.Velocity.Z * timeStep);
  89. }
  90. else
  91. {
  92. actor.Position.Z = actor.Position.Z + ((-9.8f + actor.Velocity.Z) * timeStep);
  93. }
  94. if(actor.Position.Z < (_heightMap[(int)actor.Position.Y * 256 + (int)actor.Position.X]+1))
  95. {*/
  96. actor.Position.Z = _heightMap[(int)actor.Position.Y * 256 + (int)actor.Position.X]+1;
  97. //}
  98. if(actor.Position.X<0)
  99. {
  100. actor.Position.X = 0;
  101. actor.Velocity.X = 0;
  102. }
  103. if(actor.Position.Y < 0)
  104. {
  105. actor.Position.Y = 0;
  106. actor.Velocity.Y = 0;
  107. }
  108. if(actor.Position.X > 255)
  109. {
  110. actor.Position.X = 255;
  111. actor.Velocity.X = 0;
  112. }
  113. if(actor.Position.Y > 255)
  114. {
  115. actor.Position.Y = 255;
  116. actor.Velocity.X = 0;
  117. }
  118. }
  119. }
  120. public override void GetResults()
  121. {
  122. }
  123. public override bool IsThreaded
  124. {
  125. get
  126. {
  127. return(false); // for now we won't be multithreaded
  128. }
  129. }
  130. public override void SetTerrain(float[] heightMap)
  131. {
  132. this._heightMap = heightMap;
  133. }
  134. public override void DeleteTerrain()
  135. {
  136. }
  137. }
  138. public class BasicActor : PhysicsActor
  139. {
  140. private PhysicsVector _position;
  141. private PhysicsVector _velocity;
  142. private PhysicsVector _acceleration;
  143. private bool flying;
  144. public BasicActor()
  145. {
  146. _velocity = new PhysicsVector();
  147. _position = new PhysicsVector();
  148. _acceleration = new PhysicsVector();
  149. }
  150. public override bool Flying
  151. {
  152. get
  153. {
  154. return false;
  155. }
  156. set
  157. {
  158. flying= value;
  159. }
  160. }
  161. public override PhysicsVector Position
  162. {
  163. get
  164. {
  165. return _position;
  166. }
  167. set
  168. {
  169. _position = value;
  170. }
  171. }
  172. public override PhysicsVector Velocity
  173. {
  174. get
  175. {
  176. return _velocity;
  177. }
  178. set
  179. {
  180. _velocity = value;
  181. }
  182. }
  183. public override Axiom.MathLib.Quaternion Orientation
  184. {
  185. get
  186. {
  187. return Axiom.MathLib.Quaternion.Identity;
  188. }
  189. set
  190. {
  191. }
  192. }
  193. public override PhysicsVector Acceleration
  194. {
  195. get
  196. {
  197. return _acceleration;
  198. }
  199. }
  200. public override bool Kinematic
  201. {
  202. get
  203. {
  204. return true;
  205. }
  206. set
  207. {
  208. }
  209. }
  210. public void SetAcceleration (PhysicsVector accel)
  211. {
  212. this._acceleration = accel;
  213. }
  214. public override void AddForce(PhysicsVector force)
  215. {
  216. }
  217. public override void SetMomentum(PhysicsVector momentum)
  218. {
  219. }
  220. }
  221. }