collision.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef COLLISION_HEADER
  17. #define COLLISION_HEADER
  18. #include "irrlichttypes_bloated.h"
  19. #include <vector>
  20. class Map;
  21. class IGameDef;
  22. class Environment;
  23. class ActiveObject;
  24. enum CollisionType
  25. {
  26. COLLISION_NODE,
  27. COLLISION_OBJECT,
  28. };
  29. struct CollisionInfo
  30. {
  31. enum CollisionType type;
  32. v3s16 node_p; // COLLISION_NODE
  33. bool bouncy;
  34. v3f old_speed;
  35. v3f new_speed;
  36. CollisionInfo():
  37. type(COLLISION_NODE),
  38. node_p(-32768,-32768,-32768),
  39. bouncy(false),
  40. old_speed(0,0,0),
  41. new_speed(0,0,0)
  42. {}
  43. };
  44. struct collisionMoveResult
  45. {
  46. bool touching_ground;
  47. bool collides;
  48. bool collides_xz;
  49. bool standing_on_unloaded;
  50. std::vector<CollisionInfo> collisions;
  51. collisionMoveResult():
  52. touching_ground(false),
  53. collides(false),
  54. collides_xz(false),
  55. standing_on_unloaded(false)
  56. {}
  57. };
  58. // Moves using a single iteration; speed should not exceed pos_max_d/dtime
  59. collisionMoveResult collisionMoveSimple(Environment *env,IGameDef *gamedef,
  60. f32 pos_max_d, const aabb3f &box_0,
  61. f32 stepheight, f32 dtime,
  62. v3f &pos_f, v3f &speed_f,
  63. v3f &accel_f,ActiveObject* self=0,
  64. bool collideWithObjects=true);
  65. #if 0
  66. // This doesn't seem to work and isn't used
  67. // Moves using as many iterations as needed
  68. collisionMoveResult collisionMovePrecise(Map *map, IGameDef *gamedef,
  69. f32 pos_max_d, const aabb3f &box_0,
  70. f32 stepheight, f32 dtime,
  71. v3f &pos_f, v3f &speed_f, v3f &accel_f);
  72. #endif
  73. // Helper function:
  74. // Checks for collision of a moving aabbox with a static aabbox
  75. // Returns -1 if no collision, 0 if X collision, 1 if Y collision, 2 if Z collision
  76. // dtime receives time until first collision, invalid if -1 is returned
  77. int axisAlignedCollision(
  78. const aabb3f &staticbox, const aabb3f &movingbox,
  79. const v3f &speed, f32 d, f32 &dtime);
  80. // Helper function:
  81. // Checks if moving the movingbox up by the given distance would hit a ceiling.
  82. bool wouldCollideWithCeiling(
  83. const std::vector<aabb3f> &staticboxes,
  84. const aabb3f &movingbox,
  85. f32 y_increase, f32 d);
  86. #endif