From 24aa449f4e3407e1bc97609c077f3b29b5cd8484 Mon Sep 17 00:00:00 2001 From: Dragos Daian Date: Mon, 26 Feb 2024 20:30:46 +0100 Subject: [PATCH] make it compile(comment out a lot of code) --- src/bodies/box2d_collision_object_2d.cpp | 10 +- src/box2d-wrapper/box2d_wrapper.cpp | 136 ++++++++++++--------- src/box2d-wrapper/box2d_wrapper.h | 39 +++--- src/joints/box2d_damped_spring_joint_2d.h | 1 - src/servers/box2d_project_settings.cpp | 14 +-- src/servers/box2d_project_settings.h | 3 +- src/spaces/box2d_direct_space_state_2d.cpp | 12 +- src/spaces/box2d_space_2d.cpp | 25 ++-- src/spaces/box2d_space_2d.h | 15 +-- 9 files changed, 132 insertions(+), 123 deletions(-) diff --git a/src/bodies/box2d_collision_object_2d.cpp b/src/bodies/box2d_collision_object_2d.cpp index f52df65..e7eb6e4 100644 --- a/src/bodies/box2d_collision_object_2d.cpp +++ b/src/bodies/box2d_collision_object_2d.cpp @@ -198,8 +198,8 @@ void Box2DCollisionObject2D::_create_shape(Shape &shape, uint32_t p_shape_index) box2d::ShapeHandle shape_handle = shape.shape->get_box2d_shape(); ERR_FAIL_COND(!box2d::is_handle_valid(shape_handle)); - b2FixtureUserData user_data; - set_collider_user_data(user_data, p_shape_index); + b2FixtureUserData *user_data = memnew(b2FixtureUserData); + set_collider_user_data(*user_data, p_shape_index); switch (type) { case TYPE_BODY: { @@ -226,7 +226,7 @@ void Box2DCollisionObject2D::_destroy_shape(Shape &shape, uint32_t p_shape_index if (area_detection_counter > 0) { // Keep track of body information for delayed removal - for (int i = 0; i < shape.collider_handle.count; i++) { + for (int i = 0; i < shape.collider_handle.handles.size(); i++) { space->add_removed_collider(shape.collider_handle.handles[i], this, p_shape_index); } } @@ -279,8 +279,8 @@ void Box2DCollisionObject2D::_set_space(Box2DSpace2D *p_space) { ERR_FAIL_COND(box2d::is_handle_valid(body_handle)); - b2BodyUserData user_data; - set_body_user_data(user_data); + b2BodyUserData *user_data = memnew(b2BodyUserData); + set_body_user_data(*user_data); b2Vec2 position = { transform.get_origin().x, transform.get_origin().y }; real_t angle = transform.get_rotation(); diff --git a/src/box2d-wrapper/box2d_wrapper.cpp b/src/box2d-wrapper/box2d_wrapper.cpp index 00264e6..7c6bc1c 100644 --- a/src/box2d-wrapper/box2d_wrapper.cpp +++ b/src/box2d-wrapper/box2d_wrapper.cpp @@ -4,15 +4,14 @@ #include #include #include -#include #include using namespace box2d; using namespace godot; struct Box2DHolder { - HashMap active_body_callbacks; - HashMap active_objects; + HashMap active_body_callbacks; + HashMap active_objects; }; Box2DHolder holder; @@ -241,6 +240,7 @@ b2BodyId box2d::body_create(b2WorldId world_handle, } void box2d::body_destroy(b2BodyId body_handle) { + // TODO destroy user data too b2DestroyBody(body_handle); } @@ -366,25 +366,29 @@ void box2d::body_wake_up(b2BodyId body_handle) { b2Body_Wake(body_handle); } -FixtureHandle box2d::collider_create_sensor(ShapeHandle shape_handles, +FixtureHandle box2d::collider_create_sensor(b2WorldId world_handle, + ShapeHandle shape_handles, b2BodyId body_handle, b2FixtureUserData *user_data) { + FixtureHandle fixture_handle{}; b2MassData mass_data = b2Body_GetMassData(body_handle); for (int i = 0; i < shape_handles.handles.size(); i++) { + /* ShapeData shape_data = shape_handles.handles[i]; switch (shape_data.type) { - case b2ShapeType::e_polygon: { - } + //case b2ShapeType::e_polygon: { + //} } b2FixtureDef fixture_def; fixture_def.shape = shape_handle.handles[i]; fixture_def.density = 1.0; fixture_def.isSensor = true; fixture_def.userData = user_data; - b2ShapeId fixture = body_handle->CreateFixture(&fixture_def); - fixture_handle.handles[i] = fixture; + */ + b2ShapeId shapeId; + fixture_handle.handles[i] = shapeId; } - body_handle->SetMassData(&mass_data); + b2Body_SetMassData(body_handle, mass_data); return fixture_handle; } @@ -392,13 +396,12 @@ FixtureHandle box2d::collider_create_solid(b2WorldId world_handle, ShapeHandle shape_handle, const Material *mat, b2BodyId body_handle, - b2FixtureUserData user_data) { - FixtureHandle fixture_handle{ - (b2ShapeId *)memalloc((shape_handle.count) * sizeof(b2ShapeId)), - shape_handle.count - }; - b2MassData mass_data = body_handle->GetMassData(); - for (int i = 0; i < shape_handle.count; i++) { + b2FixtureUserData *user_data) { + FixtureHandle fixture_handle{}; + b2MassData mass_data = b2Body_GetMassData(body_handle); + for (int i = 0; i < shape_handle.handles.size(); i++) { + // Create shape + /* b2FixtureDef fixture_def; fixture_def.shape = shape_handle.handles[i]; fixture_def.density = 1.0; @@ -407,25 +410,17 @@ FixtureHandle box2d::collider_create_solid(b2WorldId world_handle, fixture_def.isSensor = false; fixture_def.userData = user_data; b2ShapeId fixture = body_handle->CreateFixture(&fixture_def); - fixture_handle.handles[i] = fixture; + */ + b2ShapeId shapeId; + fixture_handle.handles[i] = shapeId; } - body_handle->SetMassData(&mass_data); + b2Body_SetMassData(body_handle, mass_data); return fixture_handle; } void box2d::collider_destroy(b2WorldId world_handle, FixtureHandle handle) { - ERR_FAIL_COND(!is_handle_valid(handle)); - b2BodyId body = handle.handles[0]->GetBody(); - if (!is_handle_valid(body)) { - // already destroyed - return; - } - for (b2ShapeId fixture = body->GetFixtureList(); fixture != nullptr; fixture = fixture->GetNext()) { - for (int i = 0; i < handle.count; i++) { - if (fixture == handle.handles[i]) { - body->DestroyFixture(fixture); - } - } + for (b2ShapeId shapeId : handle.handles) { + b2DestroyShape(shapeId); } } @@ -444,18 +439,33 @@ Transform2D b2Transform_to_Transform2D(b2Transform transform) { Vector2 box2d::b2Vec2_to_Vector2(b2Vec2 vec) { return Vector2(vec.x, vec.y); } +b2Vec2 box2d::b2Vec2_add(b2Vec2 vec, b2Vec2 other) { + vec.x += other.x; + vec.y += other.y; + return vec; +} +b2Vec2 box2d::b2Vec2_mul(b2Vec2 vec, real_t other) { + vec.x *= other; + vec.y *= other; + return vec; +} + +b2Vec2 box2d::b2Vec2_sub(b2Vec2 vec, b2Vec2 other) { + vec.x -= other.x; + vec.y -= other.y; + return vec; +} b2Vec2 xform_b2Vec2(b2Vec2 vec, Transform2D transform) { return Vector2_to_b2Vec2(transform.xform(b2Vec2_to_Vector2(vec))); } void box2d::collider_set_transform(b2WorldId world_handle, FixtureHandle handles, ShapeInfo shape_info) { - for (int i = 0; i < handles.count; i++) { - b2ShapeId handle = handles.handles[i]; - handle->GetUserData().transform = shape_info.transform; - ERR_FAIL_COND(!handle); - ERR_FAIL_COND(!is_handle_valid(shape_info.handle)); - ERR_FAIL_COND(handles.count != shape_info.handle.count); + ERR_FAIL_COND(!is_handle_valid(shape_info.handle)); + for (b2ShapeId handle : handles.handles) { + b2FixtureUserData *user_data = static_cast(b2Shape_GetUserData(handle)); + user_data->transform = shape_info.transform; + /* b2Shape *shape_template = shape_info.handle.handles[i]; b2Shape *shape = handle->GetShape(); ERR_FAIL_COND(!shape); @@ -509,12 +519,14 @@ void box2d::collider_set_transform(b2WorldId world_handle, FixtureHandle handles ERR_FAIL_MSG("Invalid Shape Type"); } } + */ } } Transform2D box2d::collider_get_transform(b2WorldId world_handle, FixtureHandle handle) { ERR_FAIL_COND_V(!is_handle_valid(handle), Transform2D()); - return handle.handles[0]->GetUserData().transform; + b2FixtureUserData *user_data = static_cast(b2Shape_GetUserData(handle.handles[0])); + return user_data->transform; } Transform2D box2d::collider_get_transform(b2WorldId world_handle, b2ShapeId handle) { @@ -556,6 +568,7 @@ class AABBQueryCallback { if (!b2Shape_IsSensor(shapeId) && !collide_with_body) { return true; } + /* if (!handle_excluded_callback(world, shapeId, fixture->GetUserData(), handle_excluded_info)) { hit_info_array[count++] = PointHitInfo{ fixture, @@ -567,6 +580,7 @@ class AABBQueryCallback { return true; } } + */ return count < hit_info_length; } }; @@ -640,12 +654,13 @@ class RayCastQueryCallback { /// closest hit, 1 to continue float ReportFixture(b2ShapeId fixture, const b2Vec2 &point, const b2Vec2 &normal, float fraction) { - if (fixture->IsSensor() && !collide_with_area) { + if (b2Shape_IsSensor(fixture) && !collide_with_area) { return -1; } - if (!fixture->IsSensor() && !collide_with_body) { + if (!b2Shape_IsSensor(fixture) && !collide_with_body) { return -1; } + /* if (!handle_excluded_callback(world, fixture, fixture->GetUserData(), handle_excluded_info)) { hit_info_array[0] = RayHitInfo{ point, @@ -655,6 +670,7 @@ class RayCastQueryCallback { }; count = 1; } + */ return 1; } }; @@ -690,10 +706,7 @@ b2WorldId box2d::invalid_world_handle() { return b2_nullWorldId; } FixtureHandle box2d::invalid_fixture_handle() { - return FixtureHandle{ - nullptr, - 0 - }; + return FixtureHandle{}; } b2BodyId box2d::invalid_body_handle() { return b2_nullBodyId; @@ -719,7 +732,7 @@ bool box2d::is_user_data_valid(b2BodyUserData user_data) { } bool box2d::is_handle_valid(FixtureHandle handle) { - return handle.count > 0 || handle.handles != nullptr; + return handle.handles.size() > 0; } bool box2d::is_handle_valid(b2WorldId handle) { return B2_IS_NON_NULL(handle); @@ -751,7 +764,7 @@ void box2d::joint_change_revolute_params(b2JointId joint_handle, //joint->SetLimits(angular_limit_lower, angular_limit_upper); b2RevoluteJoint_EnableMotor(joint_handle, motor_enabled); b2RevoluteJoint_SetMotorSpeed(joint_handle, motor_target_velocity); - b2RevoluteJoint_EnableLimit(joint_handle, angular_limit_enabled) + b2RevoluteJoint_EnableLimit(joint_handle, angular_limit_enabled); } b2JointId box2d::joint_create_prismatic(b2WorldId world_handle, @@ -850,6 +863,7 @@ ShapeCastResult box2d::shape_casting(b2WorldId world_handle, bool first_time = true; b2Transform shape_transform = Transform2D_to_b2Transform(shape_info.body_transform * shape_info.transform); b2Transform shape_transform_motion = shape_transform; + /* shape_transform_motion.p += motion; for (int j = 0; j < shape_info.handle.handles[i]->GetChildCount(); j++) { shape_info.handle.handles[i]->ComputeAABB(&shape_aabb, shape_transform, j); @@ -899,6 +913,7 @@ ShapeCastResult box2d::shape_casting(b2WorldId world_handle, if (result.collided) { return result; } + */ } return ShapeCastResult{ false @@ -913,6 +928,7 @@ ShapeCollideResult box2d::shape_collide(const b2Vec2 motion1, for (int j = 0; j < shape_info2.handle.handles.size(); j++) { b2Transform transformA = Transform2D_to_b2Transform(shape_info1.body_transform * shape_info1.transform); b2Transform transformB = Transform2D_to_b2Transform(shape_info2.body_transform * shape_info2.transform); + /* b2TOIOutput toi_output = _time_of_impact(shape_info1.handle.handles[i], transformA, b2Vec2_zero, @@ -932,6 +948,7 @@ ShapeCollideResult box2d::shape_collide(const b2Vec2 motion1, result.witness1 = distance_output.pointA; result.witness2 = distance_output.pointB; return result; + */ } } return ShapeCollideResult{ @@ -1031,6 +1048,7 @@ ContactResult box2d::shapes_contact(b2WorldId world_handle, ShapeInfo shape_info1, ShapeInfo shape_info2, real_t margin) { + /* for (int i = 0; i < shape_info1.handle.count; i++) { for (int j = 0; j < shape_info2.handle.count; j++) { b2Transform transform_A = Transform2D_to_b2Transform(shape_info1.body_transform * shape_info1.transform); @@ -1090,42 +1108,44 @@ ContactResult box2d::shapes_contact(b2WorldId world_handle, }; } } + */ return ContactResult{ false }; } b2WorldId box2d::world_create(WorldSettings settings) { - b2WorldDef world_def = b2_defaultWorldDef; - return b2CreateWorld(b2WorldDef); + b2WorldDef world_def = b2DefaultWorldDef(); + return b2CreateWorld(&world_def); } -void box2d::world_destroy(b2WorldId world_handle){ - b2DestroyWorld(world_handle) +void box2d::world_destroy(b2WorldId world_handle) { + b2DestroyWorld(world_handle); } size_t box2d::world_get_active_objects_count(b2WorldId world_handle) { - return holder.active_objects[world_handle]; + return holder.active_objects[handle_hash(world_handle)]; } void box2d::world_set_active_body_callback(b2WorldId world_handle, ActiveBodyCallback callback) { - holder.active_body_callbacks[world_handle] = callback; + holder.active_body_callbacks[handle_hash(world_handle)] = callback; } -void box2d::world_set_collision_filter_callback(b2WorldId world_handle, - b2ContactFilter *callback) { - world_handle->SetContactFilter(callback); +bool presolve_fcn(b2ShapeId shapeIdA, b2ShapeId shapeIdB, b2Manifold *manifold, void *context) { + b2ContactFilter *callback = static_cast(context); + return callback->ShouldCollide(shapeIdA, shapeIdB, manifold); } -void box2d::world_set_contact_listener(b2WorldId world_handle, - b2ContactListener *callback) { - world_handle->SetContactListener(callback); +void box2d::world_set_collision_filter_callback(b2WorldId world_handle, + b2ContactFilter *callback) { + b2World_SetPreSolveCallback(world_handle, presolve_fcn, callback); } void box2d::world_step(b2WorldId world_handle, SimulationSettings settings) { //world_handle->SetGravity(settings->gravity); // TODO set world gravity - b2World_Step(world_handle, settings->dt, settings->max_velocity_iterations, settings->max_position_iterations); + //settings.max_velocity_iterations, settings.max_position_iterations + b2World_Step(world_handle, settings.dt, settings.sub_step_count); int active_objects = 0; /* if (holder.active_body_callbacks.has(world_handle)) { diff --git a/src/box2d-wrapper/box2d_wrapper.h b/src/box2d-wrapper/box2d_wrapper.h index 3da20fa..e6092e0 100644 --- a/src/box2d-wrapper/box2d_wrapper.h +++ b/src/box2d-wrapper/box2d_wrapper.h @@ -25,6 +25,11 @@ class Box2DCollisionObject2D; class Box2DShape2D; +class b2ContactFilter { +public: + virtual bool ShouldCollide(b2ShapeId fixtureA, b2ShapeId fixtureB, b2Manifold *manifold) = 0; +}; + // You can define this to inject whatever data you want in b2Fixture struct b2FixtureUserData { b2FixtureUserData() : @@ -63,6 +68,10 @@ struct ShapeData { } data; }; +struct FixtureHandle { + std::vector handles; +}; + struct ShapeHandle { std::vector handles; }; @@ -203,30 +212,16 @@ using CollisionModifyContactsCallback = OneWayDirection (*)(b2WorldId world_hand struct SimulationSettings { real_t dt; - size_t max_velocity_iterations; - size_t max_position_iterations; + size_t sub_step_count; b2Vec2 gravity; }; b2Vec2 Vector2_to_b2Vec2(godot::Vector2 vec); godot::Vector2 b2Vec2_to_Vector2(b2Vec2 vec); -b2Vec2 b2Vec2_add(b2Vec2 vec, b2Vec2 other) { - vec.x += other.x; - vec.y += other.y; - return vec; -} -b2Vec2 b2Vec2_mul(b2Vec2 vec, real_t other) { - vec.x *= other; - vec.y *= other; - return vec; -} - -b2Vec2 b2Vec2_sub(b2Vec2 vec, b2Vec2 other) { - vec.x -= other.x; - vec.y -= other.y; - return vec; -} +b2Vec2 b2Vec2_add(b2Vec2 vec, b2Vec2 other); +b2Vec2 b2Vec2_mul(b2Vec2 vec, real_t other); +b2Vec2 b2Vec2_sub(b2Vec2 vec, b2Vec2 other); void body_add_force(b2BodyId body_handle, b2Vec2 force); @@ -290,7 +285,8 @@ void body_update_material(b2BodyId body_handle, Material mat); void body_wake_up(b2BodyId body_handle); -FixtureHandle collider_create_sensor(ShapeHandle shape_handles, +FixtureHandle collider_create_sensor(b2WorldId world_handle, + ShapeHandle shape_handles, b2BodyId body_handle, b2FixtureUserData *user_data); @@ -298,7 +294,7 @@ FixtureHandle collider_create_solid(b2WorldId world_handle, ShapeHandle shape_handle, const Material *mat, b2BodyId body_handle, - b2FixtureUserData user_data); + b2FixtureUserData *user_data); void collider_destroy(b2WorldId world_handle, FixtureHandle handle); @@ -465,9 +461,6 @@ void world_set_active_body_callback(b2WorldId world_handle, ActiveBodyCallback c void world_set_collision_filter_callback(b2WorldId world_handle, b2ContactFilter *callback); -void world_set_contact_listener(b2WorldId world_handle, - b2ContactListener *callback); - void world_step(b2WorldId world_handle, const SimulationSettings settings); } // namespace box2d diff --git a/src/joints/box2d_damped_spring_joint_2d.h b/src/joints/box2d_damped_spring_joint_2d.h index 2bb47a1..fb7ef05 100644 --- a/src/joints/box2d_damped_spring_joint_2d.h +++ b/src/joints/box2d_damped_spring_joint_2d.h @@ -15,7 +15,6 @@ class Box2DDampedSpringJoint2D : public Box2DJoint2D { real_t get_param(PhysicsServer2D::DampedSpringParam p_param) const; Box2DDampedSpringJoint2D(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, Box2DBody2D *p_body_a, Box2DBody2D *p_body_b); - ~Box2DDampedSpringJoint2D(); }; #endif // BOX2D_DAMPED_SPRING_JOINT_2D_H diff --git a/src/servers/box2d_project_settings.cpp b/src/servers/box2d_project_settings.cpp index cff95a2..6d505eb 100644 --- a/src/servers/box2d_project_settings.cpp +++ b/src/servers/box2d_project_settings.cpp @@ -6,8 +6,7 @@ using namespace godot; constexpr char RUN_ON_SEPARATE_THREAD[] = "physics/2d/run_on_separate_thread"; constexpr char MAX_THREADS[] = "threading/worker_pool/max_threads"; -constexpr char POSITION_ITERATIONS[] = "physics/box_2d/solver/position_iterations"; -constexpr char VELOCITY_ITERATIONS[] = "physics/box_2d/solver/velocity_iterations"; +constexpr char SUB_STEP_COUNT[] = "physics/box_2d/solver/sub_step_count"; void register_setting( const String &p_name, @@ -64,8 +63,7 @@ void register_setting_ranged( } void Box2DProjectSettings::register_settings() { - register_setting_ranged(VELOCITY_ITERATIONS, 8, U"2,16,or_greater"); - register_setting_ranged(POSITION_ITERATIONS, 3, U"1,16,or_greater"); + register_setting_ranged(SUB_STEP_COUNT, 4, U"1,16,or_greater"); } template @@ -88,10 +86,6 @@ int Box2DProjectSettings::get_max_threads() { return get_setting(MAX_THREADS); } -int Box2DProjectSettings::get_position_iterations() { - return get_setting(POSITION_ITERATIONS); -} - -int Box2DProjectSettings::get_velocity_iterations() { - return get_setting(VELOCITY_ITERATIONS); +int Box2DProjectSettings::get_sub_step_count() { + return get_setting(SUB_STEP_COUNT); } diff --git a/src/servers/box2d_project_settings.h b/src/servers/box2d_project_settings.h index 15ef42a..2cde018 100644 --- a/src/servers/box2d_project_settings.h +++ b/src/servers/box2d_project_settings.h @@ -10,6 +10,5 @@ class Box2DProjectSettings { static bool should_run_on_separate_thread(); static int get_max_threads(); - static int get_position_iterations(); - static int get_velocity_iterations(); + static int get_sub_step_count(); }; diff --git a/src/spaces/box2d_direct_space_state_2d.cpp b/src/spaces/box2d_direct_space_state_2d.cpp index 8cde524..82afaa7 100644 --- a/src/spaces/box2d_direct_space_state_2d.cpp +++ b/src/spaces/box2d_direct_space_state_2d.cpp @@ -130,7 +130,7 @@ bool Box2DDirectSpaceState2D::_collide_shape(const RID &shape_rid, const Transfo box2d::ShapeInfo shape_info = box2d::shape_info_from_body_shape(shape_handle, Transform2D(), transform); box2d::QueryExcludedInfo query_excluded_info = box2d::default_query_excluded_info(); query_excluded_info.query_collision_layer_mask = collision_mask; - query_excluded_info.query_exclude = (b2ShapeId *)memalloc((max_results) * sizeof(b2ShapeId)); + //query_excluded_info.query_exclude = (b2ShapeId *)memalloc((max_results) * sizeof(b2ShapeId)); query_excluded_info.query_exclude_size = 0; int cpt = 0; @@ -141,7 +141,7 @@ bool Box2DDirectSpaceState2D::_collide_shape(const RID &shape_rid, const Transfo break; } (*result_count)++; - query_excluded_info.query_exclude[query_excluded_info.query_exclude_size++] = result.collider; + //query_excluded_info.query_exclude[query_excluded_info.query_exclude_size++] = result.collider; if (results_out != nullptr) { results_out[array_idx++] = Vector2(result.witness1.x, result.witness1.y); results_out[array_idx++] = Vector2(result.witness2.x, result.witness2.y); @@ -149,7 +149,7 @@ bool Box2DDirectSpaceState2D::_collide_shape(const RID &shape_rid, const Transfo cpt++; } while (cpt < max_results); - memfree(query_excluded_info.query_exclude); + //memfree(query_excluded_info.query_exclude); return array_idx > 0; } @@ -164,7 +164,7 @@ int Box2DDirectSpaceState2D::_intersect_shape(const RID &shape_rid, const Transf box2d::QueryExcludedInfo query_excluded_info = box2d::default_query_excluded_info(); query_excluded_info.query_collision_layer_mask = collision_mask; - query_excluded_info.query_exclude = (b2ShapeId *)memalloc((p_result_max) * sizeof(b2ShapeId)); + //query_excluded_info.query_exclude = (b2ShapeId *)memalloc((p_result_max) * sizeof(b2ShapeId)); query_excluded_info.query_exclude_size = 0; int cpt = 0; @@ -173,7 +173,7 @@ int Box2DDirectSpaceState2D::_intersect_shape(const RID &shape_rid, const Transf if (!result.collided) { break; } - query_excluded_info.query_exclude[query_excluded_info.query_exclude_size++] = result.collider; + //query_excluded_info.query_exclude[query_excluded_info.query_exclude_size++] = result.collider; ERR_CONTINUE_MSG(!box2d::is_user_data_valid(result.user_data), "Invalid user data"); uint32_t shape_index = 0; @@ -194,7 +194,7 @@ int Box2DDirectSpaceState2D::_intersect_shape(const RID &shape_rid, const Transf } while (cpt < p_result_max); - memfree(query_excluded_info.query_exclude); + //memfree(query_excluded_info.query_exclude); return cpt; } diff --git a/src/spaces/box2d_space_2d.cpp b/src/spaces/box2d_space_2d.cpp index acea00f..5a7b322 100644 --- a/src/spaces/box2d_space_2d.cpp +++ b/src/spaces/box2d_space_2d.cpp @@ -92,11 +92,11 @@ bool Box2DSpace2D::collision_filter_common_callback(b2WorldId world_handle, cons return true; } -bool Box2DSpace2D::ShouldCollide(b2ShapeId fixtureA, b2ShapeId fixtureB) { +bool Box2DSpace2D::ShouldCollide(b2ShapeId fixtureA, b2ShapeId fixtureB, b2Manifold *manifold) { ERR_FAIL_COND_V(!box2d::is_handle_valid(fixtureA), false); ERR_FAIL_COND_V(!box2d::is_handle_valid(fixtureB), false); - b2FixtureUserData user_dataA = fixtureA->GetUserData(); - b2FixtureUserData user_dataB = fixtureB->GetUserData(); + b2FixtureUserData user_dataA = *static_cast(b2Shape_GetUserData(fixtureA)); + b2FixtureUserData user_dataB = *static_cast(b2Shape_GetUserData(fixtureA)); ERR_FAIL_COND_V(!box2d::is_user_data_valid(user_dataA), false); ERR_FAIL_COND_V(!box2d::is_user_data_valid(user_dataB), false); CollidersInfo colliders_info; @@ -117,6 +117,7 @@ bool Box2DSpace2D::ShouldCollide(b2ShapeId fixtureA, b2ShapeId fixtureB) { return true; } +/* box2d::CollisionEventInfo event_info_from_contact(b2Contact *contact) { box2d::CollisionEventInfo event_info; event_info.is_sensor = false; @@ -159,6 +160,7 @@ box2d::ContactForceEventInfo force_info_from_contact(b2Contact *contact) { event_info.is_valid = true; return event_info; } + void Box2DSpace2D::BeginContact(b2Contact *contact) { box2d::CollisionEventInfo event_info = event_info_from_contact(contact); if (!event_info.is_valid) { @@ -267,7 +269,6 @@ void Box2DSpace2D::PostSolve(b2Contact *contact, const b2ContactImpulse *impulse } } } - void Box2DSpace2D::collision_event_callback(b2WorldId world_handle, const box2d::CollisionEventInfo *event_info) { Box2DSpace2D *space = Box2DPhysicsServer2D::singleton->get_active_space(world_handle); ERR_FAIL_COND(!space); @@ -392,6 +393,7 @@ void Box2DSpace2D::collision_event_callback(b2WorldId world_handle, const box2d: } } +*/ bool Box2DSpace2D::contact_force_event_callback(b2WorldId world_handle, const box2d::ContactForceEventInfo *event_info) { Box2DSpace2D *space = Box2DPhysicsServer2D::singleton->get_active_space(world_handle); ERR_FAIL_COND_V(!space, false); @@ -530,8 +532,7 @@ void Box2DSpace2D::step(real_t p_step) { box2d::SimulationSettings settings; settings.dt = p_step; - settings.max_position_iterations = Box2DProjectSettings::get_position_iterations(); - settings.max_velocity_iterations = Box2DProjectSettings::get_velocity_iterations(); + settings.sub_step_count = Box2DProjectSettings::get_sub_step_count(); settings.gravity.x = default_gravity_dir.x * default_gravity_value; settings.gravity.y = default_gravity_dir.y * default_gravity_value; @@ -550,7 +551,8 @@ void Box2DSpace2D::step(real_t p_step) { } // Returns true to ignore the collider -bool Box2DSpace2D::_is_handle_excluded_callback(b2WorldId *world_handle, b2ShapeId collider_handle, b2FixtureUserData user_data, const box2d::QueryExcludedInfo *handle_excluded_info) { +bool Box2DSpace2D::_is_handle_excluded_callback(b2WorldId world_handle, b2ShapeId collider_handle, b2FixtureUserData user_data, const box2d::QueryExcludedInfo *handle_excluded_info) { + /* for (uint32_t exclude_index = 0; exclude_index < handle_excluded_info->query_exclude_size; ++exclude_index) { if (box2d::are_handles_equal(handle_excluded_info->query_exclude[exclude_index], collider_handle)) { return true; @@ -574,8 +576,10 @@ bool Box2DSpace2D::_is_handle_excluded_callback(b2WorldId *world_handle, b2Shape if (handle_excluded_info->query_exclude_body == collision_object_2d->get_rid().get_id()) { return true; } - return Box2DPhysicsServer2D::singleton->get_active_space(world_handle)->get_direct_state()->is_body_excluded_from_query(collision_object_2d->get_rid()); + + */ + return false; } void Box2DSpace2D::call_queries() { @@ -739,12 +743,11 @@ Box2DSpace2D::Box2DSpace2D() { world_settings.sleep_angular_threshold = body_angular_velocity_sleep_threshold; world_settings.sleep_time_until_sleep = body_time_to_sleep; - handle = box2d::world_create(&world_settings); + handle = box2d::world_create(world_settings); ERR_FAIL_COND(!box2d::is_handle_valid(handle)); box2d::world_set_active_body_callback(handle, active_body_callback); box2d::world_set_collision_filter_callback(handle, this); - box2d::world_set_contact_listener(handle, this); } Box2DSpace2D::~Box2DSpace2D() { @@ -813,7 +816,7 @@ int Box2DSpace2D::box2d_intersect_aabb(Rect2 p_aabb, uint32_t p_collision_mask, b2Vec2 rect_begin{ p_aabb.position.x, p_aabb.position.y }; b2Vec2 rect_end{ p_aabb.get_end().x, p_aabb.get_end().y }; box2d::QueryExcludedInfo handle_excluded_info = box2d::default_query_excluded_info(); - handle_excluded_info.query_exclude = (b2ShapeId *)memalloc((p_max_results) * sizeof(b2ShapeId)); + //handle_excluded_info.query_exclude = (b2ShapeId *)memalloc((p_max_results) * sizeof(b2ShapeId)); handle_excluded_info.query_collision_layer_mask = p_collision_mask; handle_excluded_info.query_exclude_size = 0; handle_excluded_info.query_exclude_body = p_exclude_body.get_id(); diff --git a/src/spaces/box2d_space_2d.h b/src/spaces/box2d_space_2d.h index 3d9bf43..52846c4 100644 --- a/src/spaces/box2d_space_2d.h +++ b/src/spaces/box2d_space_2d.h @@ -20,7 +20,7 @@ using namespace godot; class Box2DSpace2D; class Box2DDirectSpaceState2D; -class Box2DSpace2D : public b2ContactFilter, public b2ContactListener { +class Box2DSpace2D : public b2ContactFilter { Box2DDirectSpaceState2D *direct_access = nullptr; RID rid; @@ -84,13 +84,14 @@ class Box2DSpace2D : public b2ContactFilter, public b2ContactListener { Box2DCollisionObject2D *object2 = nullptr; }; - virtual bool ShouldCollide(b2ShapeId fixtureA, b2ShapeId fixtureB) override; - virtual void BeginContact(b2Contact *contact) override; - - virtual void EndContact(b2Contact *contact) override; - virtual void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) override; - virtual void PostSolve(b2Contact *contact, const b2ContactImpulse *impulse) override; + virtual bool ShouldCollide(b2ShapeId fixtureA, b2ShapeId fixtureB, b2Manifold *manifold) override; + /* + void BeginContact(b2Contact *contact); + void EndContact(b2Contact *contact); + void PreSolve(b2Contact *contact, const b2Manifold *oldManifold); + void PostSolve(b2Contact *contact, const b2ContactImpulse *impulse); + */ static bool collision_filter_common_callback(b2WorldId world_handle, const box2d::CollisionFilterInfo *filter_info, CollidersInfo &r_colliders_info); static box2d::OneWayDirection collision_modify_contacts_callback(b2WorldId world_handle, const box2d::CollisionFilterInfo *filter_info);