POSPlugin.cs 21 KB

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