Path: blob/master/scene/2d/physics/animatable_body_2d.cpp
9906 views
/**************************************************************************/1/* animatable_body_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 "animatable_body_2d.h"3132void AnimatableBody2D::set_sync_to_physics(bool p_enable) {33if (sync_to_physics == p_enable) {34return;35}3637sync_to_physics = p_enable;3839_update_kinematic_motion();40}4142bool AnimatableBody2D::is_sync_to_physics_enabled() const {43return sync_to_physics;44}4546void AnimatableBody2D::_update_kinematic_motion() {47#ifdef TOOLS_ENABLED48if (Engine::get_singleton()->is_editor_hint()) {49return;50}51#endif5253if (sync_to_physics) {54PhysicsServer2D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &AnimatableBody2D::_body_state_changed));55set_only_update_transform_changes(true);56set_notify_local_transform(true);57} else {58PhysicsServer2D::get_singleton()->body_set_state_sync_callback(get_rid(), Callable());59set_only_update_transform_changes(false);60set_notify_local_transform(false);61}62}6364void AnimatableBody2D::_body_state_changed(PhysicsDirectBodyState2D *p_state) {65if (!sync_to_physics) {66return;67}6869last_valid_transform = p_state->get_transform();70set_notify_local_transform(false);71set_global_transform(last_valid_transform);72set_notify_local_transform(true);73}7475void AnimatableBody2D::_notification(int p_what) {76switch (p_what) {77case NOTIFICATION_ENTER_TREE: {78last_valid_transform = get_global_transform();79_update_kinematic_motion();80} break;8182case NOTIFICATION_EXIT_TREE: {83set_only_update_transform_changes(false);84set_notify_local_transform(false);85} break;8687case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {88// Used by sync to physics, send the new transform to the physics...89Transform2D new_transform = get_global_transform();9091PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_TRANSFORM, new_transform);9293// ... but then revert changes.94set_notify_local_transform(false);95set_global_transform(last_valid_transform);96set_notify_local_transform(true);97} break;98}99}100101void AnimatableBody2D::_bind_methods() {102ClassDB::bind_method(D_METHOD("set_sync_to_physics", "enable"), &AnimatableBody2D::set_sync_to_physics);103ClassDB::bind_method(D_METHOD("is_sync_to_physics_enabled"), &AnimatableBody2D::is_sync_to_physics_enabled);104105ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync_to_physics"), "set_sync_to_physics", "is_sync_to_physics_enabled");106}107108AnimatableBody2D::AnimatableBody2D() :109StaticBody2D(PhysicsServer2D::BODY_MODE_KINEMATIC) {110}111112113