| 1 | /**************************************************************************/ |
| 2 | /* pin_joint_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 "pin_joint_2d.h" |
| 32 | |
| 33 | #include "scene/2d/physics/physics_body_2d.h" |
| 34 | |
| 35 | void PinJoint2D::_notification(int p_what) { |
| 36 | switch (p_what) { |
| 37 | case NOTIFICATION_DRAW: { |
| 38 | if (!is_inside_tree()) { |
| 39 | break; |
| 40 | } |
| 41 | |
| 42 | if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) { |
| 43 | break; |
| 44 | } |
| 45 | |
| 46 | draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3); |
| 47 | draw_line(Point2(0, -10), Point2(0, +10), Color(0.7, 0.6, 0.0, 0.5), 3); |
| 48 | } break; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void PinJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) { |
| 53 | PhysicsServer2D::get_singleton()->joint_make_pin(p_joint, get_global_position(), body_a->get_rid(), body_b ? body_b->get_rid() : RID()); |
| 54 | PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_SOFTNESS, softness); |
| 55 | PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_LIMIT_UPPER, angular_limit_upper); |
| 56 | PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_LIMIT_LOWER, angular_limit_lower); |
| 57 | PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_MOTOR_TARGET_VELOCITY, motor_target_velocity); |
| 58 | PhysicsServer2D::get_singleton()->pin_joint_set_flag(p_joint, PhysicsServer2D::PIN_JOINT_FLAG_MOTOR_ENABLED, motor_enabled); |
| 59 | PhysicsServer2D::get_singleton()->pin_joint_set_flag(p_joint, PhysicsServer2D::PIN_JOINT_FLAG_ANGULAR_LIMIT_ENABLED, angular_limit_enabled); |
| 60 | } |
| 61 | |
| 62 | void PinJoint2D::set_softness(real_t p_softness) { |
| 63 | if (softness == p_softness) { |
| 64 | return; |
| 65 | } |
| 66 | softness = p_softness; |
| 67 | queue_redraw(); |
| 68 | if (is_configured()) { |
| 69 | PhysicsServer2D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer2D::PIN_JOINT_SOFTNESS, p_softness); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | real_t PinJoint2D::get_softness() const { |
| 74 | return softness; |
| 75 | } |
| 76 | |
| 77 | void PinJoint2D::set_angular_limit_lower(real_t p_angular_limit_lower) { |
| 78 | if (angular_limit_lower == p_angular_limit_lower) { |
| 79 | return; |
| 80 | } |
| 81 | angular_limit_lower = p_angular_limit_lower; |
| 82 | queue_redraw(); |
| 83 | if (is_configured()) { |
| 84 | PhysicsServer2D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer2D::PIN_JOINT_LIMIT_LOWER, p_angular_limit_lower); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | real_t PinJoint2D::get_angular_limit_lower() const { |
| 89 | return angular_limit_lower; |
| 90 | } |
| 91 | |
| 92 | void PinJoint2D::set_angular_limit_upper(real_t p_angular_limit_upper) { |
| 93 | if (angular_limit_upper == p_angular_limit_upper) { |
| 94 | return; |
| 95 | } |
| 96 | angular_limit_upper = p_angular_limit_upper; |
| 97 | queue_redraw(); |
| 98 | if (is_configured()) { |
| 99 | PhysicsServer2D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer2D::PIN_JOINT_LIMIT_UPPER, p_angular_limit_upper); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | real_t PinJoint2D::get_angular_limit_upper() const { |
| 104 | return angular_limit_upper; |
| 105 | } |
| 106 | |
| 107 | void PinJoint2D::set_motor_target_velocity(real_t p_motor_target_velocity) { |
| 108 | if (motor_target_velocity == p_motor_target_velocity) { |
| 109 | return; |
| 110 | } |
| 111 | motor_target_velocity = p_motor_target_velocity; |
| 112 | queue_redraw(); |
| 113 | if (is_configured()) { |
| 114 | PhysicsServer2D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer2D::PIN_JOINT_MOTOR_TARGET_VELOCITY, motor_target_velocity); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | real_t PinJoint2D::get_motor_target_velocity() const { |
| 119 | return motor_target_velocity; |
| 120 | } |
| 121 | |
| 122 | void PinJoint2D::set_motor_enabled(bool p_motor_enabled) { |
| 123 | if (motor_enabled == p_motor_enabled) { |
| 124 | return; |
| 125 | } |
| 126 | motor_enabled = p_motor_enabled; |
| 127 | queue_redraw(); |
| 128 | if (is_configured()) { |
| 129 | PhysicsServer2D::get_singleton()->pin_joint_set_flag(get_rid(), PhysicsServer2D::PIN_JOINT_FLAG_MOTOR_ENABLED, motor_enabled); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | bool PinJoint2D::is_motor_enabled() const { |
| 134 | return motor_enabled; |
| 135 | } |
| 136 | |
| 137 | void PinJoint2D::set_angular_limit_enabled(bool p_angular_limit_enabled) { |
| 138 | if (angular_limit_enabled == p_angular_limit_enabled) { |
| 139 | return; |
| 140 | } |
| 141 | angular_limit_enabled = p_angular_limit_enabled; |
| 142 | queue_redraw(); |
| 143 | if (is_configured()) { |
| 144 | PhysicsServer2D::get_singleton()->pin_joint_set_flag(get_rid(), PhysicsServer2D::PIN_JOINT_FLAG_ANGULAR_LIMIT_ENABLED, angular_limit_enabled); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | bool PinJoint2D::is_angular_limit_enabled() const { |
| 149 | return angular_limit_enabled; |
| 150 | } |
| 151 | |
| 152 | void PinJoint2D::_bind_methods() { |
| 153 | ClassDB::bind_method(D_METHOD("set_softness", "softness"), &PinJoint2D::set_softness); |
| 154 | ClassDB::bind_method(D_METHOD("get_softness"), &PinJoint2D::get_softness); |
| 155 | ClassDB::bind_method(D_METHOD("set_angular_limit_lower", "angular_limit_lower"), &PinJoint2D::set_angular_limit_lower); |
| 156 | ClassDB::bind_method(D_METHOD("get_angular_limit_lower"), &PinJoint2D::get_angular_limit_lower); |
| 157 | ClassDB::bind_method(D_METHOD("set_angular_limit_upper", "angular_limit_upper"), &PinJoint2D::set_angular_limit_upper); |
| 158 | ClassDB::bind_method(D_METHOD("get_angular_limit_upper"), &PinJoint2D::get_angular_limit_upper); |
| 159 | ClassDB::bind_method(D_METHOD("set_motor_target_velocity", "motor_target_velocity"), &PinJoint2D::set_motor_target_velocity); |
| 160 | ClassDB::bind_method(D_METHOD("get_motor_target_velocity"), &PinJoint2D::get_motor_target_velocity); |
| 161 | ClassDB::bind_method(D_METHOD("set_motor_enabled", "enabled"), &PinJoint2D::set_motor_enabled); |
| 162 | ClassDB::bind_method(D_METHOD("is_motor_enabled"), &PinJoint2D::is_motor_enabled); |
| 163 | ClassDB::bind_method(D_METHOD("set_angular_limit_enabled", "enabled"), &PinJoint2D::set_angular_limit_enabled); |
| 164 | ClassDB::bind_method(D_METHOD("is_angular_limit_enabled"), &PinJoint2D::is_angular_limit_enabled); |
| 165 | |
| 166 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "softness", PROPERTY_HINT_RANGE, "0.00,16,0.01,exp"), "set_softness", "get_softness"); |
| 167 | ADD_GROUP("Angular Limit", "angular_limit_"); |
| 168 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "angular_limit_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_angular_limit_enabled", "is_angular_limit_enabled"); |
| 169 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_limit_lower", PROPERTY_HINT_RANGE, "-180,180,0.1,radians_as_degrees"), "set_angular_limit_lower", "get_angular_limit_lower"); |
| 170 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_limit_upper", PROPERTY_HINT_RANGE, "-180,180,0.1,radians_as_degrees"), "set_angular_limit_upper", "get_angular_limit_upper"); |
| 171 | ADD_GROUP("Motor", "motor_"); |
| 172 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "motor_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_motor_enabled", "is_motor_enabled"); |
| 173 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "motor_target_velocity", PROPERTY_HINT_RANGE, U"-200,200,0.01,or_greater,or_less,radians_as_degrees,suffix:\u00B0/s"), "set_motor_target_velocity", "get_motor_target_velocity"); |
| 174 | } |
| 175 | |
| 176 | PinJoint2D::PinJoint2D() { |
| 177 | } |