2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* joint_2d.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2021-10-01 19:00:32 +00:00
|
|
|
#include "joint_2d.h"
|
2017-08-18 23:02:56 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "physics_body_2d.h"
|
2020-12-26 14:23:30 +00:00
|
|
|
#include "scene/scene_string_names.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-12-26 14:23:30 +00:00
|
|
|
void Joint2D::_disconnect_signals() {
|
|
|
|
Node *node_a = get_node_or_null(a);
|
|
|
|
PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a);
|
|
|
|
if (body_a) {
|
|
|
|
body_a->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint2D::_body_exit_tree));
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *node_b = get_node_or_null(b);
|
|
|
|
PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b);
|
|
|
|
if (body_b) {
|
|
|
|
body_b->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint2D::_body_exit_tree));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:48:44 +00:00
|
|
|
void Joint2D::_body_exit_tree() {
|
2020-12-26 14:23:30 +00:00
|
|
|
_disconnect_signals();
|
2021-03-02 00:48:44 +00:00
|
|
|
_update_joint(true);
|
2022-05-23 09:39:44 +00:00
|
|
|
update_configuration_warnings();
|
2020-12-26 14:23:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:15:12 +00:00
|
|
|
void Joint2D::_update_joint(bool p_only_free) {
|
2021-02-09 16:19:03 +00:00
|
|
|
if (ba.is_valid() && bb.is_valid() && exclude_from_collision) {
|
|
|
|
PhysicsServer2D::get_singleton()->joint_disable_collisions_between_bodies(joint, false);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
ba = RID();
|
|
|
|
bb = RID();
|
|
|
|
configured = false;
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_only_free || !is_inside_tree()) {
|
2021-02-09 16:19:03 +00:00
|
|
|
PhysicsServer2D::get_singleton()->joint_clear(joint);
|
2020-11-25 18:21:33 +00:00
|
|
|
warning = String();
|
2017-08-29 21:15:12 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-08-29 21:15:12 +00:00
|
|
|
|
2020-12-26 14:23:30 +00:00
|
|
|
Node *node_a = get_node_or_null(a);
|
|
|
|
Node *node_b = get_node_or_null(b);
|
2017-08-29 21:15:12 +00:00
|
|
|
|
2020-11-25 18:21:33 +00:00
|
|
|
PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a);
|
|
|
|
PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b);
|
|
|
|
|
2021-10-07 17:18:44 +00:00
|
|
|
bool valid = false;
|
|
|
|
|
2020-11-25 18:21:33 +00:00
|
|
|
if (node_a && !body_a && node_b && !body_b) {
|
2022-03-28 13:24:14 +00:00
|
|
|
warning = RTR("Node A and Node B must be PhysicsBody2Ds");
|
2020-10-29 10:01:28 +00:00
|
|
|
} else if (node_a && !body_a) {
|
2022-03-28 13:24:14 +00:00
|
|
|
warning = RTR("Node A must be a PhysicsBody2D");
|
2020-10-29 10:01:28 +00:00
|
|
|
} else if (node_b && !body_b) {
|
2022-03-28 13:24:14 +00:00
|
|
|
warning = RTR("Node B must be a PhysicsBody2D");
|
2020-10-29 10:01:28 +00:00
|
|
|
} else if (!body_a || !body_b) {
|
2022-03-28 13:24:14 +00:00
|
|
|
warning = RTR("Joint is not connected to two PhysicsBody2Ds");
|
2020-10-29 10:01:28 +00:00
|
|
|
} else if (body_a == body_b) {
|
2022-03-28 13:24:14 +00:00
|
|
|
warning = RTR("Node A and Node B must be different PhysicsBody2Ds");
|
2020-10-29 10:01:28 +00:00
|
|
|
} else {
|
|
|
|
warning = String();
|
2021-10-07 17:18:44 +00:00
|
|
|
valid = true;
|
2020-11-25 18:21:33 +00:00
|
|
|
}
|
2017-08-29 21:15:12 +00:00
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
update_configuration_warnings();
|
|
|
|
|
2021-10-07 17:18:44 +00:00
|
|
|
if (!valid) {
|
2021-02-09 16:19:03 +00:00
|
|
|
PhysicsServer2D::get_singleton()->joint_clear(joint);
|
2017-08-29 21:15:12 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-12-24 03:04:22 +00:00
|
|
|
if (body_a) {
|
|
|
|
body_a->force_update_transform();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body_b) {
|
|
|
|
body_b->force_update_transform();
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
configured = true;
|
|
|
|
|
|
|
|
_configure_joint(joint, body_a, body_b);
|
2020-11-25 18:21:33 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND_MSG(!joint.is_valid(), "Failed to configure the joint.");
|
|
|
|
|
2022-12-20 19:23:47 +00:00
|
|
|
PhysicsServer2D::get_singleton()->joint_set_param(joint, PhysicsServer2D::JOINT_PARAM_BIAS, bias);
|
2017-08-29 21:15:12 +00:00
|
|
|
|
|
|
|
ba = body_a->get_rid();
|
|
|
|
bb = body_b->get_rid();
|
|
|
|
|
2021-03-02 00:48:44 +00:00
|
|
|
body_a->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint2D::_body_exit_tree));
|
|
|
|
body_b->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint2D::_body_exit_tree));
|
2020-12-26 14:23:30 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
PhysicsServer2D::get_singleton()->joint_disable_collisions_between_bodies(joint, exclude_from_collision);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Joint2D::set_node_a(const NodePath &p_node_a) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (a == p_node_a) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-02-27 15:55:57 +00:00
|
|
|
if (is_configured()) {
|
2020-12-26 14:23:30 +00:00
|
|
|
_disconnect_signals();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
a = p_node_a;
|
2022-10-21 13:37:36 +00:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
// When in editor, the setter may be called as a result of node rename.
|
|
|
|
// It happens before the node actually changes its name, which triggers false warning.
|
|
|
|
callable_mp(this, &Joint2D::_update_joint).call_deferred();
|
|
|
|
} else {
|
|
|
|
_update_joint();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
NodePath Joint2D::get_node_a() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Joint2D::set_node_b(const NodePath &p_node_b) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (b == p_node_b) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-12-26 14:23:30 +00:00
|
|
|
|
2022-02-27 15:55:57 +00:00
|
|
|
if (is_configured()) {
|
2020-12-26 14:23:30 +00:00
|
|
|
_disconnect_signals();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
b = p_node_b;
|
2022-10-21 13:37:36 +00:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
callable_mp(this, &Joint2D::_update_joint).call_deferred();
|
|
|
|
} else {
|
|
|
|
_update_joint();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
NodePath Joint2D::get_node_b() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Joint2D::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2022-02-28 21:21:32 +00:00
|
|
|
case NOTIFICATION_POST_ENTER_TREE: {
|
|
|
|
if (is_configured()) {
|
|
|
|
_disconnect_signals();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_joint();
|
|
|
|
} break;
|
2022-02-15 17:06:48 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2022-02-27 15:55:57 +00:00
|
|
|
if (is_configured()) {
|
2020-12-26 14:23:30 +00:00
|
|
|
_disconnect_signals();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2022-02-28 21:21:32 +00:00
|
|
|
_update_joint(true);
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Joint2D::set_bias(real_t p_bias) {
|
2017-03-05 15:44:50 +00:00
|
|
|
bias = p_bias;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (joint.is_valid()) {
|
2022-12-20 19:23:47 +00:00
|
|
|
PhysicsServer2D::get_singleton()->joint_set_param(joint, PhysicsServer2D::JOINT_PARAM_BIAS, bias);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
real_t Joint2D::get_bias() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return bias;
|
|
|
|
}
|
|
|
|
|
2015-12-08 20:47:12 +00:00
|
|
|
void Joint2D::set_exclude_nodes_from_collision(bool p_enable) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (exclude_from_collision == p_enable) {
|
2015-12-08 20:47:12 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2022-02-28 21:25:45 +00:00
|
|
|
if (is_configured()) {
|
|
|
|
_disconnect_signals();
|
|
|
|
}
|
2019-10-17 17:22:41 +00:00
|
|
|
_update_joint(true);
|
2017-03-05 15:44:50 +00:00
|
|
|
exclude_from_collision = p_enable;
|
2015-12-08 20:47:12 +00:00
|
|
|
_update_joint();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool Joint2D::get_exclude_nodes_from_collision() const {
|
2015-12-08 20:47:12 +00:00
|
|
|
return exclude_from_collision;
|
|
|
|
}
|
|
|
|
|
2022-09-19 15:43:15 +00:00
|
|
|
PackedStringArray Joint2D::get_configuration_warnings() const {
|
|
|
|
PackedStringArray warnings = Node2D::get_configuration_warnings();
|
2020-11-25 18:21:33 +00:00
|
|
|
|
2020-12-15 12:04:21 +00:00
|
|
|
if (!warning.is_empty()) {
|
2020-10-29 10:01:28 +00:00
|
|
|
warnings.push_back(warning);
|
2020-11-25 18:21:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 10:01:28 +00:00
|
|
|
return warnings;
|
2020-11-25 18:21:33 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Joint2D::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_node_a", "node"), &Joint2D::set_node_a);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_node_a"), &Joint2D::get_node_a);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_node_b", "node"), &Joint2D::set_node_b);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_node_b"), &Joint2D::get_node_b);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_bias", "bias"), &Joint2D::set_bias);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bias"), &Joint2D::get_bias);
|
2015-12-08 20:47:12 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_exclude_nodes_from_collision", "enable"), &Joint2D::set_exclude_nodes_from_collision);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_exclude_nodes_from_collision"), &Joint2D::get_exclude_nodes_from_collision);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2023-08-17 14:30:18 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_rid"), &Joint2D::get_rid);
|
|
|
|
|
2020-11-25 18:21:33 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_a", "get_node_a");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_b", "get_node_b");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0,0.9,0.001"), "set_bias", "get_bias");
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_collision"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Joint2D::Joint2D() {
|
2021-02-09 16:19:03 +00:00
|
|
|
joint = PhysicsServer2D::get_singleton()->joint_create();
|
2023-01-26 12:17:26 +00:00
|
|
|
set_hide_clip_children(true);
|
2021-02-09 16:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Joint2D::~Joint2D() {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(PhysicsServer2D::get_singleton());
|
2021-02-09 16:19:03 +00:00
|
|
|
PhysicsServer2D::get_singleton()->free(joint);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void PinJoint2D::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2014-02-10 01:10:30 +00:00
|
|
|
case NOTIFICATION_DRAW: {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_inside_tree()) {
|
2015-12-29 22:07:37 +00:00
|
|
|
break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-12-29 22:07:37 +00:00
|
|
|
|
2017-08-18 23:02:56 +00:00
|
|
|
if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
|
2015-12-29 22:07:37 +00:00
|
|
|
break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2015-12-29 22:07:37 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
|
|
|
|
draw_line(Point2(0, -10), Point2(0, +10), Color(0.7, 0.6, 0.0, 0.5), 3);
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
void PinJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
|
2021-08-14 08:16:57 +00:00
|
|
|
PhysicsServer2D::get_singleton()->joint_make_pin(p_joint, get_global_position(), body_a->get_rid(), body_b ? body_b->get_rid() : RID());
|
2021-02-09 16:19:03 +00:00
|
|
|
PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_SOFTNESS, softness);
|
2023-09-13 11:45:24 +00:00
|
|
|
PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_LIMIT_UPPER, angular_limit_upper);
|
|
|
|
PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_LIMIT_LOWER, angular_limit_lower);
|
|
|
|
PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_MOTOR_TARGET_VELOCITY, motor_target_velocity);
|
|
|
|
PhysicsServer2D::get_singleton()->pin_joint_set_flag(p_joint, PhysicsServer2D::PIN_JOINT_FLAG_MOTOR_ENABLED, motor_enabled);
|
|
|
|
PhysicsServer2D::get_singleton()->pin_joint_set_flag(p_joint, PhysicsServer2D::PIN_JOINT_FLAG_ANGULAR_LIMIT_ENABLED, angular_limit_enabled);
|
2015-10-10 20:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PinJoint2D::set_softness(real_t p_softness) {
|
2023-09-13 11:45:24 +00:00
|
|
|
if (softness == p_softness) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
softness = p_softness;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2021-02-09 16:19:03 +00:00
|
|
|
if (is_configured()) {
|
2023-08-17 14:30:18 +00:00
|
|
|
PhysicsServer2D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer2D::PIN_JOINT_SOFTNESS, p_softness);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2015-10-10 20:28:05 +00:00
|
|
|
real_t PinJoint2D::get_softness() const {
|
|
|
|
return softness;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2023-09-13 11:45:24 +00:00
|
|
|
void PinJoint2D::set_angular_limit_lower(real_t p_angular_limit_lower) {
|
|
|
|
if (angular_limit_lower == p_angular_limit_lower) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
angular_limit_lower = p_angular_limit_lower;
|
|
|
|
queue_redraw();
|
|
|
|
if (is_configured()) {
|
|
|
|
PhysicsServer2D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer2D::PIN_JOINT_LIMIT_LOWER, p_angular_limit_lower);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t PinJoint2D::get_angular_limit_lower() const {
|
|
|
|
return angular_limit_lower;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PinJoint2D::set_angular_limit_upper(real_t p_angular_limit_upper) {
|
|
|
|
if (angular_limit_upper == p_angular_limit_upper) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
angular_limit_upper = p_angular_limit_upper;
|
|
|
|
queue_redraw();
|
|
|
|
if (is_configured()) {
|
|
|
|
PhysicsServer2D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer2D::PIN_JOINT_LIMIT_UPPER, p_angular_limit_upper);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t PinJoint2D::get_angular_limit_upper() const {
|
|
|
|
return angular_limit_upper;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PinJoint2D::set_motor_target_velocity(real_t p_motor_target_velocity) {
|
|
|
|
if (motor_target_velocity == p_motor_target_velocity) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
motor_target_velocity = p_motor_target_velocity;
|
|
|
|
queue_redraw();
|
|
|
|
if (is_configured()) {
|
|
|
|
PhysicsServer2D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer2D::PIN_JOINT_MOTOR_TARGET_VELOCITY, motor_target_velocity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t PinJoint2D::get_motor_target_velocity() const {
|
|
|
|
return motor_target_velocity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PinJoint2D::set_motor_enabled(bool p_motor_enabled) {
|
|
|
|
if (motor_enabled == p_motor_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
motor_enabled = p_motor_enabled;
|
|
|
|
queue_redraw();
|
|
|
|
if (is_configured()) {
|
|
|
|
PhysicsServer2D::get_singleton()->pin_joint_set_flag(get_rid(), PhysicsServer2D::PIN_JOINT_FLAG_MOTOR_ENABLED, motor_enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PinJoint2D::is_motor_enabled() const {
|
|
|
|
return motor_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PinJoint2D::set_angular_limit_enabled(bool p_angular_limit_enabled) {
|
|
|
|
if (angular_limit_enabled == p_angular_limit_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
angular_limit_enabled = p_angular_limit_enabled;
|
|
|
|
queue_redraw();
|
|
|
|
if (is_configured()) {
|
|
|
|
PhysicsServer2D::get_singleton()->pin_joint_set_flag(get_rid(), PhysicsServer2D::PIN_JOINT_FLAG_ANGULAR_LIMIT_ENABLED, angular_limit_enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PinJoint2D::is_angular_limit_enabled() const {
|
|
|
|
return angular_limit_enabled;
|
|
|
|
}
|
|
|
|
|
2015-10-10 20:28:05 +00:00
|
|
|
void PinJoint2D::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_softness", "softness"), &PinJoint2D::set_softness);
|
2017-02-13 11:47:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_softness"), &PinJoint2D::get_softness);
|
2023-09-13 11:45:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_angular_limit_lower", "angular_limit_lower"), &PinJoint2D::set_angular_limit_lower);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_angular_limit_lower"), &PinJoint2D::get_angular_limit_lower);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_angular_limit_upper", "angular_limit_upper"), &PinJoint2D::set_angular_limit_upper);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_angular_limit_upper"), &PinJoint2D::get_angular_limit_upper);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_motor_target_velocity", "motor_target_velocity"), &PinJoint2D::set_motor_target_velocity);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_motor_target_velocity"), &PinJoint2D::get_motor_target_velocity);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_motor_enabled", "enabled"), &PinJoint2D::set_motor_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_motor_enabled"), &PinJoint2D::is_motor_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_angular_limit_enabled", "enabled"), &PinJoint2D::set_angular_limit_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_angular_limit_enabled"), &PinJoint2D::is_angular_limit_enabled);
|
2015-10-10 20:28:05 +00:00
|
|
|
|
Fix editor suffixes and degrees conversion
* Functions to convert to/from degrees are all gone. Conversion is done by the editor.
* Use PROPERTY_HINT_ANGLE instead of PROPERTY_HINT_RANGE to edit radian angles in degrees.
* Added possibility to add suffixes to range properties, use "min,max[,step][,suffix:<something>]" example "0,100,1,suffix:m"
* In general, can add suffixes for EditorSpinSlider
Not covered by this PR, will have to be addressed by future ones:
* Ability to switch radians/degrees in the inspector for angle properties (if actually wanted).
* Animations previously made will most likely break, need to add a way to make old ones compatible.
* Only added a "px" suffix to 2D position and a "m" one to 3D position, someone needs to go through the rest of the engine and add all remaining suffixes.
* Likely also need to track down usage of EditorSpinSlider outside properties to add suffixes to it too.
2021-06-29 19:42:12 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "softness", PROPERTY_HINT_RANGE, "0.00,16,0.01,exp"), "set_softness", "get_softness");
|
2023-09-13 11:45:24 +00:00
|
|
|
ADD_GROUP("Angular Limit", "angular_limit_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "angular_limit_enabled"), "set_angular_limit_enabled", "is_angular_limit_enabled");
|
|
|
|
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");
|
|
|
|
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");
|
|
|
|
ADD_GROUP("Motor", "motor_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "motor_enabled"), "set_motor_enabled", "is_motor_enabled");
|
|
|
|
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");
|
2015-10-10 20:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PinJoint2D::PinJoint2D() {
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void GrooveJoint2D::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2014-02-10 01:10:30 +00:00
|
|
|
case NOTIFICATION_DRAW: {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_inside_tree()) {
|
2015-12-29 22:07:37 +00:00
|
|
|
break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-18 23:02:56 +00:00
|
|
|
if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
|
2015-12-29 22:07:37 +00:00
|
|
|
break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2015-12-29 22:07:37 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
|
|
|
|
draw_line(Point2(-10, length), Point2(+10, length), Color(0.7, 0.6, 0.0, 0.5), 3);
|
|
|
|
draw_line(Point2(0, 0), Point2(0, length), Color(0.7, 0.6, 0.0, 0.5), 3);
|
|
|
|
draw_line(Point2(-10, initial_offset), Point2(+10, initial_offset), Color(0.8, 0.8, 0.9, 0.5), 5);
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
void GrooveJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
|
2017-01-11 03:52:51 +00:00
|
|
|
Transform2D gt = get_global_transform();
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector2 groove_A1 = gt.get_origin();
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 groove_A2 = gt.xform(Vector2(0, length));
|
|
|
|
Vector2 anchor_B = gt.xform(Vector2(0, initial_offset));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
PhysicsServer2D::get_singleton()->joint_make_groove(p_joint, groove_A1, groove_A2, anchor_B, body_a->get_rid(), body_b->get_rid());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GrooveJoint2D::set_length(real_t p_length) {
|
2017-03-05 15:44:50 +00:00
|
|
|
length = p_length;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
real_t GrooveJoint2D::get_length() const {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GrooveJoint2D::set_initial_offset(real_t p_initial_offset) {
|
2017-03-05 15:44:50 +00:00
|
|
|
initial_offset = p_initial_offset;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
real_t GrooveJoint2D::get_initial_offset() const {
|
|
|
|
return initial_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GrooveJoint2D::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_length", "length"), &GrooveJoint2D::set_length);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_length"), &GrooveJoint2D::get_length);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_initial_offset", "offset"), &GrooveJoint2D::set_initial_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_initial_offset"), &GrooveJoint2D::get_initial_offset);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-12-03 00:09:19 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_length", "get_length");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "initial_offset", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_initial_offset", "get_initial_offset");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GrooveJoint2D::GrooveJoint2D() {
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void DampedSpringJoint2D::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2014-02-10 01:10:30 +00:00
|
|
|
case NOTIFICATION_DRAW: {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_inside_tree()) {
|
2015-12-29 22:07:37 +00:00
|
|
|
break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-12-29 22:07:37 +00:00
|
|
|
|
2017-08-18 23:02:56 +00:00
|
|
|
if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
|
2015-12-29 22:07:37 +00:00
|
|
|
break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2015-12-29 22:07:37 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
|
|
|
|
draw_line(Point2(-10, length), Point2(+10, length), Color(0.7, 0.6, 0.0, 0.5), 3);
|
|
|
|
draw_line(Point2(0, 0), Point2(0, length), Color(0.7, 0.6, 0.0, 0.5), 3);
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
void DampedSpringJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
|
2017-01-11 03:52:51 +00:00
|
|
|
Transform2D gt = get_global_transform();
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector2 anchor_A = gt.get_origin();
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 anchor_B = gt.xform(Vector2(0, length));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-02-09 16:19:03 +00:00
|
|
|
PhysicsServer2D::get_singleton()->joint_make_damped_spring(p_joint, anchor_A, anchor_B, body_a->get_rid(), body_b->get_rid());
|
2020-05-14 14:41:43 +00:00
|
|
|
if (rest_length) {
|
2021-02-09 16:19:03 +00:00
|
|
|
PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PhysicsServer2D::DAMPED_SPRING_REST_LENGTH, rest_length);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-02-09 16:19:03 +00:00
|
|
|
PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PhysicsServer2D::DAMPED_SPRING_STIFFNESS, stiffness);
|
|
|
|
PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PhysicsServer2D::DAMPED_SPRING_DAMPING, damping);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DampedSpringJoint2D::set_length(real_t p_length) {
|
2017-03-05 15:44:50 +00:00
|
|
|
length = p_length;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
real_t DampedSpringJoint2D::get_length() const {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DampedSpringJoint2D::set_rest_length(real_t p_rest_length) {
|
2017-03-05 15:44:50 +00:00
|
|
|
rest_length = p_rest_length;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2021-02-09 16:19:03 +00:00
|
|
|
if (is_configured()) {
|
2023-08-17 14:30:18 +00:00
|
|
|
PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_rid(), PhysicsServer2D::DAMPED_SPRING_REST_LENGTH, p_rest_length ? p_rest_length : length);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
real_t DampedSpringJoint2D::get_rest_length() const {
|
|
|
|
return rest_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DampedSpringJoint2D::set_stiffness(real_t p_stiffness) {
|
2017-03-05 15:44:50 +00:00
|
|
|
stiffness = p_stiffness;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2021-02-09 16:19:03 +00:00
|
|
|
if (is_configured()) {
|
2023-08-17 14:30:18 +00:00
|
|
|
PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_rid(), PhysicsServer2D::DAMPED_SPRING_STIFFNESS, p_stiffness);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
real_t DampedSpringJoint2D::get_stiffness() const {
|
|
|
|
return stiffness;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DampedSpringJoint2D::set_damping(real_t p_damping) {
|
2017-03-05 15:44:50 +00:00
|
|
|
damping = p_damping;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2021-02-09 16:19:03 +00:00
|
|
|
if (is_configured()) {
|
2023-08-17 14:30:18 +00:00
|
|
|
PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_rid(), PhysicsServer2D::DAMPED_SPRING_DAMPING, p_damping);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
real_t DampedSpringJoint2D::get_damping() const {
|
|
|
|
return damping;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DampedSpringJoint2D::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_length", "length"), &DampedSpringJoint2D::set_length);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_length"), &DampedSpringJoint2D::get_length);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_rest_length", "rest_length"), &DampedSpringJoint2D::set_rest_length);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_rest_length"), &DampedSpringJoint2D::get_rest_length);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_stiffness", "stiffness"), &DampedSpringJoint2D::set_stiffness);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_stiffness"), &DampedSpringJoint2D::get_stiffness);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_damping", "damping"), &DampedSpringJoint2D::set_damping);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_damping"), &DampedSpringJoint2D::get_damping);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-12-03 00:09:19 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_length", "get_length");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rest_length", PROPERTY_HINT_RANGE, "0,65535,1,exp,suffix:px"), "set_rest_length", "get_rest_length");
|
Fix editor suffixes and degrees conversion
* Functions to convert to/from degrees are all gone. Conversion is done by the editor.
* Use PROPERTY_HINT_ANGLE instead of PROPERTY_HINT_RANGE to edit radian angles in degrees.
* Added possibility to add suffixes to range properties, use "min,max[,step][,suffix:<something>]" example "0,100,1,suffix:m"
* In general, can add suffixes for EditorSpinSlider
Not covered by this PR, will have to be addressed by future ones:
* Ability to switch radians/degrees in the inspector for angle properties (if actually wanted).
* Animations previously made will most likely break, need to add a way to make old ones compatible.
* Only added a "px" suffix to 2D position and a "m" one to 3D position, someone needs to go through the rest of the engine and add all remaining suffixes.
* Likely also need to track down usage of EditorSpinSlider outside properties to add suffixes to it too.
2021-06-29 19:42:12 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stiffness", PROPERTY_HINT_RANGE, "0.1,64,0.1,exp"), "set_stiffness", "get_stiffness");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping", PROPERTY_HINT_RANGE, "0.01,16,0.01,exp"), "set_damping", "get_damping");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DampedSpringJoint2D::DampedSpringJoint2D() {
|
|
|
|
}
|