| 1 | /**************************************************************************/ |
| 2 | /* godot_area_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_2d.h" |
| 32 | #include "godot_body_2d.h" |
| 33 | #include "godot_space_2d.h" |
| 34 | |
| 35 | GodotArea2D::BodyKey::BodyKey(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) { |
| 36 | rid = p_body->get_self(); |
| 37 | instance_id = p_body->get_instance_id(); |
| 38 | body_shape = p_body_shape; |
| 39 | area_shape = p_area_shape; |
| 40 | } |
| 41 | |
| 42 | GodotArea2D::BodyKey::BodyKey(GodotArea2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) { |
| 43 | rid = p_body->get_self(); |
| 44 | instance_id = p_body->get_instance_id(); |
| 45 | body_shape = p_body_shape; |
| 46 | area_shape = p_area_shape; |
| 47 | } |
| 48 | |
| 49 | void GodotArea2D::_shapes_changed() { |
| 50 | if (!moved_list.in_list() && get_space()) { |
| 51 | get_space()->area_add_to_moved_list(&moved_list); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void GodotArea2D::set_transform(const Transform2D &p_transform) { |
| 56 | if (!moved_list.in_list() && get_space()) { |
| 57 | get_space()->area_add_to_moved_list(&moved_list); |
| 58 | } |
| 59 | |
| 60 | _set_transform(p_transform); |
| 61 | _set_inv_transform(p_transform.affine_inverse()); |
| 62 | } |
| 63 | |
| 64 | void GodotArea2D::set_space(GodotSpace2D *p_space) { |
| 65 | if (get_space()) { |
| 66 | if (monitor_query_list.in_list()) { |
| 67 | get_space()->area_remove_from_monitor_query_list(&monitor_query_list); |
| 68 | } |
| 69 | if (moved_list.in_list()) { |
| 70 | get_space()->area_remove_from_moved_list(&moved_list); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | monitored_bodies.clear(); |
| 75 | monitored_areas.clear(); |
| 76 | |
| 77 | _set_space(p_space); |
| 78 | } |
| 79 | |
| 80 | void GodotArea2D::set_monitor_callback(const Callable &p_callback) { |
| 81 | ObjectID id = p_callback.get_object_id(); |
| 82 | |
| 83 | if (id == monitor_callback.get_object_id()) { |
| 84 | monitor_callback = p_callback; |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | _unregister_shapes(); |
| 89 | |
| 90 | monitor_callback = p_callback; |
| 91 | |
| 92 | monitored_bodies.clear(); |
| 93 | monitored_areas.clear(); |
| 94 | |
| 95 | _shape_changed(); |
| 96 | |
| 97 | if (!moved_list.in_list() && get_space()) { |
| 98 | get_space()->area_add_to_moved_list(&moved_list); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void GodotArea2D::set_area_monitor_callback(const Callable &p_callback) { |
| 103 | ObjectID id = p_callback.get_object_id(); |
| 104 | |
| 105 | if (id == area_monitor_callback.get_object_id()) { |
| 106 | area_monitor_callback = p_callback; |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | _unregister_shapes(); |
| 111 | |
| 112 | area_monitor_callback = p_callback; |
| 113 | |
| 114 | monitored_bodies.clear(); |
| 115 | monitored_areas.clear(); |
| 116 | |
| 117 | _shape_changed(); |
| 118 | |
| 119 | if (!moved_list.in_list() && get_space()) { |
| 120 | get_space()->area_add_to_moved_list(&moved_list); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void GodotArea2D::_set_space_override_mode(PhysicsServer2D::AreaSpaceOverrideMode &r_mode, PhysicsServer2D::AreaSpaceOverrideMode p_new_mode) { |
| 125 | bool do_override = p_new_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED; |
| 126 | if (do_override == (r_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED)) { |
| 127 | return; |
| 128 | } |
| 129 | _unregister_shapes(); |
| 130 | r_mode = p_new_mode; |
| 131 | _shape_changed(); |
| 132 | } |
| 133 | |
| 134 | void GodotArea2D::set_param(PhysicsServer2D::AreaParameter p_param, const Variant &p_value) { |
| 135 | switch (p_param) { |
| 136 | case PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE: |
| 137 | _set_space_override_mode(gravity_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value); |
| 138 | break; |
| 139 | case PhysicsServer2D::AREA_PARAM_GRAVITY: |
| 140 | gravity = p_value; |
| 141 | break; |
| 142 | case PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR: |
| 143 | gravity_vector = p_value; |
| 144 | break; |
| 145 | case PhysicsServer2D::AREA_PARAM_GRAVITY_IS_POINT: |
| 146 | gravity_is_point = p_value; |
| 147 | break; |
| 148 | case PhysicsServer2D::AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE: |
| 149 | gravity_point_unit_distance = p_value; |
| 150 | break; |
| 151 | case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE: |
| 152 | _set_space_override_mode(linear_damping_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value); |
| 153 | break; |
| 154 | case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP: |
| 155 | linear_damp = p_value; |
| 156 | break; |
| 157 | case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE: |
| 158 | _set_space_override_mode(angular_damping_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value); |
| 159 | break; |
| 160 | case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP: |
| 161 | angular_damp = p_value; |
| 162 | break; |
| 163 | case PhysicsServer2D::AREA_PARAM_PRIORITY: |
| 164 | priority = p_value; |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | Variant GodotArea2D::get_param(PhysicsServer2D::AreaParameter p_param) const { |
| 170 | switch (p_param) { |
| 171 | case PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE: |
| 172 | return gravity_override_mode; |
| 173 | case PhysicsServer2D::AREA_PARAM_GRAVITY: |
| 174 | return gravity; |
| 175 | case PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR: |
| 176 | return gravity_vector; |
| 177 | case PhysicsServer2D::AREA_PARAM_GRAVITY_IS_POINT: |
| 178 | return gravity_is_point; |
| 179 | case PhysicsServer2D::AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE: |
| 180 | return gravity_point_unit_distance; |
| 181 | case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE: |
| 182 | return linear_damping_override_mode; |
| 183 | case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP: |
| 184 | return linear_damp; |
| 185 | case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE: |
| 186 | return angular_damping_override_mode; |
| 187 | case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP: |
| 188 | return angular_damp; |
| 189 | case PhysicsServer2D::AREA_PARAM_PRIORITY: |
| 190 | return priority; |
| 191 | } |
| 192 | |
| 193 | return Variant(); |
| 194 | } |
| 195 | |
| 196 | void GodotArea2D::_queue_monitor_update() { |
| 197 | ERR_FAIL_COND(!get_space()); |
| 198 | |
| 199 | if (!monitor_query_list.in_list()) { |
| 200 | get_space()->area_add_to_monitor_query_list(&monitor_query_list); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void GodotArea2D::set_monitorable(bool p_monitorable) { |
| 205 | if (monitorable == p_monitorable) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | monitorable = p_monitorable; |
| 210 | _set_static(!monitorable); |
| 211 | _shapes_changed(); |
| 212 | } |
| 213 | |
| 214 | void GodotArea2D::call_queries() { |
| 215 | if (!monitor_callback.is_null() && !monitored_bodies.is_empty()) { |
| 216 | if (monitor_callback.is_valid()) { |
| 217 | Variant res[5]; |
| 218 | Variant *resptr[5]; |
| 219 | for (int i = 0; i < 5; i++) { |
| 220 | resptr[i] = &res[i]; |
| 221 | } |
| 222 | |
| 223 | for (HashMap<BodyKey, BodyState, BodyKey>::Iterator E = monitored_bodies.begin(); E;) { |
| 224 | if (E->value.state == 0) { // Nothing happened |
| 225 | HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E; |
| 226 | ++next; |
| 227 | monitored_bodies.remove(E); |
| 228 | E = next; |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | res[0] = E->value.state > 0 ? PhysicsServer2D::AREA_BODY_ADDED : PhysicsServer2D::AREA_BODY_REMOVED; |
| 233 | res[1] = E->key.rid; |
| 234 | res[2] = E->key.instance_id; |
| 235 | res[3] = E->key.body_shape; |
| 236 | res[4] = E->key.area_shape; |
| 237 | |
| 238 | HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E; |
| 239 | ++next; |
| 240 | monitored_bodies.remove(E); |
| 241 | E = next; |
| 242 | |
| 243 | Callable::CallError ce; |
| 244 | Variant ret; |
| 245 | monitor_callback.callp((const Variant **)resptr, 5, ret, ce); |
| 246 | |
| 247 | if (ce.error != Callable::CallError::CALL_OK) { |
| 248 | ERR_PRINT_ONCE("Error calling event callback method " + Variant::get_callable_error_text(monitor_callback, (const Variant **)resptr, 5, ce)); |
| 249 | } |
| 250 | } |
| 251 | } else { |
| 252 | monitored_bodies.clear(); |
| 253 | monitor_callback = Callable(); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (!area_monitor_callback.is_null() && !monitored_areas.is_empty()) { |
| 258 | if (area_monitor_callback.is_valid()) { |
| 259 | Variant res[5]; |
| 260 | Variant *resptr[5]; |
| 261 | for (int i = 0; i < 5; i++) { |
| 262 | resptr[i] = &res[i]; |
| 263 | } |
| 264 | |
| 265 | for (HashMap<BodyKey, BodyState, BodyKey>::Iterator E = monitored_areas.begin(); E;) { |
| 266 | if (E->value.state == 0) { // Nothing happened |
| 267 | HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E; |
| 268 | ++next; |
| 269 | monitored_areas.remove(E); |
| 270 | E = next; |
| 271 | continue; |
| 272 | } |
| 273 | |
| 274 | res[0] = E->value.state > 0 ? PhysicsServer2D::AREA_BODY_ADDED : PhysicsServer2D::AREA_BODY_REMOVED; |
| 275 | res[1] = E->key.rid; |
| 276 | res[2] = E->key.instance_id; |
| 277 | res[3] = E->key.body_shape; |
| 278 | res[4] = E->key.area_shape; |
| 279 | |
| 280 | HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E; |
| 281 | ++next; |
| 282 | monitored_areas.remove(E); |
| 283 | E = next; |
| 284 | |
| 285 | Callable::CallError ce; |
| 286 | Variant ret; |
| 287 | area_monitor_callback.callp((const Variant **)resptr, 5, ret, ce); |
| 288 | |
| 289 | if (ce.error != Callable::CallError::CALL_OK) { |
| 290 | ERR_PRINT_ONCE("Error calling event callback method " + Variant::get_callable_error_text(area_monitor_callback, (const Variant **)resptr, 5, ce)); |
| 291 | } |
| 292 | } |
| 293 | } else { |
| 294 | monitored_areas.clear(); |
| 295 | area_monitor_callback = Callable(); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void GodotArea2D::compute_gravity(const Vector2 &p_position, Vector2 &r_gravity) const { |
| 301 | if (is_gravity_point()) { |
| 302 | const real_t gr_unit_dist = get_gravity_point_unit_distance(); |
| 303 | Vector2 v = get_transform().xform(get_gravity_vector()) - p_position; |
| 304 | if (gr_unit_dist > 0) { |
| 305 | const real_t v_length_sq = v.length_squared(); |
| 306 | if (v_length_sq > 0) { |
| 307 | const real_t gravity_strength = get_gravity() * gr_unit_dist * gr_unit_dist / v_length_sq; |
| 308 | r_gravity = v.normalized() * gravity_strength; |
| 309 | } else { |
| 310 | r_gravity = Vector2(); |
| 311 | } |
| 312 | } else { |
| 313 | r_gravity = v.normalized() * get_gravity(); |
| 314 | } |
| 315 | } else { |
| 316 | r_gravity = get_gravity_vector() * get_gravity(); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | GodotArea2D::GodotArea2D() : |
| 321 | GodotCollisionObject2D(TYPE_AREA), |
| 322 | monitor_query_list(this), |
| 323 | moved_list(this) { |
| 324 | _set_static(true); //areas are not active by default |
| 325 | } |
| 326 | |
| 327 | GodotArea2D::~GodotArea2D() { |
| 328 | } |
| 329 | |