Path: blob/master/scene/2d/physics/joints/joint_2d.cpp
9912 views
/**************************************************************************/1/* joint_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "joint_2d.h"3132#include "scene/2d/physics/physics_body_2d.h"3334void Joint2D::_disconnect_signals() {35Node *node_a = get_node_or_null(a);36PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a);37if (body_a) {38body_a->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree));39}4041Node *node_b = get_node_or_null(b);42PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b);43if (body_b) {44body_b->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree));45}46}4748void Joint2D::_body_exit_tree() {49_disconnect_signals();50_update_joint(true);51update_configuration_warnings();52}5354void Joint2D::_update_joint(bool p_only_free) {55if (ba.is_valid() && bb.is_valid() && exclude_from_collision) {56PhysicsServer2D::get_singleton()->joint_disable_collisions_between_bodies(joint, false);57}5859ba = RID();60bb = RID();61configured = false;6263if (p_only_free || !is_inside_tree()) {64PhysicsServer2D::get_singleton()->joint_clear(joint);65warning = String();66return;67}6869Node *node_a = get_node_or_null(a);70Node *node_b = get_node_or_null(b);7172PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a);73PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b);7475bool valid = false;7677if (node_a && !body_a && node_b && !body_b) {78warning = RTR("Node A and Node B must be PhysicsBody2Ds");79} else if (node_a && !body_a) {80warning = RTR("Node A must be a PhysicsBody2D");81} else if (node_b && !body_b) {82warning = RTR("Node B must be a PhysicsBody2D");83} else if (!body_a || !body_b) {84warning = RTR("Joint is not connected to two PhysicsBody2Ds");85} else if (body_a == body_b) {86warning = RTR("Node A and Node B must be different PhysicsBody2Ds");87} else {88warning = String();89valid = true;90}9192update_configuration_warnings();9394if (!valid) {95PhysicsServer2D::get_singleton()->joint_clear(joint);96return;97}9899if (body_a) {100body_a->force_update_transform();101}102103if (body_b) {104body_b->force_update_transform();105}106107configured = true;108109_configure_joint(joint, body_a, body_b);110111ERR_FAIL_COND_MSG(!joint.is_valid(), "Failed to configure the joint.");112113PhysicsServer2D::get_singleton()->joint_set_param(joint, PhysicsServer2D::JOINT_PARAM_BIAS, bias);114115ba = body_a->get_rid();116bb = body_b->get_rid();117118if (!body_a->is_connected(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree))) {119body_a->connect(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree));120}121if (!body_b->is_connected(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree))) {122body_b->connect(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree));123}124125PhysicsServer2D::get_singleton()->joint_disable_collisions_between_bodies(joint, exclude_from_collision);126}127128void Joint2D::set_node_a(const NodePath &p_node_a) {129if (a == p_node_a) {130return;131}132133if (is_configured()) {134_disconnect_signals();135}136137a = p_node_a;138if (Engine::get_singleton()->is_editor_hint()) {139// When in editor, the setter may be called as a result of node rename.140// It happens before the node actually changes its name, which triggers false warning.141callable_mp(this, &Joint2D::_update_joint).call_deferred(false);142} else {143_update_joint();144}145}146147NodePath Joint2D::get_node_a() const {148return a;149}150151void Joint2D::set_node_b(const NodePath &p_node_b) {152if (b == p_node_b) {153return;154}155156if (is_configured()) {157_disconnect_signals();158}159160b = p_node_b;161if (Engine::get_singleton()->is_editor_hint()) {162callable_mp(this, &Joint2D::_update_joint).call_deferred(false);163} else {164_update_joint();165}166}167168NodePath Joint2D::get_node_b() const {169return b;170}171172void Joint2D::_notification(int p_what) {173switch (p_what) {174case NOTIFICATION_POST_ENTER_TREE: {175if (is_configured()) {176_disconnect_signals();177}178_update_joint();179} break;180181case NOTIFICATION_EXIT_TREE: {182if (is_configured()) {183_disconnect_signals();184}185_update_joint(true);186} break;187}188}189190void Joint2D::set_bias(real_t p_bias) {191bias = p_bias;192if (joint.is_valid()) {193PhysicsServer2D::get_singleton()->joint_set_param(joint, PhysicsServer2D::JOINT_PARAM_BIAS, bias);194}195}196197real_t Joint2D::get_bias() const {198return bias;199}200201void Joint2D::set_exclude_nodes_from_collision(bool p_enable) {202if (exclude_from_collision == p_enable) {203return;204}205if (is_configured()) {206_disconnect_signals();207}208_update_joint(true);209exclude_from_collision = p_enable;210_update_joint();211}212213bool Joint2D::get_exclude_nodes_from_collision() const {214return exclude_from_collision;215}216217PackedStringArray Joint2D::get_configuration_warnings() const {218PackedStringArray warnings = Node2D::get_configuration_warnings();219220if (!warning.is_empty()) {221warnings.push_back(warning);222}223224return warnings;225}226227void Joint2D::_bind_methods() {228ClassDB::bind_method(D_METHOD("set_node_a", "node"), &Joint2D::set_node_a);229ClassDB::bind_method(D_METHOD("get_node_a"), &Joint2D::get_node_a);230231ClassDB::bind_method(D_METHOD("set_node_b", "node"), &Joint2D::set_node_b);232ClassDB::bind_method(D_METHOD("get_node_b"), &Joint2D::get_node_b);233234ClassDB::bind_method(D_METHOD("set_bias", "bias"), &Joint2D::set_bias);235ClassDB::bind_method(D_METHOD("get_bias"), &Joint2D::get_bias);236237ClassDB::bind_method(D_METHOD("set_exclude_nodes_from_collision", "enable"), &Joint2D::set_exclude_nodes_from_collision);238ClassDB::bind_method(D_METHOD("get_exclude_nodes_from_collision"), &Joint2D::get_exclude_nodes_from_collision);239240ClassDB::bind_method(D_METHOD("get_rid"), &Joint2D::get_rid);241242ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_a", "get_node_a");243ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_b", "get_node_b");244ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0,0.9,0.001"), "set_bias", "get_bias");245ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_collision"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision");246}247248Joint2D::Joint2D() {249joint = PhysicsServer2D::get_singleton()->joint_create();250set_hide_clip_children(true);251}252253Joint2D::~Joint2D() {254ERR_FAIL_NULL(PhysicsServer2D::get_singleton());255PhysicsServer2D::get_singleton()->free(joint);256}257258259