collision.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #pragma once
  17. #include "irrlichttypes_bloated.h"
  18. #include <vector>
  19. class Map;
  20. class IGameDef;
  21. class Environment;
  22. class ActiveObject;
  23. enum CollisionType
  24. {
  25. COLLISION_NODE,
  26. COLLISION_OBJECT,
  27. };
  28. enum CollisionAxis
  29. {
  30. COLLISION_AXIS_NONE = -1,
  31. COLLISION_AXIS_X,
  32. COLLISION_AXIS_Y,
  33. COLLISION_AXIS_Z,
  34. };
  35. struct CollisionInfo
  36. {
  37. CollisionInfo() = default;
  38. CollisionType type = COLLISION_NODE;
  39. CollisionAxis axis = COLLISION_AXIS_NONE;
  40. v3s16 node_p = v3s16(-32768,-32768,-32768); // COLLISION_NODE
  41. ActiveObject *object = nullptr; // COLLISION_OBJECT
  42. v3f old_speed;
  43. v3f new_speed;
  44. int plane = -1;
  45. };
  46. struct collisionMoveResult
  47. {
  48. collisionMoveResult() = default;
  49. bool touching_ground = false;
  50. bool collides = false;
  51. bool standing_on_object = false;
  52. std::vector<CollisionInfo> collisions;
  53. };
  54. // Moves using a single iteration; speed should not exceed pos_max_d/dtime
  55. collisionMoveResult collisionMoveSimple(Environment *env,IGameDef *gamedef,
  56. f32 pos_max_d, const aabb3f &box_0,
  57. f32 stepheight, f32 dtime,
  58. v3f *pos_f, v3f *speed_f,
  59. v3f accel_f, ActiveObject *self=NULL,
  60. bool collideWithObjects=true);
  61. // Helper function:
  62. // Checks for collision of a moving aabbox with a static aabbox
  63. // Returns -1 if no collision, 0 if X collision, 1 if Y collision, 2 if Z collision
  64. // dtime receives time until first collision, invalid if -1 is returned
  65. CollisionAxis axisAlignedCollision(
  66. const aabb3f &staticbox, const aabb3f &movingbox,
  67. v3f speed, f32 *dtime);
  68. // Helper function:
  69. // Checks if moving the movingbox up by the given distance would hit a ceiling.
  70. bool wouldCollideWithCeiling(
  71. const std::vector<aabb3f> &staticboxes,
  72. const aabb3f &movingbox,
  73. f32 y_increase, f32 d);