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

Commit 24aa449

Browse files
committed
make it compile(comment out a lot of code)
1 parent f970186 commit 24aa449

9 files changed

+132
-123
lines changed

src/bodies/box2d_collision_object_2d.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ void Box2DCollisionObject2D::_create_shape(Shape &shape, uint32_t p_shape_index)
198198
box2d::ShapeHandle shape_handle = shape.shape->get_box2d_shape();
199199
ERR_FAIL_COND(!box2d::is_handle_valid(shape_handle));
200200

201-
b2FixtureUserData user_data;
202-
set_collider_user_data(user_data, p_shape_index);
201+
b2FixtureUserData *user_data = memnew(b2FixtureUserData);
202+
set_collider_user_data(*user_data, p_shape_index);
203203

204204
switch (type) {
205205
case TYPE_BODY: {
@@ -226,7 +226,7 @@ void Box2DCollisionObject2D::_destroy_shape(Shape &shape, uint32_t p_shape_index
226226

227227
if (area_detection_counter > 0) {
228228
// Keep track of body information for delayed removal
229-
for (int i = 0; i < shape.collider_handle.count; i++) {
229+
for (int i = 0; i < shape.collider_handle.handles.size(); i++) {
230230
space->add_removed_collider(shape.collider_handle.handles[i], this, p_shape_index);
231231
}
232232
}
@@ -279,8 +279,8 @@ void Box2DCollisionObject2D::_set_space(Box2DSpace2D *p_space) {
279279

280280
ERR_FAIL_COND(box2d::is_handle_valid(body_handle));
281281

282-
b2BodyUserData user_data;
283-
set_body_user_data(user_data);
282+
b2BodyUserData *user_data = memnew(b2BodyUserData);
283+
set_body_user_data(*user_data);
284284

285285
b2Vec2 position = { transform.get_origin().x, transform.get_origin().y };
286286
real_t angle = transform.get_rotation();

src/box2d-wrapper/box2d_wrapper.cpp

+78-58
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
#include <box2d/geometry.h>
55
#include <box2d/hull.h>
66
#include <box2d/math.h>
7-
#include <world.h>
87
#include <godot_cpp/templates/hash_set.hpp>
98

109
using namespace box2d;
1110
using namespace godot;
1211

1312
struct Box2DHolder {
14-
HashMap<b2WorldId, ActiveBodyCallback> active_body_callbacks;
15-
HashMap<b2WorldId, int> active_objects;
13+
HashMap<int, ActiveBodyCallback> active_body_callbacks;
14+
HashMap<int, int> active_objects;
1615
};
1716

1817
Box2DHolder holder;
@@ -241,6 +240,7 @@ b2BodyId box2d::body_create(b2WorldId world_handle,
241240
}
242241

243242
void box2d::body_destroy(b2BodyId body_handle) {
243+
// TODO destroy user data too
244244
b2DestroyBody(body_handle);
245245
}
246246

@@ -366,39 +366,42 @@ void box2d::body_wake_up(b2BodyId body_handle) {
366366
b2Body_Wake(body_handle);
367367
}
368368

369-
FixtureHandle box2d::collider_create_sensor(ShapeHandle shape_handles,
369+
FixtureHandle box2d::collider_create_sensor(b2WorldId world_handle,
370+
ShapeHandle shape_handles,
370371
b2BodyId body_handle,
371372
b2FixtureUserData *user_data) {
373+
FixtureHandle fixture_handle{};
372374
b2MassData mass_data = b2Body_GetMassData(body_handle);
373375
for (int i = 0; i < shape_handles.handles.size(); i++) {
376+
/*
374377
ShapeData shape_data = shape_handles.handles[i];
375378
switch (shape_data.type) {
376-
case b2ShapeType::e_polygon: {
377-
}
379+
//case b2ShapeType::e_polygon: {
380+
//}
378381
}
379382
b2FixtureDef fixture_def;
380383
fixture_def.shape = shape_handle.handles[i];
381384
fixture_def.density = 1.0;
382385
fixture_def.isSensor = true;
383386
fixture_def.userData = user_data;
384-
b2ShapeId fixture = body_handle->CreateFixture(&fixture_def);
385-
fixture_handle.handles[i] = fixture;
387+
*/
388+
b2ShapeId shapeId;
389+
fixture_handle.handles[i] = shapeId;
386390
}
387-
body_handle->SetMassData(&mass_data);
391+
b2Body_SetMassData(body_handle, mass_data);
388392
return fixture_handle;
389393
}
390394

391395
FixtureHandle box2d::collider_create_solid(b2WorldId world_handle,
392396
ShapeHandle shape_handle,
393397
const Material *mat,
394398
b2BodyId body_handle,
395-
b2FixtureUserData user_data) {
396-
FixtureHandle fixture_handle{
397-
(b2ShapeId *)memalloc((shape_handle.count) * sizeof(b2ShapeId)),
398-
shape_handle.count
399-
};
400-
b2MassData mass_data = body_handle->GetMassData();
401-
for (int i = 0; i < shape_handle.count; i++) {
399+
b2FixtureUserData *user_data) {
400+
FixtureHandle fixture_handle{};
401+
b2MassData mass_data = b2Body_GetMassData(body_handle);
402+
for (int i = 0; i < shape_handle.handles.size(); i++) {
403+
// Create shape
404+
/*
402405
b2FixtureDef fixture_def;
403406
fixture_def.shape = shape_handle.handles[i];
404407
fixture_def.density = 1.0;
@@ -407,25 +410,17 @@ FixtureHandle box2d::collider_create_solid(b2WorldId world_handle,
407410
fixture_def.isSensor = false;
408411
fixture_def.userData = user_data;
409412
b2ShapeId fixture = body_handle->CreateFixture(&fixture_def);
410-
fixture_handle.handles[i] = fixture;
413+
*/
414+
b2ShapeId shapeId;
415+
fixture_handle.handles[i] = shapeId;
411416
}
412-
body_handle->SetMassData(&mass_data);
417+
b2Body_SetMassData(body_handle, mass_data);
413418
return fixture_handle;
414419
}
415420

416421
void box2d::collider_destroy(b2WorldId world_handle, FixtureHandle handle) {
417-
ERR_FAIL_COND(!is_handle_valid(handle));
418-
b2BodyId body = handle.handles[0]->GetBody();
419-
if (!is_handle_valid(body)) {
420-
// already destroyed
421-
return;
422-
}
423-
for (b2ShapeId fixture = body->GetFixtureList(); fixture != nullptr; fixture = fixture->GetNext()) {
424-
for (int i = 0; i < handle.count; i++) {
425-
if (fixture == handle.handles[i]) {
426-
body->DestroyFixture(fixture);
427-
}
428-
}
422+
for (b2ShapeId shapeId : handle.handles) {
423+
b2DestroyShape(shapeId);
429424
}
430425
}
431426

@@ -444,18 +439,33 @@ Transform2D b2Transform_to_Transform2D(b2Transform transform) {
444439
Vector2 box2d::b2Vec2_to_Vector2(b2Vec2 vec) {
445440
return Vector2(vec.x, vec.y);
446441
}
442+
b2Vec2 box2d::b2Vec2_add(b2Vec2 vec, b2Vec2 other) {
443+
vec.x += other.x;
444+
vec.y += other.y;
445+
return vec;
446+
}
447+
b2Vec2 box2d::b2Vec2_mul(b2Vec2 vec, real_t other) {
448+
vec.x *= other;
449+
vec.y *= other;
450+
return vec;
451+
}
452+
453+
b2Vec2 box2d::b2Vec2_sub(b2Vec2 vec, b2Vec2 other) {
454+
vec.x -= other.x;
455+
vec.y -= other.y;
456+
return vec;
457+
}
447458

448459
b2Vec2 xform_b2Vec2(b2Vec2 vec, Transform2D transform) {
449460
return Vector2_to_b2Vec2(transform.xform(b2Vec2_to_Vector2(vec)));
450461
}
451462

452463
void box2d::collider_set_transform(b2WorldId world_handle, FixtureHandle handles, ShapeInfo shape_info) {
453-
for (int i = 0; i < handles.count; i++) {
454-
b2ShapeId handle = handles.handles[i];
455-
handle->GetUserData().transform = shape_info.transform;
456-
ERR_FAIL_COND(!handle);
457-
ERR_FAIL_COND(!is_handle_valid(shape_info.handle));
458-
ERR_FAIL_COND(handles.count != shape_info.handle.count);
464+
ERR_FAIL_COND(!is_handle_valid(shape_info.handle));
465+
for (b2ShapeId handle : handles.handles) {
466+
b2FixtureUserData *user_data = static_cast<b2FixtureUserData *>(b2Shape_GetUserData(handle));
467+
user_data->transform = shape_info.transform;
468+
/*
459469
b2Shape *shape_template = shape_info.handle.handles[i];
460470
b2Shape *shape = handle->GetShape();
461471
ERR_FAIL_COND(!shape);
@@ -509,12 +519,14 @@ void box2d::collider_set_transform(b2WorldId world_handle, FixtureHandle handles
509519
ERR_FAIL_MSG("Invalid Shape Type");
510520
}
511521
}
522+
*/
512523
}
513524
}
514525

515526
Transform2D box2d::collider_get_transform(b2WorldId world_handle, FixtureHandle handle) {
516527
ERR_FAIL_COND_V(!is_handle_valid(handle), Transform2D());
517-
return handle.handles[0]->GetUserData().transform;
528+
b2FixtureUserData *user_data = static_cast<b2FixtureUserData *>(b2Shape_GetUserData(handle.handles[0]));
529+
return user_data->transform;
518530
}
519531

520532
Transform2D box2d::collider_get_transform(b2WorldId world_handle, b2ShapeId handle) {
@@ -556,6 +568,7 @@ class AABBQueryCallback {
556568
if (!b2Shape_IsSensor(shapeId) && !collide_with_body) {
557569
return true;
558570
}
571+
/*
559572
if (!handle_excluded_callback(world, shapeId, fixture->GetUserData(), handle_excluded_info)) {
560573
hit_info_array[count++] = PointHitInfo{
561574
fixture,
@@ -567,6 +580,7 @@ class AABBQueryCallback {
567580
return true;
568581
}
569582
}
583+
*/
570584
return count < hit_info_length;
571585
}
572586
};
@@ -640,12 +654,13 @@ class RayCastQueryCallback {
640654
/// closest hit, 1 to continue
641655
float ReportFixture(b2ShapeId fixture, const b2Vec2 &point,
642656
const b2Vec2 &normal, float fraction) {
643-
if (fixture->IsSensor() && !collide_with_area) {
657+
if (b2Shape_IsSensor(fixture) && !collide_with_area) {
644658
return -1;
645659
}
646-
if (!fixture->IsSensor() && !collide_with_body) {
660+
if (!b2Shape_IsSensor(fixture) && !collide_with_body) {
647661
return -1;
648662
}
663+
/*
649664
if (!handle_excluded_callback(world, fixture, fixture->GetUserData(), handle_excluded_info)) {
650665
hit_info_array[0] = RayHitInfo{
651666
point,
@@ -655,6 +670,7 @@ class RayCastQueryCallback {
655670
};
656671
count = 1;
657672
}
673+
*/
658674
return 1;
659675
}
660676
};
@@ -690,10 +706,7 @@ b2WorldId box2d::invalid_world_handle() {
690706
return b2_nullWorldId;
691707
}
692708
FixtureHandle box2d::invalid_fixture_handle() {
693-
return FixtureHandle{
694-
nullptr,
695-
0
696-
};
709+
return FixtureHandle{};
697710
}
698711
b2BodyId box2d::invalid_body_handle() {
699712
return b2_nullBodyId;
@@ -719,7 +732,7 @@ bool box2d::is_user_data_valid(b2BodyUserData user_data) {
719732
}
720733

721734
bool box2d::is_handle_valid(FixtureHandle handle) {
722-
return handle.count > 0 || handle.handles != nullptr;
735+
return handle.handles.size() > 0;
723736
}
724737
bool box2d::is_handle_valid(b2WorldId handle) {
725738
return B2_IS_NON_NULL(handle);
@@ -751,7 +764,7 @@ void box2d::joint_change_revolute_params(b2JointId joint_handle,
751764
//joint->SetLimits(angular_limit_lower, angular_limit_upper);
752765
b2RevoluteJoint_EnableMotor(joint_handle, motor_enabled);
753766
b2RevoluteJoint_SetMotorSpeed(joint_handle, motor_target_velocity);
754-
b2RevoluteJoint_EnableLimit(joint_handle, angular_limit_enabled)
767+
b2RevoluteJoint_EnableLimit(joint_handle, angular_limit_enabled);
755768
}
756769

757770
b2JointId box2d::joint_create_prismatic(b2WorldId world_handle,
@@ -850,6 +863,7 @@ ShapeCastResult box2d::shape_casting(b2WorldId world_handle,
850863
bool first_time = true;
851864
b2Transform shape_transform = Transform2D_to_b2Transform(shape_info.body_transform * shape_info.transform);
852865
b2Transform shape_transform_motion = shape_transform;
866+
/*
853867
shape_transform_motion.p += motion;
854868
for (int j = 0; j < shape_info.handle.handles[i]->GetChildCount(); j++) {
855869
shape_info.handle.handles[i]->ComputeAABB(&shape_aabb, shape_transform, j);
@@ -899,6 +913,7 @@ ShapeCastResult box2d::shape_casting(b2WorldId world_handle,
899913
if (result.collided) {
900914
return result;
901915
}
916+
*/
902917
}
903918
return ShapeCastResult{
904919
false
@@ -913,6 +928,7 @@ ShapeCollideResult box2d::shape_collide(const b2Vec2 motion1,
913928
for (int j = 0; j < shape_info2.handle.handles.size(); j++) {
914929
b2Transform transformA = Transform2D_to_b2Transform(shape_info1.body_transform * shape_info1.transform);
915930
b2Transform transformB = Transform2D_to_b2Transform(shape_info2.body_transform * shape_info2.transform);
931+
/*
916932
b2TOIOutput toi_output = _time_of_impact(shape_info1.handle.handles[i],
917933
transformA,
918934
b2Vec2_zero,
@@ -932,6 +948,7 @@ ShapeCollideResult box2d::shape_collide(const b2Vec2 motion1,
932948
result.witness1 = distance_output.pointA;
933949
result.witness2 = distance_output.pointB;
934950
return result;
951+
*/
935952
}
936953
}
937954
return ShapeCollideResult{
@@ -1031,6 +1048,7 @@ ContactResult box2d::shapes_contact(b2WorldId world_handle,
10311048
ShapeInfo shape_info1,
10321049
ShapeInfo shape_info2,
10331050
real_t margin) {
1051+
/*
10341052
for (int i = 0; i < shape_info1.handle.count; i++) {
10351053
for (int j = 0; j < shape_info2.handle.count; j++) {
10361054
b2Transform transform_A = Transform2D_to_b2Transform(shape_info1.body_transform * shape_info1.transform);
@@ -1090,42 +1108,44 @@ ContactResult box2d::shapes_contact(b2WorldId world_handle,
10901108
};
10911109
}
10921110
}
1111+
*/
10931112
return ContactResult{
10941113
false
10951114
};
10961115
}
10971116

10981117
b2WorldId box2d::world_create(WorldSettings settings) {
1099-
b2WorldDef world_def = b2_defaultWorldDef;
1100-
return b2CreateWorld(b2WorldDef);
1118+
b2WorldDef world_def = b2DefaultWorldDef();
1119+
return b2CreateWorld(&world_def);
11011120
}
11021121

1103-
void box2d::world_destroy(b2WorldId world_handle){
1104-
b2DestroyWorld(world_handle)
1122+
void box2d::world_destroy(b2WorldId world_handle) {
1123+
b2DestroyWorld(world_handle);
11051124
}
11061125

11071126
size_t box2d::world_get_active_objects_count(b2WorldId world_handle) {
1108-
return holder.active_objects[world_handle];
1127+
return holder.active_objects[handle_hash(world_handle)];
11091128
}
11101129

11111130
void box2d::world_set_active_body_callback(b2WorldId world_handle, ActiveBodyCallback callback) {
1112-
holder.active_body_callbacks[world_handle] = callback;
1131+
holder.active_body_callbacks[handle_hash(world_handle)] = callback;
11131132
}
11141133

1115-
void box2d::world_set_collision_filter_callback(b2WorldId world_handle,
1116-
b2ContactFilter *callback) {
1117-
world_handle->SetContactFilter(callback);
1134+
bool presolve_fcn(b2ShapeId shapeIdA, b2ShapeId shapeIdB, b2Manifold *manifold, void *context) {
1135+
b2ContactFilter *callback = static_cast<b2ContactFilter *>(context);
1136+
return callback->ShouldCollide(shapeIdA, shapeIdB, manifold);
11181137
}
11191138

1120-
void box2d::world_set_contact_listener(b2WorldId world_handle,
1121-
b2ContactListener *callback) {
1122-
world_handle->SetContactListener(callback);
1139+
void box2d::world_set_collision_filter_callback(b2WorldId world_handle,
1140+
b2ContactFilter *callback) {
1141+
b2World_SetPreSolveCallback(world_handle, presolve_fcn, callback);
11231142
}
11241143

11251144
void box2d::world_step(b2WorldId world_handle, SimulationSettings settings) {
11261145
//world_handle->SetGravity(settings->gravity);
11271146
// TODO set world gravity
1128-
b2World_Step(world_handle, settings->dt, settings->max_velocity_iterations, settings->max_position_iterations);
1147+
//settings.max_velocity_iterations, settings.max_position_iterations
1148+
b2World_Step(world_handle, settings.dt, settings.sub_step_count);
11291149
int active_objects = 0;
11301150
/*
11311151
if (holder.active_body_callbacks.has(world_handle)) {

0 commit comments

Comments
 (0)