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

Commit

Permalink
lint/update groove joint
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu committed Nov 12, 2023
1 parent 5b25e6d commit 14996a7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 22 deletions.
29 changes: 20 additions & 9 deletions src/box2d-wrapper/box2d_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,10 @@ bool box2d::is_handle_valid(b2Joint *handle) {
return handle != nullptr;
}

void box2d::joint_set_disable_collision(b2Joint *joint_handle,
bool disable_collision) {
}

void box2d::joint_change_revolute_params(b2World *world_handle,
b2Joint *joint_handle,
real_t angular_limit_lower,
Expand All @@ -796,14 +800,17 @@ b2Joint *box2d::joint_create_prismatic(b2World *world_handle,
const b2Vec2 axis,
const b2Vec2 anchor_1,
const b2Vec2 anchor_2,
const b2Vec2 limits) {
const b2Vec2 limits,
bool disable_collision) {
b2PrismaticJointDef joint_def;
joint_def.bodyA = body_handle_1;
joint_def.bodyB = body_handle_2;
joint_def.Initialize(body_handle_1, body_handle_2, anchor_1, axis);
joint_def.localAnchorA = anchor_1;
joint_def.localAnchorB = anchor_2;
joint_def.lowerTranslation = limits.x;
joint_def.upperTranslation = limits.y;
joint_def.localAxisA = axis;
joint_def.lowerTranslation = 0;
joint_def.upperTranslation = limits.x + limits.y * 2;
joint_def.enableLimit = true;
joint_def.collideConnected = !disable_collision;
return world_handle->CreateJoint(&joint_def);
}

Expand All @@ -816,10 +823,10 @@ b2Joint *box2d::joint_create_revolute(b2World *world_handle,
real_t angular_limit_upper,
bool angular_limit_enabled,
real_t motor_target_velocity,
bool motor_enabled) {
bool motor_enabled,
bool disable_collision) {
b2RevoluteJointDef joint_def;
joint_def.bodyA = body_handle_1;
joint_def.bodyB = body_handle_2;
joint_def.Initialize(body_handle_1, body_handle_2, anchor_1);
joint_def.localAnchorA = anchor_1;
joint_def.localAnchorB = anchor_2;
joint_def.enableLimit = angular_limit_enabled;
Expand All @@ -828,6 +835,8 @@ b2Joint *box2d::joint_create_revolute(b2World *world_handle,
joint_def.upperAngle = angular_limit_upper;
joint_def.motorSpeed = motor_target_velocity;
joint_def.maxMotorTorque = 100000.0;
joint_def.collideConnected = true;
joint_def.collideConnected = !disable_collision;
return world_handle->CreateJoint(&joint_def);
}

Expand All @@ -849,7 +858,8 @@ b2Joint *box2d::joint_create_distance_joint(b2World *world_handle,
const b2Vec2 anchor_2,
real_t rest_length,
real_t stiffness,
real_t damping) {
real_t damping,
bool disable_collision) {
b2DistanceJointDef joint_def;
joint_def.bodyA = body_handle_1;
joint_def.bodyB = body_handle_2;
Expand All @@ -858,6 +868,7 @@ b2Joint *box2d::joint_create_distance_joint(b2World *world_handle,
joint_def.length = rest_length;
joint_def.stiffness = stiffness;
joint_def.damping = damping;
joint_def.collideConnected = !disable_collision;
return world_handle->CreateJoint(&joint_def);
}

Expand Down
12 changes: 9 additions & 3 deletions src/box2d-wrapper/box2d_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ bool is_handle_valid(b2Fixture *handle);
bool is_user_data_valid(b2FixtureUserData user_data);
bool is_user_data_valid(b2BodyUserData user_data);

void joint_set_disable_collision(b2Joint *joint_handle,
bool disable_collision);

void joint_change_revolute_params(b2World *world_handle,
b2Joint *joint_handle,
real_t angular_limit_lower,
Expand All @@ -403,7 +406,8 @@ b2Joint *joint_create_prismatic(b2World *world_handle,
const b2Vec2 axis,
const b2Vec2 anchor_1,
const b2Vec2 anchor_2,
const b2Vec2 limits);
const b2Vec2 limits,
bool disable_collision);

b2Joint *joint_create_revolute(b2World *world_handle,
b2Body *body_handle_1,
Expand All @@ -414,7 +418,8 @@ b2Joint *joint_create_revolute(b2World *world_handle,
real_t angular_limit_upper,
bool angular_limit_enabled,
real_t motor_target_velocity,
bool motor_enabled);
bool motor_enabled,
bool disable_collision);

b2Joint *joint_create_distance_joint(b2World *world_handle,
b2Body *body_handle_1,
Expand All @@ -423,7 +428,8 @@ b2Joint *joint_create_distance_joint(b2World *world_handle,
const b2Vec2 anchor_2,
real_t rest_length,
real_t stiffness,
real_t damping);
real_t damping,
bool disable_collision);

void joint_change_distance_joint(b2World *world_handle,
b2Joint *joint_handle,
Expand Down
2 changes: 1 addition & 1 deletion src/joints/box2d_damped_spring_joint_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ Box2DDampedSpringJoint2D::Box2DDampedSpringJoint2D(const Vector2 &p_anchor_a, co
ERR_FAIL_COND(p_body_a->get_space() != p_body_b->get_space());
space_handle = p_body_a->get_space()->get_handle();
ERR_FAIL_COND(!box2d::is_handle_valid(space_handle));
handle = box2d::joint_create_distance_joint(space_handle, p_body_a->get_body_handle(), p_body_b->get_body_handle(), box2d_anchor_A, box2d_anchor_B, rest_length, stiffness, damping);
handle = box2d::joint_create_distance_joint(space_handle, p_body_a->get_body_handle(), p_body_b->get_body_handle(), box2d_anchor_A, box2d_anchor_B, rest_length, stiffness, damping, is_disabled_collisions_between_bodies());
ERR_FAIL_COND(!box2d::is_handle_valid(handle));
}
11 changes: 4 additions & 7 deletions src/joints/box2d_groove_joint_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,19 @@ Box2DGrooveJoint2D::Box2DGrooveJoint2D(const Vector2 &p_a_groove1, const Vector2
Vector2 anchor_B = B->get_inv_transform().xform(p_b_anchor);

Vector2 axis = (point_A_2 - point_A_1).normalized();
real_t length = (point_A_2 - point_A_1).length();
real_t length_lower = (p_a_groove2 - p_b_anchor).length();
real_t length_upper = (p_a_groove1 - p_b_anchor).length();

b2Vec2 box2d_anchor_A = { point_A_1.x, point_A_1.y };
b2Vec2 box2d_anchor_B = { anchor_B.x, anchor_B.y };

b2Vec2 box2d_axis = { axis.x, axis.y };
b2Vec2 box2d_limits = { 0.0, length };
b2Vec2 box2d_limits = { length_lower, length_upper };

ERR_FAIL_COND(!p_body_a->get_space());
ERR_FAIL_COND(p_body_a->get_space() != p_body_b->get_space());
space_handle = p_body_a->get_space()->get_handle();
ERR_FAIL_COND(!box2d::is_handle_valid(space_handle));

handle = box2d::joint_create_prismatic(space_handle, p_body_a->get_body_handle(), p_body_b->get_body_handle(), box2d_axis, box2d_anchor_A, box2d_anchor_B, box2d_limits);
handle = box2d::joint_create_prismatic(space_handle, p_body_a->get_body_handle(), p_body_b->get_body_handle(), box2d_axis, box2d_anchor_A, box2d_anchor_B, box2d_limits, is_disabled_collisions_between_bodies());
ERR_FAIL_COND(!box2d::is_handle_valid(handle));

//A->add_constraint(this, 0);
//B->add_constraint(this, 1);
}
7 changes: 7 additions & 0 deletions src/joints/box2d_joint_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ void Box2DJoint2D::copy_settings_from(Box2DJoint2D *p_joint) {
disable_collisions_between_bodies(p_joint->is_disabled_collisions_between_bodies());
}

void Box2DJoint2D::disable_collisions_between_bodies(const bool p_disabled) {
disabled_collisions_between_bodies = p_disabled;
if (box2d::is_handle_valid(handle)) {
box2d::joint_set_disable_collision(handle, disabled_collisions_between_bodies);
}
}

Box2DJoint2D::~Box2DJoint2D() {
if (box2d::is_handle_valid(handle)) {
ERR_FAIL_COND(!box2d::is_handle_valid(space_handle));
Expand Down
2 changes: 1 addition & 1 deletion src/joints/box2d_joint_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Box2DJoint2D {
_FORCE_INLINE_ void set_rid(const RID &p_rid) { rid = p_rid; }
_FORCE_INLINE_ RID get_rid() const { return rid; }

_FORCE_INLINE_ void disable_collisions_between_bodies(const bool p_disabled) { disabled_collisions_between_bodies = p_disabled; }
void disable_collisions_between_bodies(const bool p_disabled);
_FORCE_INLINE_ bool is_disabled_collisions_between_bodies() const { return disabled_collisions_between_bodies; }

_FORCE_INLINE_ void set_max_force(real_t p_force) { max_force = p_force; }
Expand Down
2 changes: 1 addition & 1 deletion src/joints/box2d_pin_joint_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ Box2DPinJoint2D::Box2DPinJoint2D(const Vector2 &p_pos, Box2DBody2D *p_body_a, Bo
ERR_FAIL_COND(p_body_a->get_space() != p_body_b->get_space());
space_handle = p_body_a->get_space()->get_handle();
ERR_FAIL_COND(!box2d::is_handle_valid(space_handle));
handle = box2d::joint_create_revolute(space_handle, p_body_a->get_body_handle(), p_body_b->get_body_handle(), box2d_anchor_A, box2d_anchor_B, angular_limit_lower, angular_limit_upper, angular_limit_enabled, motor_target_velocity, motor_enabled);
handle = box2d::joint_create_revolute(space_handle, p_body_a->get_body_handle(), p_body_b->get_body_handle(), box2d_anchor_A, box2d_anchor_B, angular_limit_lower, angular_limit_upper, angular_limit_enabled, motor_target_velocity, motor_enabled, is_disabled_collisions_between_bodies());
ERR_FAIL_COND(!box2d::is_handle_valid(handle));
}

0 comments on commit 14996a7

Please sign in to comment.