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

Commit

Permalink
fix joint crash. also fix test_move and shape_collide
Browse files Browse the repository at this point in the history
Update box2d_joint.cpp

Update README.md

update readme

Update README.md
  • Loading branch information
Ughuuu committed Aug 2, 2023
1 parent 505bee3 commit 40e58f9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

## [v0.5](https://github.com/godot-box2d/godot-box2d/releases/tag/v0.5)

- Fix shapes not updating(changing shape geometry)
- Fix deleting shapes/body/spaces bug
- Fix [shapes not updating(changing shape geometry)](https://github.com/godot-box2d/godot-box2d/issues/16)
- Fix [deleting shapes/body/spaces bug](https://github.com/godot-box2d/godot-box2d/issues/18)
- Possible fix for [collision between static body and area bug](https://github.com/godot-box2d/godot-box2d/issues/19)

## [v0.4](https://github.com/godot-box2d/godot-box2d/releases/tag/v0.4)

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

A [box2D](https://github.com/erincatto/box2d) physics server for [Godot Engine](https://github.com/godotengine/godot) 4.1, implemented as a GDExtension.

Currently passes 127/150 of [Godot-Physics-Tests](https://github.com/fabriceci/Godot-Physics-Tests).

## Determinism

Box2D is binary deterministic, but doesn't make any guaratee as for cross platform determinism.
Godot Box2D should also be binary deterministic, however no such tests were run yet.
However, when Box2C gets released, that will be one of this repo's goals to make it cross platform deterministic (from what I spoke to Erin, it should be just the cos and sin functions that are not determinstic).

## Limitations

- Having non symetrical physics mask/layers results in no collision. Eg. if we had the following:
Expand Down
20 changes: 11 additions & 9 deletions src/joints/box2d_joint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ void Box2DJoint::make_pin(const Vector2 &p_anchor, Box2DBody *p_body_a, Box2DBod
joint_def = revolute_joint_def;
common.body_a = p_body_a;
common.body_b = p_body_b;
// body_a and body_b are set when joint is created
configured = true;
configured = common.body_a && common.body_b;
}

void Box2DJoint::make_groove(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, Box2DBody *p_body_a, Box2DBody *p_body_b) {
Expand All @@ -90,8 +89,7 @@ void Box2DJoint::make_groove(const Vector2 &p_a_groove1, const Vector2 &p_a_groo
joint_def = prismatic_joint_def;
common.body_a = p_body_a;
common.body_b = p_body_b;
// body_a and body_b are set when joint is created
configured = true;
configured = common.body_a && common.body_b;
}

void Box2DJoint::make_damped_spring(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, Box2DBody *p_body_a, Box2DBody *p_body_b) {
Expand All @@ -104,8 +102,7 @@ void Box2DJoint::make_damped_spring(const Vector2 &p_anchor_a, const Vector2 &p_
joint_def = distance_joint_def;
common.body_a = p_body_a;
common.body_b = p_body_b;
// body_a and body_b are set when joint is created
configured = true;
configured = common.body_a && common.body_b;
}

void Box2DJoint::set_pin_softness(real_t p_softness) {
Expand All @@ -121,7 +118,7 @@ void Box2DJoint::set_damped_spring_rest_length(real_t p_damped_spring_rest_lengt
return;
}
damped_spring.rest_length = godot_to_box2d(p_damped_spring_rest_length);
if (joint && (b2DistanceJoint *)joint) {
if (joint) {
b2DistanceJoint *distance_joint = (b2DistanceJoint *)joint;
distance_joint->SetLength(damped_spring.rest_length);
}
Expand All @@ -135,7 +132,7 @@ void Box2DJoint::set_damped_spring_stiffness(real_t p_damped_spring_stiffness) {
return;
}
damped_spring.stiffness = godot_to_box2d(p_damped_spring_stiffness);
if (joint && (b2DistanceJoint *)joint && common.body_a && common.body_b) {
if (joint && common.body_a && common.body_b) {
b2DistanceJoint *distance_joint = (b2DistanceJoint *)joint;
float stiffness = 0;
float damping = 0;
Expand All @@ -152,7 +149,7 @@ void Box2DJoint::set_damped_spring_damping(real_t p_damped_spring_damping) {
return;
}
damped_spring.damping = godot_to_box2d(p_damped_spring_damping);
if (joint && (b2DistanceJoint *)joint && common.body_a && common.body_b) {
if (joint && common.body_a && common.body_b) {
b2DistanceJoint *distance_joint = (b2DistanceJoint *)joint;
float stiffness = 0;
float damping = 0;
Expand All @@ -172,6 +169,10 @@ Box2DBody *Box2DJoint::get_body_b() {
}

b2JointDef *Box2DJoint::get_b2JointDef() {
if (!common.body_a || !common.body_b) {
WARN_PRINT("Cannot configure joint. Missing one body.");
return joint_def;
}
switch (type) {
case PhysicsServer2D::JointType::JOINT_TYPE_PIN: {
b2RevoluteJointDef *revolute_joint_def = (b2RevoluteJointDef *)joint_def;
Expand All @@ -184,6 +185,7 @@ b2JointDef *Box2DJoint::get_b2JointDef() {
distance_joint_def->Initialize(common.body_a->get_b2Body(), common.body_b->get_b2Body(), common.anchor_a, common.anchor_b);

distance_joint_def->length = damped_spring.rest_length;
distance_joint_def->minLength = 0;
} break;
case PhysicsServer2D::JointType::JOINT_TYPE_GROOVE: {
b2PrismaticJointDef *prismatic_joint_def = (b2PrismaticJointDef *)joint_def;
Expand Down
6 changes: 3 additions & 3 deletions src/servers/physics_server_box2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ bool PhysicsServerBox2D::_body_collide_shape(const RID &p_body, int32_t p_body_s
}
Box2DDirectSpaceState *space_state = (Box2DDirectSpaceState *)body->get_space_state();
ERR_FAIL_COND_V(!space_state, false);
return space_state->_collide_shape(shape->get_self(), p_shape_xform, p_motion, 0, body->get_collision_mask(), true, true, p_results, p_result_max, p_result_count);
return space_state->_collide_shape(shape->get_self(), p_shape_xform, p_motion, 0, body->get_collision_mask(), true, false, p_results, p_result_max, p_result_count);
}
void PhysicsServerBox2D::_body_set_pickable(const RID &p_body, bool p_pickable) {
Box2DBody *body = body_owner.get_or_null(p_body);
Expand All @@ -1001,8 +1001,8 @@ bool PhysicsServerBox2D::_body_test_motion(const RID &p_body, const Transform2D
shapes.append(shape);
}

Vector<b2Fixture *> query_result = Box2DSweepTest::query_aabb_motion(shapes, p_from, p_motion, p_margin, 0xff, true, true, (Box2DDirectSpaceState *)body->get_space_state());
Vector<SweepTestResult> sweep_test_results = Box2DSweepTest::multiple_shapes_cast(shapes, p_from, p_motion, p_margin, 0xff, true, true, 2048, query_result, (Box2DDirectSpaceState *)body->get_space_state());
Vector<b2Fixture *> query_result = Box2DSweepTest::query_aabb_motion(shapes, p_from, p_motion, p_margin, 0xff, true, false, (Box2DDirectSpaceState *)body->get_space_state());
Vector<SweepTestResult> sweep_test_results = Box2DSweepTest::multiple_shapes_cast(shapes, p_from, p_motion, p_margin, 0xff, true, false, 2048, query_result, (Box2DDirectSpaceState *)body->get_space_state());
SweepTestResult sweep_test_result = Box2DSweepTest::closest_result_in_cast(sweep_test_results);
PhysicsServer2DExtensionMotionResult &current_result = *p_result;
if (!sweep_test_result.collision) {
Expand Down

0 comments on commit 40e58f9

Please sign in to comment.