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

Commit

Permalink
update skewing and scaling
Browse files Browse the repository at this point in the history
update capsule scaling
  • Loading branch information
Ughuuu committed Jul 26, 2023
1 parent 7331b9a commit 3b6cf20
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fix concave polygon
- Add support for conveyer belt
- Add build for web but not enabled yet (bug in godot-cpp)
- Update/fix support for skewing/scaling for shapes.

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

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ A [box2D](https://github.com/erincatto/box2d) physics server for [Godot Engine](

In godot would result a collision, but would be very weird, as only one of the objects would receive collision restution.

- Shape scaling and skewing:
- Circles and capsules only support uniform scaling and don't support skewing

## Missing/Not implemented

- Skewed shapes
Expand Down
8 changes: 8 additions & 0 deletions src/bodies/box2d_collision_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void Box2DCollisionObject::reset_mass_properties() {
//mass_data = body->GetMassData();
mass_data.mass = 1.0f;
body->SetMassData(&mass_data);
mass_data.center = body->GetLocalCenter();
mass_data.I = body->GetMassData().I;
} else {
mass_data.mass = 1.0f;
Expand All @@ -35,6 +36,7 @@ void Box2DCollisionObject::set_mass(real_t p_mass) {
mass_data.mass = p_mass;
if (body) {
body->SetMassData(&mass_data);
mass_data.center = body->GetLocalCenter();
mass_data.I = body->GetMassData().I;
}
}
Expand All @@ -51,6 +53,7 @@ void Box2DCollisionObject::set_inertia(real_t p_inertia) {
mass_data.I = godot_to_box2d(godot_to_box2d(p_inertia));
if (body) {
body->SetMassData(&mass_data);
mass_data.center = body->GetLocalCenter();
mass_data.I = body->GetMassData().I;
}
}
Expand All @@ -64,6 +67,7 @@ void Box2DCollisionObject::set_center_of_mass(Vector2 p_center_of_mass) {
godot_to_box2d(p_center_of_mass, mass_data.center);
if (body) {
body->SetMassData(&mass_data);
mass_data.center = body->GetLocalCenter();
mass_data.I = body->GetMassData().I;
}
}
Expand Down Expand Up @@ -807,6 +811,9 @@ void Box2DCollisionObject::_update_shapes() {
mass_data = body->GetMassData();
// revert mass
mass_data.mass = old_mass;
// TODO only do this if we need to automatically compute local center.
mass_data.center = body->GetLocalCenter();
//mass_data.center = b2Vec2_zero;
body->SetMassData(&mass_data);
//space->get_broadphase()->move(s.bpid, shape_aabb);
}
Expand Down Expand Up @@ -890,6 +897,7 @@ void Box2DCollisionObject::set_b2Body(b2Body *p_body) {
// set additional properties here
if (body) {
body->SetMassData(&mass_data);
mass_data.center = body->GetLocalCenter();
mass_data.I = body->GetMassData().I;
body->SetAwake(true);
//recreate_shapes();
Expand Down
21 changes: 17 additions & 4 deletions src/shapes/box2d_shape_capsule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,39 @@ int Box2DShapeCapsule::get_b2Shape_count(bool is_static) const {

b2Shape *Box2DShapeCapsule::get_transformed_b2Shape(int p_index, Transform2D &p_transform, bool one_way, bool is_static) {
ERR_FAIL_INDEX_V(p_index, 3, nullptr);
Vector2 scale = p_transform.get_scale();
if (scale.x != scale.y) {
ERR_PRINT("Capsules don't support non uniform scale.");
}
float radius_scaled;
godot_to_box2d(radius * scale.x, radius_scaled);
if (p_index == 0 || p_index == 1) {
b2CircleShape *shape = memnew(b2CircleShape);
godot_to_box2d(radius, shape->m_radius);
shape->m_radius = radius_scaled;
real_t circle_height = (height * 0.5 - radius) * (p_index == 0 ? 1.0 : -1.0);
if (circle_height < b2_linearSlop) {
circle_height = b2_linearSlop;
}
godot_to_box2d(p_transform.xform(Vector2(0, circle_height)), shape->m_p);
return shape;
}
b2PolygonShape *shape = memnew(b2PolygonShape);
Vector2 half_extents(radius, height * 0.5 - radius);
if (half_extents.x < GODOT_LINEAR_SLOP) {
half_extents.x = GODOT_LINEAR_SLOP;
}
if (half_extents.y < GODOT_LINEAR_SLOP) {
half_extents.y = GODOT_LINEAR_SLOP;
}

b2PolygonShape *shape = memnew(b2PolygonShape);
b2Vec2 box2d_half_extents = godot_to_box2d(half_extents);
b2Vec2 box2d_origin = godot_to_box2d(p_transform.get_origin());
shape->SetAsBox(box2d_half_extents.x, box2d_half_extents.y, box2d_origin, p_transform.get_rotation());
b2Vec2 *box2d_points = new b2Vec2[4];
godot_to_box2d(p_transform.xform(Vector2(-half_extents.x, -half_extents.y)), box2d_points[0]);
godot_to_box2d(p_transform.xform(Vector2(-half_extents.x, half_extents.y)), box2d_points[1]);
godot_to_box2d(p_transform.xform(Vector2(half_extents.x, half_extents.y)), box2d_points[2]);
godot_to_box2d(p_transform.xform(Vector2(half_extents.x, -half_extents.y)), box2d_points[3]);
shape->Set(box2d_points, 4);
delete[] box2d_points;

return shape;
}
10 changes: 5 additions & 5 deletions src/shapes/box2d_shape_concave_polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void Box2DShapeConcavePolygon::set_data(const Variant &p_data) {
for (int i = 0; i < points_array.size(); i++) {
points.write[i] = points_array[i];
}
points = Box2DShapeConvexPolygon::make_sure_polygon_is_counterclockwise(points);
points = Box2DShapeConvexPolygon::remove_points_that_are_too_close(points);
configured = true;
}
Expand Down Expand Up @@ -44,7 +45,6 @@ b2Shape *Box2DShapeConcavePolygon::get_transformed_b2Shape(int p_index, Transfor
godot_to_box2d(p_transform.xform(points[i]), box2d_points[i]);
}
int points_count = points.size();
points_count = Box2DShapeConvexPolygon::remove_bad_points(box2d_points, points_count);
ERR_FAIL_COND_V(points_count < 3, nullptr);
shape->CreateLoop(box2d_points, points_count);
delete[] box2d_points;
Expand All @@ -58,10 +58,10 @@ b2Shape *Box2DShapeConcavePolygon::get_transformed_b2Shape(int p_index, Transfor
Vector2 b = points[(p_index + 1) % points.size()];
Vector2 dir = (a - b).normalized();
Vector2 right(dir.y, -dir.x);
godot_to_box2d(p_transform.xform(a - right * 0.1), box2d_points[0]);
godot_to_box2d(p_transform.xform(a + right * 0.1), box2d_points[1]);
godot_to_box2d(p_transform.xform(b - right * 0.1), box2d_points[2]);
godot_to_box2d(p_transform.xform(b + right * 0.1), box2d_points[3]);
godot_to_box2d(p_transform.xform(a - right * 0.5), box2d_points[0]);
godot_to_box2d(p_transform.xform(a + right * 0.5), box2d_points[1]);
godot_to_box2d(p_transform.xform(b - right * 0.5), box2d_points[2]);
godot_to_box2d(p_transform.xform(b + right * 0.5), box2d_points[3]);
shape->Set(box2d_points, 4);
delete[] box2d_points;
return shape;
Expand Down
20 changes: 19 additions & 1 deletion src/shapes/box2d_shape_convex_polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void Box2DShapeConvexPolygon::set_data(const Variant &p_data) {
for (int i = 0; i < points_array.size(); i++) {
points.write[i] = points_array[i];
}

points = make_sure_polygon_is_counterclockwise(points);
points = remove_points_that_are_too_close(points);
ERR_FAIL_COND(points.size() < 3);
polygons.clear();
Expand Down Expand Up @@ -51,6 +51,24 @@ int Box2DShapeConvexPolygon::get_b2Shape_count(bool is_static) const {
return polygons.size();
}

float compute_polygon_area(Vector<Vector2> points) {
float area = 0.0f;
for (int i = 0; i < points.size(); i++) {
int j = (i + 1) % points.size();
area += points[j][0] * points[i][1] - points[i][0] * points[j][1];
}
return area / 2.0f;
}

Vector<Vector2> Box2DShapeConvexPolygon::make_sure_polygon_is_counterclockwise(Vector<Vector2> points) {
Vector<Vector2> ccw_points = points;
// polygon is clockwise
if (compute_polygon_area(ccw_points) > 0) {
ccw_points.reverse();
}
return ccw_points;
}

Vector<Vector2> Box2DShapeConvexPolygon::remove_points_that_are_too_close(Vector<Vector2> points) {
int32 tempCount = 0;
Vector<Vector2> new_points;
Expand Down
1 change: 1 addition & 0 deletions src/shapes/box2d_shape_convex_polygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Box2DShapeConvexPolygon : public Box2DShape {
public:
static Vector<Vector2> remove_points_that_are_too_close(Vector<Vector2> points);
static int remove_bad_points(b2Vec2 *vertices, int32 count);
static Vector<Vector2> make_sure_polygon_is_counterclockwise(Vector<Vector2> points);
virtual void set_data(const Variant &p_data) override;
virtual Variant get_data() const override;
virtual int get_b2Shape_count(bool is_static) const override;
Expand Down

0 comments on commit 3b6cf20

Please sign in to comment.