1/**************************************************************************/
2/* godot_area_pair_2d.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 "godot_area_pair_2d.h"
32#include "godot_collision_solver_2d.h"
33
34bool GodotAreaPair2D::setup(real_t p_step) {
35 bool result = false;
36 if (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)) {
37 result = true;
38 }
39
40 process_collision = false;
41 has_space_override = false;
42 if (result != colliding) {
43 if ((int)area->get_param(PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE) != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
44 has_space_override = true;
45 } else if ((int)area->get_param(PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE) != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
46 has_space_override = true;
47 } else if ((int)area->get_param(PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE) != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
48 has_space_override = true;
49 }
50 process_collision = has_space_override;
51
52 if (area->has_monitor_callback()) {
53 process_collision = true;
54 }
55
56 colliding = result;
57 }
58
59 return process_collision;
60}
61
62bool GodotAreaPair2D::pre_solve(real_t p_step) {
63 if (!process_collision) {
64 return false;
65 }
66
67 if (colliding) {
68 if (has_space_override) {
69 body->add_area(area);
70 }
71
72 if (area->has_monitor_callback()) {
73 area->add_body_to_query(body, body_shape, area_shape);
74 }
75 } else {
76 if (has_space_override) {
77 body->remove_area(area);
78 }
79
80 if (area->has_monitor_callback()) {
81 area->remove_body_from_query(body, body_shape, area_shape);
82 }
83 }
84
85 return false; // Never do any post solving.
86}
87
88void GodotAreaPair2D::solve(real_t p_step) {
89 // Nothing to do.
90}
91
92GodotAreaPair2D::GodotAreaPair2D(GodotBody2D *p_body, int p_body_shape, GodotArea2D *p_area, int p_area_shape) {
93 body = p_body;
94 area = p_area;
95 body_shape = p_body_shape;
96 area_shape = p_area_shape;
97 body->add_constraint(this, 0);
98 area->add_constraint(this);
99 if (p_body->get_mode() == PhysicsServer2D::BODY_MODE_KINEMATIC) { //need to be active to process pair
100 p_body->set_active(true);
101 }
102}
103
104GodotAreaPair2D::~GodotAreaPair2D() {
105 if (colliding) {
106 if (has_space_override) {
107 body->remove_area(area);
108 }
109 if (area->has_monitor_callback()) {
110 area->remove_body_from_query(body, body_shape, area_shape);
111 }
112 }
113 body->remove_constraint(this, 0);
114 area->remove_constraint(this);
115}
116
117//////////////////////////////////
118
119bool GodotArea2Pair2D::setup(real_t p_step) {
120 bool result_a = area_a->collides_with(area_b);
121 bool result_b = area_b->collides_with(area_a);
122 if ((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)) {
123 result_a = false;
124 result_b = false;
125 }
126
127 bool process_collision = false;
128
129 process_collision_a = false;
130 if (result_a != colliding_a) {
131 if (area_a->has_area_monitor_callback() && area_b_monitorable) {
132 process_collision_a = true;
133 process_collision = true;
134 }
135 colliding_a = result_a;
136 }
137
138 process_collision_b = false;
139 if (result_b != colliding_b) {
140 if (area_b->has_area_monitor_callback() && area_a_monitorable) {
141 process_collision_b = true;
142 process_collision = true;
143 }
144 colliding_b = result_b;
145 }
146
147 return process_collision;
148}
149
150bool GodotArea2Pair2D::pre_solve(real_t p_step) {
151 if (process_collision_a) {
152 if (colliding_a) {
153 area_a->add_area_to_query(area_b, shape_b, shape_a);
154 } else {
155 area_a->remove_area_from_query(area_b, shape_b, shape_a);
156 }
157 }
158
159 if (process_collision_b) {
160 if (colliding_b) {
161 area_b->add_area_to_query(area_a, shape_a, shape_b);
162 } else {
163 area_b->remove_area_from_query(area_a, shape_a, shape_b);
164 }
165 }
166
167 return false; // Never do any post solving.
168}
169
170void GodotArea2Pair2D::solve(real_t p_step) {
171 // Nothing to do.
172}
173
174GodotArea2Pair2D::GodotArea2Pair2D(GodotArea2D *p_area_a, int p_shape_a, GodotArea2D *p_area_b, int p_shape_b) {
175 area_a = p_area_a;
176 area_b = p_area_b;
177 shape_a = p_shape_a;
178 shape_b = p_shape_b;
179 area_a_monitorable = area_a->is_monitorable();
180 area_b_monitorable = area_b->is_monitorable();
181 area_a->add_constraint(this);
182 area_b->add_constraint(this);
183}
184
185GodotArea2Pair2D::~GodotArea2Pair2D() {
186 if (colliding_a) {
187 if (area_a->has_area_monitor_callback() && area_b_monitorable) {
188 area_a->remove_area_from_query(area_b, shape_b, shape_a);
189 }
190 }
191
192 if (colliding_b) {
193 if (area_b->has_area_monitor_callback() && area_a_monitorable) {
194 area_b->remove_area_from_query(area_a, shape_a, shape_b);
195 }
196 }
197
198 area_a->remove_constraint(this);
199 area_b->remove_constraint(this);
200}
201