OdePlugin.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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;
  29. using System.Collections.Generic;
  30. using Axiom.Math;
  31. using Ode.NET;
  32. using OpenSim.Physics.Manager;
  33. namespace OpenSim.Region.Physics.OdePlugin
  34. {
  35. /// <summary>
  36. /// ODE plugin
  37. /// </summary>
  38. public class OdePlugin : IPhysicsPlugin
  39. {
  40. private OdeScene _mScene;
  41. public OdePlugin()
  42. {
  43. }
  44. public bool Init()
  45. {
  46. return true;
  47. }
  48. public PhysicsScene GetScene()
  49. {
  50. if (_mScene == null)
  51. {
  52. _mScene = new OdeScene();
  53. }
  54. return (_mScene);
  55. }
  56. public string GetName()
  57. {
  58. return ("OpenDynamicsEngine");
  59. }
  60. public void Dispose()
  61. {
  62. }
  63. }
  64. public class OdeScene : PhysicsScene
  65. {
  66. static public IntPtr world;
  67. static public IntPtr space;
  68. static private IntPtr contactgroup;
  69. static private IntPtr LandGeom;
  70. //static private IntPtr Land;
  71. private double[] _heightmap;
  72. static private d.NearCallback nearCallback = near;
  73. private List<OdeCharacter> _characters = new List<OdeCharacter>();
  74. private static d.ContactGeom[] contacts = new d.ContactGeom[30];
  75. private static d.Contact contact;
  76. public OdeScene()
  77. {
  78. contact.surface.mode = d.ContactFlags.Bounce | d.ContactFlags.SoftCFM;
  79. contact.surface.mu = d.Infinity;
  80. contact.surface.mu2 = 0.0f;
  81. contact.surface.bounce = 0.1f;
  82. contact.surface.bounce_vel = 0.1f;
  83. contact.surface.soft_cfm = 0.01f;
  84. world = d.WorldCreate();
  85. space = d.HashSpaceCreate(IntPtr.Zero);
  86. contactgroup = d.JointGroupCreate(0);
  87. d.WorldSetGravity(world, 0.0f, 0.0f, -0.5f);
  88. //d.WorldSetCFM(world, 1e-5f);
  89. d.WorldSetAutoDisableFlag(world, false);
  90. d.WorldSetContactSurfaceLayer(world, 0.001f);
  91. // d.CreatePlane(space, 0, 0, 1, 0);
  92. this._heightmap = new double[65536];
  93. }
  94. // This function blatantly ripped off from BoxStack.cs
  95. static private void near(IntPtr space, IntPtr g1, IntPtr g2)
  96. {
  97. IntPtr b1 = d.GeomGetBody(g1);
  98. IntPtr b2 = d.GeomGetBody(g2);
  99. if (b1 != IntPtr.Zero && b2 != IntPtr.Zero && d.AreConnectedExcluding(b1, b2, d.JointType.Contact))
  100. return;
  101. int count = d.Collide(g1, g2, 500, contacts, d.ContactGeom.SizeOf);
  102. for (int i = 0; i < count; ++i)
  103. {
  104. contact.geom = contacts[i];
  105. IntPtr joint = d.JointCreateContact(world, contactgroup, ref contact);
  106. d.JointAttach(joint, b1, b2);
  107. }
  108. }
  109. public override PhysicsActor AddAvatar(PhysicsVector position)
  110. {
  111. PhysicsVector pos = new PhysicsVector();
  112. pos.X = position.X;
  113. pos.Y = position.Y;
  114. pos.Z = position.Z + 20;
  115. OdeCharacter newAv = new OdeCharacter(this, pos);
  116. this._characters.Add(newAv);
  117. return newAv;
  118. }
  119. public override void RemoveAvatar(PhysicsActor actor)
  120. {
  121. }
  122. public override PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size)
  123. {
  124. PhysicsVector pos = new PhysicsVector();
  125. pos.X = position.X;
  126. pos.Y = position.Y;
  127. pos.Z = position.Z;
  128. PhysicsVector siz = new PhysicsVector();
  129. siz.X = size.X;
  130. siz.Y = size.Y;
  131. siz.Z = size.Z;
  132. return new OdePrim();
  133. }
  134. public override void Simulate(float timeStep)
  135. {
  136. foreach (OdeCharacter actor in _characters)
  137. {
  138. actor.Move(timeStep * 5f);
  139. }
  140. d.SpaceCollide(space, IntPtr.Zero, nearCallback);
  141. d.WorldQuickStep(world, timeStep * 5f);
  142. d.JointGroupEmpty(contactgroup);
  143. foreach (OdeCharacter actor in _characters)
  144. {
  145. actor.UpdatePosition();
  146. }
  147. }
  148. public override void GetResults()
  149. {
  150. }
  151. public override bool IsThreaded
  152. {
  153. get
  154. {
  155. return (false); // for now we won't be multithreaded
  156. }
  157. }
  158. public override void SetTerrain(float[] heightMap)
  159. {
  160. for (int i = 0; i < 65536; i++)
  161. {
  162. // this._heightmap[i] = (double)heightMap[i];
  163. // dbm (danx0r) -- heightmap x,y must be swapped for Ode (should fix ODE, but for now...)
  164. int x = i & 0xff;
  165. int y = i >> 8;
  166. this._heightmap[i] = (double)heightMap[x * 256 + y];
  167. }
  168. IntPtr HeightmapData = d.GeomHeightfieldDataCreate();
  169. d.GeomHeightfieldDataBuildDouble(HeightmapData, _heightmap, 0, 256, 256, 256, 256, 1.0f, 0.0f, 2.0f, 0);
  170. d.GeomHeightfieldDataSetBounds(HeightmapData, 256, 256);
  171. LandGeom = d.CreateHeightfield(space, HeightmapData, 1);
  172. d.Matrix3 R = new d.Matrix3();
  173. Quaternion q1 =Quaternion.FromAngleAxis(1.5707f, new Vector3(1,0,0));
  174. Quaternion q2 =Quaternion.FromAngleAxis(1.5707f, new Vector3(0,1,0));
  175. //Axiom.Math.Quaternion q3 = Axiom.Math.Quaternion.FromAngleAxis(3.14f, new Axiom.Math.Vector3(0, 0, 1));
  176. q1 = q1 * q2;
  177. //q1 = q1 * q3;
  178. Vector3 v3 = new Vector3();
  179. float angle = 0;
  180. q1.ToAngleAxis(ref angle, ref v3);
  181. d.RFromAxisAndAngle(out R, v3.x, v3.y, v3.z, angle);
  182. d.GeomSetRotation(LandGeom, ref R);
  183. d.GeomSetPosition(LandGeom, 128, 128, 0);
  184. }
  185. public override void DeleteTerrain()
  186. {
  187. }
  188. }
  189. public class OdeCharacter : PhysicsActor
  190. {
  191. private PhysicsVector _position;
  192. private PhysicsVector _velocity;
  193. private PhysicsVector _acceleration;
  194. private bool flying;
  195. //private float gravityAccel;
  196. private IntPtr BoundingCapsule;
  197. IntPtr capsule_geom;
  198. d.Mass capsule_mass;
  199. public OdeCharacter(OdeScene parent_scene, PhysicsVector pos)
  200. {
  201. _velocity = new PhysicsVector();
  202. _position = pos;
  203. _acceleration = new PhysicsVector();
  204. d.MassSetCapsule(out capsule_mass, 5.0f, 3, 0.5f, 2f);
  205. capsule_geom = d.CreateCapsule(OdeScene.space, 0.5f, 2f);
  206. this.BoundingCapsule = d.BodyCreate(OdeScene.world);
  207. d.BodySetMass(BoundingCapsule, ref capsule_mass);
  208. d.BodySetPosition(BoundingCapsule, pos.X, pos.Y, pos.Z);
  209. d.GeomSetBody(capsule_geom, BoundingCapsule);
  210. }
  211. public override bool Flying
  212. {
  213. get
  214. {
  215. return flying;
  216. }
  217. set
  218. {
  219. flying = value;
  220. }
  221. }
  222. public override PhysicsVector Position
  223. {
  224. get
  225. {
  226. return _position;
  227. }
  228. set
  229. {
  230. _position = value;
  231. }
  232. }
  233. public override PhysicsVector Velocity
  234. {
  235. get
  236. {
  237. return _velocity;
  238. }
  239. set
  240. {
  241. _velocity = value;
  242. }
  243. }
  244. public override bool Kinematic
  245. {
  246. get
  247. {
  248. return false;
  249. }
  250. set
  251. {
  252. }
  253. }
  254. public override Quaternion Orientation
  255. {
  256. get
  257. {
  258. return Quaternion.Identity;
  259. }
  260. set
  261. {
  262. }
  263. }
  264. public override PhysicsVector Acceleration
  265. {
  266. get
  267. {
  268. return _acceleration;
  269. }
  270. }
  271. public void SetAcceleration(PhysicsVector accel)
  272. {
  273. this._acceleration = accel;
  274. }
  275. public override void AddForce(PhysicsVector force)
  276. {
  277. }
  278. public override void SetMomentum(PhysicsVector momentum)
  279. {
  280. }
  281. public void Move(float timeStep)
  282. {
  283. PhysicsVector vec = new PhysicsVector();
  284. vec.X = this._velocity.X * timeStep;
  285. vec.Y = this._velocity.Y * timeStep;
  286. if (flying)
  287. {
  288. vec.Z = (this._velocity.Z + 0.5f) * timeStep;
  289. }
  290. d.BodySetLinearVel(this.BoundingCapsule, vec.X, vec.Y, vec.Z);
  291. }
  292. public void UpdatePosition()
  293. {
  294. d.Vector3 vec = d.BodyGetPosition(BoundingCapsule);
  295. this._position.X = vec.X;
  296. this._position.Y = vec.Y;
  297. this._position.Z = vec.Z+1.0f;
  298. }
  299. }
  300. public class OdePrim : PhysicsActor
  301. {
  302. private PhysicsVector _position;
  303. private PhysicsVector _velocity;
  304. private PhysicsVector _acceleration;
  305. public OdePrim()
  306. {
  307. _velocity = new PhysicsVector();
  308. _position = new PhysicsVector();
  309. _acceleration = new PhysicsVector();
  310. }
  311. public override bool Flying
  312. {
  313. get
  314. {
  315. return false; //no flying prims for you
  316. }
  317. set
  318. {
  319. }
  320. }
  321. public override PhysicsVector Position
  322. {
  323. get
  324. {
  325. PhysicsVector pos = new PhysicsVector();
  326. // PhysicsVector vec = this._prim.Position;
  327. //pos.X = vec.X;
  328. //pos.Y = vec.Y;
  329. //pos.Z = vec.Z;
  330. return pos;
  331. }
  332. set
  333. {
  334. /*PhysicsVector vec = value;
  335. PhysicsVector pos = new PhysicsVector();
  336. pos.X = vec.X;
  337. pos.Y = vec.Y;
  338. pos.Z = vec.Z;
  339. this._prim.Position = pos;*/
  340. }
  341. }
  342. public override PhysicsVector Velocity
  343. {
  344. get
  345. {
  346. return _velocity;
  347. }
  348. set
  349. {
  350. _velocity = value;
  351. }
  352. }
  353. public override bool Kinematic
  354. {
  355. get
  356. {
  357. return false;
  358. //return this._prim.Kinematic;
  359. }
  360. set
  361. {
  362. //this._prim.Kinematic = value;
  363. }
  364. }
  365. public override Quaternion Orientation
  366. {
  367. get
  368. {
  369. Quaternion res = new Quaternion();
  370. return res;
  371. }
  372. set
  373. {
  374. }
  375. }
  376. public override PhysicsVector Acceleration
  377. {
  378. get
  379. {
  380. return _acceleration;
  381. }
  382. }
  383. public void SetAcceleration(PhysicsVector accel)
  384. {
  385. this._acceleration = accel;
  386. }
  387. public override void AddForce(PhysicsVector force)
  388. {
  389. }
  390. public override void SetMomentum(PhysicsVector momentum)
  391. {
  392. }
  393. }
  394. }