kinematic_collision_3d.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**************************************************************************/
  2. /* kinematic_collision_3d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "kinematic_collision_3d.h"
  31. #include "scene/3d/physics/character_body_3d.h"
  32. #include "scene/3d/physics/physics_body_3d.h"
  33. Vector3 KinematicCollision3D::get_travel() const {
  34. return result.travel;
  35. }
  36. Vector3 KinematicCollision3D::get_remainder() const {
  37. return result.remainder;
  38. }
  39. int KinematicCollision3D::get_collision_count() const {
  40. return result.collision_count;
  41. }
  42. real_t KinematicCollision3D::get_depth() const {
  43. return result.collision_depth;
  44. }
  45. Vector3 KinematicCollision3D::get_position(int p_collision_index) const {
  46. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, Vector3());
  47. return result.collisions[p_collision_index].position;
  48. }
  49. Vector3 KinematicCollision3D::get_normal(int p_collision_index) const {
  50. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, Vector3());
  51. return result.collisions[p_collision_index].normal;
  52. }
  53. real_t KinematicCollision3D::get_angle(int p_collision_index, const Vector3 &p_up_direction) const {
  54. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, 0.0);
  55. ERR_FAIL_COND_V(p_up_direction == Vector3(), 0);
  56. return result.collisions[p_collision_index].get_angle(p_up_direction);
  57. }
  58. Object *KinematicCollision3D::get_local_shape(int p_collision_index) const {
  59. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, nullptr);
  60. PhysicsBody3D *owner = Object::cast_to<PhysicsBody3D>(ObjectDB::get_instance(owner_id));
  61. if (!owner) {
  62. return nullptr;
  63. }
  64. uint32_t ownerid = owner->shape_find_owner(result.collisions[p_collision_index].local_shape);
  65. return owner->shape_owner_get_owner(ownerid);
  66. }
  67. Object *KinematicCollision3D::get_collider(int p_collision_index) const {
  68. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, nullptr);
  69. if (result.collisions[p_collision_index].collider_id.is_valid()) {
  70. return ObjectDB::get_instance(result.collisions[p_collision_index].collider_id);
  71. }
  72. return nullptr;
  73. }
  74. ObjectID KinematicCollision3D::get_collider_id(int p_collision_index) const {
  75. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, ObjectID());
  76. return result.collisions[p_collision_index].collider_id;
  77. }
  78. RID KinematicCollision3D::get_collider_rid(int p_collision_index) const {
  79. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, RID());
  80. return result.collisions[p_collision_index].collider;
  81. }
  82. Object *KinematicCollision3D::get_collider_shape(int p_collision_index) const {
  83. Object *collider = get_collider(p_collision_index);
  84. if (collider) {
  85. CollisionObject3D *obj2d = Object::cast_to<CollisionObject3D>(collider);
  86. if (obj2d) {
  87. uint32_t ownerid = obj2d->shape_find_owner(result.collisions[p_collision_index].collider_shape);
  88. return obj2d->shape_owner_get_owner(ownerid);
  89. }
  90. }
  91. return nullptr;
  92. }
  93. int KinematicCollision3D::get_collider_shape_index(int p_collision_index) const {
  94. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, 0);
  95. return result.collisions[p_collision_index].collider_shape;
  96. }
  97. Vector3 KinematicCollision3D::get_collider_velocity(int p_collision_index) const {
  98. ERR_FAIL_INDEX_V(p_collision_index, result.collision_count, Vector3());
  99. return result.collisions[p_collision_index].collider_velocity;
  100. }
  101. void KinematicCollision3D::_bind_methods() {
  102. ClassDB::bind_method(D_METHOD("get_travel"), &KinematicCollision3D::get_travel);
  103. ClassDB::bind_method(D_METHOD("get_remainder"), &KinematicCollision3D::get_remainder);
  104. ClassDB::bind_method(D_METHOD("get_depth"), &KinematicCollision3D::get_depth);
  105. ClassDB::bind_method(D_METHOD("get_collision_count"), &KinematicCollision3D::get_collision_count);
  106. ClassDB::bind_method(D_METHOD("get_position", "collision_index"), &KinematicCollision3D::get_position, DEFVAL(0));
  107. ClassDB::bind_method(D_METHOD("get_normal", "collision_index"), &KinematicCollision3D::get_normal, DEFVAL(0));
  108. ClassDB::bind_method(D_METHOD("get_angle", "collision_index", "up_direction"), &KinematicCollision3D::get_angle, DEFVAL(0), DEFVAL(Vector3(0.0, 1.0, 0.0)));
  109. ClassDB::bind_method(D_METHOD("get_local_shape", "collision_index"), &KinematicCollision3D::get_local_shape, DEFVAL(0));
  110. ClassDB::bind_method(D_METHOD("get_collider", "collision_index"), &KinematicCollision3D::get_collider, DEFVAL(0));
  111. ClassDB::bind_method(D_METHOD("get_collider_id", "collision_index"), &KinematicCollision3D::get_collider_id, DEFVAL(0));
  112. ClassDB::bind_method(D_METHOD("get_collider_rid", "collision_index"), &KinematicCollision3D::get_collider_rid, DEFVAL(0));
  113. ClassDB::bind_method(D_METHOD("get_collider_shape", "collision_index"), &KinematicCollision3D::get_collider_shape, DEFVAL(0));
  114. ClassDB::bind_method(D_METHOD("get_collider_shape_index", "collision_index"), &KinematicCollision3D::get_collider_shape_index, DEFVAL(0));
  115. ClassDB::bind_method(D_METHOD("get_collider_velocity", "collision_index"), &KinematicCollision3D::get_collider_velocity, DEFVAL(0));
  116. }