POSPlugin.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. using System;
  28. using System.Collections.Generic;
  29. using Axiom.Math;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Physics.Manager;
  32. namespace OpenSim.Region.Physics.POSPlugin
  33. {
  34. /// <summary>
  35. /// for now will be a very POS physics engine
  36. /// </summary>
  37. public class POSPlugin : IPhysicsPlugin
  38. {
  39. public POSPlugin()
  40. {
  41. }
  42. public bool Init()
  43. {
  44. return true;
  45. }
  46. public PhysicsScene GetScene()
  47. {
  48. return new POSScene();
  49. }
  50. public string GetName()
  51. {
  52. return ("POS");
  53. }
  54. public void Dispose()
  55. {
  56. }
  57. }
  58. public class POSScene : PhysicsScene
  59. {
  60. private List<POSCharacter> _characters = new List<POSCharacter>();
  61. private List<POSPrim> _prims = new List<POSPrim>();
  62. private float[] _heightMap;
  63. private const float gravity = -9.8f;
  64. public POSScene()
  65. {
  66. }
  67. public override void Initialise(IMesher meshmerizer)
  68. {
  69. // Does nothing right now
  70. }
  71. public override void Dispose()
  72. {
  73. }
  74. public override PhysicsActor AddAvatar(string avName, PhysicsVector position, PhysicsVector size)
  75. {
  76. POSCharacter act = new POSCharacter();
  77. act.Position = position;
  78. _characters.Add(act);
  79. return act;
  80. }
  81. public override void SetWaterLevel(float baseheight)
  82. {
  83. }
  84. public override void RemovePrim(PhysicsActor prim)
  85. {
  86. POSPrim p = (POSPrim) prim;
  87. if (_prims.Contains(p))
  88. {
  89. _prims.Remove(p);
  90. }
  91. }
  92. public override void RemoveAvatar(PhysicsActor character)
  93. {
  94. POSCharacter act = (POSCharacter) character;
  95. if (_characters.Contains(act))
  96. {
  97. _characters.Remove(act);
  98. }
  99. }
  100. /*
  101. public override PhysicsActor AddPrim(PhysicsVector position, PhysicsVector size, Quaternion rotation)
  102. {
  103. return null;
  104. }
  105. */
  106. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  107. PhysicsVector size, Quaternion rotation)
  108. {
  109. return AddPrimShape(primName, pbs, position, size, rotation, false);
  110. }
  111. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, PhysicsVector position,
  112. PhysicsVector size, Quaternion rotation, bool isPhysical)
  113. {
  114. POSPrim prim = new POSPrim();
  115. prim.Position = position;
  116. prim.Orientation = rotation;
  117. prim.Size = size;
  118. _prims.Add(prim);
  119. return prim;
  120. }
  121. private bool check_collision(POSCharacter c, POSPrim p)
  122. {
  123. /*
  124. Console.WriteLine("checking whether " + c + " collides with " + p +
  125. " absX: " + Math.Abs(p.Position.X - c.Position.X) +
  126. " sizeX: " + p.Size.X * 0.5 + 0.5);
  127. */
  128. Vector3 rotatedPos = p.Orientation.Inverse()*
  129. new Vector3(c.Position.X - p.Position.X, c.Position.Y - p.Position.Y,
  130. c.Position.Z - p.Position.Z);
  131. Vector3 avatarSize = p.Orientation.Inverse()*new Vector3(c.Size.X, c.Size.Y, c.Size.Z);
  132. if (Math.Abs(rotatedPos.x) >= (p.Size.X*0.5 + Math.Abs(avatarSize.x)))
  133. {
  134. return false;
  135. }
  136. if (Math.Abs(rotatedPos.y) >= (p.Size.Y*0.5 + Math.Abs(avatarSize.y)))
  137. {
  138. return false;
  139. }
  140. if (Math.Abs(rotatedPos.z) >= (p.Size.Z*0.5 + Math.Abs(avatarSize.z)))
  141. {
  142. return false;
  143. }
  144. return true;
  145. }
  146. private bool check_all_prims(POSCharacter c)
  147. {
  148. for (int i = 0; i < _prims.Count; ++i)
  149. {
  150. if (check_collision(c, _prims[i]))
  151. {
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. public override void AddPhysicsActorTaint(PhysicsActor prim)
  158. {
  159. }
  160. public override float Simulate(float timeStep)
  161. {
  162. float fps = 0;
  163. for (int i = 0; i < _characters.Count; ++i)
  164. {
  165. fps++;
  166. POSCharacter character = _characters[i];
  167. float oldposX = character.Position.X;
  168. float oldposY = character.Position.Y;
  169. float oldposZ = character.Position.Z;
  170. if (!character.Flying)
  171. {
  172. character._target_velocity.Z += gravity*timeStep;
  173. }
  174. bool forcedZ = false;
  175. character.Position.X += character._target_velocity.X*timeStep;
  176. character.Position.Y += character._target_velocity.Y*timeStep;
  177. if (character.Position.Y < 0)
  178. {
  179. character.Position.Y = 0.1F;
  180. }
  181. else if (character.Position.Y >= Constants.RegionSize)
  182. {
  183. character.Position.Y = Constants.RegionSize - 0.1f;
  184. }
  185. if (character.Position.X < 0)
  186. {
  187. character.Position.X = 0.1F;
  188. }
  189. else if (character.Position.X >= Constants.RegionSize)
  190. {
  191. character.Position.X = Constants.RegionSize - 0.1f;
  192. }
  193. float terrainheight = _heightMap[(int)character.Position.Y * Constants.RegionSize + (int)character.Position.X];
  194. if (character.Position.Z + (character._target_velocity.Z*timeStep) < terrainheight + 2)
  195. {
  196. character.Position.Z = terrainheight + 1.0f;
  197. forcedZ = true;
  198. }
  199. else
  200. {
  201. character.Position.Z += character._target_velocity.Z*timeStep;
  202. }
  203. /// this is it -- the magic you've all been waiting for! Ladies and gentlemen --
  204. /// Completely Bogus Collision Detection!!!
  205. /// better known as the CBCD algorithm
  206. if (check_all_prims(character))
  207. {
  208. character.Position.Z = oldposZ; // first try Z axis
  209. if (check_all_prims(character))
  210. {
  211. character.Position.Z = oldposZ + 0.4f; // try harder
  212. if (check_all_prims(character))
  213. {
  214. character.Position.X = oldposX;
  215. character.Position.Y = oldposY;
  216. character.Position.Z = oldposZ;
  217. character.Position.X = character.Position.X + (character._target_velocity.X*timeStep);
  218. if (check_all_prims(character))
  219. {
  220. character.Position.X = oldposX;
  221. }
  222. character.Position.Y = character.Position.Y + (character._target_velocity.Y*timeStep);
  223. if (check_all_prims(character))
  224. {
  225. character.Position.Y = oldposY;
  226. }
  227. }
  228. else
  229. {
  230. forcedZ = true;
  231. }
  232. }
  233. else
  234. {
  235. forcedZ = true;
  236. }
  237. }
  238. if (character.Position.Y < 0)
  239. {
  240. character.Position.Y = 0.1F;
  241. }
  242. else if (character.Position.Y >= Constants.RegionSize)
  243. {
  244. character.Position.Y = Constants.RegionSize - 0.1f;
  245. }
  246. if (character.Position.X < 0)
  247. {
  248. character.Position.X = 0.1F;
  249. }
  250. else if (character.Position.X >= Constants.RegionSize)
  251. {
  252. character.Position.X = Constants.RegionSize - 0.1f;
  253. }
  254. character._velocity.X = (character.Position.X - oldposX)/timeStep;
  255. character._velocity.Y = (character.Position.Y - oldposY)/timeStep;
  256. if (forcedZ)
  257. {
  258. character._velocity.Z = 0;
  259. character._target_velocity.Z = 0;
  260. ((PhysicsActor)character).IsColliding = true;
  261. character.RequestPhysicsterseUpdate();
  262. }
  263. else
  264. {
  265. ((PhysicsActor)character).IsColliding = false;
  266. character._velocity.Z = (character.Position.Z - oldposZ)/timeStep;
  267. }
  268. }
  269. return fps;
  270. }
  271. public override void GetResults()
  272. {
  273. }
  274. public override bool IsThreaded
  275. {
  276. get { return (false); // for now we won't be multithreaded
  277. }
  278. }
  279. public override void SetTerrain(float[] heightMap)
  280. {
  281. _heightMap = heightMap;
  282. }
  283. public override void DeleteTerrain()
  284. {
  285. }
  286. }
  287. public class POSCharacter : PhysicsActor
  288. {
  289. private PhysicsVector _position;
  290. public PhysicsVector _velocity;
  291. public PhysicsVector _target_velocity = PhysicsVector.Zero;
  292. private PhysicsVector _acceleration;
  293. private PhysicsVector m_rotationalVelocity = PhysicsVector.Zero;
  294. private bool flying;
  295. private bool iscolliding;
  296. public POSCharacter()
  297. {
  298. _velocity = new PhysicsVector();
  299. _position = new PhysicsVector();
  300. _acceleration = new PhysicsVector();
  301. }
  302. public override int PhysicsActorType
  303. {
  304. get { return (int) ActorTypes.Agent; }
  305. set { return; }
  306. }
  307. public override PhysicsVector RotationalVelocity
  308. {
  309. get { return m_rotationalVelocity; }
  310. set { m_rotationalVelocity = value; }
  311. }
  312. public override bool SetAlwaysRun
  313. {
  314. get { return false; }
  315. set { return; }
  316. }
  317. public override uint LocalID
  318. {
  319. set { return; }
  320. }
  321. public override bool Grabbed
  322. {
  323. set { return; }
  324. }
  325. public override bool Selected
  326. {
  327. set { return; }
  328. }
  329. public override float Buoyancy
  330. {
  331. get { return 0f; }
  332. set { return; }
  333. }
  334. public override bool FloatOnWater
  335. {
  336. set { return; }
  337. }
  338. public override bool IsPhysical
  339. {
  340. get { return false; }
  341. set { return; }
  342. }
  343. public override bool ThrottleUpdates
  344. {
  345. get { return false; }
  346. set { return; }
  347. }
  348. public override bool Flying
  349. {
  350. get { return flying; }
  351. set { flying = value; }
  352. }
  353. public override bool IsColliding
  354. {
  355. get { return iscolliding; }
  356. set { iscolliding = value; }
  357. }
  358. public override bool CollidingGround
  359. {
  360. get { return false; }
  361. set { return; }
  362. }
  363. public override bool CollidingObj
  364. {
  365. get { return false; }
  366. set { return; }
  367. }
  368. public override bool Stopped
  369. {
  370. get { return false; }
  371. }
  372. public override PhysicsVector Position
  373. {
  374. get { return _position; }
  375. set { _position = value; }
  376. }
  377. public override PhysicsVector Size
  378. {
  379. get { return new PhysicsVector(0.5f, 0.5f, 1.0f); }
  380. set { }
  381. }
  382. public override float Mass
  383. {
  384. get { return 0f; }
  385. }
  386. public override PhysicsVector Force
  387. {
  388. get { return PhysicsVector.Zero; }
  389. }
  390. public override PhysicsVector CenterOfMass
  391. {
  392. get { return PhysicsVector.Zero; }
  393. }
  394. public override PhysicsVector GeometricCenter
  395. {
  396. get { return PhysicsVector.Zero; }
  397. }
  398. public override PrimitiveBaseShape Shape
  399. {
  400. set { return; }
  401. }
  402. public override PhysicsVector Velocity
  403. {
  404. get { return _velocity; }
  405. set { _target_velocity = value; }
  406. }
  407. public override float CollisionScore
  408. {
  409. get { return 0f; }
  410. }
  411. public override Quaternion Orientation
  412. {
  413. get { return Quaternion.Identity; }
  414. set { }
  415. }
  416. public override PhysicsVector Acceleration
  417. {
  418. get { return _acceleration; }
  419. }
  420. public override bool Kinematic
  421. {
  422. get { return true; }
  423. set { }
  424. }
  425. public override void link(PhysicsActor obj)
  426. {
  427. }
  428. public override void delink()
  429. {
  430. }
  431. public override void LockAngularMotion(PhysicsVector axis)
  432. {
  433. }
  434. public void SetAcceleration(PhysicsVector accel)
  435. {
  436. _acceleration = accel;
  437. }
  438. public override void AddForce(PhysicsVector force)
  439. {
  440. }
  441. public override void SetMomentum(PhysicsVector momentum)
  442. {
  443. }
  444. public override void CrossingFailure()
  445. {
  446. }
  447. public override PhysicsVector PIDTarget { set { return; } }
  448. public override bool PIDActive { set { return; } }
  449. public override float PIDTau { set { return; } }
  450. }
  451. public class POSPrim : PhysicsActor
  452. {
  453. private PhysicsVector _position;
  454. private PhysicsVector _velocity;
  455. private PhysicsVector _acceleration;
  456. private PhysicsVector _size;
  457. private PhysicsVector m_rotationalVelocity = PhysicsVector.Zero;
  458. private Quaternion _orientation;
  459. private bool iscolliding;
  460. public POSPrim()
  461. {
  462. _velocity = new PhysicsVector();
  463. _position = new PhysicsVector();
  464. _acceleration = new PhysicsVector();
  465. }
  466. public override int PhysicsActorType
  467. {
  468. get { return (int) ActorTypes.Prim; }
  469. set { return; }
  470. }
  471. public override PhysicsVector RotationalVelocity
  472. {
  473. get { return m_rotationalVelocity; }
  474. set { m_rotationalVelocity = value; }
  475. }
  476. public override bool IsPhysical
  477. {
  478. get { return false; }
  479. set { return; }
  480. }
  481. public override bool ThrottleUpdates
  482. {
  483. get { return false; }
  484. set { return; }
  485. }
  486. public override bool IsColliding
  487. {
  488. get { return iscolliding; }
  489. set { iscolliding = value; }
  490. }
  491. public override bool CollidingGround
  492. {
  493. get { return false; }
  494. set { return; }
  495. }
  496. public override bool CollidingObj
  497. {
  498. get { return false; }
  499. set { return; }
  500. }
  501. public override bool Stopped
  502. {
  503. get { return false; }
  504. }
  505. public override PhysicsVector Position
  506. {
  507. get { return _position; }
  508. set { _position = value; }
  509. }
  510. public override PhysicsVector Size
  511. {
  512. get { return _size; }
  513. set { _size = value; }
  514. }
  515. public override float Mass
  516. {
  517. get { return 0f; }
  518. }
  519. public override PhysicsVector Force
  520. {
  521. get { return PhysicsVector.Zero; }
  522. }
  523. public override PhysicsVector CenterOfMass
  524. {
  525. get { return PhysicsVector.Zero; }
  526. }
  527. public override PhysicsVector GeometricCenter
  528. {
  529. get { return PhysicsVector.Zero; }
  530. }
  531. public override PrimitiveBaseShape Shape
  532. {
  533. set { return; }
  534. }
  535. public override float Buoyancy
  536. {
  537. get { return 0f; }
  538. set { return; }
  539. }
  540. public override bool FloatOnWater
  541. {
  542. set { return; }
  543. }
  544. public override PhysicsVector Velocity
  545. {
  546. get { return _velocity; }
  547. set { _velocity = value; }
  548. }
  549. public override float CollisionScore
  550. {
  551. get { return 0f; }
  552. }
  553. public override Quaternion Orientation
  554. {
  555. get { return _orientation; }
  556. set { _orientation = value; }
  557. }
  558. public override PhysicsVector Acceleration
  559. {
  560. get { return _acceleration; }
  561. }
  562. public override bool Kinematic
  563. {
  564. get { return true; }
  565. set { }
  566. }
  567. public void SetAcceleration(PhysicsVector accel)
  568. {
  569. _acceleration = accel;
  570. }
  571. public override void AddForce(PhysicsVector force)
  572. {
  573. }
  574. public override void SetMomentum(PhysicsVector momentum)
  575. {
  576. }
  577. public override bool Flying
  578. {
  579. get { return false; }
  580. set { }
  581. }
  582. public override bool SetAlwaysRun
  583. {
  584. get { return false; }
  585. set { return; }
  586. }
  587. public override uint LocalID
  588. {
  589. set { return; }
  590. }
  591. public override bool Grabbed
  592. {
  593. set { return; }
  594. }
  595. public override void link(PhysicsActor obj)
  596. {
  597. }
  598. public override void delink()
  599. {
  600. }
  601. public override void LockAngularMotion(PhysicsVector axis)
  602. {
  603. }
  604. public override bool Selected
  605. {
  606. set { return; }
  607. }
  608. public override void CrossingFailure()
  609. {
  610. }
  611. public override PhysicsVector PIDTarget { set { return; } }
  612. public override bool PIDActive { set { return; } }
  613. public override float PIDTau { set { return; } }
  614. }
  615. }