2017-11-05 03:52:59 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* area_bullet.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2018-01-05 07:50:27 +08:00
|
|
|
/* https://godotengine.org */
|
2017-11-05 03:52:59 +08:00
|
|
|
/*************************************************************************/
|
2020-01-01 18:16:22 +08:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-11-05 03:52:59 +08:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "area_bullet.h"
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2018-09-10 13:55:11 +08:00
|
|
|
#include "bullet_physics_server.h"
|
2017-11-05 03:52:59 +08:00
|
|
|
#include "bullet_types_converter.h"
|
|
|
|
#include "bullet_utilities.h"
|
|
|
|
#include "collision_object_bullet.h"
|
|
|
|
#include "space_bullet.h"
|
|
|
|
|
2018-01-05 07:50:27 +08:00
|
|
|
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
|
|
|
|
#include <btBulletCollisionCommon.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
@author AndreaCatania
|
|
|
|
*/
|
|
|
|
|
2017-12-07 04:36:34 +08:00
|
|
|
AreaBullet::AreaBullet() :
|
2020-05-12 23:01:17 +08:00
|
|
|
RigidCollisionObjectBullet(CollisionObjectBullet::TYPE_AREA) {
|
2017-11-05 03:52:59 +08:00
|
|
|
btGhost = bulletnew(btGhostObject);
|
2018-10-06 22:50:10 +08:00
|
|
|
reload_shapes();
|
2017-11-05 03:52:59 +08:00
|
|
|
setupBulletCollisionObject(btGhost);
|
|
|
|
/// Collision objects with a callback still have collision response with dynamic rigid bodies.
|
|
|
|
/// In order to use collision objects as trigger, you have to disable the collision response.
|
|
|
|
set_collision_enabled(false);
|
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
for (int i = 0; i < 5; ++i) {
|
2017-11-05 03:52:59 +08:00
|
|
|
call_event_res_ptr[i] = &call_event_res[i];
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-11-05 03:52:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AreaBullet::~AreaBullet() {
|
2018-04-09 15:33:29 +08:00
|
|
|
// signal are handled by godot, so just clear without notify
|
2020-05-14 22:41:43 +08:00
|
|
|
for (int i = overlappingObjects.size() - 1; 0 <= i; --i) {
|
2018-04-09 15:33:29 +08:00
|
|
|
overlappingObjects[i].object->on_exit_area(this);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-11-05 03:52:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AreaBullet::dispatch_callbacks() {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!isScratched) {
|
2017-11-05 03:52:59 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-11-05 03:52:59 +08:00
|
|
|
isScratched = false;
|
|
|
|
|
|
|
|
// Reverse order because I've to remove EXIT objects
|
|
|
|
for (int i = overlappingObjects.size() - 1; 0 <= i; --i) {
|
2018-07-25 09:11:03 +08:00
|
|
|
OverlappingObjectData &otherObj = overlappingObjects.write[i];
|
2017-11-05 03:52:59 +08:00
|
|
|
|
|
|
|
switch (otherObj.state) {
|
|
|
|
case OVERLAP_STATE_ENTER:
|
|
|
|
otherObj.state = OVERLAP_STATE_INSIDE;
|
2020-03-28 02:21:27 +08:00
|
|
|
call_event(otherObj.object, PhysicsServer3D::AREA_BODY_ADDED);
|
2017-11-05 03:52:59 +08:00
|
|
|
otherObj.object->on_enter_area(this);
|
|
|
|
break;
|
|
|
|
case OVERLAP_STATE_EXIT:
|
2020-03-28 02:21:27 +08:00
|
|
|
call_event(otherObj.object, PhysicsServer3D::AREA_BODY_REMOVED);
|
2017-11-05 03:52:59 +08:00
|
|
|
otherObj.object->on_exit_area(this);
|
|
|
|
overlappingObjects.remove(i); // Remove after callback
|
|
|
|
break;
|
2018-09-26 19:13:56 +08:00
|
|
|
case OVERLAP_STATE_DIRTY:
|
|
|
|
case OVERLAP_STATE_INSIDE:
|
|
|
|
break;
|
2017-11-05 03:52:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void AreaBullet::call_event(CollisionObjectBullet *p_otherObject, PhysicsServer3D::AreaBodyStatus p_status) {
|
2017-11-05 03:52:59 +08:00
|
|
|
InOutEventCallback &event = eventsCallbacks[static_cast<int>(p_otherObject->getType())];
|
|
|
|
Object *areaGodoObject = ObjectDB::get_instance(event.event_callback_id);
|
|
|
|
|
|
|
|
if (!areaGodoObject) {
|
2020-02-13 01:24:06 +08:00
|
|
|
event.event_callback_id = ObjectID();
|
2017-11-05 03:52:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
call_event_res[0] = p_status;
|
|
|
|
call_event_res[1] = p_otherObject->get_self(); // Other body
|
|
|
|
call_event_res[2] = p_otherObject->get_instance_id(); // instance ID
|
|
|
|
call_event_res[3] = 0; // other_body_shape ID
|
|
|
|
call_event_res[4] = 0; // self_shape ID
|
|
|
|
|
2020-02-20 03:27:19 +08:00
|
|
|
Callable::CallError outResp;
|
2017-11-05 03:52:59 +08:00
|
|
|
areaGodoObject->call(event.event_callback_method, (const Variant **)call_event_res_ptr, 5, outResp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AreaBullet::scratch() {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (isScratched) {
|
2017-11-05 03:52:59 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-11-05 03:52:59 +08:00
|
|
|
isScratched = true;
|
|
|
|
}
|
|
|
|
|
2018-04-09 15:33:29 +08:00
|
|
|
void AreaBullet::clear_overlaps(bool p_notify) {
|
2017-11-05 03:52:59 +08:00
|
|
|
for (int i = overlappingObjects.size() - 1; 0 <= i; --i) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (p_notify) {
|
2020-03-28 02:21:27 +08:00
|
|
|
call_event(overlappingObjects[i].object, PhysicsServer3D::AREA_BODY_REMOVED);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-04-09 15:33:29 +08:00
|
|
|
overlappingObjects[i].object->on_exit_area(this);
|
2017-11-05 03:52:59 +08:00
|
|
|
}
|
|
|
|
overlappingObjects.clear();
|
|
|
|
}
|
|
|
|
|
2018-04-09 15:33:29 +08:00
|
|
|
void AreaBullet::remove_overlap(CollisionObjectBullet *p_object, bool p_notify) {
|
2017-11-05 03:52:59 +08:00
|
|
|
for (int i = overlappingObjects.size() - 1; 0 <= i; --i) {
|
2018-04-09 15:33:29 +08:00
|
|
|
if (overlappingObjects[i].object == p_object) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (p_notify) {
|
2020-03-28 02:21:27 +08:00
|
|
|
call_event(overlappingObjects[i].object, PhysicsServer3D::AREA_BODY_REMOVED);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-04-09 15:33:29 +08:00
|
|
|
overlappingObjects[i].object->on_exit_area(this);
|
2017-11-05 03:52:59 +08:00
|
|
|
overlappingObjects.remove(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int AreaBullet::find_overlapping_object(CollisionObjectBullet *p_colObj) {
|
|
|
|
const int size = overlappingObjects.size();
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
if (overlappingObjects[i].object == p_colObj) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AreaBullet::set_monitorable(bool p_monitorable) {
|
|
|
|
monitorable = p_monitorable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AreaBullet::is_monitoring() const {
|
|
|
|
return get_godot_object_flags() & GOF_IS_MONITORING_AREA;
|
|
|
|
}
|
|
|
|
|
2018-10-06 22:50:10 +08:00
|
|
|
void AreaBullet::main_shape_changed() {
|
2019-11-07 17:37:44 +08:00
|
|
|
CRASH_COND(!get_main_shape());
|
2018-10-06 22:50:10 +08:00
|
|
|
btGhost->setCollisionShape(get_main_shape());
|
2018-09-07 00:19:05 +08:00
|
|
|
}
|
|
|
|
|
2020-06-21 18:48:40 +08:00
|
|
|
void AreaBullet::do_reload_body() {
|
2017-11-05 03:52:59 +08:00
|
|
|
if (space) {
|
|
|
|
space->remove_area(this);
|
|
|
|
space->add_area(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AreaBullet::set_space(SpaceBullet *p_space) {
|
|
|
|
// Clear the old space if there is one
|
|
|
|
if (space) {
|
2020-07-14 01:34:18 +08:00
|
|
|
clear_overlaps(false);
|
2017-11-05 03:52:59 +08:00
|
|
|
isScratched = false;
|
|
|
|
|
|
|
|
// Remove this object form the physics world
|
2020-06-21 18:48:40 +08:00
|
|
|
space->unregister_collision_object(this);
|
2017-11-05 03:52:59 +08:00
|
|
|
space->remove_area(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
space = p_space;
|
|
|
|
|
|
|
|
if (space) {
|
2020-06-21 18:48:40 +08:00
|
|
|
space->register_collision_object(this);
|
|
|
|
reload_body();
|
2017-11-05 03:52:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AreaBullet::on_collision_filters_change() {
|
|
|
|
if (space) {
|
|
|
|
space->reload_collision_filters(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AreaBullet::add_overlap(CollisionObjectBullet *p_otherObject) {
|
|
|
|
scratch();
|
|
|
|
overlappingObjects.push_back(OverlappingObjectData(p_otherObject, OVERLAP_STATE_ENTER));
|
|
|
|
p_otherObject->notify_new_overlap(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AreaBullet::put_overlap_as_exit(int p_index) {
|
|
|
|
scratch();
|
2018-07-25 09:11:03 +08:00
|
|
|
overlappingObjects.write[p_index].state = OVERLAP_STATE_EXIT;
|
2017-11-05 03:52:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AreaBullet::put_overlap_as_inside(int p_index) {
|
|
|
|
// This check is required to be sure this body was inside
|
|
|
|
if (OVERLAP_STATE_DIRTY == overlappingObjects[p_index].state) {
|
2018-07-25 09:11:03 +08:00
|
|
|
overlappingObjects.write[p_index].state = OVERLAP_STATE_INSIDE;
|
2017-11-05 03:52:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void AreaBullet::set_param(PhysicsServer3D::AreaParameter p_param, const Variant &p_value) {
|
2017-11-05 03:52:59 +08:00
|
|
|
switch (p_param) {
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_GRAVITY:
|
2017-11-05 03:52:59 +08:00
|
|
|
set_spOv_gravityMag(p_value);
|
|
|
|
break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR:
|
2017-11-05 03:52:59 +08:00
|
|
|
set_spOv_gravityVec(p_value);
|
|
|
|
break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP:
|
2017-11-05 03:52:59 +08:00
|
|
|
set_spOv_linearDump(p_value);
|
|
|
|
break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP:
|
2017-11-05 03:52:59 +08:00
|
|
|
set_spOv_angularDump(p_value);
|
|
|
|
break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_PRIORITY:
|
2017-11-05 03:52:59 +08:00
|
|
|
set_spOv_priority(p_value);
|
|
|
|
break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT:
|
2017-11-05 03:52:59 +08:00
|
|
|
set_spOv_gravityPoint(p_value);
|
|
|
|
break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
|
2017-11-05 03:52:59 +08:00
|
|
|
set_spOv_gravityPointDistanceScale(p_value);
|
|
|
|
break;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
|
2017-11-05 03:52:59 +08:00
|
|
|
set_spOv_gravityPointAttenuation(p_value);
|
|
|
|
break;
|
|
|
|
default:
|
2019-11-07 16:44:15 +08:00
|
|
|
WARN_PRINT("Area doesn't support this parameter in the Bullet backend: " + itos(p_param));
|
2017-11-05 03:52:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
Variant AreaBullet::get_param(PhysicsServer3D::AreaParameter p_param) const {
|
2017-11-05 03:52:59 +08:00
|
|
|
switch (p_param) {
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_GRAVITY:
|
2017-11-05 03:52:59 +08:00
|
|
|
return spOv_gravityMag;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR:
|
2017-11-05 03:52:59 +08:00
|
|
|
return spOv_gravityVec;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP:
|
2017-11-05 03:52:59 +08:00
|
|
|
return spOv_linearDump;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP:
|
2017-11-05 03:52:59 +08:00
|
|
|
return spOv_angularDump;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_PRIORITY:
|
2017-11-05 03:52:59 +08:00
|
|
|
return spOv_priority;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT:
|
2017-11-05 03:52:59 +08:00
|
|
|
return spOv_gravityPoint;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
|
2017-11-05 03:52:59 +08:00
|
|
|
return spOv_gravityPointDistanceScale;
|
2020-03-28 02:21:27 +08:00
|
|
|
case PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
|
2017-11-05 03:52:59 +08:00
|
|
|
return spOv_gravityPointAttenuation;
|
|
|
|
default:
|
2019-11-07 16:44:15 +08:00
|
|
|
WARN_PRINT("Area doesn't support this parameter in the Bullet backend: " + itos(p_param));
|
2017-11-05 03:52:59 +08:00
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AreaBullet::set_event_callback(Type p_callbackObjectType, ObjectID p_id, const StringName &p_method) {
|
|
|
|
InOutEventCallback &ev = eventsCallbacks[static_cast<int>(p_callbackObjectType)];
|
|
|
|
ev.event_callback_id = p_id;
|
|
|
|
ev.event_callback_method = p_method;
|
|
|
|
|
|
|
|
/// Set if monitoring
|
2020-02-13 01:24:06 +08:00
|
|
|
if (eventsCallbacks[0].event_callback_id.is_valid() || eventsCallbacks[1].event_callback_id.is_valid()) {
|
2017-11-05 03:52:59 +08:00
|
|
|
set_godot_object_flags(get_godot_object_flags() | GOF_IS_MONITORING_AREA);
|
|
|
|
} else {
|
|
|
|
set_godot_object_flags(get_godot_object_flags() & (~GOF_IS_MONITORING_AREA));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AreaBullet::has_event_callback(Type p_callbackObjectType) {
|
2020-02-13 01:24:06 +08:00
|
|
|
return eventsCallbacks[static_cast<int>(p_callbackObjectType)].event_callback_id.is_valid();
|
2017-11-05 03:52:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AreaBullet::on_enter_area(AreaBullet *p_area) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void AreaBullet::on_exit_area(AreaBullet *p_area) {
|
|
|
|
CollisionObjectBullet::on_exit_area(p_area);
|
|
|
|
}
|