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