2017-11-21 00:36:32 +00:00
/*************************************************************************/
2020-03-26 21:49:16 +00:00
/* soft_body_3d.cpp */
2017-11-21 00:36:32 +00:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2018-08-29 20:38:13 +00:00
/* https://godotengine.org */
2017-11-21 00:36:32 +00:00
/*************************************************************************/
2021-01-01 19:13:46 +00:00
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
2017-11-21 00:36:32 +00: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. */
/*************************************************************************/
2020-03-26 21:49:16 +00:00
# include "soft_body_3d.h"
2020-03-30 16:22:57 +00:00
2020-11-07 22:33:38 +00:00
# include "core/object/class_db.h"
2018-09-11 16:13:45 +00:00
# include "core/os/os.h"
2020-11-07 22:33:38 +00:00
# include "core/templates/list.h"
# include "core/templates/rid.h"
2020-03-26 21:49:16 +00:00
# include "scene/3d/collision_object_3d.h"
# include "scene/3d/physics_body_3d.h"
# include "scene/3d/skeleton_3d.h"
2017-11-21 00:36:32 +00:00
2020-03-27 18:21:27 +00:00
SoftBodyRenderingServerHandler : : SoftBodyRenderingServerHandler ( ) { }
2017-11-21 00:36:32 +00:00
2020-03-27 18:21:27 +00:00
void SoftBodyRenderingServerHandler : : prepare ( RID p_mesh , int p_surface ) {
2017-11-21 00:36:32 +00:00
clear ( ) ;
ERR_FAIL_COND ( ! p_mesh . is_valid ( ) ) ;
mesh = p_mesh ;
surface = p_surface ;
2021-03-12 03:33:46 +00:00
RS : : SurfaceData surface_data = RS : : get_singleton ( ) - > mesh_get_surface ( mesh , surface ) ;
2020-03-27 18:21:27 +00:00
uint32_t surface_offsets [ RS : : ARRAY_MAX ] ;
2021-03-12 03:33:46 +00:00
uint32_t vertex_stride ;
uint32_t attrib_stride ;
uint32_t skin_stride ;
RS : : get_singleton ( ) - > mesh_surface_make_offsets_from_format ( surface_data . format , surface_data . vertex_count , surface_data . index_count , surface_offsets , vertex_stride , attrib_stride , skin_stride ) ;
2020-03-27 18:21:27 +00:00
2021-03-12 03:33:46 +00:00
buffer = surface_data . vertex_data ;
stride = vertex_stride ;
2020-03-27 18:21:27 +00:00
offset_vertices = surface_offsets [ RS : : ARRAY_VERTEX ] ;
offset_normal = surface_offsets [ RS : : ARRAY_NORMAL ] ;
2017-11-21 00:36:32 +00:00
}
2020-03-27 18:21:27 +00:00
void SoftBodyRenderingServerHandler : : clear ( ) {
2021-03-12 03:33:46 +00:00
buffer . resize ( 0 ) ;
stride = 0 ;
offset_vertices = 0 ;
offset_normal = 0 ;
2017-11-21 00:36:32 +00:00
2021-03-12 03:33:46 +00:00
surface = 0 ;
2017-11-21 00:36:32 +00:00
mesh = RID ( ) ;
}
2020-03-27 18:21:27 +00:00
void SoftBodyRenderingServerHandler : : open ( ) {
2020-02-17 21:06:54 +00:00
write_buffer = buffer . ptrw ( ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-27 18:21:27 +00:00
void SoftBodyRenderingServerHandler : : close ( ) {
2021-03-12 03:33:46 +00:00
write_buffer = nullptr ;
2017-11-21 00:36:32 +00:00
}
2020-03-27 18:21:27 +00:00
void SoftBodyRenderingServerHandler : : commit_changes ( ) {
2021-06-30 01:55:11 +00:00
RS : : get_singleton ( ) - > mesh_surface_update_vertex_region ( mesh , surface , 0 , buffer ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-27 18:21:27 +00:00
void SoftBodyRenderingServerHandler : : set_vertex ( int p_vertex_id , const void * p_vector3 ) {
2021-04-27 14:19:21 +00:00
memcpy ( & write_buffer [ p_vertex_id * stride + offset_vertices ] , p_vector3 , sizeof ( float ) * 3 ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-27 18:21:27 +00:00
void SoftBodyRenderingServerHandler : : set_normal ( int p_vertex_id , const void * p_vector3 ) {
2021-04-27 14:19:21 +00:00
memcpy ( & write_buffer [ p_vertex_id * stride + offset_normal ] , p_vector3 , sizeof ( float ) * 3 ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-27 18:21:27 +00:00
void SoftBodyRenderingServerHandler : : set_aabb ( const AABB & p_aabb ) {
RS : : get_singleton ( ) - > mesh_set_custom_aabb ( mesh , p_aabb ) ;
2017-11-21 00:36:32 +00:00
}
2020-05-12 15:01:17 +00:00
SoftBody3D : : PinnedPoint : : PinnedPoint ( ) {
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
SoftBody3D : : PinnedPoint : : PinnedPoint ( const PinnedPoint & obj_tocopy ) {
2017-11-21 00:36:32 +00:00
point_index = obj_tocopy . point_index ;
spatial_attachment_path = obj_tocopy . spatial_attachment_path ;
spatial_attachment = obj_tocopy . spatial_attachment ;
2018-07-27 17:10:16 +00:00
offset = obj_tocopy . offset ;
2017-11-21 00:36:32 +00:00
}
2020-09-04 08:18:19 +00:00
SoftBody3D : : PinnedPoint & SoftBody3D : : PinnedPoint : : operator = ( const PinnedPoint & obj ) {
2019-03-02 12:32:29 +00:00
point_index = obj . point_index ;
spatial_attachment_path = obj . spatial_attachment_path ;
spatial_attachment = obj . spatial_attachment ;
offset = obj . offset ;
return * this ;
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : _update_pickable ( ) {
2020-05-14 14:41:43 +00:00
if ( ! is_inside_tree ( ) ) {
2017-11-21 00:36:32 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2019-10-31 15:29:41 +00:00
bool pickable = ray_pickable & & is_visible_in_tree ( ) ;
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_ray_pickable ( physics_rid , pickable ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
bool SoftBody3D : : _set ( const StringName & p_name , const Variant & p_value ) {
2017-11-21 00:36:32 +00:00
String name = p_name ;
String which = name . get_slicec ( ' / ' , 0 ) ;
if ( " pinned_points " = = which ) {
return _set_property_pinned_points_indices ( p_value ) ;
} else if ( " attachments " = = which ) {
int idx = name . get_slicec ( ' / ' , 1 ) . to_int ( ) ;
String what = name . get_slicec ( ' / ' , 2 ) ;
return _set_property_pinned_points_attachment ( idx , what , p_value ) ;
}
return false ;
}
2020-03-26 21:49:16 +00:00
bool SoftBody3D : : _get ( const StringName & p_name , Variant & r_ret ) const {
2017-11-21 00:36:32 +00:00
String name = p_name ;
String which = name . get_slicec ( ' / ' , 0 ) ;
if ( " pinned_points " = = which ) {
Array arr_ret ;
2018-08-03 15:44:09 +00:00
const int pinned_points_indices_size = pinned_points . size ( ) ;
2020-02-17 21:06:54 +00:00
const PinnedPoint * r = pinned_points . ptr ( ) ;
2017-11-21 00:36:32 +00:00
arr_ret . resize ( pinned_points_indices_size ) ;
for ( int i = 0 ; i < pinned_points_indices_size ; + + i ) {
arr_ret [ i ] = r [ i ] . point_index ;
}
r_ret = arr_ret ;
return true ;
} else if ( " attachments " = = which ) {
int idx = name . get_slicec ( ' / ' , 1 ) . to_int ( ) ;
String what = name . get_slicec ( ' / ' , 2 ) ;
return _get_property_pinned_points ( idx , what , r_ret ) ;
}
return false ;
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : _get_property_list ( List < PropertyInfo > * p_list ) const {
2018-08-03 15:44:09 +00:00
const int pinned_points_indices_size = pinned_points . size ( ) ;
2017-11-21 00:36:32 +00:00
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
p_list - > push_back ( PropertyInfo ( Variant : : PACKED_INT32_ARRAY , " pinned_points " ) ) ;
2017-11-21 00:36:32 +00:00
for ( int i = 0 ; i < pinned_points_indices_size ; + + i ) {
p_list - > push_back ( PropertyInfo ( Variant : : INT , " attachments/ " + itos ( i ) + " /point_index " ) ) ;
p_list - > push_back ( PropertyInfo ( Variant : : NODE_PATH , " attachments/ " + itos ( i ) + " /spatial_attachment_path " ) ) ;
2018-07-27 17:10:16 +00:00
p_list - > push_back ( PropertyInfo ( Variant : : VECTOR3 , " attachments/ " + itos ( i ) + " /offset " ) ) ;
2017-11-21 00:36:32 +00:00
}
}
2020-03-26 21:49:16 +00:00
bool SoftBody3D : : _set_property_pinned_points_indices ( const Array & p_indices ) {
2017-11-21 00:36:32 +00:00
const int p_indices_size = p_indices . size ( ) ;
{ // Remove the pined points on physics server that will be removed by resize
2020-02-17 21:06:54 +00:00
const PinnedPoint * r = pinned_points . ptr ( ) ;
2018-08-03 15:44:09 +00:00
if ( p_indices_size < pinned_points . size ( ) ) {
for ( int i = pinned_points . size ( ) - 1 ; i > = p_indices_size ; - - i ) {
2017-11-21 00:36:32 +00:00
pin_point ( r [ i ] . point_index , false ) ;
}
}
}
2018-08-03 15:44:09 +00:00
pinned_points . resize ( p_indices_size ) ;
2017-11-21 00:36:32 +00:00
2020-02-17 21:06:54 +00:00
PinnedPoint * w = pinned_points . ptrw ( ) ;
2017-11-21 00:36:32 +00:00
int point_index ;
for ( int i = 0 ; i < p_indices_size ; + + i ) {
point_index = p_indices . get ( i ) ;
if ( w [ i ] . point_index ! = point_index ) {
2020-05-14 14:41:43 +00:00
if ( - 1 ! = w [ i ] . point_index ) {
2017-11-21 00:36:32 +00:00
pin_point ( w [ i ] . point_index , false ) ;
2020-05-14 14:41:43 +00:00
}
2017-11-21 00:36:32 +00:00
w [ i ] . point_index = point_index ;
pin_point ( w [ i ] . point_index , true ) ;
}
}
return true ;
}
2020-03-26 21:49:16 +00:00
bool SoftBody3D : : _set_property_pinned_points_attachment ( int p_item , const String & p_what , const Variant & p_value ) {
2018-08-03 15:44:09 +00:00
if ( pinned_points . size ( ) < = p_item ) {
2017-11-21 00:36:32 +00:00
return false ;
}
if ( " spatial_attachment_path " = = p_what ) {
2020-02-17 21:06:54 +00:00
PinnedPoint * w = pinned_points . ptrw ( ) ;
2017-11-21 00:36:32 +00:00
pin_point ( w [ p_item ] . point_index , true , p_value ) ;
2018-07-27 17:10:16 +00:00
_make_cache_dirty ( ) ;
} else if ( " offset " = = p_what ) {
2020-02-17 21:06:54 +00:00
PinnedPoint * w = pinned_points . ptrw ( ) ;
2018-07-27 17:10:16 +00:00
w [ p_item ] . offset = p_value ;
2017-11-21 00:36:32 +00:00
} else {
return false ;
}
return true ;
}
2020-03-26 21:49:16 +00:00
bool SoftBody3D : : _get_property_pinned_points ( int p_item , const String & p_what , Variant & r_ret ) const {
2018-08-03 15:44:09 +00:00
if ( pinned_points . size ( ) < = p_item ) {
2017-11-21 00:36:32 +00:00
return false ;
}
2020-02-17 21:06:54 +00:00
const PinnedPoint * r = pinned_points . ptr ( ) ;
2017-11-21 00:36:32 +00:00
if ( " point_index " = = p_what ) {
r_ret = r [ p_item ] . point_index ;
} else if ( " spatial_attachment_path " = = p_what ) {
r_ret = r [ p_item ] . spatial_attachment_path ;
2018-07-27 17:10:16 +00:00
} else if ( " offset " = = p_what ) {
r_ret = r [ p_item ] . offset ;
2017-11-21 00:36:32 +00:00
} else {
return false ;
}
return true ;
}
2021-02-10 20:18:45 +00:00
void SoftBody3D : : _softbody_changed ( ) {
2018-08-30 15:45:27 +00:00
prepare_physics_server ( ) ;
2018-08-03 15:44:09 +00:00
_reset_points_offsets ( ) ;
2017-11-21 00:36:32 +00:00
# ifdef TOOLS_ENABLED
2020-10-29 10:01:28 +00:00
update_configuration_warnings ( ) ;
2017-11-21 00:36:32 +00:00
# endif
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : _notification ( int p_what ) {
2017-11-21 00:36:32 +00:00
switch ( p_what ) {
case NOTIFICATION_ENTER_WORLD : {
2018-08-03 15:44:09 +00:00
if ( Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
2021-02-10 20:18:45 +00:00
// I have no idea what this is supposed to do, it's really weird
// leaving for upcoming PK work on physics
//add_change_receptor(this);
2018-08-03 15:44:09 +00:00
}
2017-11-21 00:36:32 +00:00
2020-04-18 09:00:51 +00:00
RID space = get_world_3d ( ) - > get_space ( ) ;
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_space ( physics_rid , space ) ;
2018-08-30 15:45:27 +00:00
prepare_physics_server ( ) ;
2017-11-21 00:36:32 +00:00
} break ;
2021-06-18 01:09:40 +00:00
2017-11-21 00:36:32 +00:00
case NOTIFICATION_READY : {
2020-05-14 14:41:43 +00:00
if ( ! parent_collision_ignore . is_empty ( ) ) {
2017-11-21 00:36:32 +00:00
add_collision_exception_with ( get_node ( parent_collision_ignore ) ) ;
2020-05-14 14:41:43 +00:00
}
2017-11-21 00:36:32 +00:00
} break ;
2021-06-18 01:09:40 +00:00
2017-11-21 00:36:32 +00:00
case NOTIFICATION_TRANSFORM_CHANGED : {
2018-08-03 15:44:09 +00:00
if ( Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
_reset_points_offsets ( ) ;
2018-07-27 17:10:16 +00:00
return ;
2018-08-03 15:44:09 +00:00
}
2018-07-27 17:10:16 +00:00
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_transform ( physics_rid , get_global_transform ( ) ) ;
2018-07-27 17:10:16 +00:00
set_notify_transform ( false ) ;
2020-03-27 18:21:27 +00:00
// Required to be top level with Transform at center of world in order to modify RenderingServer only to support custom Transform
2020-10-01 07:17:33 +00:00
set_as_top_level ( true ) ;
2020-10-17 05:08:21 +00:00
set_transform ( Transform3D ( ) ) ;
2018-07-27 17:10:16 +00:00
set_notify_transform ( true ) ;
} break ;
2021-06-18 01:09:40 +00:00
2017-11-21 00:36:32 +00:00
case NOTIFICATION_VISIBILITY_CHANGED : {
_update_pickable ( ) ;
} break ;
2021-06-18 01:09:40 +00:00
2017-11-21 00:36:32 +00:00
case NOTIFICATION_EXIT_WORLD : {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_space ( physics_rid , RID ( ) ) ;
2017-11-21 00:36:32 +00:00
} break ;
2021-06-18 01:09:40 +00:00
case NOTIFICATION_DISABLED : {
if ( is_inside_tree ( ) & & ( disable_mode = = DISABLE_MODE_REMOVE ) ) {
prepare_physics_server ( ) ;
}
} break ;
2017-11-21 00:36:32 +00:00
2021-06-18 01:09:40 +00:00
case NOTIFICATION_ENABLED : {
if ( is_inside_tree ( ) & & ( disable_mode = = DISABLE_MODE_REMOVE ) ) {
prepare_physics_server ( ) ;
}
} break ;
2017-11-21 00:36:32 +00:00
2021-06-18 01:09:40 +00:00
# ifdef TOOLS_ENABLED
case NOTIFICATION_LOCAL_TRANSFORM_CHANGED : {
if ( Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
update_configuration_warnings ( ) ;
}
} break ;
2017-11-21 00:36:32 +00:00
# endif
2021-06-18 01:09:40 +00:00
}
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : _bind_methods ( ) {
2021-03-12 03:33:46 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_physics_rid " ) , & SoftBody3D : : get_physics_rid ) ;
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_collision_mask " , " collision_mask " ) , & SoftBody3D : : set_collision_mask ) ;
ClassDB : : bind_method ( D_METHOD ( " get_collision_mask " ) , & SoftBody3D : : get_collision_mask ) ;
2017-11-21 00:36:32 +00:00
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_collision_layer " , " collision_layer " ) , & SoftBody3D : : set_collision_layer ) ;
ClassDB : : bind_method ( D_METHOD ( " get_collision_layer " ) , & SoftBody3D : : get_collision_layer ) ;
2017-11-21 00:36:32 +00:00
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_collision_mask_bit " , " bit " , " value " ) , & SoftBody3D : : set_collision_mask_bit ) ;
ClassDB : : bind_method ( D_METHOD ( " get_collision_mask_bit " , " bit " ) , & SoftBody3D : : get_collision_mask_bit ) ;
2017-11-21 00:36:32 +00:00
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_collision_layer_bit " , " bit " , " value " ) , & SoftBody3D : : set_collision_layer_bit ) ;
ClassDB : : bind_method ( D_METHOD ( " get_collision_layer_bit " , " bit " ) , & SoftBody3D : : get_collision_layer_bit ) ;
2017-11-21 00:36:32 +00:00
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_parent_collision_ignore " , " parent_collision_ignore " ) , & SoftBody3D : : set_parent_collision_ignore ) ;
ClassDB : : bind_method ( D_METHOD ( " get_parent_collision_ignore " ) , & SoftBody3D : : get_parent_collision_ignore ) ;
2017-11-21 00:36:32 +00:00
2021-06-18 01:09:40 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_disable_mode " , " mode " ) , & SoftBody3D : : set_disable_mode ) ;
ClassDB : : bind_method ( D_METHOD ( " get_disable_mode " ) , & SoftBody3D : : get_disable_mode ) ;
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_collision_exceptions " ) , & SoftBody3D : : get_collision_exceptions ) ;
ClassDB : : bind_method ( D_METHOD ( " add_collision_exception_with " , " body " ) , & SoftBody3D : : add_collision_exception_with ) ;
ClassDB : : bind_method ( D_METHOD ( " remove_collision_exception_with " , " body " ) , & SoftBody3D : : remove_collision_exception_with ) ;
2017-11-21 00:36:32 +00:00
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_simulation_precision " , " simulation_precision " ) , & SoftBody3D : : set_simulation_precision ) ;
ClassDB : : bind_method ( D_METHOD ( " get_simulation_precision " ) , & SoftBody3D : : get_simulation_precision ) ;
2017-11-21 00:36:32 +00:00
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_total_mass " , " mass " ) , & SoftBody3D : : set_total_mass ) ;
ClassDB : : bind_method ( D_METHOD ( " get_total_mass " ) , & SoftBody3D : : get_total_mass ) ;
2017-11-21 00:36:32 +00:00
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_linear_stiffness " , " linear_stiffness " ) , & SoftBody3D : : set_linear_stiffness ) ;
ClassDB : : bind_method ( D_METHOD ( " get_linear_stiffness " ) , & SoftBody3D : : get_linear_stiffness ) ;
2017-11-21 00:36:32 +00:00
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_pressure_coefficient " , " pressure_coefficient " ) , & SoftBody3D : : set_pressure_coefficient ) ;
ClassDB : : bind_method ( D_METHOD ( " get_pressure_coefficient " ) , & SoftBody3D : : get_pressure_coefficient ) ;
2017-11-21 00:36:32 +00:00
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_damping_coefficient " , " damping_coefficient " ) , & SoftBody3D : : set_damping_coefficient ) ;
ClassDB : : bind_method ( D_METHOD ( " get_damping_coefficient " ) , & SoftBody3D : : get_damping_coefficient ) ;
2017-11-21 00:36:32 +00:00
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_drag_coefficient " , " drag_coefficient " ) , & SoftBody3D : : set_drag_coefficient ) ;
ClassDB : : bind_method ( D_METHOD ( " get_drag_coefficient " ) , & SoftBody3D : : get_drag_coefficient ) ;
2017-11-21 00:36:32 +00:00
2020-03-26 21:49:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_ray_pickable " , " ray_pickable " ) , & SoftBody3D : : set_ray_pickable ) ;
ClassDB : : bind_method ( D_METHOD ( " is_ray_pickable " ) , & SoftBody3D : : is_ray_pickable ) ;
2017-11-21 00:36:32 +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 " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : NODE_PATH , " parent_collision_ignore " , PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE , " Parent collision object " ) , " set_parent_collision_ignore " , " get_parent_collision_ignore " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " simulation_precision " , PROPERTY_HINT_RANGE , " 1,100,1 " ) , " set_simulation_precision " , " get_simulation_precision " ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " total_mass " , PROPERTY_HINT_RANGE , " 0.01,10000,1 " ) , " set_total_mass " , " get_total_mass " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " linear_stiffness " , PROPERTY_HINT_RANGE , " 0,1,0.01 " ) , " set_linear_stiffness " , " get_linear_stiffness " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " pressure_coefficient " ) , " set_pressure_coefficient " , " get_pressure_coefficient " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " damping_coefficient " , PROPERTY_HINT_RANGE , " 0,1,0.01 " ) , " set_damping_coefficient " , " get_damping_coefficient " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " drag_coefficient " , PROPERTY_HINT_RANGE , " 0,1,0.01 " ) , " set_drag_coefficient " , " get_drag_coefficient " ) ;
2019-10-31 15:29:41 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " ray_pickable " ) , " set_ray_pickable " , " is_ray_pickable " ) ;
2021-06-18 01:09:40 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " disable_mode " , PROPERTY_HINT_ENUM , " Remove,KeepActive " ) , " set_disable_mode " , " get_disable_mode " ) ;
BIND_ENUM_CONSTANT ( DISABLE_MODE_REMOVE ) ;
BIND_ENUM_CONSTANT ( DISABLE_MODE_KEEP_ACTIVE ) ;
2017-11-21 00:36:32 +00:00
}
2020-10-29 10:01:28 +00:00
TypedArray < String > SoftBody3D : : get_configuration_warnings ( ) const {
TypedArray < String > warnings = Node : : get_configuration_warnings ( ) ;
2017-11-21 00:36:32 +00:00
if ( get_mesh ( ) . is_null ( ) ) {
2020-10-29 10:01:28 +00:00
warnings . push_back ( TTR ( " This body will be ignored until you set a mesh. " ) ) ;
2017-11-21 00:36:32 +00:00
}
2020-10-17 05:08:21 +00:00
Transform3D t = get_transform ( ) ;
Fix many errors found by PVS-Studio
Fix errors 2, 3, 4, 6, 8, 9, 11, 12, 13, 14, and 15.
2018-11-28 02:58:00 +00:00
if ( ( ABS ( t . basis . get_axis ( 0 ) . length ( ) - 1.0 ) > 0.05 | | ABS ( t . basis . get_axis ( 1 ) . length ( ) - 1.0 ) > 0.05 | | ABS ( t . basis . get_axis ( 2 ) . length ( ) - 1.0 ) > 0.05 ) ) {
2020-10-29 10:01:28 +00:00
warnings . push_back ( TTR ( " Size changes to SoftBody3D will be overridden by the physics engine when running. \n Change the size in children collision shapes instead. " ) ) ;
2017-11-21 00:36:32 +00:00
}
2020-10-29 10:01:28 +00:00
return warnings ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : _update_physics_server ( ) {
2020-05-14 14:41:43 +00:00
if ( ! simulation_started ) {
2018-08-30 15:45:27 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2018-08-30 15:45:27 +00:00
_update_cache_pin_points_datas ( ) ;
// Submit bone attachment
const int pinned_points_indices_size = pinned_points . size ( ) ;
2020-02-17 21:06:54 +00:00
const PinnedPoint * r = pinned_points . ptr ( ) ;
2018-08-30 15:45:27 +00:00
for ( int i = 0 ; i < pinned_points_indices_size ; + + i ) {
if ( r [ i ] . spatial_attachment ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_move_point ( physics_rid , r [ i ] . point_index , r [ i ] . spatial_attachment - > get_global_transform ( ) . xform ( r [ i ] . offset ) ) ;
2018-08-30 15:45:27 +00:00
}
}
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : _draw_soft_mesh ( ) {
2020-05-14 14:41:43 +00:00
if ( get_mesh ( ) . is_null ( ) ) {
2017-11-21 00:36:32 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2017-11-21 00:36:32 +00:00
2020-03-27 18:21:27 +00:00
if ( ! rendering_server_handler . is_ready ( ) ) {
rendering_server_handler . prepare ( get_mesh ( ) - > get_rid ( ) , 0 ) ;
2017-11-21 00:36:32 +00:00
/// Necessary in order to render the mesh correctly (Soft body nodes are in global space)
simulation_started = true ;
2021-07-17 21:22:52 +00:00
call_deferred ( SNAME ( " set_as_top_level " ) , true ) ;
call_deferred ( SNAME ( " set_transform " ) , Transform3D ( ) ) ;
2017-11-21 00:36:32 +00:00
}
2018-08-30 15:45:27 +00:00
_update_physics_server ( ) ;
2020-03-27 18:21:27 +00:00
rendering_server_handler . open ( ) ;
PhysicsServer3D : : get_singleton ( ) - > soft_body_update_rendering_server ( physics_rid , & rendering_server_handler ) ;
rendering_server_handler . close ( ) ;
2017-11-21 00:36:32 +00:00
2020-03-27 18:21:27 +00:00
rendering_server_handler . commit_changes ( ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : prepare_physics_server ( ) {
2021-06-18 01:09:40 +00:00
# ifdef TOOLS_ENABLED
2018-07-27 17:10:16 +00:00
if ( Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
2020-05-14 14:41:43 +00:00
if ( get_mesh ( ) . is_valid ( ) ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_mesh ( physics_rid , get_mesh ( ) ) ;
2020-05-14 14:41:43 +00:00
} else {
2020-04-01 23:20:12 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_mesh ( physics_rid , nullptr ) ;
2020-05-14 14:41:43 +00:00
}
2018-07-27 17:10:16 +00:00
2017-11-21 00:36:32 +00:00
return ;
2018-07-27 17:10:16 +00:00
}
2021-06-18 01:09:40 +00:00
# endif
2017-11-21 00:36:32 +00:00
2021-06-18 01:09:40 +00:00
if ( get_mesh ( ) . is_valid ( ) & & ( is_enabled ( ) | | ( disable_mode ! = DISABLE_MODE_REMOVE ) ) ) {
2017-11-21 00:36:32 +00:00
become_mesh_owner ( ) ;
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_mesh ( physics_rid , get_mesh ( ) ) ;
RS : : get_singleton ( ) - > connect ( " frame_pre_draw " , callable_mp ( this , & SoftBody3D : : _draw_soft_mesh ) ) ;
2017-11-21 00:36:32 +00:00
} else {
2020-04-01 23:20:12 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_mesh ( physics_rid , nullptr ) ;
2020-03-27 18:21:27 +00:00
if ( RS : : get_singleton ( ) - > is_connected ( " frame_pre_draw " , callable_mp ( this , & SoftBody3D : : _draw_soft_mesh ) ) ) {
RS : : get_singleton ( ) - > disconnect ( " frame_pre_draw " , callable_mp ( this , & SoftBody3D : : _draw_soft_mesh ) ) ;
2019-10-28 23:40:36 +00:00
}
2017-11-21 00:36:32 +00:00
}
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : become_mesh_owner ( ) {
2020-05-14 14:41:43 +00:00
if ( mesh . is_null ( ) ) {
2017-11-21 00:36:32 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2017-11-21 00:36:32 +00:00
if ( ! mesh_owner ) {
mesh_owner = true ;
2020-03-17 06:33:00 +00:00
Vector < Ref < Material > > copy_materials ;
2021-04-14 03:45:16 +00:00
copy_materials . append_array ( surface_override_materials ) ;
2018-07-28 18:32:28 +00:00
2017-11-21 00:36:32 +00:00
ERR_FAIL_COND ( ! mesh - > get_surface_count ( ) ) ;
// Get current mesh array and create new mesh array with necessary flag for softbody
Array surface_arrays = mesh - > surface_get_arrays ( 0 ) ;
Array surface_blend_arrays = mesh - > surface_get_blend_shape_arrays ( 0 ) ;
2019-08-18 22:40:52 +00:00
Dictionary surface_lods = mesh - > surface_get_lods ( 0 ) ;
2017-11-21 00:36:32 +00:00
uint32_t surface_format = mesh - > surface_get_format ( 0 ) ;
surface_format | = Mesh : : ARRAY_FLAG_USE_DYNAMIC_UPDATE ;
Ref < ArrayMesh > soft_mesh ;
2021-06-17 22:03:09 +00:00
soft_mesh . instantiate ( ) ;
2019-08-18 22:40:52 +00:00
soft_mesh - > add_surface_from_arrays ( Mesh : : PRIMITIVE_TRIANGLES , surface_arrays , surface_blend_arrays , surface_lods , surface_format ) ;
2018-07-28 18:32:28 +00:00
soft_mesh - > surface_set_material ( 0 , mesh - > surface_get_material ( 0 ) ) ;
2017-11-21 00:36:32 +00:00
set_mesh ( soft_mesh ) ;
for ( int i = copy_materials . size ( ) - 1 ; 0 < = i ; - - i ) {
2021-04-14 03:45:16 +00:00
set_surface_override_material ( i , copy_materials [ i ] ) ;
2017-11-21 00:36:32 +00:00
}
}
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_collision_mask ( uint32_t p_mask ) {
2017-11-21 00:36:32 +00:00
collision_mask = p_mask ;
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_collision_mask ( physics_rid , p_mask ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
uint32_t SoftBody3D : : get_collision_mask ( ) const {
2017-11-21 00:36:32 +00:00
return collision_mask ;
}
2020-05-14 12:29:06 +00:00
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_collision_layer ( uint32_t p_layer ) {
2017-11-21 00:36:32 +00:00
collision_layer = p_layer ;
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_collision_layer ( physics_rid , p_layer ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
uint32_t SoftBody3D : : get_collision_layer ( ) const {
2017-11-21 00:36:32 +00:00
return collision_layer ;
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_collision_mask_bit ( int p_bit , bool p_value ) {
2021-04-30 15:19:04 +00:00
ERR_FAIL_INDEX_MSG ( p_bit , 32 , " Collision mask bit must be between 0 and 31 inclusive. " ) ;
2017-11-21 00:36:32 +00:00
uint32_t mask = get_collision_mask ( ) ;
2020-05-14 14:41:43 +00:00
if ( p_value ) {
2017-11-21 00:36:32 +00:00
mask | = 1 < < p_bit ;
2020-05-14 14:41:43 +00:00
} else {
2017-11-21 00:36:32 +00:00
mask & = ~ ( 1 < < p_bit ) ;
2020-05-14 14:41:43 +00:00
}
2017-11-21 00:36:32 +00:00
set_collision_mask ( mask ) ;
}
2020-03-26 21:49:16 +00:00
bool SoftBody3D : : get_collision_mask_bit ( int p_bit ) const {
2021-04-30 15:19:04 +00:00
ERR_FAIL_INDEX_V_MSG ( p_bit , 32 , false , " Collision mask bit must be between 0 and 31 inclusive. " ) ;
2017-11-21 00:36:32 +00:00
return get_collision_mask ( ) & ( 1 < < p_bit ) ;
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_collision_layer_bit ( int p_bit , bool p_value ) {
2021-04-30 15:19:04 +00:00
ERR_FAIL_INDEX_MSG ( p_bit , 32 , " Collision layer bit must be between 0 and 31 inclusive. " ) ;
2017-11-21 00:36:32 +00:00
uint32_t layer = get_collision_layer ( ) ;
2020-05-14 14:41:43 +00:00
if ( p_value ) {
2017-11-21 00:36:32 +00:00
layer | = 1 < < p_bit ;
2020-05-14 14:41:43 +00:00
} else {
2017-11-21 00:36:32 +00:00
layer & = ~ ( 1 < < p_bit ) ;
2020-05-14 14:41:43 +00:00
}
2017-11-21 00:36:32 +00:00
set_collision_layer ( layer ) ;
}
2020-03-26 21:49:16 +00:00
bool SoftBody3D : : get_collision_layer_bit ( int p_bit ) const {
2021-04-30 15:19:04 +00:00
ERR_FAIL_INDEX_V_MSG ( p_bit , 32 , false , " Collision layer bit must be between 0 and 31 inclusive. " ) ;
2017-11-21 00:36:32 +00:00
return get_collision_layer ( ) & ( 1 < < p_bit ) ;
}
2021-06-18 01:09:40 +00:00
void SoftBody3D : : set_disable_mode ( DisableMode p_mode ) {
if ( disable_mode = = p_mode ) {
return ;
}
bool inside_tree = is_inside_tree ( ) ;
if ( inside_tree & & ( disable_mode = = DISABLE_MODE_REMOVE ) ) {
prepare_physics_server ( ) ;
}
disable_mode = p_mode ;
if ( inside_tree & & ( disable_mode = = DISABLE_MODE_REMOVE ) ) {
prepare_physics_server ( ) ;
}
}
SoftBody3D : : DisableMode SoftBody3D : : get_disable_mode ( ) const {
return disable_mode ;
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_parent_collision_ignore ( const NodePath & p_parent_collision_ignore ) {
2017-11-21 00:36:32 +00:00
parent_collision_ignore = p_parent_collision_ignore ;
}
2020-03-26 21:49:16 +00:00
const NodePath & SoftBody3D : : get_parent_collision_ignore ( ) const {
2017-11-21 00:36:32 +00:00
return parent_collision_ignore ;
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_pinned_points_indices ( Vector < SoftBody3D : : PinnedPoint > p_pinned_points_indices ) {
2018-08-03 15:44:09 +00:00
pinned_points = p_pinned_points_indices ;
for ( int i = pinned_points . size ( ) - 1 ; 0 < = i ; - - i ) {
2017-11-21 00:36:32 +00:00
pin_point ( p_pinned_points_indices [ i ] . point_index , true ) ;
}
}
2020-03-26 21:49:16 +00:00
Vector < SoftBody3D : : PinnedPoint > SoftBody3D : : get_pinned_points_indices ( ) {
2018-08-03 15:44:09 +00:00
return pinned_points ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
Array SoftBody3D : : get_collision_exceptions ( ) {
2018-10-24 19:32:55 +00:00
List < RID > exceptions ;
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_get_collision_exceptions ( physics_rid , & exceptions ) ;
2018-10-24 19:32:55 +00:00
Array ret ;
2021-07-24 13:46:25 +00:00
for ( const RID & body : exceptions ) {
2020-03-27 18:21:27 +00:00
ObjectID instance_id = PhysicsServer3D : : get_singleton ( ) - > body_get_object_instance_id ( body ) ;
2018-10-24 19:32:55 +00:00
Object * obj = ObjectDB : : get_instance ( instance_id ) ;
2020-03-26 21:49:16 +00:00
PhysicsBody3D * physics_body = Object : : cast_to < PhysicsBody3D > ( obj ) ;
2018-10-24 19:32:55 +00:00
ret . append ( physics_body ) ;
}
return ret ;
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : add_collision_exception_with ( Node * p_node ) {
2017-11-21 00:36:32 +00:00
ERR_FAIL_NULL ( p_node ) ;
2020-03-26 21:49:16 +00:00
CollisionObject3D * collision_object = Object : : cast_to < CollisionObject3D > ( p_node ) ;
2020-03-30 16:22:57 +00:00
ERR_FAIL_COND_MSG ( ! collision_object , " Collision exception only works between two CollisionObject3Ds. " ) ;
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_add_collision_exception ( physics_rid , collision_object - > get_rid ( ) ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : remove_collision_exception_with ( Node * p_node ) {
2017-11-21 00:36:32 +00:00
ERR_FAIL_NULL ( p_node ) ;
2020-03-26 21:49:16 +00:00
CollisionObject3D * collision_object = Object : : cast_to < CollisionObject3D > ( p_node ) ;
2020-03-30 16:22:57 +00:00
ERR_FAIL_COND_MSG ( ! collision_object , " Collision exception only works between two CollisionObject3Ds. " ) ;
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_remove_collision_exception ( physics_rid , collision_object - > get_rid ( ) ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
int SoftBody3D : : get_simulation_precision ( ) {
2020-03-27 18:21:27 +00:00
return PhysicsServer3D : : get_singleton ( ) - > soft_body_get_simulation_precision ( physics_rid ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_simulation_precision ( int p_simulation_precision ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_simulation_precision ( physics_rid , p_simulation_precision ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
real_t SoftBody3D : : get_total_mass ( ) {
2020-03-27 18:21:27 +00:00
return PhysicsServer3D : : get_singleton ( ) - > soft_body_get_total_mass ( physics_rid ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_total_mass ( real_t p_total_mass ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_total_mass ( physics_rid , p_total_mass ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_linear_stiffness ( real_t p_linear_stiffness ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_linear_stiffness ( physics_rid , p_linear_stiffness ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
real_t SoftBody3D : : get_linear_stiffness ( ) {
2020-03-27 18:21:27 +00:00
return PhysicsServer3D : : get_singleton ( ) - > soft_body_get_linear_stiffness ( physics_rid ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
real_t SoftBody3D : : get_pressure_coefficient ( ) {
2020-03-27 18:21:27 +00:00
return PhysicsServer3D : : get_singleton ( ) - > soft_body_get_pressure_coefficient ( physics_rid ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_pressure_coefficient ( real_t p_pressure_coefficient ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_pressure_coefficient ( physics_rid , p_pressure_coefficient ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
real_t SoftBody3D : : get_damping_coefficient ( ) {
2020-03-27 18:21:27 +00:00
return PhysicsServer3D : : get_singleton ( ) - > soft_body_get_damping_coefficient ( physics_rid ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_damping_coefficient ( real_t p_damping_coefficient ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_damping_coefficient ( physics_rid , p_damping_coefficient ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
real_t SoftBody3D : : get_drag_coefficient ( ) {
2020-03-27 18:21:27 +00:00
return PhysicsServer3D : : get_singleton ( ) - > soft_body_get_drag_coefficient ( physics_rid ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_drag_coefficient ( real_t p_drag_coefficient ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_set_drag_coefficient ( physics_rid , p_drag_coefficient ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
Vector3 SoftBody3D : : get_point_transform ( int p_point_index ) {
2020-03-27 18:21:27 +00:00
return PhysicsServer3D : : get_singleton ( ) - > soft_body_get_point_global_position ( physics_rid , p_point_index ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : pin_point_toggle ( int p_point_index ) {
2017-11-21 00:36:32 +00:00
pin_point ( p_point_index , ! ( - 1 ! = _has_pinned_point ( p_point_index ) ) ) ;
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : pin_point ( int p_point_index , bool pin , const NodePath & p_spatial_attachment_path ) {
2017-11-21 00:36:32 +00:00
_pin_point_on_physics_server ( p_point_index , pin ) ;
if ( pin ) {
_add_pinned_point ( p_point_index , p_spatial_attachment_path ) ;
} else {
_remove_pinned_point ( p_point_index ) ;
}
}
2020-03-26 21:49:16 +00:00
bool SoftBody3D : : is_point_pinned ( int p_point_index ) const {
2017-11-21 00:36:32 +00:00
return - 1 ! = _has_pinned_point ( p_point_index ) ;
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : set_ray_pickable ( bool p_ray_pickable ) {
2017-11-21 00:36:32 +00:00
ray_pickable = p_ray_pickable ;
_update_pickable ( ) ;
}
2020-03-26 21:49:16 +00:00
bool SoftBody3D : : is_ray_pickable ( ) const {
2017-11-21 00:36:32 +00:00
return ray_pickable ;
}
2020-03-26 21:49:16 +00:00
SoftBody3D : : SoftBody3D ( ) :
2020-05-12 15:01:17 +00:00
physics_rid ( PhysicsServer3D : : get_singleton ( ) - > soft_body_create ( ) ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > body_attach_object_instance_id ( physics_rid , get_instance_id ( ) ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
SoftBody3D : : ~ SoftBody3D ( ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > free ( physics_rid ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : reset_softbody_pin ( ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_remove_all_pinned_points ( physics_rid ) ;
2020-02-17 21:06:54 +00:00
const PinnedPoint * pps = pinned_points . ptr ( ) ;
2018-08-03 15:44:09 +00:00
for ( int i = pinned_points . size ( ) - 1 ; 0 < i ; - - i ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_pin_point ( physics_rid , pps [ i ] . point_index , true ) ;
2017-11-21 00:36:32 +00:00
}
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : _make_cache_dirty ( ) {
2018-07-27 17:10:16 +00:00
pinned_points_cache_dirty = true ;
}
2017-11-21 00:36:32 +00:00
2020-03-26 21:49:16 +00:00
void SoftBody3D : : _update_cache_pin_points_datas ( ) {
2020-05-14 14:41:43 +00:00
if ( ! pinned_points_cache_dirty ) {
2018-07-27 17:10:16 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2017-11-21 00:36:32 +00:00
2018-07-27 17:10:16 +00:00
pinned_points_cache_dirty = false ;
2017-11-21 00:36:32 +00:00
2020-02-17 21:06:54 +00:00
PinnedPoint * w = pinned_points . ptrw ( ) ;
2018-08-03 15:44:09 +00:00
for ( int i = pinned_points . size ( ) - 1 ; 0 < = i ; - - i ) {
2018-07-27 17:10:16 +00:00
if ( ! w [ i ] . spatial_attachment_path . is_empty ( ) ) {
2020-03-26 21:49:16 +00:00
w [ i ] . spatial_attachment = Object : : cast_to < Node3D > ( get_node ( w [ i ] . spatial_attachment_path ) ) ;
2018-07-27 17:10:16 +00:00
}
if ( ! w [ i ] . spatial_attachment ) {
2020-11-20 08:52:31 +00:00
ERR_PRINT ( " Node3D node not defined in the pinned point, this is undefined behavior for SoftBody3D! " ) ;
2017-11-21 00:36:32 +00:00
}
}
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : _pin_point_on_physics_server ( int p_point_index , bool pin ) {
2020-03-27 18:21:27 +00:00
PhysicsServer3D : : get_singleton ( ) - > soft_body_pin_point ( physics_rid , p_point_index , pin ) ;
2017-11-21 00:36:32 +00:00
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : _add_pinned_point ( int p_point_index , const NodePath & p_spatial_attachment_path ) {
SoftBody3D : : PinnedPoint * pinned_point ;
2017-11-21 00:36:32 +00:00
if ( - 1 = = _get_pinned_point ( p_point_index , pinned_point ) ) {
// Create new
PinnedPoint pp ;
pp . point_index = p_point_index ;
pp . spatial_attachment_path = p_spatial_attachment_path ;
2018-07-27 17:10:16 +00:00
if ( ! p_spatial_attachment_path . is_empty ( ) & & has_node ( p_spatial_attachment_path ) ) {
2020-03-26 21:49:16 +00:00
pp . spatial_attachment = Object : : cast_to < Node3D > ( get_node ( p_spatial_attachment_path ) ) ;
2020-03-27 18:21:27 +00:00
pp . offset = ( pp . spatial_attachment - > get_global_transform ( ) . affine_inverse ( ) * get_global_transform ( ) ) . xform ( PhysicsServer3D : : get_singleton ( ) - > soft_body_get_point_global_position ( physics_rid , pp . point_index ) ) ;
2018-07-27 17:10:16 +00:00
}
2018-08-03 15:44:09 +00:00
pinned_points . push_back ( pp ) ;
2018-07-27 17:10:16 +00:00
2017-11-21 00:36:32 +00:00
} else {
pinned_point - > point_index = p_point_index ;
pinned_point - > spatial_attachment_path = p_spatial_attachment_path ;
2018-07-27 17:10:16 +00:00
if ( ! p_spatial_attachment_path . is_empty ( ) & & has_node ( p_spatial_attachment_path ) ) {
2020-03-26 21:49:16 +00:00
pinned_point - > spatial_attachment = Object : : cast_to < Node3D > ( get_node ( p_spatial_attachment_path ) ) ;
2020-03-27 18:21:27 +00:00
pinned_point - > offset = ( pinned_point - > spatial_attachment - > get_global_transform ( ) . affine_inverse ( ) * get_global_transform ( ) ) . xform ( PhysicsServer3D : : get_singleton ( ) - > soft_body_get_point_global_position ( physics_rid , pinned_point - > point_index ) ) ;
2018-07-27 17:10:16 +00:00
}
2017-11-21 00:36:32 +00:00
}
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : _reset_points_offsets ( ) {
2020-05-14 14:41:43 +00:00
if ( ! Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
2018-08-03 15:44:09 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2018-08-03 15:44:09 +00:00
2020-02-17 21:06:54 +00:00
const PinnedPoint * r = pinned_points . ptr ( ) ;
PinnedPoint * w = pinned_points . ptrw ( ) ;
2018-08-03 15:44:09 +00:00
for ( int i = pinned_points . size ( ) - 1 ; 0 < = i ; - - i ) {
2020-05-14 14:41:43 +00:00
if ( ! r [ i ] . spatial_attachment ) {
2021-03-18 23:26:42 +00:00
if ( ! r [ i ] . spatial_attachment_path . is_empty ( ) & & has_node ( r [ i ] . spatial_attachment_path ) ) {
w [ i ] . spatial_attachment = Object : : cast_to < Node3D > ( get_node ( r [ i ] . spatial_attachment_path ) ) ;
}
2020-05-14 14:41:43 +00:00
}
2018-08-03 15:44:09 +00:00
2020-05-14 14:41:43 +00:00
if ( ! r [ i ] . spatial_attachment ) {
2018-08-03 15:44:09 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2018-08-03 15:44:09 +00:00
2020-03-27 18:21:27 +00:00
w [ i ] . offset = ( r [ i ] . spatial_attachment - > get_global_transform ( ) . affine_inverse ( ) * get_global_transform ( ) ) . xform ( PhysicsServer3D : : get_singleton ( ) - > soft_body_get_point_global_position ( physics_rid , r [ i ] . point_index ) ) ;
2018-08-03 15:44:09 +00:00
}
}
2020-03-26 21:49:16 +00:00
void SoftBody3D : : _remove_pinned_point ( int p_point_index ) {
2017-11-21 00:36:32 +00:00
const int id ( _has_pinned_point ( p_point_index ) ) ;
if ( - 1 ! = id ) {
2018-08-03 15:44:09 +00:00
pinned_points . remove ( id ) ;
2017-11-21 00:36:32 +00:00
}
}
2020-03-26 21:49:16 +00:00
int SoftBody3D : : _get_pinned_point ( int p_point_index , SoftBody3D : : PinnedPoint * & r_point ) const {
2017-11-21 00:36:32 +00:00
const int id = _has_pinned_point ( p_point_index ) ;
if ( - 1 = = id ) {
2020-04-01 23:20:12 +00:00
r_point = nullptr ;
2017-11-21 00:36:32 +00:00
return - 1 ;
} else {
2020-03-26 21:49:16 +00:00
r_point = const_cast < SoftBody3D : : PinnedPoint * > ( & pinned_points . ptr ( ) [ id ] ) ;
2017-11-21 00:36:32 +00:00
return id ;
}
}
2020-03-26 21:49:16 +00:00
int SoftBody3D : : _has_pinned_point ( int p_point_index ) const {
2020-02-17 21:06:54 +00:00
const PinnedPoint * r = pinned_points . ptr ( ) ;
2018-08-03 15:44:09 +00:00
for ( int i = pinned_points . size ( ) - 1 ; 0 < = i ; - - i ) {
2017-11-21 00:36:32 +00:00
if ( p_point_index = = r [ i ] . point_index ) {
return i ;
}
}
return - 1 ;
}