collision.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. struct CollisionInfo
  29. {
  30. CollisionInfo() = default;
  31. CollisionType type = COLLISION_NODE;
  32. v3s16 node_p = v3s16(-32768,-32768,-32768); // COLLISION_NODE
  33. v3f old_speed;
  34. v3f new_speed;
  35. };
  36. struct collisionMoveResult
  37. {
  38. collisionMoveResult() = default;
  39. bool touching_ground = false;
  40. bool collides = false;
  41. bool standing_on_object = false;
  42. std::vector<CollisionInfo> collisions;
  43. };
  44. // Moves using a single iteration; speed should not exceed pos_max_d/dtime
  45. collisionMoveResult collisionMoveSimple(Environment *env,IGameDef *gamedef,
  46. f32 pos_max_d, const aabb3f &box_0,
  47. f32 stepheight, f32 dtime,
  48. v3f *pos_f, v3f *speed_f,
  49. v3f accel_f, ActiveObject *self=NULL,
  50. bool collideWithObjects=true);
  51. // Helper function:
  52. // Checks for collision of a moving aabbox with a static aabbox
  53. // Returns -1 if no collision, 0 if X collision, 1 if Y collision, 2 if Z collision
  54. // dtime receives time until first collision, invalid if -1 is returned
  55. int axisAlignedCollision(
  56. const aabb3f &staticbox, const aabb3f &movingbox,
  57. const v3f &speed, f32 d, f32 *dtime);
  58. // Helper function:
  59. // Checks if moving the movingbox up by the given distance would hit a ceiling.
  60. bool wouldCollideWithCeiling(
  61. const std::vector<aabb3f> &staticboxes,
  62. const aabb3f &movingbox,
  63. f32 y_increase, f32 d);