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

fix large rotation and also do rotation in smallest direction #71

Merged
merged 4 commits into from
Nov 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/box2d-wrapper/box2d_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,15 @@ void box2d::body_set_transform(b2World *world_handle,
b2Vec2 new_pos = (pos - body_handle->GetPosition());
new_pos.x /= step;
new_pos.y /= step;
if (body_handle->GetType() == b2BodyType::b2_kinematicBody && b2Dot(new_pos, new_pos) < b2_maxTranslationSquared) {
real_t new_rot1 = (rot - body_handle->GetAngle()) / step;
real_t new_rot2 = (b2_pi + rot + body_handle->GetAngle()) / step;
real_t new_rot = new_rot1;
if (ABS(new_rot2) < ABS(new_rot1)) {
new_rot = new_rot2;
}
if (body_handle->GetType() == b2BodyType::b2_kinematicBody && b2Dot(new_pos, new_pos) < b2_maxTranslationSquared && (new_rot * new_rot < 180.0 * 180.0 * 0.5 * 0.5)) {
body_handle->SetLinearVelocity(new_pos);
body_handle->SetAngularVelocity((rot - body_handle->GetAngle()) / step);
body_handle->SetAngularVelocity(new_rot);
body_handle->SetAwake(true);
} else {
body_handle->SetTransform(pos, rot);
Expand Down Expand Up @@ -858,7 +864,6 @@ 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 Down Expand Up @@ -1020,12 +1025,16 @@ ShapeHandle box2d::shape_create_box(const b2Vec2 size) {
ShapeHandle box2d::shape_create_capsule(real_t half_height, real_t radius) {
ERR_FAIL_COND_V(radius < CMP_EPSILON, invalid_shape_handle());
ERR_FAIL_COND_V(half_height < CMP_EPSILON, invalid_shape_handle());
ERR_FAIL_COND_V(half_height < radius + CMP_EPSILON, invalid_shape_handle());
ShapeHandle top_circle = shape_create_circle(radius, { 0.0, -half_height * real_t(0.5) });
ERR_FAIL_COND_V(half_height < radius, invalid_shape_handle());
real_t half_height_circle = half_height - radius;
ShapeHandle top_circle = shape_create_circle(radius, { 0.0, -half_height_circle });
ERR_FAIL_COND_V(!is_handle_valid(top_circle), invalid_shape_handle());
ShapeHandle bottom_circle = shape_create_circle(radius, { 0.0, half_height * real_t(0.5) });
if (half_height - radius == 0.0) {
return top_circle;
}
ShapeHandle bottom_circle = shape_create_circle(radius, { 0.0, half_height_circle });
ERR_FAIL_COND_V(!is_handle_valid(bottom_circle), invalid_shape_handle());
ShapeHandle square = shape_create_box({ radius, half_height - radius });
ShapeHandle square = shape_create_box({ radius * 2, half_height * 2 - radius * 2 });
ERR_FAIL_COND_V(!is_handle_valid(square), invalid_shape_handle());
b2Shape **shapes = (b2Shape **)memalloc((3) * sizeof(b2Shape *));
// TODO fix this leak.
Expand Down
Loading