diff options
Diffstat (limited to 'servers/physics_server_2d.cpp')
-rw-r--r-- | servers/physics_server_2d.cpp | 130 |
1 files changed, 87 insertions, 43 deletions
diff --git a/servers/physics_server_2d.cpp b/servers/physics_server_2d.cpp index 41aae36785..21be311637 100644 --- a/servers/physics_server_2d.cpp +++ b/servers/physics_server_2d.cpp @@ -32,6 +32,7 @@ #include "core/config/project_settings.h" #include "core/string/print_string.h" +#include "core/variant/typed_array.h" PhysicsServer2D *PhysicsServer2D::singleton = nullptr; @@ -147,24 +148,36 @@ PhysicsDirectBodyState2D::PhysicsDirectBodyState2D() {} /////////////////////////////////////////////////////// -void PhysicsRayQueryParameters2D::set_exclude(const Vector<RID> &p_exclude) { +Ref<PhysicsRayQueryParameters2D> PhysicsRayQueryParameters2D::create(Vector2 p_from, Vector2 p_to, uint32_t p_mask, const TypedArray<RID> &p_exclude) { + Ref<PhysicsRayQueryParameters2D> params; + params.instantiate(); + params->set_from(p_from); + params->set_to(p_to); + params->set_collision_mask(p_mask); + params->set_exclude(p_exclude); + return params; +} + +void PhysicsRayQueryParameters2D::set_exclude(const TypedArray<RID> &p_exclude) { parameters.exclude.clear(); for (int i = 0; i < p_exclude.size(); i++) { parameters.exclude.insert(p_exclude[i]); } } -Vector<RID> PhysicsRayQueryParameters2D::get_exclude() const { - Vector<RID> ret; +TypedArray<RID> PhysicsRayQueryParameters2D::get_exclude() const { + TypedArray<RID> ret; ret.resize(parameters.exclude.size()); int idx = 0; for (const RID &E : parameters.exclude) { - ret.write[idx++] = E; + ret[idx++] = E; } return ret; } void PhysicsRayQueryParameters2D::_bind_methods() { + ClassDB::bind_static_method("PhysicsRayQueryParameters2D", D_METHOD("create", "from", "to", "collision_mask", "exclude"), &PhysicsRayQueryParameters2D::create, DEFVAL(UINT32_MAX), DEFVAL(TypedArray<RID>())); + ClassDB::bind_method(D_METHOD("set_from", "from"), &PhysicsRayQueryParameters2D::set_from); ClassDB::bind_method(D_METHOD("get_from"), &PhysicsRayQueryParameters2D::get_from); @@ -197,19 +210,19 @@ void PhysicsRayQueryParameters2D::_bind_methods() { /////////////////////////////////////////////////////// -void PhysicsPointQueryParameters2D::set_exclude(const Vector<RID> &p_exclude) { +void PhysicsPointQueryParameters2D::set_exclude(const TypedArray<RID> &p_exclude) { parameters.exclude.clear(); for (int i = 0; i < p_exclude.size(); i++) { parameters.exclude.insert(p_exclude[i]); } } -Vector<RID> PhysicsPointQueryParameters2D::get_exclude() const { - Vector<RID> ret; +TypedArray<RID> PhysicsPointQueryParameters2D::get_exclude() const { + TypedArray<RID> ret; ret.resize(parameters.exclude.size()); int idx = 0; for (const RID &E : parameters.exclude) { - ret.write[idx++] = E; + ret[idx++] = E; } return ret; } @@ -256,19 +269,19 @@ void PhysicsShapeQueryParameters2D::set_shape_rid(const RID &p_shape) { } } -void PhysicsShapeQueryParameters2D::set_exclude(const Vector<RID> &p_exclude) { +void PhysicsShapeQueryParameters2D::set_exclude(const TypedArray<RID> &p_exclude) { parameters.exclude.clear(); for (int i = 0; i < p_exclude.size(); i++) { parameters.exclude.insert(p_exclude[i]); } } -Vector<RID> PhysicsShapeQueryParameters2D::get_exclude() const { - Vector<RID> ret; +TypedArray<RID> PhysicsShapeQueryParameters2D::get_exclude() const { + TypedArray<RID> ret; ret.resize(parameters.exclude.size()); int idx = 0; for (const RID &E : parameters.exclude) { - ret.write[idx++] = E; + ret[idx++] = E; } return ret; } @@ -335,7 +348,7 @@ Dictionary PhysicsDirectSpaceState2D::_intersect_ray(const Ref<PhysicsRayQueryPa return d; } -Array PhysicsDirectSpaceState2D::_intersect_point(const Ref<PhysicsPointQueryParameters2D> &p_point_query, int p_max_results) { +TypedArray<Dictionary> PhysicsDirectSpaceState2D::_intersect_point(const Ref<PhysicsPointQueryParameters2D> &p_point_query, int p_max_results) { ERR_FAIL_COND_V(p_point_query.is_null(), Array()); Vector<ShapeResult> ret; @@ -344,10 +357,10 @@ Array PhysicsDirectSpaceState2D::_intersect_point(const Ref<PhysicsPointQueryPar int rc = intersect_point(p_point_query->get_parameters(), ret.ptrw(), ret.size()); if (rc == 0) { - return Array(); + return TypedArray<Dictionary>(); } - Array r; + TypedArray<Dictionary> r; r.resize(rc); for (int i = 0; i < rc; i++) { Dictionary d; @@ -360,13 +373,13 @@ Array PhysicsDirectSpaceState2D::_intersect_point(const Ref<PhysicsPointQueryPar return r; } -Array PhysicsDirectSpaceState2D::_intersect_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results) { - ERR_FAIL_COND_V(!p_shape_query.is_valid(), Array()); +TypedArray<Dictionary> PhysicsDirectSpaceState2D::_intersect_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results) { + ERR_FAIL_COND_V(!p_shape_query.is_valid(), TypedArray<Dictionary>()); Vector<ShapeResult> sr; sr.resize(p_max_results); int rc = intersect_shape(p_shape_query->get_parameters(), sr.ptrw(), sr.size()); - Array ret; + TypedArray<Dictionary> ret; ret.resize(rc); for (int i = 0; i < rc; i++) { Dictionary d; @@ -380,22 +393,22 @@ Array PhysicsDirectSpaceState2D::_intersect_shape(const Ref<PhysicsShapeQueryPar return ret; } -Array PhysicsDirectSpaceState2D::_cast_motion(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query) { - ERR_FAIL_COND_V(!p_shape_query.is_valid(), Array()); +Vector<real_t> PhysicsDirectSpaceState2D::_cast_motion(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query) { + ERR_FAIL_COND_V(!p_shape_query.is_valid(), Vector<real_t>()); real_t closest_safe, closest_unsafe; bool res = cast_motion(p_shape_query->get_parameters(), closest_safe, closest_unsafe); if (!res) { - return Array(); + return Vector<real_t>(); } - Array ret; + Vector<real_t> ret; ret.resize(2); - ret[0] = closest_safe; - ret[1] = closest_unsafe; + ret.write[0] = closest_safe; + ret.write[1] = closest_unsafe; return ret; } -Array PhysicsDirectSpaceState2D::_collide_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results) { +TypedArray<PackedVector2Array> PhysicsDirectSpaceState2D::_collide_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results) { ERR_FAIL_COND_V(!p_shape_query.is_valid(), Array()); Vector<Vector2> ret; @@ -403,9 +416,9 @@ Array PhysicsDirectSpaceState2D::_collide_shape(const Ref<PhysicsShapeQueryParam int rc = 0; bool res = collide_shape(p_shape_query->get_parameters(), ret.ptrw(), p_max_results, rc); if (!res) { - return Array(); + return TypedArray<PackedVector2Array>(); } - Array r; + TypedArray<PackedVector2Array> r; r.resize(rc * 2); for (int i = 0; i < rc * 2; i++) { r[i] = ret[i]; @@ -448,21 +461,21 @@ void PhysicsDirectSpaceState2D::_bind_methods() { /////////////////////////////// -Vector<RID> PhysicsTestMotionParameters2D::get_exclude_bodies() const { - Vector<RID> exclude; +TypedArray<RID> PhysicsTestMotionParameters2D::get_exclude_bodies() const { + TypedArray<RID> exclude; exclude.resize(parameters.exclude_bodies.size()); int body_index = 0; for (RID body : parameters.exclude_bodies) { - exclude.write[body_index++] = body; + exclude[body_index++] = body; } return exclude; } -void PhysicsTestMotionParameters2D::set_exclude_bodies(const Vector<RID> &p_exclude) { - for (RID body : p_exclude) { - parameters.exclude_bodies.insert(body); +void PhysicsTestMotionParameters2D::set_exclude_bodies(const TypedArray<RID> &p_exclude) { + for (int i = 0; i < p_exclude.size(); i++) { + parameters.exclude_bodies.insert(p_exclude[i]); } } @@ -639,7 +652,10 @@ void PhysicsServer2D::_bind_methods() { ClassDB::bind_method(D_METHOD("area_clear_shapes", "area"), &PhysicsServer2D::area_clear_shapes); ClassDB::bind_method(D_METHOD("area_set_collision_layer", "area", "layer"), &PhysicsServer2D::area_set_collision_layer); + ClassDB::bind_method(D_METHOD("area_get_collision_layer", "area"), &PhysicsServer2D::area_get_collision_layer); + ClassDB::bind_method(D_METHOD("area_set_collision_mask", "area", "mask"), &PhysicsServer2D::area_set_collision_mask); + ClassDB::bind_method(D_METHOD("area_get_collision_mask", "area"), &PhysicsServer2D::area_get_collision_mask); ClassDB::bind_method(D_METHOD("area_set_param", "area", "param", "value"), &PhysicsServer2D::area_set_param); ClassDB::bind_method(D_METHOD("area_set_transform", "area", "transform"), &PhysicsServer2D::area_set_transform); @@ -694,6 +710,9 @@ void PhysicsServer2D::_bind_methods() { ClassDB::bind_method(D_METHOD("body_set_collision_mask", "body", "mask"), &PhysicsServer2D::body_set_collision_mask); ClassDB::bind_method(D_METHOD("body_get_collision_mask", "body"), &PhysicsServer2D::body_get_collision_mask); + ClassDB::bind_method(D_METHOD("body_set_collision_priority", "body", "priority"), &PhysicsServer2D::body_set_collision_priority); + ClassDB::bind_method(D_METHOD("body_get_collision_priority", "body"), &PhysicsServer2D::body_get_collision_priority); + ClassDB::bind_method(D_METHOD("body_set_param", "body", "param", "value"), &PhysicsServer2D::body_set_param); ClassDB::bind_method(D_METHOD("body_get_param", "body", "param"), &PhysicsServer2D::body_get_param); @@ -801,8 +820,8 @@ void PhysicsServer2D::_bind_methods() { BIND_ENUM_CONSTANT(BODY_MODE_STATIC); BIND_ENUM_CONSTANT(BODY_MODE_KINEMATIC); - BIND_ENUM_CONSTANT(BODY_MODE_DYNAMIC); - BIND_ENUM_CONSTANT(BODY_MODE_DYNAMIC_LINEAR); + BIND_ENUM_CONSTANT(BODY_MODE_RIGID); + BIND_ENUM_CONSTANT(BODY_MODE_RIGID_LINEAR); BIND_ENUM_CONSTANT(BODY_PARAM_BOUNCE); BIND_ENUM_CONSTANT(BODY_PARAM_FRICTION); @@ -834,6 +853,8 @@ void PhysicsServer2D::_bind_methods() { BIND_ENUM_CONSTANT(JOINT_PARAM_MAX_BIAS); BIND_ENUM_CONSTANT(JOINT_PARAM_MAX_FORCE); + BIND_ENUM_CONSTANT(PIN_JOINT_SOFTNESS); + BIND_ENUM_CONSTANT(DAMPED_SPRING_REST_LENGTH); BIND_ENUM_CONSTANT(DAMPED_SPRING_STIFFNESS); BIND_ENUM_CONSTANT(DAMPED_SPRING_DAMPING); @@ -858,9 +879,7 @@ PhysicsServer2D::~PhysicsServer2D() { singleton = nullptr; } -Vector<PhysicsServer2DManager::ClassInfo> PhysicsServer2DManager::physics_2d_servers; -int PhysicsServer2DManager::default_server_id = -1; -int PhysicsServer2DManager::default_server_priority = -1; +PhysicsServer2DManager *PhysicsServer2DManager::singleton = nullptr; const String PhysicsServer2DManager::setting_property_name(PNAME("physics/2d/physics_engine")); void PhysicsServer2DManager::on_servers_changed() { @@ -871,10 +890,19 @@ void PhysicsServer2DManager::on_servers_changed() { ProjectSettings::get_singleton()->set_custom_property_info(setting_property_name, PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers)); } -void PhysicsServer2DManager::register_server(const String &p_name, CreatePhysicsServer2DCallback p_creat_callback) { - ERR_FAIL_COND(!p_creat_callback); +void PhysicsServer2DManager::_bind_methods() { + ClassDB::bind_method(D_METHOD("register_server", "name", "create_callback"), &PhysicsServer2DManager::register_server); + ClassDB::bind_method(D_METHOD("set_default_server", "name", "priority"), &PhysicsServer2DManager::set_default_server); +} + +PhysicsServer2DManager *PhysicsServer2DManager::get_singleton() { + return singleton; +} + +void PhysicsServer2DManager::register_server(const String &p_name, const Callable &p_create_callback) { + //ERR_FAIL_COND(!p_create_callback.is_valid()); ERR_FAIL_COND(find_server_id(p_name) != -1); - physics_2d_servers.push_back(ClassInfo(p_name, p_creat_callback)); + physics_2d_servers.push_back(ClassInfo(p_name, p_create_callback)); on_servers_changed(); } @@ -907,7 +935,11 @@ String PhysicsServer2DManager::get_server_name(int p_id) { PhysicsServer2D *PhysicsServer2DManager::new_default_server() { ERR_FAIL_COND_V(default_server_id == -1, nullptr); - return physics_2d_servers[default_server_id].create_callback(); + Variant ret; + Callable::CallError ce; + physics_2d_servers[default_server_id].create_callback.callp(nullptr, 0, ret, ce); + ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr); + return Object::cast_to<PhysicsServer2D>(ret.get_validated_object()); } PhysicsServer2D *PhysicsServer2DManager::new_server(const String &p_name) { @@ -915,6 +947,18 @@ PhysicsServer2D *PhysicsServer2DManager::new_server(const String &p_name) { if (id == -1) { return nullptr; } else { - return physics_2d_servers[id].create_callback(); + Variant ret; + Callable::CallError ce; + physics_2d_servers[id].create_callback.callp(nullptr, 0, ret, ce); + ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr); + return Object::cast_to<PhysicsServer2D>(ret.get_validated_object()); } } + +PhysicsServer2DManager::PhysicsServer2DManager() { + singleton = this; +} + +PhysicsServer2DManager::~PhysicsServer2DManager() { + singleton = nullptr; +} |