2014-02-10 01:10:30 +00:00
/*************************************************************************/
2020-03-28 12:19:05 +00:00
/* physics_server_2d_sw.cpp */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 12:16:55 +00:00
/* https://godotengine.org */
2014-02-10 01:10:30 +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). */
2014-02-10 01:10:30 +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. */
/*************************************************************************/
2018-01-04 23:50:27 +00:00
2020-03-27 18:21:27 +00:00
# include "physics_server_2d_sw.h"
2020-03-28 12:19:05 +00:00
2021-05-10 21:43:13 +00:00
# include "broad_phase_2d_bvh.h"
2014-02-19 14:57:14 +00:00
# include "collision_solver_2d_sw.h"
2020-11-07 22:33:38 +00:00
# include "core/config/project_settings.h"
2020-02-27 02:30:20 +00:00
# include "core/debugger/engine_debugger.h"
2018-09-11 16:13:45 +00:00
# include "core/os/os.h"
2016-05-22 00:18:16 +00:00
2019-08-08 20:08:27 +00:00
# define FLUSH_QUERY_CHECK(m_object) \
ERR_FAIL_COND_MSG ( m_object - > get_space ( ) & & flushing_queries , " Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead. " ) ;
2018-11-16 11:49:26 +00:00
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : _shape_create ( ShapeType p_shape ) {
2020-04-01 23:20:12 +00:00
Shape2DSW * shape = nullptr ;
2017-03-05 15:44:50 +00:00
switch ( p_shape ) {
2014-02-10 01:10:30 +00:00
case SHAPE_LINE : {
2017-03-05 15:44:50 +00:00
shape = memnew ( LineShape2DSW ) ;
2014-02-10 01:10:30 +00:00
} break ;
case SHAPE_SEGMENT : {
2017-03-05 15:44:50 +00:00
shape = memnew ( SegmentShape2DSW ) ;
2014-02-10 01:10:30 +00:00
} break ;
case SHAPE_CIRCLE : {
2017-03-05 15:44:50 +00:00
shape = memnew ( CircleShape2DSW ) ;
2014-02-10 01:10:30 +00:00
} break ;
case SHAPE_RECTANGLE : {
2017-03-05 15:44:50 +00:00
shape = memnew ( RectangleShape2DSW ) ;
2014-02-10 01:10:30 +00:00
} break ;
case SHAPE_CAPSULE : {
2017-03-05 15:44:50 +00:00
shape = memnew ( CapsuleShape2DSW ) ;
2014-02-10 01:10:30 +00:00
} break ;
case SHAPE_CONVEX_POLYGON : {
2017-03-05 15:44:50 +00:00
shape = memnew ( ConvexPolygonShape2DSW ) ;
2014-02-10 01:10:30 +00:00
} break ;
case SHAPE_CONCAVE_POLYGON : {
2017-03-05 15:44:50 +00:00
shape = memnew ( ConcavePolygonShape2DSW ) ;
2014-02-10 01:10:30 +00:00
} break ;
case SHAPE_CUSTOM : {
ERR_FAIL_V ( RID ( ) ) ;
} break ;
}
RID id = shape_owner . make_rid ( shape ) ;
shape - > set_self ( id ) ;
return id ;
2017-11-10 02:34:01 +00:00
}
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : line_shape_create ( ) {
2017-11-10 02:34:01 +00:00
return _shape_create ( SHAPE_LINE ) ;
}
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : segment_shape_create ( ) {
2017-11-10 02:34:01 +00:00
return _shape_create ( SHAPE_SEGMENT ) ;
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : circle_shape_create ( ) {
2017-11-10 02:34:01 +00:00
return _shape_create ( SHAPE_CIRCLE ) ;
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : rectangle_shape_create ( ) {
2017-11-10 02:34:01 +00:00
return _shape_create ( SHAPE_RECTANGLE ) ;
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : capsule_shape_create ( ) {
2017-11-10 02:34:01 +00:00
return _shape_create ( SHAPE_CAPSULE ) ;
}
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : convex_polygon_shape_create ( ) {
2017-11-10 02:34:01 +00:00
return _shape_create ( SHAPE_CONVEX_POLYGON ) ;
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : concave_polygon_shape_create ( ) {
2017-11-10 02:34:01 +00:00
return _shape_create ( SHAPE_CONCAVE_POLYGON ) ;
}
2014-02-10 01:10:30 +00:00
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : shape_set_data ( RID p_shape , const Variant & p_data ) {
2019-06-10 15:38:51 +00:00
Shape2DSW * shape = shape_owner . getornull ( p_shape ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! shape ) ;
shape - > set_data ( p_data ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : shape_set_custom_solver_bias ( RID p_shape , real_t p_bias ) {
2019-06-10 15:38:51 +00:00
Shape2DSW * shape = shape_owner . getornull ( p_shape ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! shape ) ;
shape - > set_custom_bias ( p_bias ) ;
}
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : ShapeType PhysicsServer2DSW : : shape_get_type ( RID p_shape ) const {
2019-06-10 15:38:51 +00:00
const Shape2DSW * shape = shape_owner . getornull ( p_shape ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! shape , SHAPE_CUSTOM ) ;
2014-02-10 01:10:30 +00:00
return shape - > get_type ( ) ;
} ;
2020-03-27 18:21:27 +00:00
Variant PhysicsServer2DSW : : shape_get_data ( RID p_shape ) const {
2019-06-10 15:38:51 +00:00
const Shape2DSW * shape = shape_owner . getornull ( p_shape ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! shape , Variant ( ) ) ;
ERR_FAIL_COND_V ( ! shape - > is_configured ( ) , Variant ( ) ) ;
2014-02-10 01:10:30 +00:00
return shape - > get_data ( ) ;
} ;
2020-03-27 18:21:27 +00:00
real_t PhysicsServer2DSW : : shape_get_custom_solver_bias ( RID p_shape ) const {
2019-06-10 15:38:51 +00:00
const Shape2DSW * shape = shape_owner . getornull ( p_shape ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! shape , 0 ) ;
2014-02-10 01:10:30 +00:00
return shape - > get_custom_bias ( ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : _shape_col_cbk ( const Vector2 & p_point_A , const Vector2 & p_point_B , void * p_userdata ) {
2017-03-05 15:44:50 +00:00
CollCbkData * cbk = ( CollCbkData * ) p_userdata ;
2014-02-22 23:28:19 +00:00
2020-05-14 14:41:43 +00:00
if ( cbk - > max = = 0 ) {
2014-02-22 23:28:19 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2014-02-22 23:28:19 +00:00
2020-10-05 13:55:11 +00:00
Vector2 rel_dir = ( p_point_A - p_point_B ) ;
real_t rel_length2 = rel_dir . length_squared ( ) ;
2017-03-05 15:44:50 +00:00
if ( cbk - > valid_dir ! = Vector2 ( ) ) {
2020-10-05 13:55:11 +00:00
if ( cbk - > valid_depth < 10e20 ) {
if ( rel_length2 > cbk - > valid_depth * cbk - > valid_depth | |
( rel_length2 > CMP_EPSILON & & cbk - > valid_dir . dot ( rel_dir . normalized ( ) ) < CMP_EPSILON ) ) {
cbk - > invalid_by_dir + + ;
return ;
}
} else {
if ( rel_length2 > 0 & & cbk - > valid_dir . dot ( rel_dir . normalized ( ) ) < CMP_EPSILON ) {
return ;
}
2015-01-14 00:19:11 +00:00
}
}
2014-02-19 14:57:14 +00:00
if ( cbk - > amount = = cbk - > max ) {
//find least deep
2017-03-05 15:44:50 +00:00
real_t min_depth = 1e20 ;
int min_depth_idx = 0 ;
for ( int i = 0 ; i < cbk - > amount ; i + + ) {
real_t d = cbk - > ptr [ i * 2 + 0 ] . distance_squared_to ( cbk - > ptr [ i * 2 + 1 ] ) ;
if ( d < min_depth ) {
min_depth = d ;
min_depth_idx = i ;
2014-02-19 14:57:14 +00:00
}
}
2020-10-05 13:55:11 +00:00
if ( rel_length2 < min_depth ) {
2014-02-19 14:57:14 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2017-03-05 15:44:50 +00:00
cbk - > ptr [ min_depth_idx * 2 + 0 ] = p_point_A ;
cbk - > ptr [ min_depth_idx * 2 + 1 ] = p_point_B ;
2019-01-18 17:15:05 +00:00
cbk - > passed + + ;
2014-02-19 14:57:14 +00:00
} else {
2017-03-05 15:44:50 +00:00
cbk - > ptr [ cbk - > amount * 2 + 0 ] = p_point_A ;
cbk - > ptr [ cbk - > amount * 2 + 1 ] = p_point_B ;
2014-02-22 23:28:19 +00:00
cbk - > amount + + ;
2019-01-18 17:15:05 +00:00
cbk - > passed + + ;
2014-02-19 14:57:14 +00:00
}
}
2020-03-27 18:21:27 +00:00
bool PhysicsServer2DSW : : shape_collide ( RID p_shape_A , const Transform2D & p_xform_A , const Vector2 & p_motion_A , RID p_shape_B , const Transform2D & p_xform_B , const Vector2 & p_motion_B , Vector2 * r_results , int p_result_max , int & r_result_count ) {
2019-06-10 15:38:51 +00:00
Shape2DSW * shape_A = shape_owner . getornull ( p_shape_A ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! shape_A , false ) ;
2019-06-10 15:38:51 +00:00
Shape2DSW * shape_B = shape_owner . getornull ( p_shape_B ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! shape_B , false ) ;
2014-02-19 14:57:14 +00:00
2017-03-05 15:44:50 +00:00
if ( p_result_max = = 0 ) {
2020-04-01 23:20:12 +00:00
return CollisionSolver2DSW : : solve ( shape_A , p_xform_A , p_motion_A , shape_B , p_xform_B , p_motion_B , nullptr , nullptr ) ;
2014-02-19 14:57:14 +00:00
}
CollCbkData cbk ;
2017-03-05 15:44:50 +00:00
cbk . max = p_result_max ;
cbk . amount = 0 ;
2019-01-18 17:15:05 +00:00
cbk . passed = 0 ;
2017-03-05 15:44:50 +00:00
cbk . ptr = r_results ;
2014-02-19 14:57:14 +00:00
2017-03-05 15:44:50 +00:00
bool res = CollisionSolver2DSW : : solve ( shape_A , p_xform_A , p_motion_A , shape_B , p_xform_B , p_motion_B , _shape_col_cbk , & cbk ) ;
r_result_count = cbk . amount ;
2014-02-19 14:57:14 +00:00
return res ;
}
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : space_create ( ) {
2017-03-05 15:44:50 +00:00
Space2DSW * space = memnew ( Space2DSW ) ;
2014-02-10 01:10:30 +00:00
RID id = space_owner . make_rid ( space ) ;
space - > set_self ( id ) ;
RID area_id = area_create ( ) ;
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( area_id ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! area , RID ( ) ) ;
2014-02-10 01:10:30 +00:00
space - > set_default_area ( area ) ;
area - > set_space ( space ) ;
area - > set_priority ( - 1 ) ;
return id ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : space_set_active ( RID p_space , bool p_active ) {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_space ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! space ) ;
2020-05-14 14:41:43 +00:00
if ( p_active ) {
2014-02-10 01:10:30 +00:00
active_spaces . insert ( space ) ;
2020-05-14 14:41:43 +00:00
} else {
2014-02-10 01:10:30 +00:00
active_spaces . erase ( space ) ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
bool PhysicsServer2DSW : : space_is_active ( RID p_space ) const {
2019-06-10 15:38:51 +00:00
const Space2DSW * space = space_owner . getornull ( p_space ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! space , false ) ;
2014-02-10 01:10:30 +00:00
return active_spaces . has ( space ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : space_set_param ( RID p_space , SpaceParameter p_param , real_t p_value ) {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_space ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! space ) ;
2017-03-05 15:44:50 +00:00
space - > set_param ( p_param , p_value ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
real_t PhysicsServer2DSW : : space_get_param ( RID p_space , SpaceParameter p_param ) const {
2019-06-10 15:38:51 +00:00
const Space2DSW * space = space_owner . getornull ( p_space ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! space , 0 ) ;
2014-02-10 01:10:30 +00:00
return space - > get_param ( p_param ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : space_set_debug_contacts ( RID p_space , int p_max_contacts ) {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_space ) ;
2015-09-20 16:03:46 +00:00
ERR_FAIL_COND ( ! space ) ;
space - > set_debug_contacts ( p_max_contacts ) ;
}
2020-03-27 18:21:27 +00:00
Vector < Vector2 > PhysicsServer2DSW : : space_get_contacts ( RID p_space ) const {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_space ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! space , Vector < Vector2 > ( ) ) ;
2015-09-20 16:03:46 +00:00
return space - > get_debug_contacts ( ) ;
}
2020-03-27 18:21:27 +00:00
int PhysicsServer2DSW : : space_get_contact_count ( RID p_space ) const {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_space ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! space , 0 ) ;
2015-09-20 16:03:46 +00:00
return space - > get_debug_contact_count ( ) ;
}
2020-03-27 18:21:27 +00:00
PhysicsDirectSpaceState2D * PhysicsServer2DSW : : space_get_direct_state ( RID p_space ) {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_space ) ;
2020-04-01 23:20:12 +00:00
ERR_FAIL_COND_V ( ! space , nullptr ) ;
ERR_FAIL_COND_V_MSG ( ( using_threads & & ! doing_sync ) | | space - > is_locked ( ) , nullptr , " Space state is inaccessible right now, wait for iteration or physics process notification. " ) ;
2014-02-10 01:10:30 +00:00
return space - > get_direct_state ( ) ;
}
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : area_create ( ) {
2017-03-05 15:44:50 +00:00
Area2DSW * area = memnew ( Area2DSW ) ;
2014-02-10 01:10:30 +00:00
RID rid = area_owner . make_rid ( area ) ;
area - > set_self ( rid ) ;
return rid ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_space ( RID p_area , RID p_space ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! area ) ;
2017-07-25 02:18:03 +00:00
2020-04-01 23:20:12 +00:00
Space2DSW * space = nullptr ;
2014-02-10 01:10:30 +00:00
if ( p_space . is_valid ( ) ) {
2019-06-10 15:38:51 +00:00
space = space_owner . getornull ( p_space ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! space ) ;
}
2020-05-14 14:41:43 +00:00
if ( area - > get_space ( ) = = space ) {
2017-07-25 02:18:03 +00:00
return ; //pointless
2020-05-14 14:41:43 +00:00
}
2017-07-25 02:18:03 +00:00
2017-07-25 02:26:47 +00:00
area - > clear_constraints ( ) ;
2014-02-10 01:10:30 +00:00
area - > set_space ( space ) ;
} ;
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : area_get_space ( RID p_area ) const {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! area , RID ( ) ) ;
2014-02-10 01:10:30 +00:00
Space2DSW * space = area - > get_space ( ) ;
2020-05-14 14:41:43 +00:00
if ( ! space ) {
2014-02-10 01:10:30 +00:00
return RID ( ) ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
return space - > get_self ( ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_space_override_mode ( RID p_area , AreaSpaceOverrideMode p_mode ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! area ) ;
area - > set_space_override_mode ( p_mode ) ;
}
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : AreaSpaceOverrideMode PhysicsServer2DSW : : area_get_space_override_mode ( RID p_area ) const {
2019-06-10 15:38:51 +00:00
const Area2DSW * area = area_owner . getornull ( p_area ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! area , AREA_SPACE_OVERRIDE_DISABLED ) ;
2014-02-10 01:10:30 +00:00
return area - > get_space_override_mode ( ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_add_shape ( RID p_area , RID p_shape , const Transform2D & p_transform , bool p_disabled ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! area ) ;
2019-06-10 15:38:51 +00:00
Shape2DSW * shape = shape_owner . getornull ( p_shape ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! shape ) ;
2019-03-24 09:38:31 +00:00
area - > add_shape ( shape , p_transform , p_disabled ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_shape ( RID p_area , int p_shape_idx , RID p_shape ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! area ) ;
2019-06-10 15:38:51 +00:00
Shape2DSW * shape = shape_owner . getornull ( p_shape ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! shape ) ;
ERR_FAIL_COND ( ! shape - > is_configured ( ) ) ;
2017-03-05 15:44:50 +00:00
area - > set_shape ( p_shape_idx , shape ) ;
2014-02-10 01:10:30 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_shape_transform ( RID p_area , int p_shape_idx , const Transform2D & p_transform ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! area ) ;
2017-03-05 15:44:50 +00:00
area - > set_shape_transform ( p_shape_idx , p_transform ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_shape_disabled ( RID p_area , int p_shape , bool p_disabled ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2017-06-24 02:30:43 +00:00
ERR_FAIL_COND ( ! area ) ;
ERR_FAIL_INDEX ( p_shape , area - > get_shape_count ( ) ) ;
2019-03-02 11:48:13 +00:00
FLUSH_QUERY_CHECK ( area ) ;
2021-06-22 23:36:43 +00:00
area - > set_shape_disabled ( p_shape , p_disabled ) ;
2017-06-24 02:30:43 +00:00
}
2020-03-27 18:21:27 +00:00
int PhysicsServer2DSW : : area_get_shape_count ( RID p_area ) const {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! area , - 1 ) ;
2014-02-10 01:10:30 +00:00
return area - > get_shape_count ( ) ;
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : area_get_shape ( RID p_area , int p_shape_idx ) const {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! area , RID ( ) ) ;
2014-02-10 01:10:30 +00:00
Shape2DSW * shape = area - > get_shape ( p_shape_idx ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! shape , RID ( ) ) ;
2014-02-10 01:10:30 +00:00
return shape - > get_self ( ) ;
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
Transform2D PhysicsServer2DSW : : area_get_shape_transform ( RID p_area , int p_shape_idx ) const {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! area , Transform2D ( ) ) ;
2014-02-10 01:10:30 +00:00
return area - > get_shape_transform ( p_shape_idx ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_remove_shape ( RID p_area , int p_shape_idx ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! area ) ;
area - > remove_shape ( p_shape_idx ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_clear_shapes ( RID p_area ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! area ) ;
2020-05-14 14:41:43 +00:00
while ( area - > get_shape_count ( ) ) {
2014-02-10 01:10:30 +00:00
area - > remove_shape ( 0 ) ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_attach_object_instance_id ( RID p_area , ObjectID p_id ) {
2014-02-10 01:10:30 +00:00
if ( space_owner . owns ( p_area ) ) {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_area ) ;
2017-03-05 15:44:50 +00:00
p_area = space - > get_default_area ( ) - > get_self ( ) ;
2014-02-10 01:10:30 +00:00
}
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! area ) ;
2019-05-09 09:21:49 +00:00
area - > set_instance_id ( p_id ) ;
2014-02-10 01:10:30 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
ObjectID PhysicsServer2DSW : : area_get_object_instance_id ( RID p_area ) const {
2014-02-10 01:10:30 +00:00
if ( space_owner . owns ( p_area ) ) {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_area ) ;
2017-03-05 15:44:50 +00:00
p_area = space - > get_default_area ( ) - > get_self ( ) ;
2014-02-10 01:10:30 +00:00
}
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2020-02-12 17:24:06 +00:00
ERR_FAIL_COND_V ( ! area , ObjectID ( ) ) ;
2014-02-10 01:10:30 +00:00
return area - > get_instance_id ( ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_attach_canvas_instance_id ( RID p_area , ObjectID p_id ) {
2018-08-24 22:03:26 +00:00
if ( space_owner . owns ( p_area ) ) {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_area ) ;
2018-08-24 22:03:26 +00:00
p_area = space - > get_default_area ( ) - > get_self ( ) ;
}
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2018-08-24 22:03:26 +00:00
ERR_FAIL_COND ( ! area ) ;
2019-05-09 09:21:49 +00:00
area - > set_canvas_instance_id ( p_id ) ;
2018-08-24 22:03:26 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
ObjectID PhysicsServer2DSW : : area_get_canvas_instance_id ( RID p_area ) const {
2018-08-24 22:03:26 +00:00
if ( space_owner . owns ( p_area ) ) {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_area ) ;
2018-08-24 22:03:26 +00:00
p_area = space - > get_default_area ( ) - > get_self ( ) ;
}
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2020-02-12 17:24:06 +00:00
ERR_FAIL_COND_V ( ! area , ObjectID ( ) ) ;
2018-08-24 22:03:26 +00:00
return area - > get_canvas_instance_id ( ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_param ( RID p_area , AreaParameter p_param , const Variant & p_value ) {
2014-02-10 01:10:30 +00:00
if ( space_owner . owns ( p_area ) ) {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_area ) ;
2017-03-05 15:44:50 +00:00
p_area = space - > get_default_area ( ) - > get_self ( ) ;
2014-02-10 01:10:30 +00:00
}
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! area ) ;
2017-03-05 15:44:50 +00:00
area - > set_param ( p_param , p_value ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_transform ( RID p_area , const Transform2D & p_transform ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! area ) ;
area - > set_transform ( p_transform ) ;
} ;
2020-03-27 18:21:27 +00:00
Variant PhysicsServer2DSW : : area_get_param ( RID p_area , AreaParameter p_param ) const {
2014-02-10 01:10:30 +00:00
if ( space_owner . owns ( p_area ) ) {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_area ) ;
2017-03-05 15:44:50 +00:00
p_area = space - > get_default_area ( ) - > get_self ( ) ;
2014-02-10 01:10:30 +00:00
}
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! area , Variant ( ) ) ;
2014-02-10 01:10:30 +00:00
return area - > get_param ( p_param ) ;
} ;
2020-03-27 18:21:27 +00:00
Transform2D PhysicsServer2DSW : : area_get_transform ( RID p_area ) const {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! area , Transform2D ( ) ) ;
2014-02-10 01:10:30 +00:00
return area - > get_transform ( ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_pickable ( RID p_area , bool p_pickable ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2015-03-22 04:46:18 +00:00
ERR_FAIL_COND ( ! area ) ;
area - > set_pickable ( p_pickable ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_monitorable ( RID p_area , bool p_monitorable ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2015-03-17 03:45:25 +00:00
ERR_FAIL_COND ( ! area ) ;
2019-03-02 11:48:13 +00:00
FLUSH_QUERY_CHECK ( area ) ;
2015-03-17 03:45:25 +00:00
area - > set_monitorable ( p_monitorable ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_collision_mask ( RID p_area , uint32_t p_mask ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2015-05-03 19:47:21 +00:00
ERR_FAIL_COND ( ! area ) ;
area - > set_collision_mask ( p_mask ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_collision_layer ( RID p_area , uint32_t p_layer ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2015-05-03 19:47:21 +00:00
ERR_FAIL_COND ( ! area ) ;
2017-06-13 15:45:01 +00:00
area - > set_collision_layer ( p_layer ) ;
2015-05-03 19:47:21 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_monitor_callback ( RID p_area , Object * p_receiver , const StringName & p_method ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! area ) ;
2020-02-12 17:24:06 +00:00
area - > set_monitor_callback ( p_receiver ? p_receiver - > get_instance_id ( ) : ObjectID ( ) , p_method ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : area_set_area_monitor_callback ( RID p_area , Object * p_receiver , const StringName & p_method ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_area ) ;
2015-03-17 03:45:25 +00:00
ERR_FAIL_COND ( ! area ) ;
2020-02-12 17:24:06 +00:00
area - > set_area_monitor_callback ( p_receiver ? p_receiver - > get_instance_id ( ) : ObjectID ( ) , p_method ) ;
2015-03-17 03:45:25 +00:00
}
2014-02-10 01:10:30 +00:00
/* BODY API */
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : body_create ( ) {
2017-03-05 15:44:50 +00:00
Body2DSW * body = memnew ( Body2DSW ) ;
2014-02-10 01:10:30 +00:00
RID rid = body_owner . make_rid ( body ) ;
body - > set_self ( rid ) ;
return rid ;
2017-11-10 02:34:01 +00:00
}
2014-02-10 01:10:30 +00:00
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_space ( RID p_body , RID p_space ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2020-04-01 23:20:12 +00:00
Space2DSW * space = nullptr ;
2014-02-10 01:10:30 +00:00
if ( p_space . is_valid ( ) ) {
2019-06-10 15:38:51 +00:00
space = space_owner . getornull ( p_space ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! space ) ;
}
2020-05-14 14:41:43 +00:00
if ( body - > get_space ( ) = = space ) {
2017-07-25 02:18:03 +00:00
return ; //pointless
2020-05-14 14:41:43 +00:00
}
2017-07-25 02:18:03 +00:00
2020-12-05 04:12:47 +00:00
body - > clear_constraint_list ( ) ;
2014-02-10 01:10:30 +00:00
body - > set_space ( space ) ;
} ;
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : body_get_space ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , RID ( ) ) ;
2014-02-10 01:10:30 +00:00
Space2DSW * space = body - > get_space ( ) ;
2020-05-14 14:41:43 +00:00
if ( ! space ) {
2014-02-10 01:10:30 +00:00
return RID ( ) ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
return space - > get_self ( ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_mode ( RID p_body , BodyMode p_mode ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2019-03-02 11:48:13 +00:00
FLUSH_QUERY_CHECK ( body ) ;
2014-02-10 01:10:30 +00:00
body - > set_mode ( p_mode ) ;
} ;
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : BodyMode PhysicsServer2DSW : : body_get_mode ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , BODY_MODE_STATIC ) ;
2014-02-10 01:10:30 +00:00
return body - > get_mode ( ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_add_shape ( RID p_body , RID p_shape , const Transform2D & p_transform , bool p_disabled ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2019-06-10 15:38:51 +00:00
Shape2DSW * shape = shape_owner . getornull ( p_shape ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! shape ) ;
2019-03-24 09:38:31 +00:00
body - > add_shape ( shape , p_transform , p_disabled ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_shape ( RID p_body , int p_shape_idx , RID p_shape ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2019-06-10 15:38:51 +00:00
Shape2DSW * shape = shape_owner . getornull ( p_shape ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! shape ) ;
ERR_FAIL_COND ( ! shape - > is_configured ( ) ) ;
2017-03-05 15:44:50 +00:00
body - > set_shape ( p_shape_idx , shape ) ;
2014-02-10 01:10:30 +00:00
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_shape_transform ( RID p_body , int p_shape_idx , const Transform2D & p_transform ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2017-03-05 15:44:50 +00:00
body - > set_shape_transform ( p_shape_idx , p_transform ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_shape_metadata ( RID p_body , int p_shape_idx , const Variant & p_metadata ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-10-16 03:06:34 +00:00
ERR_FAIL_COND ( ! body ) ;
2017-03-05 15:44:50 +00:00
body - > set_shape_metadata ( p_shape_idx , p_metadata ) ;
2014-10-16 03:06:34 +00:00
}
2020-03-27 18:21:27 +00:00
Variant PhysicsServer2DSW : : body_get_shape_metadata ( RID p_body , int p_shape_idx ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , Variant ( ) ) ;
2014-10-16 03:06:34 +00:00
return body - > get_shape_metadata ( p_shape_idx ) ;
}
2020-03-27 18:21:27 +00:00
int PhysicsServer2DSW : : body_get_shape_count ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , - 1 ) ;
2014-02-10 01:10:30 +00:00
return body - > get_shape_count ( ) ;
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
RID PhysicsServer2DSW : : body_get_shape ( RID p_body , int p_shape_idx ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , RID ( ) ) ;
2014-02-10 01:10:30 +00:00
Shape2DSW * shape = body - > get_shape ( p_shape_idx ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! shape , RID ( ) ) ;
2014-02-10 01:10:30 +00:00
return shape - > get_self ( ) ;
}
2020-05-14 12:29:06 +00:00
2020-03-27 18:21:27 +00:00
Transform2D PhysicsServer2DSW : : body_get_shape_transform ( RID p_body , int p_shape_idx ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , Transform2D ( ) ) ;
2014-02-10 01:10:30 +00:00
return body - > get_shape_transform ( p_shape_idx ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_remove_shape ( RID p_body , int p_shape_idx ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
body - > remove_shape ( p_shape_idx ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_clear_shapes ( RID p_body ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2020-05-14 14:41:43 +00:00
while ( body - > get_shape_count ( ) ) {
2014-02-10 01:10:30 +00:00
body - > remove_shape ( 0 ) ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_shape_disabled ( RID p_body , int p_shape_idx , bool p_disabled ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_INDEX ( p_shape_idx , body - > get_shape_count ( ) ) ;
2019-03-02 11:48:13 +00:00
FLUSH_QUERY_CHECK ( body ) ;
2014-02-10 01:10:30 +00:00
2021-06-22 23:36:43 +00:00
body - > set_shape_disabled ( p_shape_idx , p_disabled ) ;
2014-02-10 01:10:30 +00:00
}
2020-05-14 12:29:06 +00:00
2021-01-28 06:34:26 +00:00
void PhysicsServer2DSW : : body_set_shape_as_one_way_collision ( RID p_body , int p_shape_idx , bool p_enable , real_t p_margin ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-06-24 02:30:43 +00:00
ERR_FAIL_COND ( ! body ) ;
ERR_FAIL_INDEX ( p_shape_idx , body - > get_shape_count ( ) ) ;
2019-03-02 11:48:13 +00:00
FLUSH_QUERY_CHECK ( body ) ;
2014-02-10 01:10:30 +00:00
2019-01-18 17:15:05 +00:00
body - > set_shape_as_one_way_collision ( p_shape_idx , p_enable , p_margin ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_continuous_collision_detection_mode ( RID p_body , CCDMode p_mode ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2014-02-19 14:57:14 +00:00
body - > set_continuous_collision_detection_mode ( p_mode ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
PhysicsServer2DSW : : CCDMode PhysicsServer2DSW : : body_get_continuous_collision_detection_mode ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
const Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , CCD_MODE_DISABLED ) ;
2014-02-19 14:57:14 +00:00
return body - > get_continuous_collision_detection_mode ( ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_attach_object_instance_id ( RID p_body , ObjectID p_id ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2019-05-09 09:21:49 +00:00
body - > set_instance_id ( p_id ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
ObjectID PhysicsServer2DSW : : body_get_object_instance_id ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2020-02-12 17:24:06 +00:00
ERR_FAIL_COND_V ( ! body , ObjectID ( ) ) ;
2014-02-10 01:10:30 +00:00
return body - > get_instance_id ( ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_attach_canvas_instance_id ( RID p_body , ObjectID p_id ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2018-08-24 22:03:26 +00:00
ERR_FAIL_COND ( ! body ) ;
2019-05-09 09:21:49 +00:00
body - > set_canvas_instance_id ( p_id ) ;
2018-08-24 22:03:26 +00:00
} ;
2020-03-27 18:21:27 +00:00
ObjectID PhysicsServer2DSW : : body_get_canvas_instance_id ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2020-02-12 17:24:06 +00:00
ERR_FAIL_COND_V ( ! body , ObjectID ( ) ) ;
2018-08-24 22:03:26 +00:00
return body - > get_canvas_instance_id ( ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_collision_layer ( RID p_body , uint32_t p_layer ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-05-14 04:22:15 +00:00
ERR_FAIL_COND ( ! body ) ;
2017-08-11 19:10:05 +00:00
body - > set_collision_layer ( p_layer ) ;
2014-05-14 04:22:15 +00:00
} ;
2020-03-27 18:21:27 +00:00
uint32_t PhysicsServer2DSW : : body_get_collision_layer ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , 0 ) ;
2014-05-14 04:22:15 +00:00
2017-06-13 15:45:01 +00:00
return body - > get_collision_layer ( ) ;
2014-05-14 04:22:15 +00:00
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_collision_mask ( RID p_body , uint32_t p_mask ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2017-08-11 19:10:05 +00:00
body - > set_collision_mask ( p_mask ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
uint32_t PhysicsServer2DSW : : body_get_collision_mask ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , 0 ) ;
2014-02-10 01:10:30 +00:00
2015-05-03 19:47:21 +00:00
return body - > get_collision_mask ( ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_param ( RID p_body , BodyParameter p_param , real_t p_value ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2017-03-05 15:44:50 +00:00
body - > set_param ( p_param , p_value ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
real_t PhysicsServer2DSW : : body_get_param ( RID p_body , BodyParameter p_param ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , 0 ) ;
2014-02-10 01:10:30 +00:00
return body - > get_param ( p_param ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_state ( RID p_body , BodyState p_state , const Variant & p_variant ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2017-03-05 15:44:50 +00:00
body - > set_state ( p_state , p_variant ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
Variant PhysicsServer2DSW : : body_get_state ( RID p_body , BodyState p_state ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , Variant ( ) ) ;
2014-02-10 01:10:30 +00:00
return body - > get_state ( p_state ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_applied_force ( RID p_body , const Vector2 & p_force ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
body - > set_applied_force ( p_force ) ;
2015-04-26 19:20:00 +00:00
body - > wakeup ( ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
Vector2 PhysicsServer2DSW : : body_get_applied_force ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , Vector2 ( ) ) ;
2014-02-10 01:10:30 +00:00
return body - > get_applied_force ( ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_applied_torque ( RID p_body , real_t p_torque ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
body - > set_applied_torque ( p_torque ) ;
2015-04-26 19:20:00 +00:00
body - > wakeup ( ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
real_t PhysicsServer2DSW : : body_get_applied_torque ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , 0 ) ;
2014-02-10 01:10:30 +00:00
return body - > get_applied_torque ( ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_apply_central_impulse ( RID p_body , const Vector2 & p_impulse ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2018-07-24 07:49:12 +00:00
ERR_FAIL_COND ( ! body ) ;
body - > apply_central_impulse ( p_impulse ) ;
body - > wakeup ( ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_apply_torque_impulse ( RID p_body , real_t p_torque ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2018-07-24 07:49:12 +00:00
ERR_FAIL_COND ( ! body ) ;
2019-06-21 11:33:01 +00:00
_update_shapes ( ) ;
2018-07-24 07:49:12 +00:00
body - > apply_torque_impulse ( p_torque ) ;
2020-08-01 12:36:24 +00:00
body - > wakeup ( ) ;
2018-07-24 07:49:12 +00:00
}
2020-03-26 04:23:34 +00:00
void PhysicsServer2DSW : : body_apply_impulse ( RID p_body , const Vector2 & p_impulse , const Vector2 & p_position ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2019-06-21 11:33:01 +00:00
_update_shapes ( ) ;
2020-03-26 04:23:34 +00:00
body - > apply_impulse ( p_impulse , p_position ) ;
2015-04-26 19:20:00 +00:00
body - > wakeup ( ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_add_central_force ( RID p_body , const Vector2 & p_force ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2018-07-24 07:49:12 +00:00
ERR_FAIL_COND ( ! body ) ;
body - > add_central_force ( p_force ) ;
body - > wakeup ( ) ;
} ;
2020-03-26 04:23:34 +00:00
void PhysicsServer2DSW : : body_add_force ( RID p_body , const Vector2 & p_force , const Vector2 & p_position ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2016-04-26 12:15:15 +00:00
ERR_FAIL_COND ( ! body ) ;
2020-03-26 04:23:34 +00:00
body - > add_force ( p_force , p_position ) ;
2018-07-24 07:49:12 +00:00
body - > wakeup ( ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_add_torque ( RID p_body , real_t p_torque ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2018-07-24 07:49:12 +00:00
ERR_FAIL_COND ( ! body ) ;
body - > add_torque ( p_torque ) ;
2016-04-26 12:15:15 +00:00
body - > wakeup ( ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_axis_velocity ( RID p_body , const Vector2 & p_axis_velocity ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2019-06-21 11:33:01 +00:00
_update_shapes ( ) ;
2014-02-10 01:10:30 +00:00
Vector2 v = body - > get_linear_velocity ( ) ;
Vector2 axis = p_axis_velocity . normalized ( ) ;
2017-03-05 15:44:50 +00:00
v - = axis * axis . dot ( v ) ;
v + = p_axis_velocity ;
2014-02-10 01:10:30 +00:00
body - > set_linear_velocity ( v ) ;
2015-04-26 19:20:00 +00:00
body - > wakeup ( ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_add_collision_exception ( RID p_body , RID p_body_b ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
body - > add_exception ( p_body_b ) ;
2015-04-26 19:20:00 +00:00
body - > wakeup ( ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_remove_collision_exception ( RID p_body , RID p_body_b ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2014-04-15 01:43:44 +00:00
body - > remove_exception ( p_body_b ) ;
2015-04-26 19:20:00 +00:00
body - > wakeup ( ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_get_collision_exceptions ( RID p_body , List < RID > * p_exceptions ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < body - > get_exceptions ( ) . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
p_exceptions - > push_back ( body - > get_exceptions ( ) [ i ] ) ;
}
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_contacts_reported_depth_threshold ( RID p_body , real_t p_threshold ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
} ;
2020-03-27 18:21:27 +00:00
real_t PhysicsServer2DSW : : body_get_contacts_reported_depth_threshold ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , 0 ) ;
2014-02-10 01:10:30 +00:00
return 0 ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_omit_force_integration ( RID p_body , bool p_omit ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
body - > set_omit_force_integration ( p_omit ) ;
} ;
2020-03-27 18:21:27 +00:00
bool PhysicsServer2DSW : : body_is_omitting_force_integration ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , false ) ;
2014-02-10 01:10:30 +00:00
return body - > get_omit_force_integration ( ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_max_contacts_reported ( RID p_body , int p_contacts ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
body - > set_max_contacts_reported ( p_contacts ) ;
}
2020-03-27 18:21:27 +00:00
int PhysicsServer2DSW : : body_get_max_contacts_reported ( RID p_body ) const {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , - 1 ) ;
2014-02-10 01:10:30 +00:00
return body - > get_max_contacts_reported ( ) ;
}
2021-03-30 06:22:23 +00:00
void PhysicsServer2DSW : : body_set_force_integration_callback ( RID p_body , const Callable & p_callable , const Variant & p_udata ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! body ) ;
2021-03-30 06:22:23 +00:00
body - > set_force_integration_callback ( p_callable , p_udata ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
bool PhysicsServer2DSW : : body_collide_shape ( RID p_body , int p_body_shape , RID p_shape , const Transform2D & p_shape_xform , const Vector2 & p_motion , Vector2 * r_results , int p_result_max , int & r_result_count ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , false ) ;
ERR_FAIL_INDEX_V ( p_body_shape , body - > get_shape_count ( ) , false ) ;
2014-02-19 14:57:14 +00:00
2017-03-05 15:44:50 +00:00
return shape_collide ( body - > get_shape ( p_body_shape ) - > get_self ( ) , body - > get_transform ( ) * body - > get_shape_transform ( p_body_shape ) , Vector2 ( ) , p_shape , p_shape_xform , p_motion , r_results , p_result_max , r_result_count ) ;
2014-02-19 14:57:14 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : body_set_pickable ( RID p_body , bool p_pickable ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2015-03-22 04:46:18 +00:00
ERR_FAIL_COND ( ! body ) ;
body - > set_pickable ( p_pickable ) ;
}
2014-02-10 01:10:30 +00:00
2021-08-10 18:48:19 +00:00
bool PhysicsServer2DSW : : body_test_motion ( RID p_body , const Transform2D & p_from , const Vector2 & p_motion , real_t p_margin , MotionResult * r_result , const Set < RID > & p_exclude ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! body , false ) ;
ERR_FAIL_COND_V ( ! body - > get_space ( ) , false ) ;
ERR_FAIL_COND_V ( body - > get_space ( ) - > is_locked ( ) , false ) ;
2015-04-19 23:50:55 +00:00
2019-06-21 11:33:01 +00:00
_update_shapes ( ) ;
2021-08-10 18:48:19 +00:00
return body - > get_space ( ) - > test_body_motion ( body , p_from , p_motion , p_margin , r_result , p_exclude ) ;
2015-04-19 23:50:55 +00:00
}
2020-03-27 18:21:27 +00:00
PhysicsDirectBodyState2D * PhysicsServer2DSW : : body_get_direct_state ( RID p_body ) {
2020-04-01 23:20:12 +00:00
ERR_FAIL_COND_V_MSG ( ( using_threads & & ! doing_sync ) , nullptr , " Body state is inaccessible right now, wait for iteration or physics process notification. " ) ;
2018-07-17 11:57:23 +00:00
2020-05-14 14:41:43 +00:00
if ( ! body_owner . owns ( p_body ) ) {
2020-04-01 23:20:12 +00:00
return nullptr ;
2020-05-14 14:41:43 +00:00
}
2018-07-17 11:57:23 +00:00
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_body ) ;
2020-04-01 23:20:12 +00:00
ERR_FAIL_COND_V ( ! body , nullptr ) ;
ERR_FAIL_COND_V ( ! body - > get_space ( ) , nullptr ) ;
ERR_FAIL_COND_V_MSG ( body - > get_space ( ) - > is_locked ( ) , nullptr , " Body state is inaccessible right now, wait for iteration or physics process notification. " ) ;
2017-09-29 15:33:30 +00:00
direct_state - > body = body ;
return direct_state ;
}
2014-02-10 01:10:30 +00:00
/* JOINT API */
2021-02-09 16:19:03 +00:00
RID PhysicsServer2DSW : : joint_create ( ) {
Joint2DSW * joint = memnew ( Joint2DSW ) ;
RID joint_rid = joint_owner . make_rid ( joint ) ;
joint - > set_self ( joint_rid ) ;
return joint_rid ;
}
void PhysicsServer2DSW : : joint_clear ( RID p_joint ) {
Joint2DSW * joint = joint_owner . getornull ( p_joint ) ;
if ( joint - > get_type ( ) ! = JOINT_TYPE_MAX ) {
Joint2DSW * empty_joint = memnew ( Joint2DSW ) ;
empty_joint - > copy_settings_from ( joint ) ;
joint_owner . replace ( p_joint , empty_joint ) ;
memdelete ( joint ) ;
}
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : joint_set_param ( RID p_joint , JointParam p_param , real_t p_value ) {
2019-06-10 15:38:51 +00:00
Joint2DSW * joint = joint_owner . getornull ( p_joint ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! joint ) ;
2017-03-05 15:44:50 +00:00
switch ( p_param ) {
2020-05-10 11:00:47 +00:00
case JOINT_PARAM_BIAS :
joint - > set_bias ( p_value ) ;
break ;
case JOINT_PARAM_MAX_BIAS :
joint - > set_max_bias ( p_value ) ;
break ;
case JOINT_PARAM_MAX_FORCE :
joint - > set_max_force ( p_value ) ;
break ;
2014-02-10 01:10:30 +00:00
}
}
2020-03-27 18:21:27 +00:00
real_t PhysicsServer2DSW : : joint_get_param ( RID p_joint , JointParam p_param ) const {
2019-06-10 15:38:51 +00:00
const Joint2DSW * joint = joint_owner . getornull ( p_joint ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! joint , - 1 ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
switch ( p_param ) {
2020-05-10 11:00:47 +00:00
case JOINT_PARAM_BIAS :
return joint - > get_bias ( ) ;
break ;
case JOINT_PARAM_MAX_BIAS :
return joint - > get_max_bias ( ) ;
break ;
case JOINT_PARAM_MAX_FORCE :
return joint - > get_max_force ( ) ;
break ;
2014-02-10 01:10:30 +00:00
}
return 0 ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : joint_disable_collisions_between_bodies ( RID p_joint , const bool p_disable ) {
2019-06-10 15:38:51 +00:00
Joint2DSW * joint = joint_owner . getornull ( p_joint ) ;
2018-02-05 17:20:26 +00:00
ERR_FAIL_COND ( ! joint ) ;
joint - > disable_collisions_between_bodies ( p_disable ) ;
if ( 2 = = joint - > get_body_count ( ) ) {
Body2DSW * body_a = * joint - > get_body_ptr ( ) ;
Body2DSW * body_b = * ( joint - > get_body_ptr ( ) + 1 ) ;
if ( p_disable ) {
body_add_collision_exception ( body_a - > get_self ( ) , body_b - > get_self ( ) ) ;
body_add_collision_exception ( body_b - > get_self ( ) , body_a - > get_self ( ) ) ;
} else {
body_remove_collision_exception ( body_a - > get_self ( ) , body_b - > get_self ( ) ) ;
body_remove_collision_exception ( body_b - > get_self ( ) , body_a - > get_self ( ) ) ;
}
}
}
2020-03-27 18:21:27 +00:00
bool PhysicsServer2DSW : : joint_is_disabled_collisions_between_bodies ( RID p_joint ) const {
2019-06-10 15:38:51 +00:00
const Joint2DSW * joint = joint_owner . getornull ( p_joint ) ;
2018-02-05 17:20:26 +00:00
ERR_FAIL_COND_V ( ! joint , true ) ;
return joint - > is_disabled_collisions_between_bodies ( ) ;
}
2021-02-09 16:19:03 +00:00
void PhysicsServer2DSW : : joint_make_pin ( RID p_joint , const Vector2 & p_pos , RID p_body_a , RID p_body_b ) {
2019-06-10 15:38:51 +00:00
Body2DSW * A = body_owner . getornull ( p_body_a ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( ! A ) ;
2020-04-01 23:20:12 +00:00
Body2DSW * B = nullptr ;
2014-02-10 01:10:30 +00:00
if ( body_owner . owns ( p_body_b ) ) {
2019-06-10 15:38:51 +00:00
B = body_owner . getornull ( p_body_b ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( ! B ) ;
2014-02-10 01:10:30 +00:00
}
2021-02-09 16:19:03 +00:00
Joint2DSW * prev_joint = joint_owner . getornull ( p_joint ) ;
ERR_FAIL_COND ( prev_joint = = nullptr ) ;
2017-03-05 15:44:50 +00:00
Joint2DSW * joint = memnew ( PinJoint2DSW ( p_pos , A , B ) ) ;
2014-02-10 01:10:30 +00:00
2021-02-09 16:19:03 +00:00
joint_owner . replace ( p_joint , joint ) ;
joint - > copy_settings_from ( prev_joint ) ;
memdelete ( prev_joint ) ;
2014-02-10 01:10:30 +00:00
}
2021-02-09 16:19:03 +00:00
void PhysicsServer2DSW : : joint_make_groove ( RID p_joint , const Vector2 & p_a_groove1 , const Vector2 & p_a_groove2 , const Vector2 & p_b_anchor , RID p_body_a , RID p_body_b ) {
2019-06-10 15:38:51 +00:00
Body2DSW * A = body_owner . getornull ( p_body_a ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( ! A ) ;
2014-02-10 01:10:30 +00:00
2019-06-10 15:38:51 +00:00
Body2DSW * B = body_owner . getornull ( p_body_b ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( ! B ) ;
Joint2DSW * prev_joint = joint_owner . getornull ( p_joint ) ;
ERR_FAIL_COND ( prev_joint = = nullptr ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Joint2DSW * joint = memnew ( GrooveJoint2DSW ( p_a_groove1 , p_a_groove2 , p_b_anchor , A , B ) ) ;
2021-02-09 16:19:03 +00:00
joint_owner . replace ( p_joint , joint ) ;
joint - > copy_settings_from ( prev_joint ) ;
memdelete ( prev_joint ) ;
2014-02-10 01:10:30 +00:00
}
2021-02-09 16:19:03 +00:00
void PhysicsServer2DSW : : joint_make_damped_spring ( RID p_joint , const Vector2 & p_anchor_a , const Vector2 & p_anchor_b , RID p_body_a , RID p_body_b ) {
2019-06-10 15:38:51 +00:00
Body2DSW * A = body_owner . getornull ( p_body_a ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( ! A ) ;
2014-02-10 01:10:30 +00:00
2019-06-10 15:38:51 +00:00
Body2DSW * B = body_owner . getornull ( p_body_b ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( ! B ) ;
Joint2DSW * prev_joint = joint_owner . getornull ( p_joint ) ;
ERR_FAIL_COND ( prev_joint = = nullptr ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Joint2DSW * joint = memnew ( DampedSpringJoint2DSW ( p_anchor_a , p_anchor_b , A , B ) ) ;
2021-02-09 16:19:03 +00:00
joint_owner . replace ( p_joint , joint ) ;
joint - > copy_settings_from ( prev_joint ) ;
memdelete ( prev_joint ) ;
2014-02-10 01:10:30 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : pin_joint_set_param ( RID p_joint , PinJointParam p_param , real_t p_value ) {
2019-06-10 15:38:51 +00:00
Joint2DSW * j = joint_owner . getornull ( p_joint ) ;
2015-10-10 20:28:05 +00:00
ERR_FAIL_COND ( ! j ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( j - > get_type ( ) ! = JOINT_TYPE_PIN ) ;
2015-10-10 20:28:05 +00:00
2017-03-05 15:44:50 +00:00
PinJoint2DSW * pin_joint = static_cast < PinJoint2DSW * > ( j ) ;
2015-10-10 20:28:05 +00:00
pin_joint - > set_param ( p_param , p_value ) ;
}
2020-03-27 18:21:27 +00:00
real_t PhysicsServer2DSW : : pin_joint_get_param ( RID p_joint , PinJointParam p_param ) const {
2019-06-10 15:38:51 +00:00
Joint2DSW * j = joint_owner . getornull ( p_joint ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! j , 0 ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND_V ( j - > get_type ( ) ! = JOINT_TYPE_PIN , 0 ) ;
2015-10-10 20:28:05 +00:00
2017-03-05 15:44:50 +00:00
PinJoint2DSW * pin_joint = static_cast < PinJoint2DSW * > ( j ) ;
2015-10-10 20:28:05 +00:00
return pin_joint - > get_param ( p_param ) ;
}
2020-06-04 16:53:29 +00:00
void PhysicsServer2DSW : : damped_spring_joint_set_param ( RID p_joint , DampedSpringParam p_param , real_t p_value ) {
2019-06-10 15:38:51 +00:00
Joint2DSW * j = joint_owner . getornull ( p_joint ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! j ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( j - > get_type ( ) ! = JOINT_TYPE_DAMPED_SPRING ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
DampedSpringJoint2DSW * dsj = static_cast < DampedSpringJoint2DSW * > ( j ) ;
dsj - > set_param ( p_param , p_value ) ;
2014-02-10 01:10:30 +00:00
}
2020-06-04 16:53:29 +00:00
real_t PhysicsServer2DSW : : damped_spring_joint_get_param ( RID p_joint , DampedSpringParam p_param ) const {
2019-06-10 15:38:51 +00:00
Joint2DSW * j = joint_owner . getornull ( p_joint ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! j , 0 ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND_V ( j - > get_type ( ) ! = JOINT_TYPE_DAMPED_SPRING , 0 ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
DampedSpringJoint2DSW * dsj = static_cast < DampedSpringJoint2DSW * > ( j ) ;
2014-02-10 01:10:30 +00:00
return dsj - > get_param ( p_param ) ;
}
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : JointType PhysicsServer2DSW : : joint_get_type ( RID p_joint ) const {
2019-06-10 15:38:51 +00:00
Joint2DSW * joint = joint_owner . getornull ( p_joint ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND_V ( ! joint , JOINT_TYPE_PIN ) ;
2014-02-10 01:10:30 +00:00
return joint - > get_type ( ) ;
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : free ( RID p_rid ) {
2019-06-21 11:33:01 +00:00
_update_shapes ( ) ; // just in case
2014-02-10 01:10:30 +00:00
if ( shape_owner . owns ( p_rid ) ) {
2019-06-10 15:38:51 +00:00
Shape2DSW * shape = shape_owner . getornull ( p_rid ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
while ( shape - > get_owners ( ) . size ( ) ) {
ShapeOwner2DSW * so = shape - > get_owners ( ) . front ( ) - > key ( ) ;
2014-02-10 01:10:30 +00:00
so - > remove_shape ( shape ) ;
}
shape_owner . free ( p_rid ) ;
memdelete ( shape ) ;
} else if ( body_owner . owns ( p_rid ) ) {
2019-06-10 15:38:51 +00:00
Body2DSW * body = body_owner . getornull ( p_rid ) ;
2014-02-10 01:10:30 +00:00
2017-01-14 11:26:56 +00:00
/*
if ( body - > get_state_query ( ) )
_clear_query ( body - > get_state_query ( ) ) ;
2014-02-10 01:10:30 +00:00
2017-01-14 11:26:56 +00:00
if ( body - > get_direct_state_query ( ) )
_clear_query ( body - > get_direct_state_query ( ) ) ;
*/
2014-02-10 01:10:30 +00:00
2017-07-25 02:26:47 +00:00
body_set_space ( p_rid , RID ( ) ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
while ( body - > get_shape_count ( ) ) {
2014-02-10 01:10:30 +00:00
body - > remove_shape ( 0 ) ;
}
body_owner . free ( p_rid ) ;
memdelete ( body ) ;
} else if ( area_owner . owns ( p_rid ) ) {
2019-06-10 15:38:51 +00:00
Area2DSW * area = area_owner . getornull ( p_rid ) ;
2014-02-10 01:10:30 +00:00
2017-01-14 11:26:56 +00:00
/*
if ( area - > get_monitor_query ( ) )
_clear_query ( area - > get_monitor_query ( ) ) ;
*/
2014-02-10 01:10:30 +00:00
2020-04-01 23:20:12 +00:00
area - > set_space ( nullptr ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
while ( area - > get_shape_count ( ) ) {
2014-02-10 01:10:30 +00:00
area - > remove_shape ( 0 ) ;
}
area_owner . free ( p_rid ) ;
memdelete ( area ) ;
} else if ( space_owner . owns ( p_rid ) ) {
2019-06-10 15:38:51 +00:00
Space2DSW * space = space_owner . getornull ( p_rid ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
while ( space - > get_objects ( ) . size ( ) ) {
2014-02-10 01:10:30 +00:00
CollisionObject2DSW * co = ( CollisionObject2DSW * ) space - > get_objects ( ) . front ( ) - > get ( ) ;
2020-04-01 23:20:12 +00:00
co - > set_space ( nullptr ) ;
2014-02-10 01:10:30 +00:00
}
active_spaces . erase ( space ) ;
free ( space - > get_default_area ( ) - > get_self ( ) ) ;
space_owner . free ( p_rid ) ;
memdelete ( space ) ;
} else if ( joint_owner . owns ( p_rid ) ) {
2019-06-10 15:38:51 +00:00
Joint2DSW * joint = joint_owner . getornull ( p_rid ) ;
2014-02-10 01:10:30 +00:00
joint_owner . free ( p_rid ) ;
memdelete ( joint ) ;
} else {
2019-08-08 20:08:27 +00:00
ERR_FAIL_MSG ( " Invalid ID. " ) ;
2014-02-10 01:10:30 +00:00
}
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : set_active ( bool p_active ) {
2017-03-05 15:44:50 +00:00
active = p_active ;
2014-02-10 01:10:30 +00:00
} ;
2021-05-19 06:19:58 +00:00
void PhysicsServer2DSW : : set_collision_iterations ( int p_iterations ) {
iterations = p_iterations ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : init ( ) {
2017-03-05 15:44:50 +00:00
doing_sync = false ;
last_step = 0.001 ;
iterations = 8 ; // 8?
stepper = memnew ( Step2DSW ) ;
2020-03-28 12:19:05 +00:00
direct_state = memnew ( PhysicsDirectBodyState2DSW ) ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : step ( real_t p_step ) {
2020-05-14 14:41:43 +00:00
if ( ! active ) {
2014-02-10 01:10:30 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2019-06-21 11:33:01 +00:00
_update_shapes ( ) ;
2017-03-05 15:44:50 +00:00
last_step = p_step ;
2020-03-28 12:19:05 +00:00
PhysicsDirectBodyState2DSW : : singleton - > step = p_step ;
2017-03-05 15:44:50 +00:00
island_count = 0 ;
active_objects = 0 ;
collision_pairs = 0 ;
for ( Set < const Space2DSW * > : : Element * E = active_spaces . front ( ) ; E ; E = E - > next ( ) ) {
stepper - > step ( ( Space2DSW * ) E - > get ( ) , p_step , iterations ) ;
island_count + = E - > get ( ) - > get_island_count ( ) ;
active_objects + = E - > get ( ) - > get_active_objects ( ) ;
collision_pairs + = E - > get ( ) - > get_collision_pairs ( ) ;
2014-02-10 01:10:30 +00:00
}
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : sync ( ) {
2017-03-05 15:44:50 +00:00
doing_sync = true ;
2014-02-10 01:10:30 +00:00
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : flush_queries ( ) {
2020-05-14 14:41:43 +00:00
if ( ! active ) {
2014-02-10 01:10:30 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2018-11-16 11:49:26 +00:00
flushing_queries = true ;
2016-05-22 00:18:16 +00:00
uint64_t time_beg = OS : : get_singleton ( ) - > get_ticks_usec ( ) ;
2015-05-26 04:05:08 +00:00
2017-03-05 15:44:50 +00:00
for ( Set < const Space2DSW * > : : Element * E = active_spaces . front ( ) ; E ; E = E - > next ( ) ) {
Space2DSW * space = ( Space2DSW * ) E - > get ( ) ;
2014-02-10 01:10:30 +00:00
space - > call_queries ( ) ;
}
2018-11-16 11:49:26 +00:00
flushing_queries = false ;
2020-02-27 02:30:20 +00:00
if ( EngineDebugger : : is_profiling ( " servers " ) ) {
2016-05-22 00:18:16 +00:00
uint64_t total_time [ Space2DSW : : ELAPSED_TIME_MAX ] ;
2017-03-05 15:44:50 +00:00
static const char * time_name [ Space2DSW : : ELAPSED_TIME_MAX ] = {
2016-05-22 00:18:16 +00:00
" integrate_forces " ,
" generate_islands " ,
" setup_constraints " ,
" solve_constraints " ,
" integrate_velocities "
} ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < Space2DSW : : ELAPSED_TIME_MAX ; i + + ) {
total_time [ i ] = 0 ;
2016-05-22 00:18:16 +00:00
}
2017-03-05 15:44:50 +00:00
for ( Set < const Space2DSW * > : : Element * E = active_spaces . front ( ) ; E ; E = E - > next ( ) ) {
for ( int i = 0 ; i < Space2DSW : : ELAPSED_TIME_MAX ; i + + ) {
total_time [ i ] + = E - > get ( ) - > get_elapsed_time ( Space2DSW : : ElapsedTime ( i ) ) ;
2016-05-22 00:18:16 +00:00
}
}
Array values ;
2017-03-05 15:44:50 +00:00
values . resize ( Space2DSW : : ELAPSED_TIME_MAX * 2 ) ;
for ( int i = 0 ; i < Space2DSW : : ELAPSED_TIME_MAX ; i + + ) {
values [ i * 2 + 0 ] = time_name [ i ] ;
values [ i * 2 + 1 ] = USEC_TO_SEC ( total_time [ i ] ) ;
2016-05-22 00:18:16 +00:00
}
values . push_back ( " flush_queries " ) ;
2017-03-05 15:44:50 +00:00
values . push_back ( USEC_TO_SEC ( OS : : get_singleton ( ) - > get_ticks_usec ( ) - time_beg ) ) ;
2016-05-22 00:18:16 +00:00
2020-02-27 02:30:20 +00:00
values . push_front ( " physics_2d " ) ;
EngineDebugger : : profiler_add_frame_data ( " servers " , values ) ;
2016-05-22 00:18:16 +00:00
}
}
2014-02-10 01:10:30 +00:00
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : end_sync ( ) {
2017-03-05 15:44:50 +00:00
doing_sync = false ;
2015-05-26 04:05:08 +00:00
}
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : finish ( ) {
2014-02-10 01:10:30 +00:00
memdelete ( stepper ) ;
memdelete ( direct_state ) ;
} ;
2020-03-27 18:21:27 +00:00
void PhysicsServer2DSW : : _update_shapes ( ) {
2019-06-21 11:33:01 +00:00
while ( pending_shape_update_list . first ( ) ) {
pending_shape_update_list . first ( ) - > self ( ) - > _shape_changed ( ) ;
pending_shape_update_list . remove ( pending_shape_update_list . first ( ) ) ;
}
}
2020-03-27 18:21:27 +00:00
int PhysicsServer2DSW : : get_process_info ( ProcessInfo p_info ) {
2017-03-05 15:44:50 +00:00
switch ( p_info ) {
2014-09-03 02:13:40 +00:00
case INFO_ACTIVE_OBJECTS : {
return active_objects ;
} break ;
case INFO_COLLISION_PAIRS : {
return collision_pairs ;
} break ;
case INFO_ISLAND_COUNT : {
return island_count ;
} break ;
}
return 0 ;
}
2020-04-01 23:20:12 +00:00
PhysicsServer2DSW * PhysicsServer2DSW : : singletonsw = nullptr ;
2016-01-03 23:23:44 +00:00
2021-02-09 16:19:03 +00:00
PhysicsServer2DSW : : PhysicsServer2DSW ( bool p_using_threads ) {
2017-03-05 15:44:50 +00:00
singletonsw = this ;
2021-05-10 21:43:13 +00:00
BroadPhase2DSW : : create_func = BroadPhase2DBVH : : _create ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
active = true ;
island_count = 0 ;
active_objects = 0 ;
collision_pairs = 0 ;
2021-02-09 16:19:03 +00:00
using_threads = p_using_threads ;
2018-11-16 11:49:26 +00:00
flushing_queries = false ;
2014-02-10 01:10:30 +00:00
} ;