0.2.0-dev Download ZIP
132 lines ·6.1 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* damped_spring_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 "damped_spring_joint_2d.h" |
| 32 | |
| 33 | #include "core/config/engine.h" |
| 34 | #include "core/object/class_db.h" |
| 35 | #include "scene/2d/physics/physics_body_2d.h" |
| 36 | #include "scene/main/scene_tree.h" |
| 37 | #include "servers/physics_2d/physics_server_2d.h" |
| 38 | |
| 39 | void DampedSpringJoint2D::_notification(int p_what) { |
| 40 | switch (p_what) { |
| 41 | case NOTIFICATION_DRAW: { |
| 42 | if (!is_inside_tree()) { |
| 43 | break; |
| 44 | } |
| 45 | |
| 46 | if (!Engine::get_singleton()->is_editor_hint() && !PhysicsServer2D::get_singleton()->debug_is_enabled()) { |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3); |
| 51 | draw_line(Point2(-10, length), Point2(+10, length), Color(0.7, 0.6, 0.0, 0.5), 3); |
| 52 | draw_line(Point2(0, 0), Point2(0, length), Color(0.7, 0.6, 0.0, 0.5), 3); |
| 53 | } break; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void DampedSpringJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) { |
| 58 | Transform2D gt = get_global_transform(); |
| 59 | Vector2 anchor_A = gt.get_origin(); |
| 60 | Vector2 anchor_B = gt.xform(Vector2(0, length)); |
| 61 | |
| 62 | PhysicsServer2D::get_singleton()->joint_make_damped_spring(p_joint, anchor_A, anchor_B, body_a->get_rid(), body_b->get_rid()); |
| 63 | if (rest_length) { |
| 64 | PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PS2DE::DAMPED_SPRING_REST_LENGTH, rest_length); |
| 65 | } |
| 66 | PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PS2DE::DAMPED_SPRING_STIFFNESS, stiffness); |
| 67 | PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PS2DE::DAMPED_SPRING_DAMPING, damping); |
| 68 | } |
| 69 | |
| 70 | void DampedSpringJoint2D::set_length(real_t p_length) { |
| 71 | length = p_length; |
| 72 | queue_redraw(); |
| 73 | } |
| 74 | |
| 75 | real_t DampedSpringJoint2D::get_length() const { |
| 76 | return length; |
| 77 | } |
| 78 | |
| 79 | void DampedSpringJoint2D::set_rest_length(real_t p_rest_length) { |
| 80 | rest_length = p_rest_length; |
| 81 | queue_redraw(); |
| 82 | if (is_configured()) { |
| 83 | PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_rid(), PS2DE::DAMPED_SPRING_REST_LENGTH, p_rest_length ? p_rest_length : length); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | real_t DampedSpringJoint2D::get_rest_length() const { |
| 88 | return rest_length; |
| 89 | } |
| 90 | |
| 91 | void DampedSpringJoint2D::set_stiffness(real_t p_stiffness) { |
| 92 | stiffness = p_stiffness; |
| 93 | queue_redraw(); |
| 94 | if (is_configured()) { |
| 95 | PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_rid(), PS2DE::DAMPED_SPRING_STIFFNESS, p_stiffness); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | real_t DampedSpringJoint2D::get_stiffness() const { |
| 100 | return stiffness; |
| 101 | } |
| 102 | |
| 103 | void DampedSpringJoint2D::set_damping(real_t p_damping) { |
| 104 | damping = p_damping; |
| 105 | queue_redraw(); |
| 106 | if (is_configured()) { |
| 107 | PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_rid(), PS2DE::DAMPED_SPRING_DAMPING, p_damping); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | real_t DampedSpringJoint2D::get_damping() const { |
| 112 | return damping; |
| 113 | } |
| 114 | |
| 115 | void DampedSpringJoint2D::_bind_methods() { |
| 116 | ClassDB::bind_method(D_METHOD("set_length", "length"), &DampedSpringJoint2D::set_length); |
| 117 | ClassDB::bind_method(D_METHOD("get_length"), &DampedSpringJoint2D::get_length); |
| 118 | ClassDB::bind_method(D_METHOD("set_rest_length", "rest_length"), &DampedSpringJoint2D::set_rest_length); |
| 119 | ClassDB::bind_method(D_METHOD("get_rest_length"), &DampedSpringJoint2D::get_rest_length); |
| 120 | ClassDB::bind_method(D_METHOD("set_stiffness", "stiffness"), &DampedSpringJoint2D::set_stiffness); |
| 121 | ClassDB::bind_method(D_METHOD("get_stiffness"), &DampedSpringJoint2D::get_stiffness); |
| 122 | ClassDB::bind_method(D_METHOD("set_damping", "damping"), &DampedSpringJoint2D::set_damping); |
| 123 | ClassDB::bind_method(D_METHOD("get_damping"), &DampedSpringJoint2D::get_damping); |
| 124 | |
| 125 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_length", "get_length"); |
| 126 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rest_length", PROPERTY_HINT_RANGE, "0,65535,1,exp,suffix:px"), "set_rest_length", "get_rest_length"); |
| 127 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stiffness", PROPERTY_HINT_RANGE, "0.1,64,0.1,exp"), "set_stiffness", "get_stiffness"); |
| 128 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping", PROPERTY_HINT_RANGE, "0.01,16,0.01,exp"), "set_damping", "get_damping"); |
| 129 | } |
| 130 | |
| 131 | DampedSpringJoint2D::DampedSpringJoint2D() { |
| 132 | } |