area_bullet.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*************************************************************************/
  2. /* area_bullet.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef AREABULLET_H
  31. #define AREABULLET_H
  32. #include "collision_object_bullet.h"
  33. #include "core/templates/vector.h"
  34. #include "servers/physics_server_3d.h"
  35. #include "space_bullet.h"
  36. /**
  37. @author AndreaCatania
  38. */
  39. class btGhostObject;
  40. class AreaBullet : public RigidCollisionObjectBullet {
  41. friend void SpaceBullet::check_ghost_overlaps();
  42. public:
  43. struct InOutEventCallback {
  44. Callable event_callback;
  45. InOutEventCallback() {}
  46. };
  47. enum OverlapState {
  48. OVERLAP_STATE_DIRTY = 0, // Mark processed overlaps
  49. OVERLAP_STATE_INSIDE, // Mark old overlap
  50. OVERLAP_STATE_ENTER, // Mark just enter overlap
  51. OVERLAP_STATE_EXIT // Mark ended overlaps
  52. };
  53. struct OverlappingObjectData {
  54. CollisionObjectBullet *object = nullptr;
  55. OverlapState state = OVERLAP_STATE_ENTER;
  56. OverlappingObjectData() {}
  57. OverlappingObjectData(CollisionObjectBullet *p_object, OverlapState p_state) :
  58. object(p_object),
  59. state(p_state) {}
  60. OverlappingObjectData(const OverlappingObjectData &other) {
  61. operator=(other);
  62. }
  63. void operator=(const OverlappingObjectData &other) {
  64. object = other.object;
  65. state = other.state;
  66. }
  67. };
  68. private:
  69. // These are used by function callEvent. Instead to create this each call I create if one time.
  70. Variant call_event_res[5];
  71. Variant *call_event_res_ptr[5] = {};
  72. btGhostObject *btGhost = nullptr;
  73. Vector<OverlappingObjectData> overlappingObjects;
  74. bool monitorable = true;
  75. PhysicsServer3D::AreaSpaceOverrideMode spOv_mode = PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;
  76. bool spOv_gravityPoint = false;
  77. real_t spOv_gravityPointDistanceScale = 0.0;
  78. real_t spOv_gravityPointAttenuation = 1.0;
  79. Vector3 spOv_gravityVec = Vector3(0, -1, 0);
  80. real_t spOv_gravityMag = 10.0;
  81. real_t spOv_linearDump = 0.1;
  82. real_t spOv_angularDump = 0.1;
  83. int spOv_priority = 0;
  84. bool isScratched = false;
  85. InOutEventCallback eventsCallbacks[2];
  86. public:
  87. AreaBullet();
  88. ~AreaBullet();
  89. _FORCE_INLINE_ btGhostObject *get_bt_ghost() const { return btGhost; }
  90. int find_overlapping_object(CollisionObjectBullet *p_colObj);
  91. void set_monitorable(bool p_monitorable);
  92. _FORCE_INLINE_ bool is_monitorable() const { return monitorable; }
  93. bool is_monitoring() const;
  94. _FORCE_INLINE_ void set_spOv_mode(PhysicsServer3D::AreaSpaceOverrideMode p_mode) { spOv_mode = p_mode; }
  95. _FORCE_INLINE_ PhysicsServer3D::AreaSpaceOverrideMode get_spOv_mode() { return spOv_mode; }
  96. _FORCE_INLINE_ void set_spOv_gravityPoint(bool p_isGP) { spOv_gravityPoint = p_isGP; }
  97. _FORCE_INLINE_ bool is_spOv_gravityPoint() { return spOv_gravityPoint; }
  98. _FORCE_INLINE_ void set_spOv_gravityPointDistanceScale(real_t p_GPDS) { spOv_gravityPointDistanceScale = p_GPDS; }
  99. _FORCE_INLINE_ real_t get_spOv_gravityPointDistanceScale() { return spOv_gravityPointDistanceScale; }
  100. _FORCE_INLINE_ void set_spOv_gravityPointAttenuation(real_t p_GPA) { spOv_gravityPointAttenuation = p_GPA; }
  101. _FORCE_INLINE_ real_t get_spOv_gravityPointAttenuation() { return spOv_gravityPointAttenuation; }
  102. _FORCE_INLINE_ void set_spOv_gravityVec(Vector3 p_vec) { spOv_gravityVec = p_vec; }
  103. _FORCE_INLINE_ const Vector3 &get_spOv_gravityVec() const { return spOv_gravityVec; }
  104. _FORCE_INLINE_ void set_spOv_gravityMag(real_t p_gravityMag) { spOv_gravityMag = p_gravityMag; }
  105. _FORCE_INLINE_ real_t get_spOv_gravityMag() { return spOv_gravityMag; }
  106. _FORCE_INLINE_ void set_spOv_linearDump(real_t p_linearDump) { spOv_linearDump = p_linearDump; }
  107. _FORCE_INLINE_ real_t get_spOv_linearDamp() { return spOv_linearDump; }
  108. _FORCE_INLINE_ void set_spOv_angularDump(real_t p_angularDump) { spOv_angularDump = p_angularDump; }
  109. _FORCE_INLINE_ real_t get_spOv_angularDamp() { return spOv_angularDump; }
  110. _FORCE_INLINE_ void set_spOv_priority(int p_priority) { spOv_priority = p_priority; }
  111. _FORCE_INLINE_ int get_spOv_priority() { return spOv_priority; }
  112. virtual void main_shape_changed();
  113. virtual void reload_body();
  114. virtual void set_space(SpaceBullet *p_space);
  115. virtual void dispatch_callbacks();
  116. void call_event(CollisionObjectBullet *p_otherObject, PhysicsServer3D::AreaBodyStatus p_status);
  117. void scratch();
  118. void clear_overlaps(bool p_notify);
  119. // Dispatch the callbacks and removes from overlapping list
  120. void remove_overlap(CollisionObjectBullet *p_object, bool p_notify);
  121. virtual void on_collision_filters_change();
  122. virtual void on_collision_checker_start() {}
  123. virtual void on_collision_checker_end() { isTransformChanged = false; }
  124. void add_overlap(CollisionObjectBullet *p_otherObject);
  125. void put_overlap_as_exit(int p_index);
  126. void put_overlap_as_inside(int p_index);
  127. void set_param(PhysicsServer3D::AreaParameter p_param, const Variant &p_value);
  128. Variant get_param(PhysicsServer3D::AreaParameter p_param) const;
  129. void set_event_callback(Type p_callbackObjectType, const Callable &p_callback);
  130. bool has_event_callback(Type p_callbackObjectType);
  131. virtual void on_enter_area(AreaBullet *p_area);
  132. virtual void on_exit_area(AreaBullet *p_area);
  133. };
  134. #endif