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

Commit

Permalink
refactor 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu committed Aug 3, 2023
1 parent fc98c68 commit b466590
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 114 deletions.
36 changes: 15 additions & 21 deletions src/b2_user_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,29 @@

// Tunable Constants

/// You can use this to change the length scale used by your game.
/// For example for inches you could use 39.4.
// You can use this to change the length scale used by your game.
// For example for inches you could use 39.4.
#define b2_lengthUnitsPerMeter 1.0f

/// The maximum number of vertices on a convex polygon. You cannot increase
/// this too much because b2BlockAllocator has a maximum object size.
// The maximum number of vertices on a convex polygon. You cannot increase
// this too much because b2BlockAllocator has a maximum object size.
#define b2_maxPolygonVertices 8

// User data

class Box2DCollisionObject;
class Box2DShape;

// You can define this to inject whatever data you want in b2Body
struct B2_API b2BodyUserData {
b2BodyUserData() {
collision_object = nullptr;
}
b2BodyUserData() :
collision_object(nullptr) {}

Box2DCollisionObject *collision_object;
};

// You can define this to inject whatever data you want in b2Fixture
struct B2_API b2FixtureUserData {
b2FixtureUserData() {
shape_idx = -1;
box2d_fixture_idx = 0;
shape = nullptr;
}
b2FixtureUserData() :
shape_idx(-1), box2d_fixture_idx(0), shape(nullptr) {}

int shape_idx;
int box2d_fixture_idx;
Expand All @@ -42,12 +38,10 @@ struct B2_API b2FixtureUserData {

/// You can define this to inject whatever data you want in b2Joint
struct B2_API b2JointUserData {
b2JointUserData() {
pointer = 0;
}
b2JointUserData() :
pointer(0) {}

/// For legacy compatibility
uintptr_t pointer;
uintptr_t pointer; // For legacy compatibility
};

// Memory Allocation using Godot's functions
Expand All @@ -60,10 +54,10 @@ inline void b2Free(void *mem) {
memfree(mem);
}

/// Default logging function
// Default logging function
B2_API void b2Log_Default(const char *string, va_list args);

/// Implement this to use your own logging.
// Implement this to use your own logging.
inline void b2Log(const char *string, ...) {
va_list args;
va_start(args, string);
Expand Down
2 changes: 1 addition & 1 deletion src/bodies/box2d_collision_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void Box2DCollisionObject::set_center_of_mass(Vector2 p_center_of_mass) {
if (godot_to_box2d(p_center_of_mass) == mass_data.center) {
return;
}
godot_to_box2d(p_center_of_mass, mass_data.center);
mass_data.center = godot_to_box2d(p_center_of_mass);
if (body) {
body->SetMassData(&mass_data);
mass_data.center = body->GetLocalCenter();
Expand Down
49 changes: 8 additions & 41 deletions src/box2d_type_conversions.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,18 @@
#include "box2d_type_conversions.h"

void godot_to_box2d(const float &p_value, float &r_box2d_value) {
r_box2d_value = godot_to_box2d(p_value);
float godot_to_box2d(const float &p_value, float scale_factor) {
return p_value * scale_factor;
}

void godot_to_box2d(const double &p_value, float &r_box2d_value) {
r_box2d_value = godot_to_box2d(p_value);
b2Vec2 godot_to_box2d(const Vector2 &p_vector, float scale_factor) {
return b2Vec2(godot_to_box2d(p_vector.x, scale_factor), godot_to_box2d(p_vector.y, scale_factor));
}

void godot_to_box2d(const Vector2 &p_vector, b2Vec2 &r_box2d_vector) {
r_box2d_vector.x = p_vector.x * G_TO_B_FACTOR;
r_box2d_vector.y = p_vector.y * G_TO_B_FACTOR;
float box2d_to_godot(const float &p_box2d_value, float scale_factor) {
return scale_factor * p_box2d_value;
}

float godot_to_box2d(const float &p_value) {
return p_value * G_TO_B_FACTOR;
}

float godot_to_box2d(const double &p_value) {
return (float)(p_value * G_TO_B_FACTOR);
}

b2Vec2 godot_to_box2d(const Vector2 &p_vector) {
return b2Vec2(p_vector.x * G_TO_B_FACTOR, p_vector.y * G_TO_B_FACTOR);
}

void box2d_to_godot(const float &p_box2d_value, float &r_value) {
r_value = box2d_to_godot(p_box2d_value);
}

void box2d_to_godot(const float &p_box2d_value, double &r_value) {
r_value = box2d_to_godot_d(p_box2d_value);
}

void box2d_to_godot(const b2Vec2 &p_box2d_vector, Vector2 &r_vector) {
r_vector.x = B_TO_G_FACTOR * p_box2d_vector.x;
r_vector.y = B_TO_G_FACTOR * p_box2d_vector.y;
}

float box2d_to_godot(const float &p_box2d_value) {
return B_TO_G_FACTOR * p_box2d_value;
}
double box2d_to_godot_d(const float &p_box2d_value) {
return (double)(B_TO_G_FACTOR * p_box2d_value);
}
Vector2 box2d_to_godot(const b2Vec2 &p_box2d_vector) {
return Vector2(B_TO_G_FACTOR * p_box2d_vector.x, B_TO_G_FACTOR * p_box2d_vector.y);
Vector2 box2d_to_godot(const b2Vec2 &p_box2d_vector, float scale_factor) {
return Vector2(box2d_to_godot(p_box2d_vector.x, scale_factor), box2d_to_godot(p_box2d_vector.y, scale_factor));
}

float variant_to_number(Variant p_variant) {
Expand Down
24 changes: 7 additions & 17 deletions src/box2d_type_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,14 @@

using namespace godot;

#define B_TO_G_FACTOR 100.0
#define G_TO_B_FACTOR 1.0 / 100.0
#define GODOT_LINEAR_SLOP b2_linearSlop *B_TO_G_FACTOR
constexpr float BOX2D_TO_GODOT_FACTOR = 100.0f;
constexpr float GODOT_TO_BOX2D_FACTOR = 1.0f / 100.0f;
constexpr float GODOT_LINEAR_SLOP = b2_linearSlop * BOX2D_TO_GODOT_FACTOR;

void godot_to_box2d(const float &p_value, float &r_box2d_value);
void godot_to_box2d(const double &p_value, float &r_box2d_value);
void godot_to_box2d(const Vector2 &p_vector, b2Vec2 &r_box2d_vector);
float godot_to_box2d(const float &p_value, float scale_factor = GODOT_TO_BOX2D_FACTOR);
b2Vec2 godot_to_box2d(const Vector2 &p_vector, float scale_factor = GODOT_TO_BOX2D_FACTOR);

float godot_to_box2d(const float &p_value);
float godot_to_box2d(const double &p_value);
b2Vec2 godot_to_box2d(const Vector2 &p_vector);

void box2d_to_godot(const float &p_box2d_value, float &r_value);
void box2d_to_godot(const float &p_box2d_value, double &r_value);
void box2d_to_godot(const b2Vec2 &p_box2d_vector, Vector2 &r_vector);

float box2d_to_godot(const float &p_box2d_value);
double box2d_to_godot_d(const float &p_box2d_value);
Vector2 box2d_to_godot(const b2Vec2 &p_box2d_vector);
float box2d_to_godot(const float &p_box2d_value, float scale_factor = BOX2D_TO_GODOT_FACTOR);
Vector2 box2d_to_godot(const b2Vec2 &p_box2d_vector, float scale_factor = BOX2D_TO_GODOT_FACTOR);

float variant_to_number(Variant p_variant);
10 changes: 5 additions & 5 deletions src/shapes/box2d_shape_capsule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ b2Shape *Box2DShapeCapsule::get_transformed_b2Shape(ShapeInfo shape_info, Box2DC
if (circle_height < b2_linearSlop) {
circle_height = b2_linearSlop;
}
godot_to_box2d(shape_info.transform.xform(Vector2(0, circle_height)), shape->m_p);
shape->m_p = godot_to_box2d(shape_info.transform.xform(Vector2(0, circle_height)));
return shape;
}
b2PolygonShape *shape = memnew(b2PolygonShape);
Expand All @@ -73,10 +73,10 @@ b2Shape *Box2DShapeCapsule::get_transformed_b2Shape(ShapeInfo shape_info, Box2DC

b2Vec2 box2d_half_extents = godot_to_box2d(half_extents);
b2Vec2 *box2d_points = new b2Vec2[4];
godot_to_box2d(shape_info.transform.xform(Vector2(-half_extents.x, -half_extents.y)), box2d_points[0]);
godot_to_box2d(shape_info.transform.xform(Vector2(-half_extents.x, half_extents.y)), box2d_points[1]);
godot_to_box2d(shape_info.transform.xform(Vector2(half_extents.x, half_extents.y)), box2d_points[2]);
godot_to_box2d(shape_info.transform.xform(Vector2(half_extents.x, -half_extents.y)), box2d_points[3]);
box2d_points[0] = godot_to_box2d(shape_info.transform.xform(Vector2(-half_extents.x, -half_extents.y)));
box2d_points[1] = godot_to_box2d(shape_info.transform.xform(Vector2(-half_extents.x, half_extents.y)));
box2d_points[2] = godot_to_box2d(shape_info.transform.xform(Vector2(half_extents.x, half_extents.y)));
box2d_points[3] = godot_to_box2d(shape_info.transform.xform(Vector2(half_extents.x, -half_extents.y)));
shape->Set(box2d_points, 4);
delete[] box2d_points;
return shape;
Expand Down
4 changes: 2 additions & 2 deletions src/shapes/box2d_shape_circle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ b2Shape *Box2DShapeCircle::get_transformed_b2Shape(ShapeInfo shape_info, Box2DCo
if (scale.x != scale.y) {
ERR_PRINT("Circles don't support non uniform scale.");
}
godot_to_box2d(radius * scale.x, shape->m_radius);
shape->m_radius = godot_to_box2d(radius * scale.x);
if (shape->m_radius < b2_linearSlop) {
shape->m_radius = b2_linearSlop;
}
godot_to_box2d(shape_info.transform.get_origin(), shape->m_p);
shape->m_p = godot_to_box2d(shape_info.transform.get_origin());
return shape;
}
12 changes: 7 additions & 5 deletions src/shapes/box2d_shape_concave_polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <box2d/b2_chain_shape.h>
#include <box2d/b2_polygon_shape.h>

constexpr float CHAIN_SEGMENT_SQUARE_SIZE = 0.5f;

void Box2DShapeConcavePolygon::set_data(const Variant &p_data) {
ERR_FAIL_COND(p_data.get_type() != Variant::PACKED_VECTOR2_ARRAY);
PackedVector2Array points_array = p_data;
Expand Down Expand Up @@ -48,7 +50,7 @@ b2Shape *Box2DShapeConcavePolygon::get_transformed_b2Shape(ShapeInfo shape_info,
ERR_FAIL_INDEX_V(shape_info.index, 1, nullptr);
b2Vec2 *box2d_points = new b2Vec2[points.size()];
for (int i = 0; i < points.size(); i++) {
godot_to_box2d(shape_info.transform.xform(points[i]), box2d_points[i]);
box2d_points[i] = godot_to_box2d(shape_info.transform.xform(points[i]));
}
int points_count = points.size();
ERR_FAIL_COND_V(points_count < 3, nullptr);
Expand All @@ -68,10 +70,10 @@ b2Shape *Box2DShapeConcavePolygon::get_transformed_b2Shape(ShapeInfo shape_info,
Vector2 b = points[(shape_info.index + 1) % points.size()];
Vector2 dir = (a - b).normalized();
Vector2 right(dir.y, -dir.x);
godot_to_box2d(shape_info.transform.xform(a - right * 0.5), box2d_points[0]);
godot_to_box2d(shape_info.transform.xform(a + right * 0.5), box2d_points[1]);
godot_to_box2d(shape_info.transform.xform(b - right * 0.5), box2d_points[2]);
godot_to_box2d(shape_info.transform.xform(b + right * 0.5), box2d_points[3]);
box2d_points[0] = godot_to_box2d(shape_info.transform.xform(a - right * CHAIN_SEGMENT_SQUARE_SIZE));
box2d_points[1] = godot_to_box2d(shape_info.transform.xform(a + right * CHAIN_SEGMENT_SQUARE_SIZE));
box2d_points[2] = godot_to_box2d(shape_info.transform.xform(b - right * CHAIN_SEGMENT_SQUARE_SIZE));
box2d_points[3] = godot_to_box2d(shape_info.transform.xform(b + right * CHAIN_SEGMENT_SQUARE_SIZE));
shape->Set(box2d_points, 4);
delete[] box2d_points;
return shape;
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/box2d_shape_convex_polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ b2Shape *Box2DShapeConvexPolygon::get_transformed_b2Shape(ShapeInfo shape_info,
ERR_FAIL_COND_V(polygon.size() < 3, nullptr);
b2Vec2 b2_points[b2_maxPolygonVertices];
for (int i = 0; i < polygon.size(); i++) {
godot_to_box2d(shape_info.transform.xform(polygon[i]), b2_points[i]);
b2_points[i] = godot_to_box2d(shape_info.transform.xform(polygon[i]));
}
int new_size = remove_bad_points(b2_points, polygon.size());
ERR_FAIL_COND_V(new_size < 3, nullptr);
Expand Down
8 changes: 4 additions & 4 deletions src/shapes/box2d_shape_rectangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ b2Shape *Box2DShapeRectangle::get_transformed_b2Shape(ShapeInfo shape_info, Box2
}
b2Vec2 box2d_half_extents = godot_to_box2d(half_extents);
b2Vec2 *box2d_points = new b2Vec2[4];
godot_to_box2d(shape_info.transform.xform(Vector2(-half_extents.x, -half_extents.y)), box2d_points[0]);
godot_to_box2d(shape_info.transform.xform(Vector2(-half_extents.x, half_extents.y)), box2d_points[1]);
godot_to_box2d(shape_info.transform.xform(Vector2(half_extents.x, half_extents.y)), box2d_points[2]);
godot_to_box2d(shape_info.transform.xform(Vector2(half_extents.x, -half_extents.y)), box2d_points[3]);
box2d_points[0] = godot_to_box2d(shape_info.transform.xform(Vector2(-half_extents.x, -half_extents.y)));
box2d_points[1] = godot_to_box2d(shape_info.transform.xform(Vector2(-half_extents.x, half_extents.y)));
box2d_points[2] = godot_to_box2d(shape_info.transform.xform(Vector2(half_extents.x, half_extents.y)));
box2d_points[3] = godot_to_box2d(shape_info.transform.xform(Vector2(half_extents.x, -half_extents.y)));
shape->Set(box2d_points, 4);
delete[] box2d_points;
return shape;
Expand Down
14 changes: 8 additions & 6 deletions src/shapes/box2d_shape_segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <box2d/b2_edge_shape.h>
#include <box2d/b2_polygon_shape.h>

constexpr float SEGMENT_SIZE = 0.5f;

void Box2DShapeSegment::set_data(const Variant &p_data) {
ERR_FAIL_COND(p_data.get_type() != Variant::RECT2);
Rect2 rect = p_data;
Expand Down Expand Up @@ -35,8 +37,8 @@ b2Shape *Box2DShapeSegment::get_transformed_b2Shape(ShapeInfo shape_info, Box2DC
shape_body_map[shape] = body;
}
b2Vec2 edge_endpoints[2];
godot_to_box2d(shape_info.transform.xform(a), edge_endpoints[0]);
godot_to_box2d(shape_info.transform.xform(b), edge_endpoints[1]);
edge_endpoints[0] = godot_to_box2d(shape_info.transform.xform(a));
edge_endpoints[1] = godot_to_box2d(shape_info.transform.xform(b));
if (shape_info.one_way) {
b2Vec2 dirV0 = edge_endpoints[0] - edge_endpoints[1];
shape->SetOneSided(edge_endpoints[1] + dirV0, edge_endpoints[0], edge_endpoints[1], edge_endpoints[0] - dirV0);
Expand All @@ -54,10 +56,10 @@ b2Shape *Box2DShapeSegment::get_transformed_b2Shape(ShapeInfo shape_info, Box2DC
b2Vec2 *box2d_points = new b2Vec2[4];
Vector2 dir = (a - b).normalized();
Vector2 right(dir.y, -dir.x);
godot_to_box2d(shape_info.transform.xform(a - right * 0.1), box2d_points[0]);
godot_to_box2d(shape_info.transform.xform(a + right * 0.1), box2d_points[1]);
godot_to_box2d(shape_info.transform.xform(b - right * 0.1), box2d_points[2]);
godot_to_box2d(shape_info.transform.xform(b + right * 0.1), box2d_points[3]);
box2d_points[0] = godot_to_box2d(shape_info.transform.xform(a - right * SEGMENT_SIZE));
box2d_points[1] = godot_to_box2d(shape_info.transform.xform(a + right * SEGMENT_SIZE));
box2d_points[2] = godot_to_box2d(shape_info.transform.xform(b - right * SEGMENT_SIZE));
box2d_points[3] = godot_to_box2d(shape_info.transform.xform(b + right * SEGMENT_SIZE));
shape->Set(box2d_points, 4);
delete[] box2d_points;
return shape;
Expand Down
15 changes: 9 additions & 6 deletions src/shapes/box2d_shape_separation_ray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
#include <box2d/b2_edge_shape.h>
#include <box2d/b2_polygon_shape.h>

constexpr const char *SEPARATION_RAY_LENGTH = "length";
constexpr const char *SEPARATION_RAY_SLIDE_ON_SLOPE = "slide_on_slope";

void Box2DShapeSeparationRay::set_data(const Variant &p_data) {
ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);
Dictionary dict = p_data;
ERR_FAIL_COND(dict.size() != 2);
ERR_FAIL_COND(!dict.has("length"));
ERR_FAIL_COND(!dict.has("slide_on_slope"));
double length = dict["length"];
bool slide_on_slope = dict["slide_on_slope"];
ERR_FAIL_COND(!dict.has(SEPARATION_RAY_LENGTH));
ERR_FAIL_COND(!dict.has(SEPARATION_RAY_SLIDE_ON_SLOPE));
double length = dict[SEPARATION_RAY_LENGTH];
bool slide_on_slope = dict[SEPARATION_RAY_SLIDE_ON_SLOPE];
a = Vector2();
b = Vector2(0, length);
half_extents = Vector2((a - b).length(), GODOT_LINEAR_SLOP);
Expand All @@ -28,7 +31,7 @@ void Box2DShapeSeparationRay::set_data(const Variant &p_data) {

Variant Box2DShapeSeparationRay::get_data() const {
Dictionary dict;
dict["length"] = b.x;
dict["slide_on_slope"] = false;
dict[SEPARATION_RAY_LENGTH] = b.x;
dict[SEPARATION_RAY_SLIDE_ON_SLOPE] = false;
return dict;
}
10 changes: 5 additions & 5 deletions src/shapes/box2d_shape_world_boundary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <box2d/b2_body.h>
#include <box2d/b2_polygon_shape.h>

#define WORLD_SHAPE_SIZE 100000
constexpr float WORLD_SHAPE_SIZE = 100000.0f;

void Box2DShapeWorldBoundary::set_data(const Variant &p_data) {
ERR_FAIL_COND(p_data.get_type() != Variant::ARRAY);
Expand Down Expand Up @@ -42,10 +42,10 @@ b2Shape *Box2DShapeWorldBoundary::get_transformed_b2Shape(ShapeInfo shape_info,
right *= WORLD_SHAPE_SIZE;
left = left + normal * distance;
right = right + normal * distance;
godot_to_box2d(shape_info.transform.xform(left), points[0]);
godot_to_box2d(shape_info.transform.xform(right), points[1]);
godot_to_box2d(shape_info.transform.xform(right - normal * WORLD_SHAPE_SIZE), points[2]);
godot_to_box2d(shape_info.transform.xform(right - normal * WORLD_SHAPE_SIZE), points[3]);
points[0] = godot_to_box2d(shape_info.transform.xform(left));
points[1] = godot_to_box2d(shape_info.transform.xform(right));
points[2] = godot_to_box2d(shape_info.transform.xform(right - normal * WORLD_SHAPE_SIZE));
points[3] = godot_to_box2d(shape_info.transform.xform(right - normal * WORLD_SHAPE_SIZE));
shape->Set(points, 4);

return shape;
Expand Down

0 comments on commit b466590

Please sign in to comment.