kinematic_collision_2d.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**************************************************************************/
  2. /* kinematic_collision_2d.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_2d.h"
  31. #include "scene/2d/physics/physics_body_2d.h"
  32. Vector2 KinematicCollision2D::get_position() const {
  33. return result.collision_point;
  34. }
  35. Vector2 KinematicCollision2D::get_normal() const {
  36. return result.collision_normal;
  37. }
  38. Vector2 KinematicCollision2D::get_travel() const {
  39. return result.travel;
  40. }
  41. Vector2 KinematicCollision2D::get_remainder() const {
  42. return result.remainder;
  43. }
  44. real_t KinematicCollision2D::get_angle(const Vector2 &p_up_direction) const {
  45. ERR_FAIL_COND_V(p_up_direction == Vector2(), 0);
  46. return result.get_angle(p_up_direction);
  47. }
  48. real_t KinematicCollision2D::get_depth() const {
  49. return result.collision_depth;
  50. }
  51. Object *KinematicCollision2D::get_local_shape() const {
  52. PhysicsBody2D *owner = Object::cast_to<PhysicsBody2D>(ObjectDB::get_instance(owner_id));
  53. if (!owner) {
  54. return nullptr;
  55. }
  56. uint32_t ownerid = owner->shape_find_owner(result.collision_local_shape);
  57. return owner->shape_owner_get_owner(ownerid);
  58. }
  59. Object *KinematicCollision2D::get_collider() const {
  60. if (result.collider_id.is_valid()) {
  61. return ObjectDB::get_instance(result.collider_id);
  62. }
  63. return nullptr;
  64. }
  65. ObjectID KinematicCollision2D::get_collider_id() const {
  66. return result.collider_id;
  67. }
  68. RID KinematicCollision2D::get_collider_rid() const {
  69. return result.collider;
  70. }
  71. Object *KinematicCollision2D::get_collider_shape() const {
  72. Object *collider = get_collider();
  73. if (collider) {
  74. CollisionObject2D *obj2d = Object::cast_to<CollisionObject2D>(collider);
  75. if (obj2d) {
  76. uint32_t ownerid = obj2d->shape_find_owner(result.collider_shape);
  77. return obj2d->shape_owner_get_owner(ownerid);
  78. }
  79. }
  80. return nullptr;
  81. }
  82. int KinematicCollision2D::get_collider_shape_index() const {
  83. return result.collider_shape;
  84. }
  85. Vector2 KinematicCollision2D::get_collider_velocity() const {
  86. return result.collider_velocity;
  87. }
  88. void KinematicCollision2D::_bind_methods() {
  89. ClassDB::bind_method(D_METHOD("get_position"), &KinematicCollision2D::get_position);
  90. ClassDB::bind_method(D_METHOD("get_normal"), &KinematicCollision2D::get_normal);
  91. ClassDB::bind_method(D_METHOD("get_travel"), &KinematicCollision2D::get_travel);
  92. ClassDB::bind_method(D_METHOD("get_remainder"), &KinematicCollision2D::get_remainder);
  93. ClassDB::bind_method(D_METHOD("get_angle", "up_direction"), &KinematicCollision2D::get_angle, DEFVAL(Vector2(0.0, -1.0)));
  94. ClassDB::bind_method(D_METHOD("get_depth"), &KinematicCollision2D::get_depth);
  95. ClassDB::bind_method(D_METHOD("get_local_shape"), &KinematicCollision2D::get_local_shape);
  96. ClassDB::bind_method(D_METHOD("get_collider"), &KinematicCollision2D::get_collider);
  97. ClassDB::bind_method(D_METHOD("get_collider_id"), &KinematicCollision2D::get_collider_id);
  98. ClassDB::bind_method(D_METHOD("get_collider_rid"), &KinematicCollision2D::get_collider_rid);
  99. ClassDB::bind_method(D_METHOD("get_collider_shape"), &KinematicCollision2D::get_collider_shape);
  100. ClassDB::bind_method(D_METHOD("get_collider_shape_index"), &KinematicCollision2D::get_collider_shape_index);
  101. ClassDB::bind_method(D_METHOD("get_collider_velocity"), &KinematicCollision2D::get_collider_velocity);
  102. }