| 1 | /**************************************************************************/ |
| 2 | /* pin_joint_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 | |
| 31 | #include "pin_joint_3d.h" |
| 32 | |
| 33 | void PinJoint3D::_bind_methods() { |
| 34 | ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &PinJoint3D::set_param); |
| 35 | ClassDB::bind_method(D_METHOD("get_param", "param"), &PinJoint3D::get_param); |
| 36 | |
| 37 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "params/bias", PROPERTY_HINT_RANGE, "0.01,0.99,0.01"), "set_param", "get_param", PARAM_BIAS); |
| 38 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "params/damping", PROPERTY_HINT_RANGE, "0.01,8.0,0.01"), "set_param", "get_param", PARAM_DAMPING); |
| 39 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "params/impulse_clamp", PROPERTY_HINT_RANGE, "0.0,64.0,0.01"), "set_param", "get_param", PARAM_IMPULSE_CLAMP); |
| 40 | |
| 41 | BIND_ENUM_CONSTANT(PARAM_BIAS); |
| 42 | BIND_ENUM_CONSTANT(PARAM_DAMPING); |
| 43 | BIND_ENUM_CONSTANT(PARAM_IMPULSE_CLAMP); |
| 44 | } |
| 45 | |
| 46 | void PinJoint3D::set_param(Param p_param, real_t p_value) { |
| 47 | ERR_FAIL_INDEX(p_param, 3); |
| 48 | params[p_param] = p_value; |
| 49 | if (is_configured()) { |
| 50 | PhysicsServer3D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer3D::PinJointParam(p_param), p_value); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | real_t PinJoint3D::get_param(Param p_param) const { |
| 55 | ERR_FAIL_INDEX_V(p_param, 3, 0); |
| 56 | return params[p_param]; |
| 57 | } |
| 58 | |
| 59 | void PinJoint3D::_configure_joint(RID p_joint, PhysicsBody3D *body_a, PhysicsBody3D *body_b) { |
| 60 | Vector3 pinpos = get_global_transform().origin; |
| 61 | Vector3 local_a = body_a->to_local(pinpos); |
| 62 | Vector3 local_b; |
| 63 | |
| 64 | if (body_b) { |
| 65 | local_b = body_b->to_local(pinpos); |
| 66 | } else { |
| 67 | local_b = pinpos; |
| 68 | } |
| 69 | |
| 70 | PhysicsServer3D::get_singleton()->joint_make_pin(p_joint, body_a->get_rid(), local_a, body_b ? body_b->get_rid() : RID(), local_b); |
| 71 | for (int i = 0; i < 3; i++) { |
| 72 | PhysicsServer3D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer3D::PinJointParam(i), params[i]); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | PinJoint3D::PinJoint3D() { |
| 77 | params[PARAM_BIAS] = 0.3; |
| 78 | params[PARAM_DAMPING] = 1; |
| 79 | params[PARAM_IMPULSE_CLAMP] = 0; |
| 80 | } |