1
0

PhysicsManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 System.Collections;
  30. using System.IO;
  31. using System.Reflection;
  32. namespace PhysicsSystem
  33. {
  34. /// <summary>
  35. /// Description of MyClass.
  36. /// </summary>
  37. public class PhysicsManager
  38. {
  39. private Dictionary<string, IPhysicsPlugin> _plugins=new Dictionary<string, IPhysicsPlugin>();
  40. public PhysicsManager()
  41. {
  42. }
  43. public PhysicsScene GetPhysicsScene(string engineName)
  44. {
  45. if(_plugins.ContainsKey(engineName))
  46. {
  47. Console.WriteLine("creating "+engineName);
  48. return _plugins[engineName].GetScene();
  49. }
  50. else
  51. {
  52. Console.WriteLine("couldn't find physicsEngine: "+ engineName);
  53. return null;
  54. }
  55. }
  56. public void LoadPlugins()
  57. {
  58. string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory ,"Physics");
  59. string[] pluginFiles = Directory.GetFiles(path, "*.Dll");
  60. for(int i= 0; i<pluginFiles.Length; i++)
  61. {
  62. this.AddPlugin(pluginFiles[i]);
  63. }
  64. }
  65. private void AddPlugin(string FileName)
  66. {
  67. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  68. foreach (Type pluginType in pluginAssembly.GetTypes())
  69. {
  70. if (pluginType.IsPublic)
  71. {
  72. if (!pluginType.IsAbstract)
  73. {
  74. Type typeInterface = pluginType.GetInterface("IPhysicsPlugin", true);
  75. if (typeInterface != null)
  76. {
  77. IPhysicsPlugin plug = (IPhysicsPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  78. plug.Init();
  79. this._plugins.Add(plug.GetName(),plug);
  80. }
  81. typeInterface = null;
  82. }
  83. }
  84. }
  85. pluginAssembly = null;
  86. }
  87. }
  88. public interface IPhysicsPlugin
  89. {
  90. bool Init();
  91. PhysicsScene GetScene();
  92. string GetName();
  93. void Dispose();
  94. }
  95. public abstract class PhysicsScene
  96. {
  97. public abstract PhysicsActor AddAvatar(PhysicsVector position);
  98. public abstract void Simulate(float timeStep);
  99. public abstract void GetResults();
  100. public abstract void SetTerrain(float[] heightMap);
  101. public abstract bool IsThreaded
  102. {
  103. get;
  104. }
  105. }
  106. public abstract class PhysicsActor
  107. {
  108. public abstract PhysicsVector Position
  109. {
  110. get;
  111. set;
  112. }
  113. public abstract PhysicsVector Velocity
  114. {
  115. get;
  116. set;
  117. }
  118. public abstract PhysicsVector Acceleration
  119. {
  120. get;
  121. }
  122. public abstract void AddForce(PhysicsVector force);
  123. public abstract void SetMomentum(PhysicsVector momentum);
  124. }
  125. public class PhysicsVector
  126. {
  127. public float X;
  128. public float Y;
  129. public float Z;
  130. public PhysicsVector()
  131. {
  132. }
  133. public PhysicsVector(float x, float y, float z)
  134. {
  135. X = x;
  136. Y = y;
  137. Z = z;
  138. }
  139. }
  140. }