Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

Commit

Permalink
fix shape transfer wrong for dynamic bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu committed Nov 30, 2023
1 parent e819f2c commit cc13ecd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/bodies/box2d_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions src/box2d-wrapper/box2d_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
}

Expand All @@ -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];
Expand All @@ -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;
}

Expand Down
26 changes: 16 additions & 10 deletions src/spaces/box2d_space_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box2DBody2D *>(collision_object_1);
Box2DBody2D *body2 = static_cast<Box2DBody2D *>(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());
}
}
}
}
Expand Down

0 comments on commit cc13ecd

Please sign in to comment.