2015-10-08 20:00:40 +02:00
/**************************************************************************/
2021-10-18 21:24:30 +02:00
/* godot_space_3d.cpp */
2015-10-08 20:00:40 +02:00
/**************************************************************************/
/* 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-05 00:50:27 +01:00
2021-10-18 21:24:30 +02:00
# include "godot_space_3d.h"
# include "godot_collision_solver_3d.h"
# include "godot_physics_server_3d.h"
2017-08-27 21:07:15 +02:00
2020-11-07 23:33:38 +01:00
# include "core/config/project_settings.h"
2015-10-08 20:00:40 +02:00
2021-12-09 18:52:27 +01:00
# define TEST_MOTION_MARGIN_MIN_VALUE 0.0001
2021-09-17 03:38:17 +02:00
# define TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR 0.05
2021-10-18 21:24:30 +02:00
_FORCE_INLINE_ static bool _can_collide_with ( GodotCollisionObject3D * p_object , uint32_t p_collision_mask , bool p_collide_with_bodies , bool p_collide_with_areas ) {
2020-07-27 22:45:01 +02:00
if ( ! ( p_object - > get_collision_layer ( ) & p_collision_mask ) ) {
2018-08-21 20:30:41 +02:00
return false ;
}
2021-10-18 21:24:30 +02:00
if ( p_object - > get_type ( ) = = GodotCollisionObject3D : : TYPE_AREA & & ! p_collide_with_areas ) {
2018-08-21 20:30:41 +02:00
return false ;
2020-05-14 16:41:43 +02:00
}
2018-08-21 20:30:41 +02:00
2021-10-18 21:24:30 +02:00
if ( p_object - > get_type ( ) = = GodotCollisionObject3D : : TYPE_BODY & & ! p_collide_with_bodies ) {
2018-08-21 20:30:41 +02:00
return false ;
2020-05-14 16:41:43 +02:00
}
2018-08-21 20:30:41 +02:00
2021-10-18 21:24:30 +02:00
if ( p_object - > get_type ( ) = = GodotCollisionObject3D : : TYPE_SOFT_BODY & & ! p_collide_with_bodies ) {
2021-03-12 04:33:46 +01:00
return false ;
}
2018-08-21 20:30:41 +02:00
return true ;
2015-10-08 20:00:40 +02:00
}
2021-11-02 02:00:58 +01:00
int GodotPhysicsDirectSpaceState3D : : intersect_point ( const PointParameters & p_parameters , ShapeResult * r_results , int p_result_max ) {
2017-07-15 06:23:10 +02:00
ERR_FAIL_COND_V ( space - > locked , false ) ;
2021-11-02 02:00:58 +01:00
int amount = space - > broadphase - > cull_point ( p_parameters . position , space - > intersection_query_results , GodotSpace3D : : INTERSECTION_QUERY_MAX , space - > intersection_query_subindex_results ) ;
2017-07-15 06:23:10 +02:00
int cc = 0 ;
2020-10-17 07:08:21 +02:00
//Transform3D ai = p_xform.affine_inverse();
2017-07-15 06:23:10 +02:00
for ( int i = 0 ; i < amount ; i + + ) {
2020-05-14 16:41:43 +02:00
if ( cc > = p_result_max ) {
2017-07-15 06:23:10 +02:00
break ;
2020-05-14 16:41:43 +02:00
}
2017-07-15 06:23:10 +02:00
2021-11-02 02:00:58 +01:00
if ( ! _can_collide_with ( space - > intersection_query_results [ i ] , p_parameters . collision_mask , p_parameters . collide_with_bodies , p_parameters . collide_with_areas ) ) {
2017-07-15 06:23:10 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2017-07-15 06:23:10 +02:00
//area can't be picked by ray (default)
2021-11-02 02:00:58 +01:00
if ( p_parameters . exclude . has ( space - > intersection_query_results [ i ] - > get_self ( ) ) ) {
2017-07-15 06:23:10 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2017-07-15 06:23:10 +02:00
2021-10-18 21:24:30 +02:00
const GodotCollisionObject3D * col_obj = space - > intersection_query_results [ i ] ;
2017-07-15 06:23:10 +02:00
int shape_idx = space - > intersection_query_subindex_results [ i ] ;
2020-10-17 07:08:21 +02:00
Transform3D inv_xform = col_obj - > get_transform ( ) * col_obj - > get_shape_transform ( shape_idx ) ;
2017-07-15 06:23:10 +02:00
inv_xform . affine_invert ( ) ;
2021-11-02 02:00:58 +01:00
if ( ! col_obj - > get_shape ( shape_idx ) - > intersect_point ( inv_xform . xform ( p_parameters . position ) ) ) {
2017-07-15 06:23:10 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2017-07-15 06:23:10 +02:00
r_results [ cc ] . collider_id = col_obj - > get_instance_id ( ) ;
2020-05-14 16:41:43 +02:00
if ( r_results [ cc ] . collider_id . is_valid ( ) ) {
2017-07-15 06:23:10 +02:00
r_results [ cc ] . collider = ObjectDB : : get_instance ( r_results [ cc ] . collider_id ) ;
2020-05-14 16:41:43 +02:00
} else {
2020-04-02 01:20:12 +02:00
r_results [ cc ] . collider = nullptr ;
2020-05-14 16:41:43 +02:00
}
2017-07-15 06:23:10 +02:00
r_results [ cc ] . rid = col_obj - > get_self ( ) ;
r_results [ cc ] . shape = shape_idx ;
cc + + ;
}
return cc ;
}
2021-11-02 02:00:58 +01:00
bool GodotPhysicsDirectSpaceState3D : : intersect_ray ( const RayParameters & p_parameters , RayResult & r_result ) {
2016-01-10 19:54:57 +01:00
ERR_FAIL_COND_V ( space - > locked , false ) ;
2015-10-08 20:00:40 +02:00
Vector3 begin , end ;
Vector3 normal ;
2021-11-02 02:00:58 +01:00
begin = p_parameters . from ;
end = p_parameters . to ;
2015-10-08 20:00:40 +02:00
normal = ( end - begin ) . normalized ( ) ;
2021-10-18 21:24:30 +02:00
int amount = space - > broadphase - > cull_segment ( begin , end , space - > intersection_query_results , GodotSpace3D : : INTERSECTION_QUERY_MAX , space - > intersection_query_subindex_results ) ;
2015-10-08 20:00:40 +02:00
2018-09-13 03:38:39 +02:00
//todo, create another array that references results, compute AABBs and check closest point to ray origin, sort, and stop evaluating results when beyond first collision
2015-10-08 20:00:40 +02:00
bool collided = false ;
Vector3 res_point , res_normal ;
2023-03-05 22:59:22 +01:00
int res_face_index = - 1 ;
2022-09-28 17:05:34 +02:00
int res_shape = - 1 ;
const GodotCollisionObject3D * res_obj = nullptr ;
2015-10-08 20:00:40 +02:00
real_t min_d = 1e10 ;
for ( int i = 0 ; i < amount ; i + + ) {
2021-11-02 02:00:58 +01:00
if ( ! _can_collide_with ( space - > intersection_query_results [ i ] , p_parameters . collision_mask , p_parameters . collide_with_bodies , p_parameters . collide_with_areas ) ) {
2015-10-08 20:00:40 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
if ( p_parameters . pick_ray & & ! ( space - > intersection_query_results [ i ] - > is_ray_pickable ( ) ) ) {
2015-10-08 20:00:40 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
if ( p_parameters . exclude . has ( space - > intersection_query_results [ i ] - > get_self ( ) ) ) {
2015-10-08 20:00:40 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-10-18 21:24:30 +02:00
const GodotCollisionObject3D * col_obj = space - > intersection_query_results [ i ] ;
2015-10-08 20:00:40 +02:00
int shape_idx = space - > intersection_query_subindex_results [ i ] ;
2020-10-17 07:08:21 +02:00
Transform3D inv_xform = col_obj - > get_shape_inv_transform ( shape_idx ) * col_obj - > get_inv_transform ( ) ;
2015-10-08 20:00:40 +02:00
Vector3 local_from = inv_xform . xform ( begin ) ;
Vector3 local_to = inv_xform . xform ( end ) ;
2021-10-18 21:24:30 +02:00
const GodotShape3D * shape = col_obj - > get_shape ( shape_idx ) ;
2015-10-08 20:00:40 +02:00
Vector3 shape_point , shape_normal ;
2023-03-05 22:59:22 +01:00
int shape_face_index = - 1 ;
2015-10-08 20:00:40 +02:00
2021-11-10 23:57:11 +01:00
if ( shape - > intersect_point ( local_from ) ) {
if ( p_parameters . hit_from_inside ) {
// Hit shape at starting point.
min_d = 0 ;
2022-03-20 10:31:03 +01:00
res_point = begin ;
2021-11-10 23:57:11 +01:00
res_normal = Vector3 ( ) ;
res_shape = shape_idx ;
res_obj = col_obj ;
collided = true ;
break ;
} else {
// Ignore shape when starting inside.
continue ;
}
}
2023-03-05 22:59:22 +01:00
if ( shape - > intersect_segment ( local_from , local_to , shape_point , shape_normal , shape_face_index , p_parameters . hit_back_faces ) ) {
2020-10-17 07:08:21 +02:00
Transform3D xform = col_obj - > get_transform ( ) * col_obj - > get_shape_transform ( shape_idx ) ;
2015-10-08 20:00:40 +02:00
shape_point = xform . xform ( shape_point ) ;
real_t ld = normal . dot ( shape_point ) ;
if ( ld < min_d ) {
min_d = ld ;
res_point = shape_point ;
res_normal = inv_xform . basis . xform_inv ( shape_normal ) . normalized ( ) ;
2023-03-05 22:59:22 +01:00
res_face_index = shape_face_index ;
2015-10-08 20:00:40 +02:00
res_shape = shape_idx ;
res_obj = col_obj ;
collided = true ;
}
}
}
2020-05-14 16:41:43 +02:00
if ( ! collided ) {
2015-10-08 20:00:40 +02:00
return false ;
2020-05-14 16:41:43 +02:00
}
2022-09-28 17:05:34 +02:00
ERR_FAIL_NULL_V ( res_obj , false ) ; // Shouldn't happen but silences warning.
2015-10-08 20:00:40 +02:00
r_result . collider_id = res_obj - > get_instance_id ( ) ;
2020-05-14 16:41:43 +02:00
if ( r_result . collider_id . is_valid ( ) ) {
2015-10-08 20:00:40 +02:00
r_result . collider = ObjectDB : : get_instance ( r_result . collider_id ) ;
2020-05-14 16:41:43 +02:00
} else {
2020-04-02 01:20:12 +02:00
r_result . collider = nullptr ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
r_result . normal = res_normal ;
2023-03-05 22:59:22 +01:00
r_result . face_index = res_face_index ;
2015-10-08 20:00:40 +02:00
r_result . position = res_point ;
r_result . rid = res_obj - > get_self ( ) ;
r_result . shape = res_shape ;
return true ;
}
2021-11-02 02:00:58 +01:00
int GodotPhysicsDirectSpaceState3D : : intersect_shape ( const ShapeParameters & p_parameters , ShapeResult * r_results , int p_result_max ) {
2020-05-14 16:41:43 +02:00
if ( p_result_max < = 0 ) {
2015-10-08 20:00:40 +02:00
return 0 ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
GodotShape3D * shape = GodotPhysicsServer3D : : godot_singleton - > shape_owner . get_or_null ( p_parameters . shape_rid ) ;
2023-09-09 17:04:18 +02:00
ERR_FAIL_NULL_V ( shape , 0 ) ;
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
AABB aabb = p_parameters . transform . xform ( shape - > get_aabb ( ) ) ;
2015-10-08 20:00:40 +02:00
2021-10-18 21:24:30 +02:00
int amount = space - > broadphase - > cull_aabb ( aabb , space - > intersection_query_results , GodotSpace3D : : INTERSECTION_QUERY_MAX , space - > intersection_query_subindex_results ) ;
2015-10-08 20:00:40 +02:00
int cc = 0 ;
2020-10-17 07:08:21 +02:00
//Transform3D ai = p_xform.affine_inverse();
2015-10-08 20:00:40 +02:00
for ( int i = 0 ; i < amount ; i + + ) {
2020-05-14 16:41:43 +02:00
if ( cc > = p_result_max ) {
2015-10-08 20:00:40 +02:00
break ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
if ( ! _can_collide_with ( space - > intersection_query_results [ i ] , p_parameters . collision_mask , p_parameters . collide_with_bodies , p_parameters . collide_with_areas ) ) {
2015-10-08 20:00:40 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2017-03-24 21:45:31 +01:00
//area can't be picked by ray (default)
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
if ( p_parameters . exclude . has ( space - > intersection_query_results [ i ] - > get_self ( ) ) ) {
2015-10-08 20:00:40 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-10-18 21:24:30 +02:00
const GodotCollisionObject3D * col_obj = space - > intersection_query_results [ i ] ;
2015-10-08 20:00:40 +02:00
int shape_idx = space - > intersection_query_subindex_results [ i ] ;
2021-11-02 02:00:58 +01:00
if ( ! GodotCollisionSolver3D : : solve_static ( shape , p_parameters . transform , col_obj - > get_shape ( shape_idx ) , col_obj - > get_transform ( ) * col_obj - > get_shape_transform ( shape_idx ) , nullptr , nullptr , nullptr , p_parameters . margin , 0 ) ) {
2015-10-08 20:00:40 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2015-12-04 09:31:01 +01:00
if ( r_results ) {
r_results [ cc ] . collider_id = col_obj - > get_instance_id ( ) ;
2020-05-14 16:41:43 +02:00
if ( r_results [ cc ] . collider_id . is_valid ( ) ) {
2015-12-04 09:31:01 +01:00
r_results [ cc ] . collider = ObjectDB : : get_instance ( r_results [ cc ] . collider_id ) ;
2020-05-14 16:41:43 +02:00
} else {
2020-04-02 01:20:12 +02:00
r_results [ cc ] . collider = nullptr ;
2020-05-14 16:41:43 +02:00
}
2015-12-04 09:31:01 +01:00
r_results [ cc ] . rid = col_obj - > get_self ( ) ;
r_results [ cc ] . shape = shape_idx ;
}
2015-11-22 14:14:07 +01:00
2015-12-04 09:31:01 +01:00
cc + + ;
2015-10-08 20:00:40 +02:00
}
return cc ;
}
2021-11-02 02:00:58 +01:00
bool GodotPhysicsDirectSpaceState3D : : cast_motion ( const ShapeParameters & p_parameters , real_t & p_closest_safe , real_t & p_closest_unsafe , ShapeRestInfo * r_info ) {
GodotShape3D * shape = GodotPhysicsServer3D : : godot_singleton - > shape_owner . get_or_null ( p_parameters . shape_rid ) ;
2023-09-09 17:04:18 +02:00
ERR_FAIL_NULL_V ( shape , false ) ;
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
AABB aabb = p_parameters . transform . xform ( shape - > get_aabb ( ) ) ;
aabb = aabb . merge ( AABB ( aabb . position + p_parameters . motion , aabb . size ) ) ; //motion
aabb = aabb . grow ( p_parameters . margin ) ;
2015-10-08 20:00:40 +02:00
2021-10-18 21:24:30 +02:00
int amount = space - > broadphase - > cull_aabb ( aabb , space - > intersection_query_results , GodotSpace3D : : INTERSECTION_QUERY_MAX , space - > intersection_query_subindex_results ) ;
2015-10-08 20:00:40 +02:00
2017-02-14 00:25:05 +01:00
real_t best_safe = 1 ;
real_t best_unsafe = 1 ;
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
Transform3D xform_inv = p_parameters . transform . affine_inverse ( ) ;
2021-10-18 21:24:30 +02:00
GodotMotionShape3D mshape ;
2015-10-08 20:00:40 +02:00
mshape . shape = shape ;
2021-11-02 02:00:58 +01:00
mshape . motion = xform_inv . basis . xform ( p_parameters . motion ) ;
2015-10-08 20:00:40 +02:00
bool best_first = true ;
2021-11-02 02:00:58 +01:00
Vector3 motion_normal = p_parameters . motion . normalized ( ) ;
2021-07-02 00:14:30 +02:00
2015-10-08 20:00:40 +02:00
Vector3 closest_A , closest_B ;
for ( int i = 0 ; i < amount ; i + + ) {
2021-11-02 02:00:58 +01:00
if ( ! _can_collide_with ( space - > intersection_query_results [ i ] , p_parameters . collision_mask , p_parameters . collide_with_bodies , p_parameters . collide_with_areas ) ) {
2015-10-08 20:00:40 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
if ( p_parameters . exclude . has ( space - > intersection_query_results [ i ] - > get_self ( ) ) ) {
2015-10-08 20:00:40 +02:00
continue ; //ignore excluded
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-10-18 21:24:30 +02:00
const GodotCollisionObject3D * col_obj = space - > intersection_query_results [ i ] ;
2015-10-08 20:00:40 +02:00
int shape_idx = space - > intersection_query_subindex_results [ i ] ;
Vector3 point_A , point_B ;
2021-07-02 00:14:30 +02:00
Vector3 sep_axis = motion_normal ;
2015-10-08 20:00:40 +02:00
2020-10-17 07:08:21 +02:00
Transform3D col_obj_xform = col_obj - > get_transform ( ) * col_obj - > get_shape_transform ( shape_idx ) ;
2015-10-08 20:00:40 +02:00
//test initial overlap, does it collide if going all the way?
2021-11-02 02:00:58 +01:00
if ( GodotCollisionSolver3D : : solve_distance ( & mshape , p_parameters . transform , col_obj - > get_shape ( shape_idx ) , col_obj_xform , point_A , point_B , aabb , & sep_axis ) ) {
2015-10-08 20:00:40 +02:00
continue ;
}
2020-11-27 15:33:10 +01:00
//test initial overlap, ignore objects it's inside of.
2021-07-02 00:14:30 +02:00
sep_axis = motion_normal ;
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
if ( ! GodotCollisionSolver3D : : solve_distance ( shape , p_parameters . transform , col_obj - > get_shape ( shape_idx ) , col_obj_xform , point_A , point_B , aabb , & sep_axis ) ) {
2020-11-27 15:33:10 +01:00
continue ;
2015-10-08 20:00:40 +02:00
}
//just do kinematic solving
2021-07-02 00:14:30 +02:00
real_t low = 0.0 ;
real_t hi = 1.0 ;
real_t fraction_coeff = 0.5 ;
2019-02-12 21:10:08 +01:00
for ( int j = 0 ; j < 8 ; j + + ) { //steps should be customizable..
2021-07-02 00:14:30 +02:00
real_t fraction = low + ( hi - low ) * fraction_coeff ;
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
mshape . motion = xform_inv . basis . xform ( p_parameters . motion * fraction ) ;
2015-10-08 20:00:40 +02:00
Vector3 lA , lB ;
2021-07-02 00:14:30 +02:00
Vector3 sep = motion_normal ; //important optimization for this to work fast enough
2021-11-02 02:00:58 +01:00
bool collided = ! GodotCollisionSolver3D : : solve_distance ( & mshape , p_parameters . transform , col_obj - > get_shape ( shape_idx ) , col_obj_xform , lA , lB , aabb , & sep ) ;
2015-10-08 20:00:40 +02:00
if ( collided ) {
2021-07-02 00:14:30 +02:00
hi = fraction ;
if ( ( j = = 0 ) | | ( low > 0.0 ) ) { // Did it not collide before?
// When alternating or first iteration, use dichotomy.
fraction_coeff = 0.5 ;
} else {
// When colliding again, converge faster towards low fraction
// for more accurate results with long motions that collide near the start.
fraction_coeff = 0.25 ;
}
2015-10-08 20:00:40 +02:00
} else {
point_A = lA ;
point_B = lB ;
2021-07-02 00:14:30 +02:00
low = fraction ;
if ( ( j = = 0 ) | | ( hi < 1.0 ) ) { // Did it collide before?
// When alternating or first iteration, use dichotomy.
fraction_coeff = 0.5 ;
} else {
// When not colliding again, converge faster towards high fraction
// for more accurate results with long motions that collide near the end.
fraction_coeff = 0.75 ;
}
2015-10-08 20:00:40 +02:00
}
}
if ( low < best_safe ) {
best_first = true ; //force reset
best_safe = low ;
best_unsafe = hi ;
}
if ( r_info & & ( best_first | | ( point_A . distance_squared_to ( point_B ) < closest_A . distance_squared_to ( closest_B ) & & low < = best_safe ) ) ) {
closest_A = point_A ;
closest_B = point_B ;
r_info - > collider_id = col_obj - > get_instance_id ( ) ;
r_info - > rid = col_obj - > get_self ( ) ;
r_info - > shape = shape_idx ;
r_info - > point = closest_B ;
r_info - > normal = ( closest_A - closest_B ) . normalized ( ) ;
best_first = false ;
2021-10-18 21:24:30 +02:00
if ( col_obj - > get_type ( ) = = GodotCollisionObject3D : : TYPE_BODY ) {
const GodotBody3D * body = static_cast < const GodotBody3D * > ( col_obj ) ;
2021-03-18 14:14:19 +01:00
Vector3 rel_vec = closest_B - ( body - > get_transform ( ) . origin + body - > get_center_of_mass ( ) ) ;
r_info - > linear_velocity = body - > get_linear_velocity ( ) + ( body - > get_angular_velocity ( ) ) . cross ( rel_vec ) ;
2015-10-08 20:00:40 +02:00
}
}
}
p_closest_safe = best_safe ;
2016-03-09 00:00:52 +01:00
p_closest_unsafe = best_unsafe ;
2015-10-08 20:00:40 +02:00
return true ;
}
2021-11-02 02:00:58 +01:00
bool GodotPhysicsDirectSpaceState3D : : collide_shape ( const ShapeParameters & p_parameters , Vector3 * r_results , int p_result_max , int & r_result_count ) {
2020-05-14 16:41:43 +02:00
if ( p_result_max < = 0 ) {
2020-05-14 11:00:19 +02:00
return false ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
GodotShape3D * shape = GodotPhysicsServer3D : : godot_singleton - > shape_owner . get_or_null ( p_parameters . shape_rid ) ;
2023-09-09 17:04:18 +02:00
ERR_FAIL_NULL_V ( shape , 0 ) ;
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
AABB aabb = p_parameters . transform . xform ( shape - > get_aabb ( ) ) ;
aabb = aabb . grow ( p_parameters . margin ) ;
2015-10-08 20:00:40 +02:00
2021-10-18 21:24:30 +02:00
int amount = space - > broadphase - > cull_aabb ( aabb , space - > intersection_query_results , GodotSpace3D : : INTERSECTION_QUERY_MAX , space - > intersection_query_subindex_results ) ;
2015-10-08 20:00:40 +02:00
bool collided = false ;
r_result_count = 0 ;
2021-10-18 21:24:30 +02:00
GodotPhysicsServer3D : : CollCbkData cbk ;
2015-10-08 20:00:40 +02:00
cbk . max = p_result_max ;
cbk . amount = 0 ;
cbk . ptr = r_results ;
2021-10-18 21:24:30 +02:00
GodotCollisionSolver3D : : CallbackResult cbkres = GodotPhysicsServer3D : : _shape_col_cbk ;
2017-03-05 16:44:50 +01:00
2021-10-18 21:24:30 +02:00
GodotPhysicsServer3D : : CollCbkData * cbkptr = & cbk ;
2015-10-08 20:00:40 +02:00
for ( int i = 0 ; i < amount ; i + + ) {
2021-11-02 02:00:58 +01:00
if ( ! _can_collide_with ( space - > intersection_query_results [ i ] , p_parameters . collision_mask , p_parameters . collide_with_bodies , p_parameters . collide_with_areas ) ) {
2015-10-08 20:00:40 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-10-18 21:24:30 +02:00
const GodotCollisionObject3D * col_obj = space - > intersection_query_results [ i ] ;
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
if ( p_parameters . exclude . has ( col_obj - > get_self ( ) ) ) {
2015-10-08 20:00:40 +02:00
continue ;
}
2021-02-10 03:00:21 +01:00
int shape_idx = space - > intersection_query_subindex_results [ i ] ;
2021-11-02 02:00:58 +01:00
if ( GodotCollisionSolver3D : : solve_static ( shape , p_parameters . transform , col_obj - > get_shape ( shape_idx ) , col_obj - > get_transform ( ) * col_obj - > get_shape_transform ( shape_idx ) , cbkres , cbkptr , nullptr , p_parameters . margin ) ) {
2015-10-08 20:00:40 +02:00
collided = true ;
}
}
r_result_count = cbk . amount ;
return collided ;
}
2021-08-30 20:49:09 +02:00
struct _RestResultData {
2021-10-18 21:24:30 +02:00
const GodotCollisionObject3D * object = nullptr ;
2021-08-30 20:49:09 +02:00
int local_shape = 0 ;
int shape = 0 ;
Vector3 contact ;
Vector3 normal ;
real_t len = 0.0 ;
} ;
2015-10-08 20:00:40 +02:00
struct _RestCallbackData {
2021-10-18 21:24:30 +02:00
const GodotCollisionObject3D * object = nullptr ;
2021-08-30 20:49:09 +02:00
int local_shape = 0 ;
int shape = 0 ;
real_t min_allowed_depth = 0.0 ;
_RestResultData best_result ;
int max_results = 0 ;
int result_count = 0 ;
_RestResultData * other_results = nullptr ;
2015-10-08 20:00:40 +02:00
} ;
2023-01-15 03:39:23 +01:00
static void _rest_cbk_result ( const Vector3 & p_point_A , int p_index_A , const Vector3 & p_point_B , int p_index_B , const Vector3 & normal , void * p_userdata ) {
2022-04-05 12:40:26 +02:00
_RestCallbackData * rd = static_cast < _RestCallbackData * > ( p_userdata ) ;
2015-10-08 20:00:40 +02:00
Vector3 contact_rel = p_point_B - p_point_A ;
2017-02-14 00:25:05 +01:00
real_t len = contact_rel . length ( ) ;
2020-05-14 16:41:43 +02:00
if ( len < rd - > min_allowed_depth ) {
2019-02-16 17:45:01 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2021-08-30 20:49:09 +02:00
bool is_best_result = ( len > rd - > best_result . len ) ;
if ( rd - > other_results & & rd - > result_count > 0 ) {
// Consider as new result by default.
int prev_result_count = rd - > result_count + + ;
int result_index = 0 ;
real_t tested_len = is_best_result ? rd - > best_result . len : len ;
for ( ; result_index < prev_result_count - 1 ; + + result_index ) {
if ( tested_len > rd - > other_results [ result_index ] . len ) {
// Re-using a previous result.
rd - > result_count - - ;
break ;
}
}
if ( result_index < rd - > max_results - 1 ) {
_RestResultData & result = rd - > other_results [ result_index ] ;
if ( is_best_result ) {
// Keep the previous best result as separate result.
result = rd - > best_result ;
} else {
// Keep this result as separate result.
result . len = len ;
result . contact = p_point_B ;
2023-01-15 03:39:23 +01:00
result . normal = normal ;
2021-08-30 20:49:09 +02:00
result . object = rd - > object ;
result . shape = rd - > shape ;
result . local_shape = rd - > local_shape ;
}
} else {
// Discarding this result.
rd - > result_count - - ;
}
} else if ( is_best_result ) {
rd - > result_count = 1 ;
}
if ( ! is_best_result ) {
2015-10-08 20:00:40 +02:00
return ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-08-30 20:49:09 +02:00
rd - > best_result . len = len ;
rd - > best_result . contact = p_point_B ;
2023-01-15 03:39:23 +01:00
rd - > best_result . normal = normal ;
2021-08-30 20:49:09 +02:00
rd - > best_result . object = rd - > object ;
rd - > best_result . shape = rd - > shape ;
rd - > best_result . local_shape = rd - > local_shape ;
2015-10-08 20:00:40 +02:00
}
2020-05-14 14:29:06 +02:00
2021-11-02 02:00:58 +01:00
bool GodotPhysicsDirectSpaceState3D : : rest_info ( const ShapeParameters & p_parameters , ShapeRestInfo * r_info ) {
GodotShape3D * shape = GodotPhysicsServer3D : : godot_singleton - > shape_owner . get_or_null ( p_parameters . shape_rid ) ;
2023-09-09 17:04:18 +02:00
ERR_FAIL_NULL_V ( shape , 0 ) ;
2015-10-08 20:00:40 +02:00
2021-12-09 18:52:27 +01:00
real_t margin = MAX ( p_parameters . margin , TEST_MOTION_MARGIN_MIN_VALUE ) ;
2021-11-02 02:00:58 +01:00
AABB aabb = p_parameters . transform . xform ( shape - > get_aabb ( ) ) ;
2021-12-09 18:52:27 +01:00
aabb = aabb . grow ( margin ) ;
2015-10-08 20:00:40 +02:00
2021-10-18 21:24:30 +02:00
int amount = space - > broadphase - > cull_aabb ( aabb , space - > intersection_query_results , GodotSpace3D : : INTERSECTION_QUERY_MAX , space - > intersection_query_subindex_results ) ;
2015-10-08 20:00:40 +02:00
_RestCallbackData rcd ;
2021-11-12 21:30:54 +01:00
// Allowed depth can't be lower than motion length, in order to handle contacts at low speed.
real_t motion_length = p_parameters . motion . length ( ) ;
2021-12-09 18:52:27 +01:00
real_t min_contact_depth = margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR ;
2021-11-12 21:30:54 +01:00
rcd . min_allowed_depth = MIN ( motion_length , min_contact_depth ) ;
2015-10-08 20:00:40 +02:00
for ( int i = 0 ; i < amount ; i + + ) {
2021-11-02 02:00:58 +01:00
if ( ! _can_collide_with ( space - > intersection_query_results [ i ] , p_parameters . collision_mask , p_parameters . collide_with_bodies , p_parameters . collide_with_areas ) ) {
2015-10-08 20:00:40 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-10-18 21:24:30 +02:00
const GodotCollisionObject3D * col_obj = space - > intersection_query_results [ i ] ;
2015-10-08 20:00:40 +02:00
2021-11-02 02:00:58 +01:00
if ( p_parameters . exclude . has ( col_obj - > get_self ( ) ) ) {
2015-10-08 20:00:40 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-02-10 03:00:21 +01:00
int shape_idx = space - > intersection_query_subindex_results [ i ] ;
2015-10-08 20:00:40 +02:00
rcd . object = col_obj ;
rcd . shape = shape_idx ;
2021-12-09 18:52:27 +01:00
bool sc = GodotCollisionSolver3D : : solve_static ( shape , p_parameters . transform , col_obj - > get_shape ( shape_idx ) , col_obj - > get_transform ( ) * col_obj - > get_shape_transform ( shape_idx ) , _rest_cbk_result , & rcd , nullptr , margin ) ;
2020-05-14 16:41:43 +02:00
if ( ! sc ) {
2015-10-08 20:00:40 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
}
2021-08-30 20:49:09 +02:00
if ( rcd . best_result . len = = 0 | | ! rcd . best_result . object ) {
2015-10-08 20:00:40 +02:00
return false ;
2020-05-14 16:41:43 +02:00
}
2015-10-08 20:00:40 +02:00
2021-08-30 20:49:09 +02:00
r_info - > collider_id = rcd . best_result . object - > get_instance_id ( ) ;
r_info - > shape = rcd . best_result . shape ;
r_info - > normal = rcd . best_result . normal ;
r_info - > point = rcd . best_result . contact ;
r_info - > rid = rcd . best_result . object - > get_self ( ) ;
2021-10-18 21:24:30 +02:00
if ( rcd . best_result . object - > get_type ( ) = = GodotCollisionObject3D : : TYPE_BODY ) {
const GodotBody3D * body = static_cast < const GodotBody3D * > ( rcd . best_result . object ) ;
2021-08-30 20:49:09 +02:00
Vector3 rel_vec = rcd . best_result . contact - ( body - > get_transform ( ) . origin + body - > get_center_of_mass ( ) ) ;
2021-03-18 14:14:19 +01:00
r_info - > linear_velocity = body - > get_linear_velocity ( ) + ( body - > get_angular_velocity ( ) ) . cross ( rel_vec ) ;
2015-10-08 20:00:40 +02:00
} else {
r_info - > linear_velocity = Vector3 ( ) ;
}
return true ;
}
2021-10-18 21:24:30 +02:00
Vector3 GodotPhysicsDirectSpaceState3D : : get_closest_point_to_object_volume ( RID p_object , const Vector3 p_point ) const {
GodotCollisionObject3D * obj = GodotPhysicsServer3D : : godot_singleton - > area_owner . get_or_null ( p_object ) ;
2017-07-15 06:23:10 +02:00
if ( ! obj ) {
2021-10-18 21:24:30 +02:00
obj = GodotPhysicsServer3D : : godot_singleton - > body_owner . get_or_null ( p_object ) ;
2017-07-15 06:23:10 +02:00
}
2023-09-09 17:04:18 +02:00
ERR_FAIL_NULL_V ( obj , Vector3 ( ) ) ;
2017-07-15 06:23:10 +02:00
ERR_FAIL_COND_V ( obj - > get_space ( ) ! = space , Vector3 ( ) ) ;
2021-01-28 07:34:26 +01:00
real_t min_distance = 1e20 ;
2017-07-15 06:23:10 +02:00
Vector3 min_point ;
bool shapes_found = false ;
for ( int i = 0 ; i < obj - > get_shape_count ( ) ; i + + ) {
2021-06-23 01:36:43 +02:00
if ( obj - > is_shape_disabled ( i ) ) {
2017-07-15 06:23:10 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2017-07-15 06:23:10 +02:00
2020-10-17 07:08:21 +02:00
Transform3D shape_xform = obj - > get_transform ( ) * obj - > get_shape_transform ( i ) ;
2021-10-18 21:24:30 +02:00
GodotShape3D * shape = obj - > get_shape ( i ) ;
2017-07-15 06:23:10 +02:00
Vector3 point = shape - > get_closest_point_to ( shape_xform . affine_inverse ( ) . xform ( p_point ) ) ;
point = shape_xform . xform ( point ) ;
2021-01-28 07:34:26 +01:00
real_t dist = point . distance_to ( p_point ) ;
2017-07-15 06:23:10 +02:00
if ( dist < min_distance ) {
min_distance = dist ;
min_point = point ;
}
shapes_found = true ;
}
if ( ! shapes_found ) {
return obj - > get_transform ( ) . origin ; //no shapes found, use distance to origin.
} else {
return min_point ;
}
}
2021-10-18 21:24:30 +02:00
GodotPhysicsDirectSpaceState3D : : GodotPhysicsDirectSpaceState3D ( ) {
2020-04-02 01:20:12 +02:00
space = nullptr ;
2015-10-08 20:00:40 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
2021-10-18 21:24:30 +02:00
int GodotSpace3D : : _cull_aabb_for_body ( GodotBody3D * p_body , const AABB & p_aabb ) {
2017-07-15 06:23:10 +02:00
int amount = broadphase - > cull_aabb ( p_aabb , intersection_query_results , INTERSECTION_QUERY_MAX , intersection_query_subindex_results ) ;
for ( int i = 0 ; i < amount ; i + + ) {
bool keep = true ;
2020-05-14 16:41:43 +02:00
if ( intersection_query_results [ i ] = = p_body ) {
2017-07-15 06:23:10 +02:00
keep = false ;
2021-10-18 21:24:30 +02:00
} else if ( intersection_query_results [ i ] - > get_type ( ) = = GodotCollisionObject3D : : TYPE_AREA ) {
2017-07-15 06:23:10 +02:00
keep = false ;
2021-10-18 21:24:30 +02:00
} else if ( intersection_query_results [ i ] - > get_type ( ) = = GodotCollisionObject3D : : TYPE_SOFT_BODY ) {
2021-03-12 04:33:46 +01:00
keep = false ;
2021-10-18 21:24:30 +02:00
} else if ( ! p_body - > collides_with ( static_cast < GodotBody3D * > ( intersection_query_results [ i ] ) ) ) {
2017-07-15 06:23:10 +02:00
keep = false ;
2021-10-18 21:24:30 +02:00
} else if ( static_cast < GodotBody3D * > ( intersection_query_results [ i ] ) - > has_exception ( p_body - > get_self ( ) ) | | p_body - > has_exception ( intersection_query_results [ i ] - > get_self ( ) ) ) {
2017-07-15 06:23:10 +02:00
keep = false ;
2020-05-14 16:41:43 +02:00
}
2017-07-15 06:23:10 +02:00
if ( ! keep ) {
if ( i < amount - 1 ) {
SWAP ( intersection_query_results [ i ] , intersection_query_results [ amount - 1 ] ) ;
SWAP ( intersection_query_subindex_results [ i ] , intersection_query_subindex_results [ amount - 1 ] ) ;
}
amount - - ;
i - - ;
}
}
return amount ;
}
2021-10-18 21:24:30 +02:00
bool GodotSpace3D : : test_body_motion ( GodotBody3D * p_body , const PhysicsServer3D : : MotionParameters & p_parameters , PhysicsServer3D : : MotionResult * r_result ) {
2017-07-15 06:23:10 +02:00
//give me back regular physics engine logic
//this is madness
//and most people using this function will think
//what it does is simpler than using physics
//this took about a week to get right..
//but is it right? who knows at this point..
2024-03-15 15:40:42 +01:00
ERR_FAIL_COND_V ( p_parameters . max_collisions < 0 | | p_parameters . max_collisions > PhysicsServer3D : : MotionResult : : MAX_COLLISIONS , false ) ;
2021-08-30 20:49:09 +02:00
2017-07-15 06:23:10 +02:00
if ( r_result ) {
2021-08-30 20:49:09 +02:00
* r_result = PhysicsServer3D : : MotionResult ( ) ;
2017-07-15 06:23:10 +02:00
}
2021-08-30 20:49:09 +02:00
2017-11-17 03:09:00 +01:00
AABB body_aabb ;
2018-11-02 19:44:29 +01:00
bool shapes_found = false ;
2017-07-15 06:23:10 +02:00
for ( int i = 0 ; i < p_body - > get_shape_count ( ) ; i + + ) {
2021-06-23 01:36:43 +02:00
if ( p_body - > is_shape_disabled ( i ) ) {
2018-11-02 19:44:29 +01:00
continue ;
2020-05-14 16:41:43 +02:00
}
2018-11-02 19:44:29 +01:00
if ( ! shapes_found ) {
2017-07-15 06:23:10 +02:00
body_aabb = p_body - > get_shape_aabb ( i ) ;
2018-11-02 19:44:29 +01:00
shapes_found = true ;
} else {
2017-07-15 06:23:10 +02:00
body_aabb = body_aabb . merge ( p_body - > get_shape_aabb ( i ) ) ;
2018-11-02 19:44:29 +01:00
}
}
if ( ! shapes_found ) {
2019-02-16 15:06:17 +01:00
if ( r_result ) {
2021-09-30 20:28:57 +02:00
r_result - > travel = p_parameters . motion ;
2019-02-16 15:06:17 +01:00
}
2018-11-02 19:44:29 +01:00
return false ;
2017-07-15 06:23:10 +02:00
}
2021-12-09 18:52:27 +01:00
real_t margin = MAX ( p_parameters . margin , TEST_MOTION_MARGIN_MIN_VALUE ) ;
2017-07-15 06:23:10 +02:00
// Undo the currently transform the physics server is aware of and apply the provided one
2021-09-30 20:28:57 +02:00
body_aabb = p_parameters . from . xform ( p_body - > get_inv_transform ( ) . xform ( body_aabb ) ) ;
2021-12-09 18:52:27 +01:00
body_aabb = body_aabb . grow ( margin ) ;
2017-07-15 06:23:10 +02:00
2021-12-09 18:52:27 +01:00
real_t min_contact_depth = margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR ;
2021-09-17 03:38:17 +02:00
2021-09-30 20:28:57 +02:00
real_t motion_length = p_parameters . motion . length ( ) ;
Vector3 motion_normal = p_parameters . motion / motion_length ;
2021-02-18 02:27:19 +01:00
2021-09-30 20:28:57 +02:00
Transform3D body_transform = p_parameters . from ;
2017-07-15 06:23:10 +02:00
2021-02-18 02:27:19 +01:00
bool recovered = false ;
2017-07-15 06:23:10 +02:00
{
//STEP 1, FREE BODY IF STUCK
const int max_results = 32 ;
int recover_attempts = 4 ;
Vector3 sr [ max_results * 2 ] ;
2022-08-10 18:45:36 +02:00
real_t priorities [ max_results ] ;
2017-07-15 06:23:10 +02:00
do {
2021-10-18 21:24:30 +02:00
GodotPhysicsServer3D : : CollCbkData cbk ;
2017-07-15 06:23:10 +02:00
cbk . max = max_results ;
cbk . amount = 0 ;
cbk . ptr = sr ;
2021-10-18 21:24:30 +02:00
GodotPhysicsServer3D : : CollCbkData * cbkptr = & cbk ;
GodotCollisionSolver3D : : CallbackResult cbkres = GodotPhysicsServer3D : : _shape_col_cbk ;
2022-08-10 18:45:36 +02:00
int priority_amount = 0 ;
2017-07-15 06:23:10 +02:00
bool collided = false ;
int amount = _cull_aabb_for_body ( p_body , body_aabb ) ;
for ( int j = 0 ; j < p_body - > get_shape_count ( ) ; j + + ) {
2021-06-23 01:36:43 +02:00
if ( p_body - > is_shape_disabled ( j ) ) {
2017-07-15 06:23:10 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2017-07-15 06:23:10 +02:00
2020-10-17 07:08:21 +02:00
Transform3D body_shape_xform = body_transform * p_body - > get_shape_transform ( j ) ;
2021-10-18 21:24:30 +02:00
GodotShape3D * body_shape = p_body - > get_shape ( j ) ;
2018-08-20 22:31:55 +02:00
2017-07-15 06:23:10 +02:00
for ( int i = 0 ; i < amount ; i + + ) {
2021-10-18 21:24:30 +02:00
const GodotCollisionObject3D * col_obj = intersection_query_results [ i ] ;
2021-09-30 20:28:57 +02:00
if ( p_parameters . exclude_bodies . has ( col_obj - > get_self ( ) ) ) {
2021-08-10 03:16:45 +02:00
continue ;
}
2021-09-30 20:05:30 +02:00
if ( p_parameters . exclude_objects . has ( col_obj - > get_instance_id ( ) ) ) {
continue ;
}
2017-07-15 06:23:10 +02:00
2021-08-10 20:48:19 +02:00
int shape_idx = intersection_query_subindex_results [ i ] ;
2020-10-08 09:57:12 +02:00
2021-12-09 18:52:27 +01:00
if ( GodotCollisionSolver3D : : solve_static ( body_shape , body_shape_xform , col_obj - > get_shape ( shape_idx ) , col_obj - > get_transform ( ) * col_obj - > get_shape_transform ( shape_idx ) , cbkres , cbkptr , nullptr , margin ) ) {
2017-07-15 06:23:10 +02:00
collided = cbk . amount > 0 ;
}
2022-08-10 18:45:36 +02:00
while ( cbk . amount > priority_amount ) {
priorities [ priority_amount ] = col_obj - > get_collision_priority ( ) ;
priority_amount + + ;
}
2017-07-15 06:23:10 +02:00
}
}
if ( ! collided ) {
break ;
}
2022-08-10 18:45:36 +02:00
real_t inv_total_weight = 0.0 ;
for ( int i = 0 ; i < cbk . amount ; i + + ) {
inv_total_weight + = priorities [ i ] ;
}
inv_total_weight = Math : : is_zero_approx ( inv_total_weight ) ? 1.0 : ( real_t ) cbk . amount / inv_total_weight ;
2021-09-17 03:38:17 +02:00
recovered = true ;
2017-07-15 06:23:10 +02:00
2021-09-17 03:38:17 +02:00
Vector3 recover_motion ;
2017-07-15 06:23:10 +02:00
for ( int i = 0 ; i < cbk . amount ; i + + ) {
Vector3 a = sr [ i * 2 + 0 ] ;
Vector3 b = sr [ i * 2 + 1 ] ;
2021-02-18 02:27:19 +01:00
// Compute plane on b towards a.
Vector3 n = ( a - b ) . normalized ( ) ;
real_t d = n . dot ( b ) ;
// Compute depth on recovered motion.
real_t depth = n . dot ( a + recover_motion ) - d ;
2021-09-17 03:38:17 +02:00
if ( depth > min_contact_depth + CMP_EPSILON ) {
2021-02-18 02:27:19 +01:00
// Only recover if there is penetration.
2022-08-10 18:45:36 +02:00
recover_motion - = n * ( depth - min_contact_depth ) * 0.4 * priorities [ i ] * inv_total_weight ;
2021-02-18 02:27:19 +01:00
}
2017-07-15 06:23:10 +02:00
}
if ( recover_motion = = Vector3 ( ) ) {
collided = false ;
break ;
}
body_transform . origin + = recover_motion ;
body_aabb . position + = recover_motion ;
recover_attempts - - ;
} while ( recover_attempts ) ;
}
real_t safe = 1.0 ;
real_t unsafe = 1.0 ;
int best_shape = - 1 ;
{
// STEP 2 ATTEMPT MOTION
2017-11-17 03:09:00 +01:00
AABB motion_aabb = body_aabb ;
2021-09-30 20:28:57 +02:00
motion_aabb . position + = p_parameters . motion ;
2017-07-15 06:23:10 +02:00
motion_aabb = motion_aabb . merge ( body_aabb ) ;
int amount = _cull_aabb_for_body ( p_body , motion_aabb ) ;
for ( int j = 0 ; j < p_body - > get_shape_count ( ) ; j + + ) {
2021-06-23 01:36:43 +02:00
if ( p_body - > is_shape_disabled ( j ) ) {
2017-07-15 06:23:10 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2017-07-15 06:23:10 +02:00
2021-10-18 21:24:30 +02:00
GodotShape3D * body_shape = p_body - > get_shape ( j ) ;
2017-07-15 06:23:10 +02:00
2021-08-19 17:28:04 +02:00
// Colliding separation rays allows to properly snap to the ground,
// otherwise it's not needed in regular motion.
2021-09-30 20:28:57 +02:00
if ( ! p_parameters . collide_separation_ray & & ( body_shape - > get_type ( ) = = PhysicsServer3D : : SHAPE_SEPARATION_RAY ) ) {
2021-08-19 20:02:40 +02:00
// When slide on slope is on, separation ray shape acts like a regular shape.
2021-10-18 21:24:30 +02:00
if ( ! static_cast < GodotSeparationRayShape3D * > ( body_shape ) - > get_slide_on_slope ( ) ) {
2021-08-19 17:28:04 +02:00
continue ;
}
}
Transform3D body_shape_xform = body_transform * p_body - > get_shape_transform ( j ) ;
2020-10-17 07:08:21 +02:00
Transform3D body_shape_xform_inv = body_shape_xform . affine_inverse ( ) ;
2021-10-18 21:24:30 +02:00
GodotMotionShape3D mshape ;
2017-07-15 06:23:10 +02:00
mshape . shape = body_shape ;
2021-09-30 20:28:57 +02:00
mshape . motion = body_shape_xform_inv . basis . xform ( p_parameters . motion ) ;
2017-07-15 06:23:10 +02:00
bool stuck = false ;
real_t best_safe = 1 ;
real_t best_unsafe = 1 ;
for ( int i = 0 ; i < amount ; i + + ) {
2021-10-18 21:24:30 +02:00
const GodotCollisionObject3D * col_obj = intersection_query_results [ i ] ;
2021-09-30 20:28:57 +02:00
if ( p_parameters . exclude_bodies . has ( col_obj - > get_self ( ) ) ) {
2021-08-10 03:16:45 +02:00
continue ;
}
2021-09-30 20:05:30 +02:00
if ( p_parameters . exclude_objects . has ( col_obj - > get_instance_id ( ) ) ) {
continue ;
}
2017-07-15 06:23:10 +02:00
2021-08-10 20:48:19 +02:00
int shape_idx = intersection_query_subindex_results [ i ] ;
2020-10-08 09:57:12 +02:00
2017-07-15 06:23:10 +02:00
//test initial overlap, does it collide if going all the way?
Vector3 point_A , point_B ;
2021-02-18 02:27:19 +01:00
Vector3 sep_axis = motion_normal ;
2017-07-15 06:23:10 +02:00
2020-10-17 07:08:21 +02:00
Transform3D col_obj_xform = col_obj - > get_transform ( ) * col_obj - > get_shape_transform ( shape_idx ) ;
2017-07-15 06:23:10 +02:00
//test initial overlap, does it collide if going all the way?
2021-10-18 21:24:30 +02:00
if ( GodotCollisionSolver3D : : solve_distance ( & mshape , body_shape_xform , col_obj - > get_shape ( shape_idx ) , col_obj_xform , point_A , point_B , motion_aabb , & sep_axis ) ) {
2017-07-15 06:23:10 +02:00
continue ;
}
2021-02-18 02:27:19 +01:00
sep_axis = motion_normal ;
2017-07-15 06:23:10 +02:00
2021-10-18 21:24:30 +02:00
if ( ! GodotCollisionSolver3D : : solve_distance ( body_shape , body_shape_xform , col_obj - > get_shape ( shape_idx ) , col_obj_xform , point_A , point_B , motion_aabb , & sep_axis ) ) {
2017-07-15 06:23:10 +02:00
stuck = true ;
break ;
}
//just do kinematic solving
2021-07-02 00:14:30 +02:00
real_t low = 0.0 ;
real_t hi = 1.0 ;
real_t fraction_coeff = 0.5 ;
2019-02-12 21:10:08 +01:00
for ( int k = 0 ; k < 8 ; k + + ) { //steps should be customizable..
2021-07-02 00:14:30 +02:00
real_t fraction = low + ( hi - low ) * fraction_coeff ;
2017-07-15 06:23:10 +02:00
2021-09-30 20:28:57 +02:00
mshape . motion = body_shape_xform_inv . basis . xform ( p_parameters . motion * fraction ) ;
2017-07-15 06:23:10 +02:00
Vector3 lA , lB ;
2021-07-02 00:14:30 +02:00
Vector3 sep = motion_normal ; //important optimization for this to work fast enough
2021-10-18 21:24:30 +02:00
bool collided = ! GodotCollisionSolver3D : : solve_distance ( & mshape , body_shape_xform , col_obj - > get_shape ( shape_idx ) , col_obj_xform , lA , lB , motion_aabb , & sep ) ;
2017-07-15 06:23:10 +02:00
if ( collided ) {
2021-07-02 00:14:30 +02:00
hi = fraction ;
if ( ( k = = 0 ) | | ( low > 0.0 ) ) { // Did it not collide before?
// When alternating or first iteration, use dichotomy.
fraction_coeff = 0.5 ;
} else {
// When colliding again, converge faster towards low fraction
// for more accurate results with long motions that collide near the start.
fraction_coeff = 0.25 ;
}
2017-07-15 06:23:10 +02:00
} else {
point_A = lA ;
point_B = lB ;
2021-07-02 00:14:30 +02:00
low = fraction ;
if ( ( k = = 0 ) | | ( hi < 1.0 ) ) { // Did it collide before?
// When alternating or first iteration, use dichotomy.
fraction_coeff = 0.5 ;
} else {
// When not colliding again, converge faster towards high fraction
// for more accurate results with long motions that collide near the end.
fraction_coeff = 0.75 ;
}
2017-07-15 06:23:10 +02:00
}
}
if ( low < best_safe ) {
best_safe = low ;
best_unsafe = hi ;
}
}
if ( stuck ) {
safe = 0 ;
unsafe = 0 ;
best_shape = j ; //sadly it's the best
break ;
}
if ( best_safe = = 1.0 ) {
continue ;
}
if ( best_safe < safe ) {
safe = best_safe ;
unsafe = best_unsafe ;
best_shape = j ;
}
}
}
bool collided = false ;
2022-04-28 22:44:09 +02:00
if ( ( p_parameters . recovery_as_collision & & recovered ) | | ( safe < 1 ) ) {
2021-02-18 02:27:19 +01:00
if ( safe > = 1 ) {
best_shape = - 1 ; //no best shape with cast, reset to -1
2017-07-15 06:23:10 +02:00
}
//it collided, let's get the rest info in unsafe advance
2020-10-17 07:08:21 +02:00
Transform3D ugt = body_transform ;
2021-09-30 20:28:57 +02:00
ugt . origin + = p_parameters . motion * unsafe ;
2017-07-15 06:23:10 +02:00
2021-08-30 20:49:09 +02:00
_RestResultData results [ PhysicsServer3D : : MotionResult : : MAX_COLLISIONS ] ;
2017-07-15 06:23:10 +02:00
_RestCallbackData rcd ;
2021-09-30 20:28:57 +02:00
if ( p_parameters . max_collisions > 1 ) {
rcd . max_results = p_parameters . max_collisions ;
2021-08-30 20:49:09 +02:00
rcd . other_results = results ;
}
2017-07-15 06:23:10 +02:00
2021-02-18 02:27:19 +01:00
// Allowed depth can't be lower than motion length, in order to handle contacts at low speed.
2021-09-17 03:38:17 +02:00
rcd . min_allowed_depth = MIN ( motion_length , min_contact_depth ) ;
2017-07-15 06:23:10 +02:00
2022-02-18 19:43:38 +01:00
body_aabb . position + = p_parameters . motion * unsafe ;
int amount = _cull_aabb_for_body ( p_body , body_aabb ) ;
2021-02-18 02:27:19 +01:00
int from_shape = best_shape ! = - 1 ? best_shape : 0 ;
int to_shape = best_shape ! = - 1 ? best_shape + 1 : p_body - > get_shape_count ( ) ;
2017-07-15 06:23:10 +02:00
2021-02-18 02:27:19 +01:00
for ( int j = from_shape ; j < to_shape ; j + + ) {
2021-06-23 01:36:43 +02:00
if ( p_body - > is_shape_disabled ( j ) ) {
2021-02-18 02:27:19 +01:00
continue ;
}
2017-07-15 06:23:10 +02:00
2020-10-17 07:08:21 +02:00
Transform3D body_shape_xform = ugt * p_body - > get_shape_transform ( j ) ;
2021-10-18 21:24:30 +02:00
GodotShape3D * body_shape = p_body - > get_shape ( j ) ;
2017-07-15 06:23:10 +02:00
2021-02-18 02:27:19 +01:00
for ( int i = 0 ; i < amount ; i + + ) {
2021-10-18 21:24:30 +02:00
const GodotCollisionObject3D * col_obj = intersection_query_results [ i ] ;
2021-09-30 20:28:57 +02:00
if ( p_parameters . exclude_bodies . has ( col_obj - > get_self ( ) ) ) {
2021-08-10 03:16:45 +02:00
continue ;
}
2021-09-30 20:05:30 +02:00
if ( p_parameters . exclude_objects . has ( col_obj - > get_instance_id ( ) ) ) {
continue ;
}
2021-02-18 02:27:19 +01:00
int shape_idx = intersection_query_subindex_results [ i ] ;
rcd . object = col_obj ;
rcd . shape = shape_idx ;
2021-12-09 18:52:27 +01:00
bool sc = GodotCollisionSolver3D : : solve_static ( body_shape , body_shape_xform , col_obj - > get_shape ( shape_idx ) , col_obj - > get_transform ( ) * col_obj - > get_shape_transform ( shape_idx ) , _rest_cbk_result , & rcd , nullptr , margin ) ;
2021-02-18 02:27:19 +01:00
if ( ! sc ) {
continue ;
}
}
2017-07-15 06:23:10 +02:00
}
2021-08-30 20:49:09 +02:00
if ( rcd . result_count > 0 ) {
2017-07-15 06:23:10 +02:00
if ( r_result ) {
2021-08-30 20:49:09 +02:00
for ( int collision_index = 0 ; collision_index < rcd . result_count ; + + collision_index ) {
const _RestResultData & result = ( collision_index > 0 ) ? rcd . other_results [ collision_index - 1 ] : rcd . best_result ;
PhysicsServer3D : : MotionCollision & collision = r_result - > collisions [ collision_index ] ;
collision . collider = result . object - > get_self ( ) ;
collision . collider_id = result . object - > get_instance_id ( ) ;
collision . collider_shape = result . shape ;
collision . local_shape = result . local_shape ;
collision . normal = result . normal ;
collision . position = result . contact ;
collision . depth = result . len ;
2021-10-18 21:24:30 +02:00
const GodotBody3D * body = static_cast < const GodotBody3D * > ( result . object ) ;
2021-08-30 20:49:09 +02:00
Vector3 rel_vec = result . contact - ( body - > get_transform ( ) . origin + body - > get_center_of_mass ( ) ) ;
collision . collider_velocity = body - > get_linear_velocity ( ) + ( body - > get_angular_velocity ( ) ) . cross ( rel_vec ) ;
2022-07-29 22:00:04 +02:00
collision . collider_angular_velocity = body - > get_angular_velocity ( ) ;
2021-08-30 20:49:09 +02:00
}
2017-07-15 06:23:10 +02:00
2021-09-30 20:28:57 +02:00
r_result - > travel = safe * p_parameters . motion ;
r_result - > remainder = p_parameters . motion - safe * p_parameters . motion ;
r_result - > travel + = ( body_transform . get_origin ( ) - p_parameters . from . get_origin ( ) ) ;
2021-08-30 20:49:09 +02:00
2021-09-30 20:28:57 +02:00
r_result - > collision_safe_fraction = safe ;
r_result - > collision_unsafe_fraction = unsafe ;
2021-08-30 20:49:09 +02:00
r_result - > collision_count = rcd . result_count ;
2022-08-10 03:14:36 +02:00
r_result - > collision_depth = rcd . best_result . len ;
2017-07-15 06:23:10 +02:00
}
collided = true ;
}
}
2021-02-18 02:27:19 +01:00
if ( ! collided & & r_result ) {
2021-09-30 20:28:57 +02:00
r_result - > travel = p_parameters . motion ;
2021-02-18 02:27:19 +01:00
r_result - > remainder = Vector3 ( ) ;
2021-09-30 20:28:57 +02:00
r_result - > travel + = ( body_transform . get_origin ( ) - p_parameters . from . get_origin ( ) ) ;
2021-08-30 20:49:09 +02:00
2021-09-30 20:28:57 +02:00
r_result - > collision_safe_fraction = 1.0 ;
r_result - > collision_unsafe_fraction = 1.0 ;
2022-08-10 03:14:36 +02:00
r_result - > collision_depth = 0.0 ;
2021-02-18 02:27:19 +01:00
}
2017-07-15 06:23:10 +02:00
return collided ;
}
2022-02-04 17:46:10 +01:00
// Assumes a valid collision pair, this should have been checked beforehand in the BVH or octree.
2021-10-18 21:24:30 +02:00
void * GodotSpace3D : : _broadphase_pair ( GodotCollisionObject3D * A , int p_subindex_A , GodotCollisionObject3D * B , int p_subindex_B , void * p_self ) {
GodotCollisionObject3D : : Type type_A = A - > get_type ( ) ;
GodotCollisionObject3D : : Type type_B = B - > get_type ( ) ;
2015-10-08 20:00:40 +02:00
if ( type_A > type_B ) {
SWAP ( A , B ) ;
SWAP ( p_subindex_A , p_subindex_B ) ;
SWAP ( type_A , type_B ) ;
}
2022-04-05 12:40:26 +02:00
GodotSpace3D * self = static_cast < GodotSpace3D * > ( p_self ) ;
2015-10-08 20:00:40 +02:00
self - > collision_pairs + + ;
2021-10-18 21:24:30 +02:00
if ( type_A = = GodotCollisionObject3D : : TYPE_AREA ) {
GodotArea3D * area = static_cast < GodotArea3D * > ( A ) ;
if ( type_B = = GodotCollisionObject3D : : TYPE_AREA ) {
GodotArea3D * area_b = static_cast < GodotArea3D * > ( B ) ;
GodotArea2Pair3D * area2_pair = memnew ( GodotArea2Pair3D ( area_b , p_subindex_B , area , p_subindex_A ) ) ;
2015-10-08 20:00:40 +02:00
return area2_pair ;
2021-10-18 21:24:30 +02:00
} else if ( type_B = = GodotCollisionObject3D : : TYPE_SOFT_BODY ) {
GodotSoftBody3D * softbody = static_cast < GodotSoftBody3D * > ( B ) ;
GodotAreaSoftBodyPair3D * soft_area_pair = memnew ( GodotAreaSoftBodyPair3D ( softbody , p_subindex_B , area , p_subindex_A ) ) ;
2021-07-20 06:23:32 +02:00
return soft_area_pair ;
2015-10-08 20:00:40 +02:00
} else {
2021-10-18 21:24:30 +02:00
GodotBody3D * body = static_cast < GodotBody3D * > ( B ) ;
GodotAreaPair3D * area_pair = memnew ( GodotAreaPair3D ( body , p_subindex_B , area , p_subindex_A ) ) ;
2015-10-08 20:00:40 +02:00
return area_pair ;
}
2021-10-18 21:24:30 +02:00
} else if ( type_A = = GodotCollisionObject3D : : TYPE_BODY ) {
if ( type_B = = GodotCollisionObject3D : : TYPE_SOFT_BODY ) {
2022-04-05 12:40:26 +02:00
GodotBodySoftBodyPair3D * soft_pair = memnew ( GodotBodySoftBodyPair3D ( static_cast < GodotBody3D * > ( A ) , p_subindex_A , static_cast < GodotSoftBody3D * > ( B ) ) ) ;
2021-03-12 04:33:46 +01:00
return soft_pair ;
} else {
2022-04-05 12:40:26 +02:00
GodotBodyPair3D * b = memnew ( GodotBodyPair3D ( static_cast < GodotBody3D * > ( A ) , p_subindex_A , static_cast < GodotBody3D * > ( B ) , p_subindex_B ) ) ;
2021-03-12 04:33:46 +01:00
return b ;
}
2015-10-08 20:00:40 +02:00
} else {
2021-03-12 04:33:46 +01:00
// Soft Body/Soft Body, not supported.
2015-10-08 20:00:40 +02:00
}
2020-04-02 01:20:12 +02:00
return nullptr ;
2015-10-08 20:00:40 +02:00
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : _broadphase_unpair ( GodotCollisionObject3D * A , int p_subindex_A , GodotCollisionObject3D * B , int p_subindex_B , void * p_data , void * p_self ) {
2020-06-08 18:46:21 +02:00
if ( ! p_data ) {
return ;
}
2022-04-05 12:40:26 +02:00
GodotSpace3D * self = static_cast < GodotSpace3D * > ( p_self ) ;
2015-10-08 20:00:40 +02:00
self - > collision_pairs - - ;
2022-04-05 12:40:26 +02:00
GodotConstraint3D * c = static_cast < GodotConstraint3D * > ( p_data ) ;
2015-10-08 20:00:40 +02:00
memdelete ( c ) ;
}
2021-10-18 21:24:30 +02:00
const SelfList < GodotBody3D > : : List & GodotSpace3D : : get_active_body_list ( ) const {
2015-10-08 20:00:40 +02:00
return active_list ;
}
2020-05-14 14:29:06 +02:00
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : body_add_to_active_list ( SelfList < GodotBody3D > * p_body ) {
2015-10-08 20:00:40 +02:00
active_list . add ( p_body ) ;
}
2020-05-14 14:29:06 +02:00
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : body_remove_from_active_list ( SelfList < GodotBody3D > * p_body ) {
2015-10-08 20:00:40 +02:00
active_list . remove ( p_body ) ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : body_add_to_mass_properties_update_list ( SelfList < GodotBody3D > * p_body ) {
2021-06-11 02:37:19 +02:00
mass_properties_update_list . add ( p_body ) ;
2015-10-08 20:00:40 +02:00
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : body_remove_from_mass_properties_update_list ( SelfList < GodotBody3D > * p_body ) {
2021-06-11 02:37:19 +02:00
mass_properties_update_list . remove ( p_body ) ;
2015-10-08 20:00:40 +02:00
}
2021-10-18 21:24:30 +02:00
GodotBroadPhase3D * GodotSpace3D : : get_broadphase ( ) {
2015-10-08 20:00:40 +02:00
return broadphase ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : add_object ( GodotCollisionObject3D * p_object ) {
2015-10-08 20:00:40 +02:00
ERR_FAIL_COND ( objects . has ( p_object ) ) ;
objects . insert ( p_object ) ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : remove_object ( GodotCollisionObject3D * p_object ) {
2015-10-08 20:00:40 +02:00
ERR_FAIL_COND ( ! objects . has ( p_object ) ) ;
objects . erase ( p_object ) ;
}
2022-05-19 17:00:06 +02:00
const HashSet < GodotCollisionObject3D * > & GodotSpace3D : : get_objects ( ) const {
2015-10-08 20:00:40 +02:00
return objects ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : body_add_to_state_query_list ( SelfList < GodotBody3D > * p_body ) {
2015-10-08 20:00:40 +02:00
state_query_list . add ( p_body ) ;
}
2020-05-14 14:29:06 +02:00
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : body_remove_from_state_query_list ( SelfList < GodotBody3D > * p_body ) {
2015-10-08 20:00:40 +02:00
state_query_list . remove ( p_body ) ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : area_add_to_monitor_query_list ( SelfList < GodotArea3D > * p_area ) {
2015-10-08 20:00:40 +02:00
monitor_query_list . add ( p_area ) ;
}
2020-05-14 14:29:06 +02:00
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : area_remove_from_monitor_query_list ( SelfList < GodotArea3D > * p_area ) {
2015-10-08 20:00:40 +02:00
monitor_query_list . remove ( p_area ) ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : area_add_to_moved_list ( SelfList < GodotArea3D > * p_area ) {
2015-10-08 20:00:40 +02:00
area_moved_list . add ( p_area ) ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : area_remove_from_moved_list ( SelfList < GodotArea3D > * p_area ) {
2015-10-08 20:00:40 +02:00
area_moved_list . remove ( p_area ) ;
}
2021-10-18 21:24:30 +02:00
const SelfList < GodotArea3D > : : List & GodotSpace3D : : get_moved_area_list ( ) const {
2015-10-08 20:00:40 +02:00
return area_moved_list ;
}
2021-10-18 21:24:30 +02:00
const SelfList < GodotSoftBody3D > : : List & GodotSpace3D : : get_active_soft_body_list ( ) const {
2021-03-12 04:33:46 +01:00
return active_soft_body_list ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : soft_body_add_to_active_list ( SelfList < GodotSoftBody3D > * p_soft_body ) {
2021-03-12 04:33:46 +01:00
active_soft_body_list . add ( p_soft_body ) ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : soft_body_remove_from_active_list ( SelfList < GodotSoftBody3D > * p_soft_body ) {
2021-03-12 04:33:46 +01:00
active_soft_body_list . remove ( p_soft_body ) ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : call_queries ( ) {
2015-10-08 20:00:40 +02:00
while ( state_query_list . first ( ) ) {
2021-10-18 21:24:30 +02:00
GodotBody3D * b = state_query_list . first ( ) - > self ( ) ;
2015-10-08 20:00:40 +02:00
state_query_list . remove ( state_query_list . first ( ) ) ;
2017-11-10 13:21:33 +01:00
b - > call_queries ( ) ;
2015-10-08 20:00:40 +02:00
}
while ( monitor_query_list . first ( ) ) {
2021-10-18 21:24:30 +02:00
GodotArea3D * a = monitor_query_list . first ( ) - > self ( ) ;
2015-10-08 20:00:40 +02:00
monitor_query_list . remove ( monitor_query_list . first ( ) ) ;
2017-11-10 13:21:33 +01:00
a - > call_queries ( ) ;
2015-10-08 20:00:40 +02:00
}
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : setup ( ) {
2015-10-08 20:00:40 +02:00
contact_debug_count = 0 ;
2021-06-11 02:37:19 +02:00
while ( mass_properties_update_list . first ( ) ) {
mass_properties_update_list . first ( ) - > self ( ) - > update_mass_properties ( ) ;
mass_properties_update_list . remove ( mass_properties_update_list . first ( ) ) ;
2015-10-08 20:00:40 +02:00
}
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : update ( ) {
2015-10-08 20:00:40 +02:00
broadphase - > update ( ) ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : set_param ( PhysicsServer3D : : SpaceParameter p_param , real_t p_value ) {
2015-10-08 20:00:40 +02:00
switch ( p_param ) {
2020-05-10 13:00:47 +02:00
case PhysicsServer3D : : SPACE_PARAM_CONTACT_RECYCLE_RADIUS :
contact_recycle_radius = p_value ;
break ;
case PhysicsServer3D : : SPACE_PARAM_CONTACT_MAX_SEPARATION :
contact_max_separation = p_value ;
break ;
2021-12-03 18:38:40 +01:00
case PhysicsServer3D : : SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION :
2020-05-10 13:00:47 +02:00
contact_max_allowed_penetration = p_value ;
break ;
2021-12-03 18:38:40 +01:00
case PhysicsServer3D : : SPACE_PARAM_CONTACT_DEFAULT_BIAS :
contact_bias = p_value ;
break ;
2020-05-10 13:00:47 +02:00
case PhysicsServer3D : : SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD :
body_linear_velocity_sleep_threshold = p_value ;
break ;
case PhysicsServer3D : : SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD :
body_angular_velocity_sleep_threshold = p_value ;
break ;
case PhysicsServer3D : : SPACE_PARAM_BODY_TIME_TO_SLEEP :
body_time_to_sleep = p_value ;
break ;
2021-12-03 18:38:40 +01:00
case PhysicsServer3D : : SPACE_PARAM_SOLVER_ITERATIONS :
solver_iterations = p_value ;
break ;
2015-10-08 20:00:40 +02:00
}
}
2021-10-18 21:24:30 +02:00
real_t GodotSpace3D : : get_param ( PhysicsServer3D : : SpaceParameter p_param ) const {
2015-10-08 20:00:40 +02:00
switch ( p_param ) {
2020-05-10 13:00:47 +02:00
case PhysicsServer3D : : SPACE_PARAM_CONTACT_RECYCLE_RADIUS :
return contact_recycle_radius ;
case PhysicsServer3D : : SPACE_PARAM_CONTACT_MAX_SEPARATION :
return contact_max_separation ;
2021-12-03 18:38:40 +01:00
case PhysicsServer3D : : SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION :
2020-05-10 13:00:47 +02:00
return contact_max_allowed_penetration ;
2021-12-03 18:38:40 +01:00
case PhysicsServer3D : : SPACE_PARAM_CONTACT_DEFAULT_BIAS :
return contact_bias ;
2020-05-10 13:00:47 +02:00
case PhysicsServer3D : : SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD :
return body_linear_velocity_sleep_threshold ;
case PhysicsServer3D : : SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD :
return body_angular_velocity_sleep_threshold ;
case PhysicsServer3D : : SPACE_PARAM_BODY_TIME_TO_SLEEP :
return body_time_to_sleep ;
2021-12-03 18:38:40 +01:00
case PhysicsServer3D : : SPACE_PARAM_SOLVER_ITERATIONS :
return solver_iterations ;
2015-10-08 20:00:40 +02:00
}
return 0 ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : lock ( ) {
2015-10-08 20:00:40 +02:00
locked = true ;
}
2021-10-18 21:24:30 +02:00
void GodotSpace3D : : unlock ( ) {
2015-10-08 20:00:40 +02:00
locked = false ;
}
2021-10-18 21:24:30 +02:00
bool GodotSpace3D : : is_locked ( ) const {
2015-10-08 20:00:40 +02:00
return locked ;
}
2021-10-18 21:24:30 +02:00
GodotPhysicsDirectSpaceState3D * GodotSpace3D : : get_direct_state ( ) {
2015-10-08 20:00:40 +02:00
return direct_access ;
}
2021-10-18 21:24:30 +02:00
GodotSpace3D : : GodotSpace3D ( ) {
2023-01-31 00:01:21 +01:00
body_linear_velocity_sleep_threshold = GLOBAL_GET ( " physics/3d/sleep_threshold_linear " ) ;
body_angular_velocity_sleep_threshold = GLOBAL_GET ( " physics/3d/sleep_threshold_angular " ) ;
body_time_to_sleep = GLOBAL_GET ( " physics/3d/time_before_sleep " ) ;
solver_iterations = GLOBAL_GET ( " physics/3d/solver/solver_iterations " ) ;
contact_recycle_radius = GLOBAL_GET ( " physics/3d/solver/contact_recycle_radius " ) ;
contact_max_separation = GLOBAL_GET ( " physics/3d/solver/contact_max_separation " ) ;
contact_max_allowed_penetration = GLOBAL_GET ( " physics/3d/solver/contact_max_allowed_penetration " ) ;
contact_bias = GLOBAL_GET ( " physics/3d/solver/default_contact_bias " ) ;
2015-10-08 20:00:40 +02:00
2021-10-18 21:24:30 +02:00
broadphase = GodotBroadPhase3D : : create_func ( ) ;
2015-10-08 20:00:40 +02:00
broadphase - > set_pair_callback ( _broadphase_pair , this ) ;
broadphase - > set_unpair_callback ( _broadphase_unpair , this ) ;
2021-10-18 21:24:30 +02:00
direct_access = memnew ( GodotPhysicsDirectSpaceState3D ) ;
2015-10-08 20:00:40 +02:00
direct_access - > space = this ;
}
2021-10-18 21:24:30 +02:00
GodotSpace3D : : ~ GodotSpace3D ( ) {
2015-10-08 20:00:40 +02:00
memdelete ( broadphase ) ;
memdelete ( direct_access ) ;
}