Path: blob/master/modules/godot_physics_2d/godot_area_pair_2d.cpp
10277 views
/**************************************************************************/1/* godot_area_pair_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 "godot_area_pair_2d.h"31#include "godot_collision_solver_2d.h"3233bool GodotAreaPair2D::setup(real_t p_step) {34bool result = false;35if (area->collides_with(body) && GodotCollisionSolver2D::solve(body->get_shape(body_shape), body->get_transform() * body->get_shape_transform(body_shape), Vector2(), area->get_shape(area_shape), area->get_transform() * area->get_shape_transform(area_shape), Vector2(), nullptr, this)) {36result = true;37}3839process_collision = false;40has_space_override = false;41if (result != colliding) {42if ((int)area->get_param(PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE) != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {43has_space_override = true;44} else if ((int)area->get_param(PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE) != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {45has_space_override = true;46} else if ((int)area->get_param(PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE) != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {47has_space_override = true;48}49process_collision = has_space_override;5051if (area->has_monitor_callback()) {52process_collision = true;53}5455colliding = result;56}5758return process_collision;59}6061bool GodotAreaPair2D::pre_solve(real_t p_step) {62if (!process_collision) {63return false;64}6566if (colliding) {67if (has_space_override) {68body_has_attached_area = true;69body->add_area(area);70}7172if (area->has_monitor_callback()) {73area->add_body_to_query(body, body_shape, area_shape);74}75} else {76if (has_space_override) {77body_has_attached_area = false;78body->remove_area(area);79}8081if (area->has_monitor_callback()) {82area->remove_body_from_query(body, body_shape, area_shape);83}84}8586return false; // Never do any post solving.87}8889void GodotAreaPair2D::solve(real_t p_step) {90// Nothing to do.91}9293GodotAreaPair2D::GodotAreaPair2D(GodotBody2D *p_body, int p_body_shape, GodotArea2D *p_area, int p_area_shape) {94body = p_body;95area = p_area;96body_shape = p_body_shape;97area_shape = p_area_shape;98body->add_constraint(this, 0);99area->add_constraint(this);100if (p_body->get_mode() == PhysicsServer2D::BODY_MODE_KINEMATIC) { //need to be active to process pair101p_body->set_active(true);102}103}104105GodotAreaPair2D::~GodotAreaPair2D() {106if (colliding) {107if (body_has_attached_area) {108body_has_attached_area = false;109body->remove_area(area);110}111if (area->has_monitor_callback()) {112area->remove_body_from_query(body, body_shape, area_shape);113}114}115body->remove_constraint(this, 0);116area->remove_constraint(this);117}118119//////////////////////////////////120121bool GodotArea2Pair2D::setup(real_t p_step) {122bool result_a = area_a->collides_with(area_b);123bool result_b = area_b->collides_with(area_a);124if ((result_a || result_b) && !GodotCollisionSolver2D::solve(area_a->get_shape(shape_a), area_a->get_transform() * area_a->get_shape_transform(shape_a), Vector2(), area_b->get_shape(shape_b), area_b->get_transform() * area_b->get_shape_transform(shape_b), Vector2(), nullptr, this)) {125result_a = false;126result_b = false;127}128129bool process_collision = false;130131process_collision_a = false;132if (result_a != colliding_a) {133if (area_a->has_area_monitor_callback() && area_b_monitorable) {134process_collision_a = true;135process_collision = true;136}137colliding_a = result_a;138}139140process_collision_b = false;141if (result_b != colliding_b) {142if (area_b->has_area_monitor_callback() && area_a_monitorable) {143process_collision_b = true;144process_collision = true;145}146colliding_b = result_b;147}148149return process_collision;150}151152bool GodotArea2Pair2D::pre_solve(real_t p_step) {153if (process_collision_a) {154if (colliding_a) {155area_a->add_area_to_query(area_b, shape_b, shape_a);156} else {157area_a->remove_area_from_query(area_b, shape_b, shape_a);158}159}160161if (process_collision_b) {162if (colliding_b) {163area_b->add_area_to_query(area_a, shape_a, shape_b);164} else {165area_b->remove_area_from_query(area_a, shape_a, shape_b);166}167}168169return false; // Never do any post solving.170}171172void GodotArea2Pair2D::solve(real_t p_step) {173// Nothing to do.174}175176GodotArea2Pair2D::GodotArea2Pair2D(GodotArea2D *p_area_a, int p_shape_a, GodotArea2D *p_area_b, int p_shape_b) {177area_a = p_area_a;178area_b = p_area_b;179shape_a = p_shape_a;180shape_b = p_shape_b;181area_a_monitorable = area_a->is_monitorable();182area_b_monitorable = area_b->is_monitorable();183area_a->add_constraint(this);184area_b->add_constraint(this);185}186187GodotArea2Pair2D::~GodotArea2Pair2D() {188if (colliding_a) {189if (area_a->has_area_monitor_callback() && area_b_monitorable) {190area_a->remove_area_from_query(area_b, shape_b, shape_a);191}192}193194if (colliding_b) {195if (area_b->has_area_monitor_callback() && area_a_monitorable) {196area_b->remove_area_from_query(area_a, shape_a, shape_b);197}198}199200area_a->remove_constraint(this);201area_b->remove_constraint(this);202}203204205