2014-02-10 02:10:30 +01:00
/*************************************************************************/
2020-03-26 22:49:16 +01:00
/* collision_object_3d.cpp */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
2021-01-01 20:13:46 +01:00
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 02:10:30 +01: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. */
/*************************************************************************/
2018-01-05 00:50:27 +01:00
2020-03-26 22:49:16 +01:00
# include "collision_object_3d.h"
2017-08-27 21:07:15 +02:00
2021-03-17 01:56:47 +01:00
# include "core/config/engine.h"
2021-02-06 23:21:04 +01:00
# include "mesh_instance_3d.h"
2014-09-15 16:33:30 +02:00
# include "scene/scene_string_names.h"
2020-03-27 19:21:27 +01:00
# include "servers/physics_server_3d.h"
2014-02-10 02:10:30 +01:00
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : _notification ( int p_what ) {
2017-03-05 16:44:50 +01:00
switch ( p_what ) {
2014-02-10 02:10:30 +01:00
case NOTIFICATION_ENTER_WORLD : {
2020-05-14 16:41:43 +02:00
if ( area ) {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > area_set_transform ( rid , get_global_transform ( ) ) ;
2020-05-14 16:41:43 +02:00
} else {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > body_set_state ( rid , PhysicsServer3D : : BODY_STATE_TRANSFORM , get_global_transform ( ) ) ;
2020-05-14 16:41:43 +02:00
}
2015-01-03 20:52:37 +01:00
2020-04-18 11:00:51 +02:00
RID space = get_world_3d ( ) - > get_space ( ) ;
2014-02-10 02:10:30 +01:00
if ( area ) {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > area_set_space ( rid , space ) ;
2020-05-14 16:41:43 +02:00
} else {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > body_set_space ( rid , space ) ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
2014-10-03 05:10:51 +02:00
_update_pickable ( ) ;
2017-03-05 16:44:50 +01:00
//get space
2019-04-05 14:06:16 +02:00
} break ;
2014-02-10 02:10:30 +01:00
case NOTIFICATION_TRANSFORM_CHANGED : {
2020-05-14 16:41:43 +02:00
if ( area ) {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > area_set_transform ( rid , get_global_transform ( ) ) ;
2020-05-14 16:41:43 +02:00
} else {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > body_set_state ( rid , PhysicsServer3D : : BODY_STATE_TRANSFORM , get_global_transform ( ) ) ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
2014-10-03 05:10:51 +02:00
} break ;
case NOTIFICATION_VISIBILITY_CHANGED : {
_update_pickable ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case NOTIFICATION_EXIT_WORLD : {
if ( area ) {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > area_set_space ( rid , RID ( ) ) ;
2020-05-14 16:41:43 +02:00
} else {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > body_set_space ( rid , RID ( ) ) ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
} break ;
2021-04-13 05:08:30 +02:00
case NOTIFICATION_PREDELETE : {
if ( debug_shape_count > 0 ) {
_clear_debug_shapes ( ) ;
}
} break ;
2014-02-10 02:10:30 +01:00
}
}
2020-10-13 15:09:31 +02:00
void CollisionObject3D : : set_collision_layer ( uint32_t p_layer ) {
collision_layer = p_layer ;
if ( area ) {
PhysicsServer3D : : get_singleton ( ) - > area_set_collision_layer ( get_rid ( ) , p_layer ) ;
} else {
PhysicsServer3D : : get_singleton ( ) - > body_set_collision_layer ( get_rid ( ) , p_layer ) ;
}
}
uint32_t CollisionObject3D : : get_collision_layer ( ) const {
return collision_layer ;
}
void CollisionObject3D : : set_collision_mask ( uint32_t p_mask ) {
collision_mask = p_mask ;
if ( area ) {
PhysicsServer3D : : get_singleton ( ) - > area_set_collision_mask ( get_rid ( ) , p_mask ) ;
} else {
PhysicsServer3D : : get_singleton ( ) - > body_set_collision_mask ( get_rid ( ) , p_mask ) ;
}
}
uint32_t CollisionObject3D : : get_collision_mask ( ) const {
return collision_mask ;
}
void CollisionObject3D : : set_collision_layer_bit ( int p_bit , bool p_value ) {
2021-04-30 17:19:04 +02:00
ERR_FAIL_INDEX_MSG ( p_bit , 32 , " Collision layer bit must be between 0 and 31 inclusive. " ) ;
2020-10-13 15:09:31 +02:00
uint32_t collision_layer = get_collision_layer ( ) ;
if ( p_value ) {
collision_layer | = 1 < < p_bit ;
} else {
collision_layer & = ~ ( 1 < < p_bit ) ;
}
set_collision_layer ( collision_layer ) ;
}
bool CollisionObject3D : : get_collision_layer_bit ( int p_bit ) const {
2021-04-30 17:19:04 +02:00
ERR_FAIL_INDEX_V_MSG ( p_bit , 32 , false , " Collision layer bit must be between 0 and 31 inclusive. " ) ;
2020-10-13 15:09:31 +02:00
return get_collision_layer ( ) & ( 1 < < p_bit ) ;
}
void CollisionObject3D : : set_collision_mask_bit ( int p_bit , bool p_value ) {
2021-04-30 17:19:04 +02:00
ERR_FAIL_INDEX_MSG ( p_bit , 32 , " Collision mask bit must be between 0 and 31 inclusive. " ) ;
2020-10-13 15:09:31 +02:00
uint32_t mask = get_collision_mask ( ) ;
if ( p_value ) {
mask | = 1 < < p_bit ;
} else {
mask & = ~ ( 1 < < p_bit ) ;
}
set_collision_mask ( mask ) ;
}
bool CollisionObject3D : : get_collision_mask_bit ( int p_bit ) const {
2021-04-30 17:19:04 +02:00
ERR_FAIL_INDEX_V_MSG ( p_bit , 32 , false , " Collision mask bit must be between 0 and 31 inclusive. " ) ;
2020-10-13 15:09:31 +02:00
return get_collision_mask ( ) & ( 1 < < p_bit ) ;
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : _input_event ( Node * p_camera , const Ref < InputEvent > & p_input_event , const Vector3 & p_pos , const Vector3 & p_normal , int p_shape ) {
2014-09-15 16:33:30 +02:00
if ( get_script_instance ( ) ) {
2017-03-05 16:44:50 +01:00
get_script_instance ( ) - > call ( SceneStringNames : : get_singleton ( ) - > _input_event , p_camera , p_input_event , p_pos , p_normal , p_shape ) ;
2014-09-15 16:33:30 +02:00
}
2017-03-05 16:44:50 +01:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > input_event , p_camera , p_input_event , p_pos , p_normal , p_shape ) ;
2014-09-15 16:33:30 +02:00
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : _mouse_enter ( ) {
2014-09-15 16:33:30 +02:00
if ( get_script_instance ( ) ) {
get_script_instance ( ) - > call ( SceneStringNames : : get_singleton ( ) - > _mouse_enter ) ;
}
2017-01-12 04:51:08 +01:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > mouse_entered ) ;
2014-09-15 16:33:30 +02:00
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : _mouse_exit ( ) {
2014-09-15 16:33:30 +02:00
if ( get_script_instance ( ) ) {
get_script_instance ( ) - > call ( SceneStringNames : : get_singleton ( ) - > _mouse_exit ) ;
}
2017-01-12 04:51:08 +01:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > mouse_exited ) ;
2014-09-15 16:33:30 +02:00
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : _update_pickable ( ) {
2020-05-14 16:41:43 +02:00
if ( ! is_inside_tree ( ) ) {
2014-10-03 05:10:51 +02:00
return ;
2020-05-14 16:41:43 +02:00
}
2019-08-30 19:20:25 +02:00
bool pickable = ray_pickable & & is_visible_in_tree ( ) ;
2020-05-14 16:41:43 +02:00
if ( area ) {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > area_set_ray_pickable ( rid , pickable ) ;
2020-05-14 16:41:43 +02:00
} else {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > body_set_ray_pickable ( rid , pickable ) ;
2020-05-14 16:41:43 +02:00
}
2014-10-03 05:10:51 +02:00
}
2021-02-06 23:21:04 +01:00
void CollisionObject3D : : _update_debug_shapes ( ) {
for ( Set < uint32_t > : : Element * shapedata_idx = debug_shapes_to_update . front ( ) ; shapedata_idx ; shapedata_idx = shapedata_idx - > next ( ) ) {
if ( shapes . has ( shapedata_idx - > get ( ) ) ) {
ShapeData & shapedata = shapes [ shapedata_idx - > get ( ) ] ;
2021-04-13 05:08:30 +02:00
ShapeData : : ShapeBase * shapes = shapedata . shapes . ptrw ( ) ;
2021-02-06 23:21:04 +01:00
for ( int i = 0 ; i < shapedata . shapes . size ( ) ; i + + ) {
2021-04-13 05:08:30 +02:00
ShapeData : : ShapeBase & s = shapes [ i ] ;
2021-02-06 23:21:04 +01:00
if ( s . debug_shape ) {
s . debug_shape - > queue_delete ( ) ;
s . debug_shape = nullptr ;
2021-04-13 05:08:30 +02:00
- - debug_shape_count ;
2021-02-06 23:21:04 +01:00
}
if ( s . shape . is_null ( ) | | shapedata . disabled ) {
continue ;
}
Ref < Mesh > mesh = s . shape - > get_debug_mesh ( ) ;
MeshInstance3D * mi = memnew ( MeshInstance3D ) ;
mi - > set_transform ( shapedata . xform ) ;
mi - > set_mesh ( mesh ) ;
add_child ( mi ) ;
mi - > force_update_transform ( ) ;
s . debug_shape = mi ;
2021-04-13 05:08:30 +02:00
+ + debug_shape_count ;
2021-02-06 23:21:04 +01:00
}
}
}
debug_shapes_to_update . clear ( ) ;
}
2021-04-13 05:08:30 +02:00
void CollisionObject3D : : _clear_debug_shapes ( ) {
for ( Map < uint32_t , ShapeData > : : Element * E = shapes . front ( ) ; E ; E = E - > next ( ) ) {
ShapeData & shapedata = E - > get ( ) ;
ShapeData : : ShapeBase * shapes = shapedata . shapes . ptrw ( ) ;
for ( int i = 0 ; i < shapedata . shapes . size ( ) ; i + + ) {
ShapeData : : ShapeBase & s = shapes [ i ] ;
if ( s . debug_shape ) {
s . debug_shape - > queue_delete ( ) ;
s . debug_shape = nullptr ;
- - debug_shape_count ;
}
}
}
debug_shape_count = 0 ;
}
2021-02-06 23:21:04 +01:00
void CollisionObject3D : : _update_shape_data ( uint32_t p_owner ) {
2021-03-17 01:56:47 +01:00
if ( is_inside_tree ( ) & & get_tree ( ) - > is_debugging_collisions_hint ( ) & & ! Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
2021-02-06 23:21:04 +01:00
if ( debug_shapes_to_update . is_empty ( ) ) {
call_deferred ( " _update_debug_shapes " ) ;
}
debug_shapes_to_update . insert ( p_owner ) ;
}
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : set_ray_pickable ( bool p_ray_pickable ) {
2017-03-05 16:44:50 +01:00
ray_pickable = p_ray_pickable ;
2014-10-03 05:10:51 +02:00
_update_pickable ( ) ;
}
2020-03-26 22:49:16 +01:00
bool CollisionObject3D : : is_ray_pickable ( ) const {
2014-10-03 05:10:51 +02:00
return ray_pickable ;
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : _bind_methods ( ) {
2020-10-13 15:09:31 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_collision_layer " , " layer " ) , & CollisionObject3D : : set_collision_layer ) ;
ClassDB : : bind_method ( D_METHOD ( " get_collision_layer " ) , & CollisionObject3D : : get_collision_layer ) ;
ClassDB : : bind_method ( D_METHOD ( " set_collision_mask " , " mask " ) , & CollisionObject3D : : set_collision_mask ) ;
ClassDB : : bind_method ( D_METHOD ( " get_collision_mask " ) , & CollisionObject3D : : get_collision_mask ) ;
ClassDB : : bind_method ( D_METHOD ( " set_collision_layer_bit " , " bit " , " value " ) , & CollisionObject3D : : set_collision_layer_bit ) ;
ClassDB : : bind_method ( D_METHOD ( " get_collision_layer_bit " , " bit " ) , & CollisionObject3D : : get_collision_layer_bit ) ;
ClassDB : : bind_method ( D_METHOD ( " set_collision_mask_bit " , " bit " , " value " ) , & CollisionObject3D : : set_collision_mask_bit ) ;
ClassDB : : bind_method ( D_METHOD ( " get_collision_mask_bit " , " bit " ) , & CollisionObject3D : : get_collision_mask_bit ) ;
2020-03-26 22:49:16 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_ray_pickable " , " ray_pickable " ) , & CollisionObject3D : : set_ray_pickable ) ;
ClassDB : : bind_method ( D_METHOD ( " is_ray_pickable " ) , & CollisionObject3D : : is_ray_pickable ) ;
ClassDB : : bind_method ( D_METHOD ( " set_capture_input_on_drag " , " enable " ) , & CollisionObject3D : : set_capture_input_on_drag ) ;
ClassDB : : bind_method ( D_METHOD ( " get_capture_input_on_drag " ) , & CollisionObject3D : : get_capture_input_on_drag ) ;
ClassDB : : bind_method ( D_METHOD ( " get_rid " ) , & CollisionObject3D : : get_rid ) ;
ClassDB : : bind_method ( D_METHOD ( " create_shape_owner " , " owner " ) , & CollisionObject3D : : create_shape_owner ) ;
ClassDB : : bind_method ( D_METHOD ( " remove_shape_owner " , " owner_id " ) , & CollisionObject3D : : remove_shape_owner ) ;
ClassDB : : bind_method ( D_METHOD ( " get_shape_owners " ) , & CollisionObject3D : : _get_shape_owners ) ;
ClassDB : : bind_method ( D_METHOD ( " shape_owner_set_transform " , " owner_id " , " transform " ) , & CollisionObject3D : : shape_owner_set_transform ) ;
ClassDB : : bind_method ( D_METHOD ( " shape_owner_get_transform " , " owner_id " ) , & CollisionObject3D : : shape_owner_get_transform ) ;
ClassDB : : bind_method ( D_METHOD ( " shape_owner_get_owner " , " owner_id " ) , & CollisionObject3D : : shape_owner_get_owner ) ;
ClassDB : : bind_method ( D_METHOD ( " shape_owner_set_disabled " , " owner_id " , " disabled " ) , & CollisionObject3D : : shape_owner_set_disabled ) ;
ClassDB : : bind_method ( D_METHOD ( " is_shape_owner_disabled " , " owner_id " ) , & CollisionObject3D : : is_shape_owner_disabled ) ;
ClassDB : : bind_method ( D_METHOD ( " shape_owner_add_shape " , " owner_id " , " shape " ) , & CollisionObject3D : : shape_owner_add_shape ) ;
ClassDB : : bind_method ( D_METHOD ( " shape_owner_get_shape_count " , " owner_id " ) , & CollisionObject3D : : shape_owner_get_shape_count ) ;
ClassDB : : bind_method ( D_METHOD ( " shape_owner_get_shape " , " owner_id " , " shape_id " ) , & CollisionObject3D : : shape_owner_get_shape ) ;
ClassDB : : bind_method ( D_METHOD ( " shape_owner_get_shape_index " , " owner_id " , " shape_id " ) , & CollisionObject3D : : shape_owner_get_shape_index ) ;
ClassDB : : bind_method ( D_METHOD ( " shape_owner_remove_shape " , " owner_id " , " shape_id " ) , & CollisionObject3D : : shape_owner_remove_shape ) ;
ClassDB : : bind_method ( D_METHOD ( " shape_owner_clear_shapes " , " owner_id " ) , & CollisionObject3D : : shape_owner_clear_shapes ) ;
ClassDB : : bind_method ( D_METHOD ( " shape_find_owner " , " shape_index " ) , & CollisionObject3D : : shape_find_owner ) ;
2017-07-24 20:29:08 +02:00
2021-02-06 23:21:04 +01:00
ClassDB : : bind_method ( D_METHOD ( " _update_debug_shapes " ) , & CollisionObject3D : : _update_debug_shapes ) ;
2017-09-10 15:37:49 +02:00
BIND_VMETHOD ( MethodInfo ( " _input_event " , PropertyInfo ( Variant : : OBJECT , " camera " ) , PropertyInfo ( Variant : : OBJECT , " event " , PROPERTY_HINT_RESOURCE_TYPE , " InputEvent " ) , PropertyInfo ( Variant : : VECTOR3 , " click_position " ) , PropertyInfo ( Variant : : VECTOR3 , " click_normal " ) , PropertyInfo ( Variant : : INT , " shape_idx " ) ) ) ;
2017-03-05 16:44:50 +01:00
2018-09-01 12:05:51 +02:00
ADD_SIGNAL ( MethodInfo ( " input_event " , PropertyInfo ( Variant : : OBJECT , " camera " , PROPERTY_HINT_RESOURCE_TYPE , " Node " ) , PropertyInfo ( Variant : : OBJECT , " event " , PROPERTY_HINT_RESOURCE_TYPE , " InputEvent " ) , PropertyInfo ( Variant : : VECTOR3 , " click_position " ) , PropertyInfo ( Variant : : VECTOR3 , " click_normal " ) , PropertyInfo ( Variant : : INT , " shape_idx " ) ) ) ;
2017-03-05 16:44:50 +01:00
ADD_SIGNAL ( MethodInfo ( " mouse_entered " ) ) ;
ADD_SIGNAL ( MethodInfo ( " mouse_exited " ) ) ;
2020-10-13 15:09:31 +02:00
ADD_GROUP ( " Collision " , " collision_ " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " collision_layer " , PROPERTY_HINT_LAYERS_3D_PHYSICS ) , " set_collision_layer " , " get_collision_layer " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " collision_mask " , PROPERTY_HINT_LAYERS_3D_PHYSICS ) , " set_collision_mask " , " get_collision_mask " ) ;
ADD_GROUP ( " Input " , " input_ " ) ;
2017-03-05 16:44:50 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " input_ray_pickable " ) , " set_ray_pickable " , " is_ray_pickable " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " input_capture_on_drag " ) , " set_capture_input_on_drag " , " get_capture_input_on_drag " ) ;
2014-02-10 02:10:30 +01:00
}
2020-03-26 22:49:16 +01:00
uint32_t CollisionObject3D : : create_shape_owner ( Object * p_owner ) {
2017-07-15 06:23:10 +02:00
ShapeData sd ;
uint32_t id ;
if ( shapes . size ( ) = = 0 ) {
2017-09-24 10:55:45 +02:00
id = 0 ;
2017-07-15 06:23:10 +02:00
} else {
id = shapes . back ( ) - > key ( ) + 1 ;
}
sd . owner = p_owner ;
shapes [ id ] = sd ;
2014-02-10 02:10:30 +01:00
2017-07-15 06:23:10 +02:00
return id ;
2014-02-10 02:10:30 +01:00
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : remove_shape_owner ( uint32_t owner ) {
2017-07-15 06:23:10 +02:00
ERR_FAIL_COND ( ! shapes . has ( owner ) ) ;
shape_owner_clear_shapes ( owner ) ;
shapes . erase ( owner ) ;
2014-02-10 02:10:30 +01:00
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : shape_owner_set_disabled ( uint32_t p_owner , bool p_disabled ) {
2017-07-15 06:23:10 +02:00
ERR_FAIL_COND ( ! shapes . has ( p_owner ) ) ;
ShapeData & sd = shapes [ p_owner ] ;
sd . disabled = p_disabled ;
for ( int i = 0 ; i < sd . shapes . size ( ) ; i + + ) {
if ( area ) {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > area_set_shape_disabled ( rid , sd . shapes [ i ] . index , p_disabled ) ;
2017-07-15 06:23:10 +02:00
} else {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > body_set_shape_disabled ( rid , sd . shapes [ i ] . index , p_disabled ) ;
2017-07-15 06:23:10 +02:00
}
}
2021-02-06 23:21:04 +01:00
_update_shape_data ( p_owner ) ;
2014-02-10 02:10:30 +01:00
}
2020-03-26 22:49:16 +01:00
bool CollisionObject3D : : is_shape_owner_disabled ( uint32_t p_owner ) const {
2017-07-15 06:23:10 +02:00
ERR_FAIL_COND_V ( ! shapes . has ( p_owner ) , false ) ;
2014-02-10 02:10:30 +01:00
2017-07-15 06:23:10 +02:00
return shapes [ p_owner ] . disabled ;
2014-02-10 02:10:30 +01:00
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : get_shape_owners ( List < uint32_t > * r_owners ) {
2017-07-15 06:23:10 +02:00
for ( Map < uint32_t , ShapeData > : : Element * E = shapes . front ( ) ; E ; E = E - > next ( ) ) {
r_owners - > push_back ( E - > key ( ) ) ;
}
2014-02-10 02:10:30 +01:00
}
2020-03-26 22:49:16 +01:00
Array CollisionObject3D : : _get_shape_owners ( ) {
2017-07-24 20:29:08 +02:00
Array ret ;
for ( Map < uint32_t , ShapeData > : : Element * E = shapes . front ( ) ; E ; E = E - > next ( ) ) {
ret . push_back ( E - > key ( ) ) ;
}
return ret ;
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : shape_owner_set_transform ( uint32_t p_owner , const Transform & p_transform ) {
2017-07-15 06:23:10 +02:00
ERR_FAIL_COND ( ! shapes . has ( p_owner ) ) ;
ShapeData & sd = shapes [ p_owner ] ;
sd . xform = p_transform ;
for ( int i = 0 ; i < sd . shapes . size ( ) ; i + + ) {
if ( area ) {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > area_set_shape_transform ( rid , sd . shapes [ i ] . index , p_transform ) ;
2017-07-15 06:23:10 +02:00
} else {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > body_set_shape_transform ( rid , sd . shapes [ i ] . index , p_transform ) ;
2017-07-15 06:23:10 +02:00
}
}
2021-02-06 23:21:04 +01:00
_update_shape_data ( p_owner ) ;
2014-02-10 02:10:30 +01:00
}
2020-05-14 14:29:06 +02:00
2020-03-26 22:49:16 +01:00
Transform CollisionObject3D : : shape_owner_get_transform ( uint32_t p_owner ) const {
2017-07-15 06:23:10 +02:00
ERR_FAIL_COND_V ( ! shapes . has ( p_owner ) , Transform ( ) ) ;
2014-02-10 02:10:30 +01:00
2017-07-15 06:23:10 +02:00
return shapes [ p_owner ] . xform ;
2014-02-10 02:10:30 +01:00
}
2020-03-26 22:49:16 +01:00
Object * CollisionObject3D : : shape_owner_get_owner ( uint32_t p_owner ) const {
2020-04-02 01:20:12 +02:00
ERR_FAIL_COND_V ( ! shapes . has ( p_owner ) , nullptr ) ;
2014-02-10 02:10:30 +01:00
2017-07-15 06:23:10 +02:00
return shapes [ p_owner ] . owner ;
2014-02-10 02:10:30 +01:00
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : shape_owner_add_shape ( uint32_t p_owner , const Ref < Shape3D > & p_shape ) {
2017-07-15 06:23:10 +02:00
ERR_FAIL_COND ( ! shapes . has ( p_owner ) ) ;
ERR_FAIL_COND ( p_shape . is_null ( ) ) ;
2014-02-10 02:10:30 +01:00
2017-07-15 06:23:10 +02:00
ShapeData & sd = shapes [ p_owner ] ;
ShapeData : : ShapeBase s ;
s . index = total_subshapes ;
s . shape = p_shape ;
2021-02-06 23:21:04 +01:00
2017-07-15 06:23:10 +02:00
if ( area ) {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > area_add_shape ( rid , p_shape - > get_rid ( ) , sd . xform , sd . disabled ) ;
2017-07-15 06:23:10 +02:00
} else {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > body_add_shape ( rid , p_shape - > get_rid ( ) , sd . xform , sd . disabled ) ;
2017-03-05 16:44:50 +01:00
}
2017-07-15 06:23:10 +02:00
sd . shapes . push_back ( s ) ;
total_subshapes + + ;
2021-02-06 23:21:04 +01:00
_update_shape_data ( p_owner ) ;
2014-02-10 02:10:30 +01:00
}
2020-05-14 14:29:06 +02:00
2020-03-26 22:49:16 +01:00
int CollisionObject3D : : shape_owner_get_shape_count ( uint32_t p_owner ) const {
2017-07-15 06:23:10 +02:00
ERR_FAIL_COND_V ( ! shapes . has ( p_owner ) , 0 ) ;
2014-02-10 02:10:30 +01:00
2017-07-15 06:23:10 +02:00
return shapes [ p_owner ] . shapes . size ( ) ;
}
2020-05-14 14:29:06 +02:00
2020-03-26 22:49:16 +01:00
Ref < Shape3D > CollisionObject3D : : shape_owner_get_shape ( uint32_t p_owner , int p_shape ) const {
ERR_FAIL_COND_V ( ! shapes . has ( p_owner ) , Ref < Shape3D > ( ) ) ;
ERR_FAIL_INDEX_V ( p_shape , shapes [ p_owner ] . shapes . size ( ) , Ref < Shape3D > ( ) ) ;
2017-07-15 06:23:10 +02:00
return shapes [ p_owner ] . shapes [ p_shape ] . shape ;
}
2020-05-14 14:29:06 +02:00
2020-03-26 22:49:16 +01:00
int CollisionObject3D : : shape_owner_get_shape_index ( uint32_t p_owner , int p_shape ) const {
2017-07-15 06:23:10 +02:00
ERR_FAIL_COND_V ( ! shapes . has ( p_owner ) , - 1 ) ;
ERR_FAIL_INDEX_V ( p_shape , shapes [ p_owner ] . shapes . size ( ) , - 1 ) ;
return shapes [ p_owner ] . shapes [ p_shape ] . index ;
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : shape_owner_remove_shape ( uint32_t p_owner , int p_shape ) {
2017-07-15 06:23:10 +02:00
ERR_FAIL_COND ( ! shapes . has ( p_owner ) ) ;
ERR_FAIL_INDEX ( p_shape , shapes [ p_owner ] . shapes . size ( ) ) ;
2021-02-06 23:21:04 +01:00
const ShapeData : : ShapeBase & s = shapes [ p_owner ] . shapes [ p_shape ] ;
int index_to_remove = s . index ;
2017-07-15 06:23:10 +02:00
if ( area ) {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > area_remove_shape ( rid , index_to_remove ) ;
2017-07-15 06:23:10 +02:00
} else {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > body_remove_shape ( rid , index_to_remove ) ;
2017-07-15 06:23:10 +02:00
}
2021-02-06 23:21:04 +01:00
if ( s . debug_shape ) {
s . debug_shape - > queue_delete ( ) ;
}
2017-07-15 06:23:10 +02:00
shapes [ p_owner ] . shapes . remove ( p_shape ) ;
for ( Map < uint32_t , ShapeData > : : Element * E = shapes . front ( ) ; E ; E = E - > next ( ) ) {
for ( int i = 0 ; i < E - > get ( ) . shapes . size ( ) ; i + + ) {
if ( E - > get ( ) . shapes [ i ] . index > index_to_remove ) {
2018-07-25 03:11:03 +02:00
E - > get ( ) . shapes . write [ i ] . index - = 1 ;
2017-07-15 06:23:10 +02:00
}
}
}
total_subshapes - - ;
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : shape_owner_clear_shapes ( uint32_t p_owner ) {
2017-07-15 06:23:10 +02:00
ERR_FAIL_COND ( ! shapes . has ( p_owner ) ) ;
while ( shape_owner_get_shape_count ( p_owner ) > 0 ) {
shape_owner_remove_shape ( p_owner , 0 ) ;
}
}
2020-03-26 22:49:16 +01:00
uint32_t CollisionObject3D : : shape_find_owner ( int p_shape_index ) const {
2017-07-15 06:23:10 +02:00
ERR_FAIL_INDEX_V ( p_shape_index , total_subshapes , 0 ) ;
for ( const Map < uint32_t , ShapeData > : : Element * E = shapes . front ( ) ; E ; E = E - > next ( ) ) {
for ( int i = 0 ; i < E - > get ( ) . shapes . size ( ) ; i + + ) {
if ( E - > get ( ) . shapes [ i ] . index = = p_shape_index ) {
return E - > key ( ) ;
}
}
}
//in theory it should be unreachable
return 0 ;
2014-02-10 02:10:30 +01:00
}
2020-03-26 22:49:16 +01:00
CollisionObject3D : : CollisionObject3D ( RID p_rid , bool p_area ) {
2017-03-05 16:44:50 +01:00
rid = p_rid ;
area = p_area ;
2017-02-15 12:29:46 +01:00
set_notify_transform ( true ) ;
2017-07-15 06:23:10 +02:00
2014-02-10 02:10:30 +01:00
if ( p_area ) {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > area_attach_object_instance_id ( rid , get_instance_id ( ) ) ;
2014-02-10 02:10:30 +01:00
} else {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > body_attach_object_instance_id ( rid , get_instance_id ( ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-01-14 12:26:56 +01:00
//set_transform_notify(true);
2014-02-10 02:10:30 +01:00
}
2020-03-26 22:49:16 +01:00
void CollisionObject3D : : set_capture_input_on_drag ( bool p_capture ) {
2017-03-05 16:44:50 +01:00
capture_input_on_drag = p_capture ;
2014-09-15 16:33:30 +02:00
}
2020-03-26 22:49:16 +01:00
bool CollisionObject3D : : get_capture_input_on_drag ( ) const {
2014-09-15 16:33:30 +02:00
return capture_input_on_drag ;
}
2020-10-29 11:01:28 +01:00
TypedArray < String > CollisionObject3D : : get_configuration_warnings ( ) const {
TypedArray < String > warnings = Node : : get_configuration_warnings ( ) ;
2018-01-18 18:30:07 +01:00
2020-12-15 13:04:21 +01:00
if ( shapes . is_empty ( ) ) {
2020-10-29 11:01:28 +01:00
warnings . push_back ( TTR ( " This node has no shape, so it can't collide or interact with other objects. \n Consider adding a CollisionShape3D or CollisionPolygon3D as a child to define its shape. " ) ) ;
2018-01-18 18:30:07 +01:00
}
2020-10-29 11:01:28 +01:00
return warnings ;
2018-01-18 18:30:07 +01:00
}
2020-03-26 22:49:16 +01:00
CollisionObject3D : : CollisionObject3D ( ) {
2017-01-13 00:35:46 +01:00
set_notify_transform ( true ) ;
2014-02-10 02:10:30 +01:00
//owner=
//set_transform_notify(true);
}
2020-03-26 22:49:16 +01:00
CollisionObject3D : : ~ CollisionObject3D ( ) {
2020-03-27 19:21:27 +01:00
PhysicsServer3D : : get_singleton ( ) - > free ( rid ) ;
2014-02-10 02:10:30 +01:00
}