From cc13ecd83795aed5ba42fc70cc10e81bff505fc8 Mon Sep 17 00:00:00 2001 From: Dragos Daian Date: Thu, 30 Nov 2023 11:25:37 +0100 Subject: [PATCH] fix shape transfer wrong for dynamic bodies --- src/bodies/box2d_body_2d.cpp | 4 ++-- src/box2d-wrapper/box2d_wrapper.cpp | 4 ++++ src/spaces/box2d_space_2d.cpp | 26 ++++++++++++++++---------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/bodies/box2d_body_2d.cpp b/src/bodies/box2d_body_2d.cpp index e4f4887..48eb01b 100644 --- a/src/bodies/box2d_body_2d.cpp +++ b/src/bodies/box2d_body_2d.cpp @@ -63,7 +63,7 @@ void Box2DBody2D::update_mass_properties(bool force_update) { const Box2DShape2D *shape = get_shape(i); real_t shape_area = shape->get_aabb().get_area(); - if (shape_area == 0.0) { + if (shape_area == 0.0 || mass == 0.0) { continue; } @@ -89,7 +89,7 @@ void Box2DBody2D::update_mass_properties(bool force_update) { const Box2DShape2D *shape = get_shape(i); real_t shape_area = shape->get_aabb().get_area(); - if (shape_area == 0.0) { + if (shape_area == 0.0 || mass == 0.0) { continue; } diff --git a/src/box2d-wrapper/box2d_wrapper.cpp b/src/box2d-wrapper/box2d_wrapper.cpp index c018c49..57f8641 100644 --- a/src/box2d-wrapper/box2d_wrapper.cpp +++ b/src/box2d-wrapper/box2d_wrapper.cpp @@ -432,6 +432,7 @@ FixtureHandle box2d::collider_create_sensor(b2World *world_handle, (b2Fixture **)memalloc((shape_handle.count) * sizeof(b2Fixture *)), shape_handle.count }; + b2MassData mass_data = body_handle->GetMassData(); for (int i = 0; i < shape_handle.count; i++) { b2FixtureDef fixture_def; fixture_def.shape = shape_handle.handles[i]; @@ -441,6 +442,7 @@ FixtureHandle box2d::collider_create_sensor(b2World *world_handle, b2Fixture *fixture = body_handle->CreateFixture(&fixture_def); fixture_handle.handles[i] = fixture; } + body_handle->SetMassData(&mass_data); return fixture_handle; } @@ -453,6 +455,7 @@ FixtureHandle box2d::collider_create_solid(b2World *world_handle, (b2Fixture **)memalloc((shape_handle.count) * sizeof(b2Fixture *)), shape_handle.count }; + b2MassData mass_data = body_handle->GetMassData(); for (int i = 0; i < shape_handle.count; i++) { b2FixtureDef fixture_def; fixture_def.shape = shape_handle.handles[i]; @@ -464,6 +467,7 @@ FixtureHandle box2d::collider_create_solid(b2World *world_handle, b2Fixture *fixture = body_handle->CreateFixture(&fixture_def); fixture_handle.handles[i] = fixture; } + body_handle->SetMassData(&mass_data); return fixture_handle; } diff --git a/src/spaces/box2d_space_2d.cpp b/src/spaces/box2d_space_2d.cpp index 1d16d4e..0a30e51 100644 --- a/src/spaces/box2d_space_2d.cpp +++ b/src/spaces/box2d_space_2d.cpp @@ -219,17 +219,23 @@ void Box2DSpace2D::PreSolve(b2Contact *contact, const b2Manifold *oldManifold) { if (collision_object_1->get_type() == Box2DCollisionObject2D::TYPE_BODY && collision_object_2->get_type() == Box2DCollisionObject2D::TYPE_BODY) { Box2DBody2D *body1 = static_cast(collision_object_1); Box2DBody2D *body2 = static_cast(collision_object_2); - if (body1->get_static_linear_velocity() != Vector2()) { - body2->to_add_static_constant_linear_velocity(body1->get_static_linear_velocity()); - } - if (body2->get_static_linear_velocity() != Vector2()) { - body1->to_add_static_constant_linear_velocity(body2->get_static_linear_velocity()); - } - if (body1->get_static_angular_velocity() != 0.0) { - body2->to_add_static_constant_angular_velocity(body1->get_static_angular_velocity()); + if (body1->get_mode() == PhysicsServer2D::BodyMode::BODY_MODE_STATIC || + body1->get_mode() == PhysicsServer2D::BodyMode::BODY_MODE_KINEMATIC) { + if (body1->get_static_linear_velocity() != Vector2()) { + body2->to_add_static_constant_linear_velocity(body1->get_static_linear_velocity()); + } + if (body1->get_static_angular_velocity() != 0.0) { + body2->to_add_static_constant_angular_velocity(body1->get_static_angular_velocity()); + } } - if (body2->get_static_angular_velocity() != 0.0) { - body1->to_add_static_constant_angular_velocity(body2->get_static_angular_velocity()); + if (body2->get_mode() == PhysicsServer2D::BodyMode::BODY_MODE_STATIC || + body2->get_mode() == PhysicsServer2D::BodyMode::BODY_MODE_KINEMATIC) { + if (body2->get_static_linear_velocity() != Vector2()) { + body1->to_add_static_constant_linear_velocity(body2->get_static_linear_velocity()); + } + if (body2->get_static_angular_velocity() != 0.0) { + body1->to_add_static_constant_angular_velocity(body2->get_static_angular_velocity()); + } } } }