2023-01-05 12:25:55 +00:00
/**************************************************************************/
/* collision_object_3d.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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-04 23:50:27 +00:00
2020-03-26 21:49:16 +00:00
# include "collision_object_3d.h"
2017-08-27 19:07:15 +00:00
2022-11-25 17:43:06 +00:00
# include "scene/resources/shape_3d.h"
2014-09-15 14:33:30 +00:00
# include "scene/scene_string_names.h"
2014-02-10 01:10:30 +00:00
2020-03-26 21:49:16 +00:00
void CollisionObject3D : : _notification ( int p_what ) {
2017-03-05 15:44:50 +00:00
switch ( p_what ) {
2021-04-25 09:27:13 +00:00
case NOTIFICATION_ENTER_TREE : {
if ( _are_collision_shapes_visible ( ) ) {
debug_shape_old_transform = get_global_transform ( ) ;
2021-08-09 20:13:42 +00:00
for ( const KeyValue < uint32_t , ShapeData > & E : shapes ) {
debug_shapes_to_update . insert ( E . key ) ;
2021-04-25 09:27:13 +00:00
}
_update_debug_shapes ( ) ;
}
} break ;
case NOTIFICATION_EXIT_TREE : {
if ( debug_shapes_count > 0 ) {
_clear_debug_shapes ( ) ;
}
} break ;
2014-02-10 01:10:30 +00:00
case NOTIFICATION_ENTER_WORLD : {
2020-05-14 14:41:43 +00:00
if ( area ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > area_set_transform ( rid , get_global_transform ( ) ) ;
2020-05-14 14:41:43 +00:00
} else {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > body_set_state ( rid , PhysicsServer3D : : BODY_STATE_TRANSFORM , get_global_transform ( ) ) ;
2020-05-14 14:41:43 +00:00
}
2015-01-03 19:52:37 +00:00
2021-06-18 01:09:40 +00:00
bool disabled = ! is_enabled ( ) ;
if ( disabled & & ( disable_mode ! = DISABLE_MODE_REMOVE ) ) {
_apply_disabled ( ) ;
}
if ( ! disabled | | ( disable_mode ! = DISABLE_MODE_REMOVE ) ) {
2021-10-23 14:22:29 +00:00
Ref < World3D > world_ref = get_world_3d ( ) ;
ERR_FAIL_COND ( ! world_ref . is_valid ( ) ) ;
RID space = world_ref - > get_space ( ) ;
2021-06-18 01:09:40 +00:00
if ( area ) {
PhysicsServer3D : : get_singleton ( ) - > area_set_space ( rid , space ) ;
} else {
PhysicsServer3D : : get_singleton ( ) - > body_set_space ( rid , space ) ;
}
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2014-10-03 03:10:51 +00:00
_update_pickable ( ) ;
2019-04-05 12:06:16 +00:00
} break ;
2014-02-10 01:10:30 +00:00
case NOTIFICATION_TRANSFORM_CHANGED : {
2021-06-05 01:16:02 +00:00
if ( only_update_transform_changes ) {
return ;
}
2020-05-14 14:41:43 +00:00
if ( area ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > area_set_transform ( rid , get_global_transform ( ) ) ;
2020-05-14 14:41:43 +00:00
} else {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > body_set_state ( rid , PhysicsServer3D : : BODY_STATE_TRANSFORM , get_global_transform ( ) ) ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2021-04-25 09:27:13 +00:00
_on_transform_changed ( ) ;
2014-10-03 03:10:51 +00:00
} break ;
2021-06-18 01:09:40 +00:00
2014-10-03 03:10:51 +00:00
case NOTIFICATION_VISIBILITY_CHANGED : {
_update_pickable ( ) ;
2014-02-10 01:10:30 +00:00
} break ;
2021-06-18 01:09:40 +00:00
2014-02-10 01:10:30 +00:00
case NOTIFICATION_EXIT_WORLD : {
2021-06-18 01:09:40 +00:00
bool disabled = ! is_enabled ( ) ;
if ( ! disabled | | ( disable_mode ! = DISABLE_MODE_REMOVE ) ) {
if ( area ) {
PhysicsServer3D : : get_singleton ( ) - > area_set_space ( rid , RID ( ) ) ;
} else {
PhysicsServer3D : : get_singleton ( ) - > body_set_space ( rid , RID ( ) ) ;
}
}
if ( disabled & & ( disable_mode ! = DISABLE_MODE_REMOVE ) ) {
_apply_enabled ( ) ;
2020-05-14 14:41:43 +00:00
}
2021-06-18 01:09:40 +00:00
} break ;
2014-02-10 01:10:30 +00:00
2021-06-18 01:09:40 +00:00
case NOTIFICATION_DISABLED : {
_apply_disabled ( ) ;
} break ;
case NOTIFICATION_ENABLED : {
_apply_enabled ( ) ;
2014-02-10 01:10:30 +00:00
} break ;
}
}
2020-10-13 13:09:31 +00: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 ;
}
2021-08-11 23:01:38 +00:00
void CollisionObject3D : : set_collision_layer_value ( int p_layer_number , bool p_value ) {
ERR_FAIL_COND_MSG ( p_layer_number < 1 , " Collision layer number must be between 1 and 32 inclusive. " ) ;
ERR_FAIL_COND_MSG ( p_layer_number > 32 , " Collision layer number must be between 1 and 32 inclusive. " ) ;
2022-09-29 09:53:28 +00:00
uint32_t collision_layer_new = get_collision_layer ( ) ;
2020-10-13 13:09:31 +00:00
if ( p_value ) {
2022-09-29 09:53:28 +00:00
collision_layer_new | = 1 < < ( p_layer_number - 1 ) ;
2020-10-13 13:09:31 +00:00
} else {
2022-09-29 09:53:28 +00:00
collision_layer_new & = ~ ( 1 < < ( p_layer_number - 1 ) ) ;
2020-10-13 13:09:31 +00:00
}
2022-09-29 09:53:28 +00:00
set_collision_layer ( collision_layer_new ) ;
2020-10-13 13:09:31 +00:00
}
2021-08-11 23:01:38 +00:00
bool CollisionObject3D : : get_collision_layer_value ( int p_layer_number ) const {
ERR_FAIL_COND_V_MSG ( p_layer_number < 1 , false , " Collision layer number must be between 1 and 32 inclusive. " ) ;
ERR_FAIL_COND_V_MSG ( p_layer_number > 32 , false , " Collision layer number must be between 1 and 32 inclusive. " ) ;
return get_collision_layer ( ) & ( 1 < < ( p_layer_number - 1 ) ) ;
2020-10-13 13:09:31 +00:00
}
2021-08-11 23:01:38 +00:00
void CollisionObject3D : : set_collision_mask_value ( int p_layer_number , bool p_value ) {
ERR_FAIL_COND_MSG ( p_layer_number < 1 , " Collision layer number must be between 1 and 32 inclusive. " ) ;
ERR_FAIL_COND_MSG ( p_layer_number > 32 , " Collision layer number must be between 1 and 32 inclusive. " ) ;
2020-10-13 13:09:31 +00:00
uint32_t mask = get_collision_mask ( ) ;
if ( p_value ) {
2021-08-11 23:01:38 +00:00
mask | = 1 < < ( p_layer_number - 1 ) ;
2020-10-13 13:09:31 +00:00
} else {
2021-08-11 23:01:38 +00:00
mask & = ~ ( 1 < < ( p_layer_number - 1 ) ) ;
2020-10-13 13:09:31 +00:00
}
set_collision_mask ( mask ) ;
}
2021-08-11 23:01:38 +00:00
bool CollisionObject3D : : get_collision_mask_value ( int p_layer_number ) const {
ERR_FAIL_COND_V_MSG ( p_layer_number < 1 , false , " Collision layer number must be between 1 and 32 inclusive. " ) ;
ERR_FAIL_COND_V_MSG ( p_layer_number > 32 , false , " Collision layer number must be between 1 and 32 inclusive. " ) ;
return get_collision_mask ( ) & ( 1 < < ( p_layer_number - 1 ) ) ;
2020-10-13 13:09:31 +00:00
}
2022-08-10 16:45:36 +00:00
void CollisionObject3D : : set_collision_priority ( real_t p_priority ) {
collision_priority = p_priority ;
if ( ! area ) {
PhysicsServer3D : : get_singleton ( ) - > body_set_collision_priority ( get_rid ( ) , p_priority ) ;
}
}
real_t CollisionObject3D : : get_collision_priority ( ) const {
return collision_priority ;
}
2021-06-18 01:09:40 +00:00
void CollisionObject3D : : set_disable_mode ( DisableMode p_mode ) {
if ( disable_mode = = p_mode ) {
return ;
}
bool disabled = is_inside_tree ( ) & & ! is_enabled ( ) ;
if ( disabled ) {
// Cancel previous disable mode.
_apply_enabled ( ) ;
}
disable_mode = p_mode ;
if ( disabled ) {
// Apply new disable mode.
_apply_disabled ( ) ;
}
}
CollisionObject3D : : DisableMode CollisionObject3D : : get_disable_mode ( ) const {
return disable_mode ;
}
void CollisionObject3D : : _apply_disabled ( ) {
switch ( disable_mode ) {
case DISABLE_MODE_REMOVE : {
if ( is_inside_tree ( ) ) {
if ( area ) {
PhysicsServer3D : : get_singleton ( ) - > area_set_space ( rid , RID ( ) ) ;
} else {
PhysicsServer3D : : get_singleton ( ) - > body_set_space ( rid , RID ( ) ) ;
}
}
} break ;
case DISABLE_MODE_MAKE_STATIC : {
if ( ! area & & ( body_mode ! = PhysicsServer3D : : BODY_MODE_STATIC ) ) {
PhysicsServer3D : : get_singleton ( ) - > body_set_mode ( rid , PhysicsServer3D : : BODY_MODE_STATIC ) ;
}
} break ;
case DISABLE_MODE_KEEP_ACTIVE : {
// Nothing to do.
} break ;
}
}
void CollisionObject3D : : _apply_enabled ( ) {
switch ( disable_mode ) {
case DISABLE_MODE_REMOVE : {
if ( is_inside_tree ( ) ) {
RID space = get_world_3d ( ) - > get_space ( ) ;
if ( area ) {
PhysicsServer3D : : get_singleton ( ) - > area_set_space ( rid , space ) ;
} else {
PhysicsServer3D : : get_singleton ( ) - > body_set_space ( rid , space ) ;
}
}
} break ;
case DISABLE_MODE_MAKE_STATIC : {
if ( ! area & & ( body_mode ! = PhysicsServer3D : : BODY_MODE_STATIC ) ) {
PhysicsServer3D : : get_singleton ( ) - > body_set_mode ( rid , body_mode ) ;
}
} break ;
case DISABLE_MODE_KEEP_ACTIVE : {
// Nothing to do.
} break ;
}
}
2021-08-22 15:37:22 +00:00
void CollisionObject3D : : _input_event_call ( Camera3D * p_camera , const Ref < InputEvent > & p_input_event , const Vector3 & p_pos , const Vector3 & p_normal , int p_shape ) {
GDVIRTUAL_CALL ( _input_event , p_camera , p_input_event , p_pos , p_normal , p_shape ) ;
2017-03-05 15:44:50 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > input_event , p_camera , p_input_event , p_pos , p_normal , p_shape ) ;
2014-09-15 14:33:30 +00:00
}
2020-03-26 21:49:16 +00:00
void CollisionObject3D : : _mouse_enter ( ) {
2014-09-15 14:33:30 +00:00
if ( get_script_instance ( ) ) {
get_script_instance ( ) - > call ( SceneStringNames : : get_singleton ( ) - > _mouse_enter ) ;
}
2017-01-12 03:51:08 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > mouse_entered ) ;
2014-09-15 14:33:30 +00:00
}
2020-03-26 21:49:16 +00:00
void CollisionObject3D : : _mouse_exit ( ) {
2014-09-15 14:33:30 +00:00
if ( get_script_instance ( ) ) {
get_script_instance ( ) - > call ( SceneStringNames : : get_singleton ( ) - > _mouse_exit ) ;
}
2017-01-12 03:51:08 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > mouse_exited ) ;
2014-09-15 14:33:30 +00:00
}
2021-06-18 01:09:40 +00:00
void CollisionObject3D : : set_body_mode ( PhysicsServer3D : : BodyMode p_mode ) {
ERR_FAIL_COND ( area ) ;
if ( body_mode = = p_mode ) {
return ;
}
body_mode = p_mode ;
if ( is_inside_tree ( ) & & ! is_enabled ( ) & & ( disable_mode = = DISABLE_MODE_MAKE_STATIC ) ) {
return ;
}
PhysicsServer3D : : get_singleton ( ) - > body_set_mode ( rid , p_mode ) ;
}
2021-06-05 01:16:02 +00:00
void CollisionObject3D : : set_only_update_transform_changes ( bool p_enable ) {
only_update_transform_changes = p_enable ;
}
bool CollisionObject3D : : is_only_update_transform_changes_enabled ( ) const {
return only_update_transform_changes ;
}
2020-03-26 21:49:16 +00:00
void CollisionObject3D : : _update_pickable ( ) {
2020-05-14 14:41:43 +00:00
if ( ! is_inside_tree ( ) ) {
2014-10-03 03:10:51 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2019-08-30 17:20:25 +00:00
bool pickable = ray_pickable & & is_visible_in_tree ( ) ;
2020-05-14 14:41:43 +00:00
if ( area ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > area_set_ray_pickable ( rid , pickable ) ;
2020-05-14 14:41:43 +00:00
} else {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > body_set_ray_pickable ( rid , pickable ) ;
2020-05-14 14:41:43 +00:00
}
2014-10-03 03:10:51 +00:00
}
2021-04-25 09:27:13 +00:00
bool CollisionObject3D : : _are_collision_shapes_visible ( ) {
return is_inside_tree ( ) & & get_tree ( ) - > is_debugging_collisions_hint ( ) & & ! Engine : : get_singleton ( ) - > is_editor_hint ( ) ;
}
void CollisionObject3D : : _update_shape_data ( uint32_t p_owner ) {
if ( _are_collision_shapes_visible ( ) ) {
if ( debug_shapes_to_update . is_empty ( ) ) {
2022-10-21 21:40:06 +00:00
callable_mp ( this , & CollisionObject3D : : _update_debug_shapes ) . call_deferred ( ) ;
2021-04-25 09:27:13 +00:00
}
debug_shapes_to_update . insert ( p_owner ) ;
}
}
2021-05-09 16:58:19 +00:00
void CollisionObject3D : : _shape_changed ( const Ref < Shape3D > & p_shape ) {
2021-08-09 20:13:42 +00:00
for ( KeyValue < uint32_t , ShapeData > & E : shapes ) {
ShapeData & shapedata = E . value ;
2022-09-29 09:53:28 +00:00
ShapeData : : ShapeBase * shape_bases = shapedata . shapes . ptrw ( ) ;
2021-04-25 09:27:13 +00:00
for ( int i = 0 ; i < shapedata . shapes . size ( ) ; i + + ) {
2022-09-29 09:53:28 +00:00
ShapeData : : ShapeBase & s = shape_bases [ i ] ;
2021-04-25 09:27:13 +00:00
if ( s . shape = = p_shape & & s . debug_shape . is_valid ( ) ) {
Ref < Mesh > mesh = s . shape - > get_debug_mesh ( ) ;
RS : : get_singleton ( ) - > instance_set_base ( s . debug_shape , mesh - > get_rid ( ) ) ;
}
}
}
}
2021-02-06 22:21:04 +00:00
void CollisionObject3D : : _update_debug_shapes ( ) {
2022-12-12 17:42:37 +00:00
ERR_FAIL_NULL ( RenderingServer : : get_singleton ( ) ) ;
2021-05-22 15:23:11 +00:00
if ( ! is_inside_tree ( ) ) {
debug_shapes_to_update . clear ( ) ;
return ;
}
2022-05-18 23:43:40 +00:00
for ( const uint32_t & shapedata_idx : debug_shapes_to_update ) {
if ( shapes . has ( shapedata_idx ) ) {
ShapeData & shapedata = shapes [ shapedata_idx ] ;
2022-09-29 09:53:28 +00:00
ShapeData : : ShapeBase * shape_bases = shapedata . shapes . ptrw ( ) ;
2021-02-06 22:21:04 +00:00
for ( int i = 0 ; i < shapedata . shapes . size ( ) ; i + + ) {
2022-09-29 09:53:28 +00:00
ShapeData : : ShapeBase & s = shape_bases [ i ] ;
2021-02-06 22:21:04 +00:00
if ( s . shape . is_null ( ) | | shapedata . disabled ) {
2021-04-25 09:27:13 +00:00
if ( s . debug_shape . is_valid ( ) ) {
RS : : get_singleton ( ) - > free ( s . debug_shape ) ;
s . debug_shape = RID ( ) ;
- - debug_shapes_count ;
}
2021-02-06 22:21:04 +00:00
continue ;
}
2021-04-25 09:27:13 +00:00
if ( s . debug_shape . is_null ( ) ) {
s . debug_shape = RS : : get_singleton ( ) - > instance_create ( ) ;
RS : : get_singleton ( ) - > instance_set_scenario ( s . debug_shape , get_world_3d ( ) - > get_scenario ( ) ) ;
if ( ! s . shape - > is_connected ( " changed " , callable_mp ( this , & CollisionObject3D : : _shape_changed ) ) ) {
2022-07-28 20:56:41 +00:00
s . shape - > connect ( " changed " , callable_mp ( this , & CollisionObject3D : : _shape_changed ) . bind ( s . shape ) , CONNECT_DEFERRED ) ;
2021-04-25 09:27:13 +00:00
}
+ + debug_shapes_count ;
}
2021-02-06 22:21:04 +00:00
Ref < Mesh > mesh = s . shape - > get_debug_mesh ( ) ;
2021-04-25 09:27:13 +00:00
RS : : get_singleton ( ) - > instance_set_base ( s . debug_shape , mesh - > get_rid ( ) ) ;
RS : : get_singleton ( ) - > instance_set_transform ( s . debug_shape , get_global_transform ( ) * shapedata . xform ) ;
2021-02-06 22:21:04 +00:00
}
}
}
debug_shapes_to_update . clear ( ) ;
}
2021-04-13 03:08:30 +00:00
void CollisionObject3D : : _clear_debug_shapes ( ) {
2022-12-12 17:42:37 +00:00
ERR_FAIL_NULL ( RenderingServer : : get_singleton ( ) ) ;
2021-08-09 20:13:42 +00:00
for ( KeyValue < uint32_t , ShapeData > & E : shapes ) {
ShapeData & shapedata = E . value ;
2022-09-29 09:53:28 +00:00
ShapeData : : ShapeBase * shape_bases = shapedata . shapes . ptrw ( ) ;
2021-04-13 03:08:30 +00:00
for ( int i = 0 ; i < shapedata . shapes . size ( ) ; i + + ) {
2022-09-29 09:53:28 +00:00
ShapeData : : ShapeBase & s = shape_bases [ i ] ;
2021-04-25 09:27:13 +00:00
if ( s . debug_shape . is_valid ( ) ) {
RS : : get_singleton ( ) - > free ( s . debug_shape ) ;
s . debug_shape = RID ( ) ;
if ( s . shape . is_valid ( ) & & s . shape - > is_connected ( " changed " , callable_mp ( this , & CollisionObject3D : : _update_shape_data ) ) ) {
s . shape - > disconnect ( " changed " , callable_mp ( this , & CollisionObject3D : : _update_shape_data ) ) ;
}
2021-04-13 03:08:30 +00:00
}
}
}
2021-04-25 09:27:13 +00:00
debug_shapes_count = 0 ;
2021-04-13 03:08:30 +00:00
}
2021-04-25 09:27:13 +00:00
void CollisionObject3D : : _on_transform_changed ( ) {
if ( debug_shapes_count > 0 & & ! debug_shape_old_transform . is_equal_approx ( get_global_transform ( ) ) ) {
debug_shape_old_transform = get_global_transform ( ) ;
2021-08-09 20:13:42 +00:00
for ( KeyValue < uint32_t , ShapeData > & E : shapes ) {
ShapeData & shapedata = E . value ;
2022-07-30 20:21:23 +00:00
if ( shapedata . disabled ) {
continue ; // If disabled then there are no debug shapes to update.
}
2022-09-29 09:53:28 +00:00
const ShapeData : : ShapeBase * shape_bases = shapedata . shapes . ptr ( ) ;
2021-04-25 09:27:13 +00:00
for ( int i = 0 ; i < shapedata . shapes . size ( ) ; i + + ) {
2022-09-29 09:53:28 +00:00
RS : : get_singleton ( ) - > instance_set_transform ( shape_bases [ i ] . debug_shape , debug_shape_old_transform * shapedata . xform ) ;
2021-04-25 09:27:13 +00:00
}
2021-02-06 22:21:04 +00:00
}
}
}
2020-03-26 21:49:16 +00:00
void CollisionObject3D : : set_ray_pickable ( bool p_ray_pickable ) {
2017-03-05 15:44:50 +00:00
ray_pickable = p_ray_pickable ;
2014-10-03 03:10:51 +00:00
_update_pickable ( ) ;
}
2020-03-26 21:49:16 +00:00
bool CollisionObject3D : : is_ray_pickable ( ) const {
2014-10-03 03:10:51 +00:00
return ray_pickable ;
}
2020-03-26 21:49:16 +00:00
void CollisionObject3D : : _bind_methods ( ) {
2020-10-13 13:09:31 +00: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 ) ;
2021-08-11 23:01:38 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_collision_layer_value " , " layer_number " , " value " ) , & CollisionObject3D : : set_collision_layer_value ) ;
ClassDB : : bind_method ( D_METHOD ( " get_collision_layer_value " , " layer_number " ) , & CollisionObject3D : : get_collision_layer_value ) ;
ClassDB : : bind_method ( D_METHOD ( " set_collision_mask_value " , " layer_number " , " value " ) , & CollisionObject3D : : set_collision_mask_value ) ;
ClassDB : : bind_method ( D_METHOD ( " get_collision_mask_value " , " layer_number " ) , & CollisionObject3D : : get_collision_mask_value ) ;
2022-08-10 16:45:36 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_collision_priority " , " priority " ) , & CollisionObject3D : : set_collision_priority ) ;
ClassDB : : bind_method ( D_METHOD ( " get_collision_priority " ) , & CollisionObject3D : : get_collision_priority ) ;
2021-06-18 01:09:40 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_disable_mode " , " mode " ) , & CollisionObject3D : : set_disable_mode ) ;
ClassDB : : bind_method ( D_METHOD ( " get_disable_mode " ) , & CollisionObject3D : : get_disable_mode ) ;
2020-03-26 21:49:16 +00: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 18:29:08 +00:00
2021-08-22 15:37:22 +00:00
GDVIRTUAL_BIND ( _input_event , " camera " , " event " , " position " , " normal " , " shape_idx " ) ;
2022-01-25 23:07:17 +00:00
GDVIRTUAL_BIND ( _mouse_enter ) ;
GDVIRTUAL_BIND ( _mouse_exit ) ;
2017-03-05 15:44:50 +00:00
2021-06-07 06:34:21 +00: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 , " position " ) , PropertyInfo ( Variant : : VECTOR3 , " normal " ) , PropertyInfo ( Variant : : INT , " shape_idx " ) ) ) ;
2017-03-05 15:44:50 +00:00
ADD_SIGNAL ( MethodInfo ( " mouse_entered " ) ) ;
ADD_SIGNAL ( MethodInfo ( " mouse_exited " ) ) ;
2022-05-12 20:03:16 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " disable_mode " , PROPERTY_HINT_ENUM , " Remove,Make Static,Keep Active " ) , " set_disable_mode " , " get_disable_mode " ) ;
2021-06-18 01:09:40 +00:00
2020-10-13 13:09:31 +00: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 " ) ;
2022-08-10 16:45:36 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " collision_priority " ) , " set_collision_priority " , " get_collision_priority " ) ;
2020-10-13 13:09:31 +00:00
ADD_GROUP ( " Input " , " input_ " ) ;
2017-03-05 15:44:50 +00: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 " ) ;
2021-06-18 01:09:40 +00:00
BIND_ENUM_CONSTANT ( DISABLE_MODE_REMOVE ) ;
BIND_ENUM_CONSTANT ( DISABLE_MODE_MAKE_STATIC ) ;
BIND_ENUM_CONSTANT ( DISABLE_MODE_KEEP_ACTIVE ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-26 21:49:16 +00:00
uint32_t CollisionObject3D : : create_shape_owner ( Object * p_owner ) {
2017-07-15 04:23:10 +00:00
ShapeData sd ;
uint32_t id ;
if ( shapes . size ( ) = = 0 ) {
2017-09-24 08:55:45 +00:00
id = 0 ;
2017-07-15 04:23:10 +00:00
} else {
id = shapes . back ( ) - > key ( ) + 1 ;
}
2022-01-25 09:16:06 +00:00
sd . owner_id = p_owner ? p_owner - > get_instance_id ( ) : ObjectID ( ) ;
2017-07-15 04:23:10 +00:00
shapes [ id ] = sd ;
2014-02-10 01:10:30 +00:00
2017-07-15 04:23:10 +00:00
return id ;
2014-02-10 01:10:30 +00:00
}
2020-03-26 21:49:16 +00:00
void CollisionObject3D : : remove_shape_owner ( uint32_t owner ) {
2017-07-15 04:23:10 +00:00
ERR_FAIL_COND ( ! shapes . has ( owner ) ) ;
shape_owner_clear_shapes ( owner ) ;
shapes . erase ( owner ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-26 21:49:16 +00:00
void CollisionObject3D : : shape_owner_set_disabled ( uint32_t p_owner , bool p_disabled ) {
2017-07-15 04:23:10 +00:00
ERR_FAIL_COND ( ! shapes . has ( p_owner ) ) ;
ShapeData & sd = shapes [ p_owner ] ;
2021-04-25 09:27:13 +00:00
if ( sd . disabled = = p_disabled ) {
return ;
}
2017-07-15 04:23:10 +00:00
sd . disabled = p_disabled ;
2021-04-25 09:27:13 +00:00
2017-07-15 04:23:10 +00:00
for ( int i = 0 ; i < sd . shapes . size ( ) ; i + + ) {
if ( area ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > area_set_shape_disabled ( rid , sd . shapes [ i ] . index , p_disabled ) ;
2017-07-15 04:23:10 +00:00
} else {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > body_set_shape_disabled ( rid , sd . shapes [ i ] . index , p_disabled ) ;
2017-07-15 04:23:10 +00:00
}
}
2021-02-06 22:21:04 +00:00
_update_shape_data ( p_owner ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-26 21:49:16 +00:00
bool CollisionObject3D : : is_shape_owner_disabled ( uint32_t p_owner ) const {
2017-07-15 04:23:10 +00:00
ERR_FAIL_COND_V ( ! shapes . has ( p_owner ) , false ) ;
2014-02-10 01:10:30 +00:00
2017-07-15 04:23:10 +00:00
return shapes [ p_owner ] . disabled ;
2014-02-10 01:10:30 +00:00
}
2020-03-26 21:49:16 +00:00
void CollisionObject3D : : get_shape_owners ( List < uint32_t > * r_owners ) {
2021-08-09 20:13:42 +00:00
for ( const KeyValue < uint32_t , ShapeData > & E : shapes ) {
r_owners - > push_back ( E . key ) ;
2017-07-15 04:23:10 +00:00
}
2014-02-10 01:10:30 +00:00
}
2022-08-05 01:41:48 +00:00
PackedInt32Array CollisionObject3D : : _get_shape_owners ( ) {
PackedInt32Array ret ;
2021-08-09 20:13:42 +00:00
for ( const KeyValue < uint32_t , ShapeData > & E : shapes ) {
ret . push_back ( E . key ) ;
2017-07-24 18:29:08 +00:00
}
return ret ;
}
2020-10-17 05:08:21 +00:00
void CollisionObject3D : : shape_owner_set_transform ( uint32_t p_owner , const Transform3D & p_transform ) {
2017-07-15 04:23:10 +00: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 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > area_set_shape_transform ( rid , sd . shapes [ i ] . index , p_transform ) ;
2017-07-15 04:23:10 +00:00
} else {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > body_set_shape_transform ( rid , sd . shapes [ i ] . index , p_transform ) ;
2017-07-15 04:23:10 +00:00
}
}
2021-02-06 22:21:04 +00:00
_update_shape_data ( p_owner ) ;
2014-02-10 01:10:30 +00:00
}
2020-10-17 05:08:21 +00:00
Transform3D CollisionObject3D : : shape_owner_get_transform ( uint32_t p_owner ) const {
ERR_FAIL_COND_V ( ! shapes . has ( p_owner ) , Transform3D ( ) ) ;
2014-02-10 01:10:30 +00:00
2017-07-15 04:23:10 +00:00
return shapes [ p_owner ] . xform ;
2014-02-10 01:10:30 +00:00
}
2020-03-26 21:49:16 +00:00
Object * CollisionObject3D : : shape_owner_get_owner ( uint32_t p_owner ) const {
2020-04-01 23:20:12 +00:00
ERR_FAIL_COND_V ( ! shapes . has ( p_owner ) , nullptr ) ;
2014-02-10 01:10:30 +00:00
2022-01-25 09:16:06 +00:00
return ObjectDB : : get_instance ( shapes [ p_owner ] . owner_id ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-26 21:49:16 +00:00
void CollisionObject3D : : shape_owner_add_shape ( uint32_t p_owner , const Ref < Shape3D > & p_shape ) {
2017-07-15 04:23:10 +00:00
ERR_FAIL_COND ( ! shapes . has ( p_owner ) ) ;
ERR_FAIL_COND ( p_shape . is_null ( ) ) ;
2014-02-10 01:10:30 +00:00
2017-07-15 04:23:10 +00:00
ShapeData & sd = shapes [ p_owner ] ;
ShapeData : : ShapeBase s ;
s . index = total_subshapes ;
s . shape = p_shape ;
2021-02-06 22:21:04 +00:00
2017-07-15 04:23:10 +00:00
if ( area ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > area_add_shape ( rid , p_shape - > get_rid ( ) , sd . xform , sd . disabled ) ;
2017-07-15 04:23:10 +00:00
} else {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > body_add_shape ( rid , p_shape - > get_rid ( ) , sd . xform , sd . disabled ) ;
2017-03-05 15:44:50 +00:00
}
2017-07-15 04:23:10 +00:00
sd . shapes . push_back ( s ) ;
total_subshapes + + ;
2021-02-06 22:21:04 +00:00
_update_shape_data ( p_owner ) ;
2014-02-10 01:10:30 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-26 21:49:16 +00:00
int CollisionObject3D : : shape_owner_get_shape_count ( uint32_t p_owner ) const {
2017-07-15 04:23:10 +00:00
ERR_FAIL_COND_V ( ! shapes . has ( p_owner ) , 0 ) ;
2014-02-10 01:10:30 +00:00
2017-07-15 04:23:10 +00:00
return shapes [ p_owner ] . shapes . size ( ) ;
}
2020-05-14 12:29:06 +00:00
2020-03-26 21:49:16 +00: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 04:23:10 +00:00
return shapes [ p_owner ] . shapes [ p_shape ] . shape ;
}
2020-05-14 12:29:06 +00:00
2020-03-26 21:49:16 +00:00
int CollisionObject3D : : shape_owner_get_shape_index ( uint32_t p_owner , int p_shape ) const {
2017-07-15 04:23:10 +00: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 21:49:16 +00:00
void CollisionObject3D : : shape_owner_remove_shape ( uint32_t p_owner , int p_shape ) {
2022-12-12 17:42:37 +00:00
ERR_FAIL_NULL ( RenderingServer : : get_singleton ( ) ) ;
2017-07-15 04:23:10 +00:00
ERR_FAIL_COND ( ! shapes . has ( p_owner ) ) ;
ERR_FAIL_INDEX ( p_shape , shapes [ p_owner ] . shapes . size ( ) ) ;
2021-04-25 09:27:13 +00:00
ShapeData : : ShapeBase & s = shapes [ p_owner ] . shapes . write [ p_shape ] ;
2021-02-06 22:21:04 +00:00
int index_to_remove = s . index ;
2017-07-15 04:23:10 +00:00
if ( area ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > area_remove_shape ( rid , index_to_remove ) ;
2017-07-15 04:23:10 +00:00
} else {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > body_remove_shape ( rid , index_to_remove ) ;
2017-07-15 04:23:10 +00:00
}
2021-04-25 09:27:13 +00:00
if ( s . debug_shape . is_valid ( ) ) {
RS : : get_singleton ( ) - > free ( s . debug_shape ) ;
2021-05-09 16:58:19 +00:00
if ( s . shape . is_valid ( ) & & s . shape - > is_connected ( " changed " , callable_mp ( this , & CollisionObject3D : : _shape_changed ) ) ) {
s . shape - > disconnect ( " changed " , callable_mp ( this , & CollisionObject3D : : _shape_changed ) ) ;
2021-04-25 09:27:13 +00:00
}
- - debug_shapes_count ;
2021-02-06 22:21:04 +00:00
}
2021-07-03 22:17:03 +00:00
shapes [ p_owner ] . shapes . remove_at ( p_shape ) ;
2017-07-15 04:23:10 +00:00
2021-08-09 20:13:42 +00:00
for ( KeyValue < uint32_t , ShapeData > & E : shapes ) {
for ( int i = 0 ; i < E . value . shapes . size ( ) ; i + + ) {
if ( E . value . shapes [ i ] . index > index_to_remove ) {
E . value . shapes . write [ i ] . index - = 1 ;
2017-07-15 04:23:10 +00:00
}
}
}
total_subshapes - - ;
}
2020-03-26 21:49:16 +00:00
void CollisionObject3D : : shape_owner_clear_shapes ( uint32_t p_owner ) {
2017-07-15 04:23:10 +00: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 21:49:16 +00:00
uint32_t CollisionObject3D : : shape_find_owner ( int p_shape_index ) const {
2021-10-19 13:40:07 +00:00
ERR_FAIL_INDEX_V ( p_shape_index , total_subshapes , UINT32_MAX ) ;
2017-07-15 04:23:10 +00:00
2021-08-09 20:13:42 +00:00
for ( const KeyValue < uint32_t , ShapeData > & E : shapes ) {
for ( int i = 0 ; i < E . value . shapes . size ( ) ; i + + ) {
if ( E . value . shapes [ i ] . index = = p_shape_index ) {
return E . key ;
2017-07-15 04:23:10 +00:00
}
}
}
//in theory it should be unreachable
2021-10-19 13:40:07 +00:00
ERR_FAIL_V_MSG ( UINT32_MAX , " Can't find owner for shape index " + itos ( p_shape_index ) + " . " ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-26 21:49:16 +00:00
CollisionObject3D : : CollisionObject3D ( RID p_rid , bool p_area ) {
2017-03-05 15:44:50 +00:00
rid = p_rid ;
area = p_area ;
2017-02-15 11:29:46 +00:00
set_notify_transform ( true ) ;
2017-07-15 04:23:10 +00:00
2014-02-10 01:10:30 +00:00
if ( p_area ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > area_attach_object_instance_id ( rid , get_instance_id ( ) ) ;
2014-02-10 01:10:30 +00:00
} else {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > body_attach_object_instance_id ( rid , get_instance_id ( ) ) ;
2021-06-18 01:09:40 +00:00
PhysicsServer3D : : get_singleton ( ) - > body_set_mode ( rid , body_mode ) ;
2014-02-10 01:10:30 +00:00
}
}
2020-03-26 21:49:16 +00:00
void CollisionObject3D : : set_capture_input_on_drag ( bool p_capture ) {
2017-03-05 15:44:50 +00:00
capture_input_on_drag = p_capture ;
2014-09-15 14:33:30 +00:00
}
2020-03-26 21:49:16 +00:00
bool CollisionObject3D : : get_capture_input_on_drag ( ) const {
2014-09-15 14:33:30 +00:00
return capture_input_on_drag ;
}
2022-09-19 15:43:15 +00:00
PackedStringArray CollisionObject3D : : get_configuration_warnings ( ) const {
PackedStringArray warnings = Node : : get_configuration_warnings ( ) ;
2018-01-18 17:30:07 +00:00
2020-12-15 12:04:21 +00:00
if ( shapes . is_empty ( ) ) {
2022-03-28 13:24:14 +00:00
warnings . push_back ( RTR ( " 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 17:30:07 +00:00
}
2020-10-29 10:01:28 +00:00
return warnings ;
2018-01-18 17:30:07 +00:00
}
2020-03-26 21:49:16 +00:00
CollisionObject3D : : CollisionObject3D ( ) {
2017-01-12 23:35:46 +00:00
set_notify_transform ( true ) ;
2014-02-10 01:10:30 +00:00
//owner=
//set_transform_notify(true);
}
2020-03-26 21:49:16 +00:00
CollisionObject3D : : ~ CollisionObject3D ( ) {
2022-12-12 17:42:37 +00:00
ERR_FAIL_NULL ( PhysicsServer3D : : get_singleton ( ) ) ;
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > free ( rid ) ;
2014-02-10 01:10:30 +00:00
}