4
4
#include < box2d/geometry.h>
5
5
#include < box2d/hull.h>
6
6
#include < box2d/math.h>
7
- #include < world.h>
8
7
#include < godot_cpp/templates/hash_set.hpp>
9
8
10
9
using namespace box2d ;
11
10
using namespace godot ;
12
11
13
12
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;
16
15
};
17
16
18
17
Box2DHolder holder;
@@ -241,6 +240,7 @@ b2BodyId box2d::body_create(b2WorldId world_handle,
241
240
}
242
241
243
242
void box2d::body_destroy (b2BodyId body_handle) {
243
+ // TODO destroy user data too
244
244
b2DestroyBody (body_handle);
245
245
}
246
246
@@ -366,39 +366,42 @@ void box2d::body_wake_up(b2BodyId body_handle) {
366
366
b2Body_Wake (body_handle);
367
367
}
368
368
369
- FixtureHandle box2d::collider_create_sensor (ShapeHandle shape_handles,
369
+ FixtureHandle box2d::collider_create_sensor (b2WorldId world_handle,
370
+ ShapeHandle shape_handles,
370
371
b2BodyId body_handle,
371
372
b2FixtureUserData *user_data) {
373
+ FixtureHandle fixture_handle{};
372
374
b2MassData mass_data = b2Body_GetMassData (body_handle);
373
375
for (int i = 0 ; i < shape_handles.handles .size (); i++) {
376
+ /*
374
377
ShapeData shape_data = shape_handles.handles[i];
375
378
switch (shape_data.type) {
376
- case b2ShapeType::e_polygon: {
377
- }
379
+ // case b2ShapeType::e_polygon: {
380
+ // }
378
381
}
379
382
b2FixtureDef fixture_def;
380
383
fixture_def.shape = shape_handle.handles[i];
381
384
fixture_def.density = 1.0;
382
385
fixture_def.isSensor = true;
383
386
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;
386
390
}
387
- body_handle-> SetMassData (& mass_data);
391
+ b2Body_SetMassData (body_handle, mass_data);
388
392
return fixture_handle;
389
393
}
390
394
391
395
FixtureHandle box2d::collider_create_solid (b2WorldId world_handle,
392
396
ShapeHandle shape_handle,
393
397
const Material *mat,
394
398
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
+ /*
402
405
b2FixtureDef fixture_def;
403
406
fixture_def.shape = shape_handle.handles[i];
404
407
fixture_def.density = 1.0;
@@ -407,25 +410,17 @@ FixtureHandle box2d::collider_create_solid(b2WorldId world_handle,
407
410
fixture_def.isSensor = false;
408
411
fixture_def.userData = user_data;
409
412
b2ShapeId fixture = body_handle->CreateFixture(&fixture_def);
410
- fixture_handle.handles [i] = fixture;
413
+ */
414
+ b2ShapeId shapeId;
415
+ fixture_handle.handles [i] = shapeId;
411
416
}
412
- body_handle-> SetMassData (& mass_data);
417
+ b2Body_SetMassData (body_handle, mass_data);
413
418
return fixture_handle;
414
419
}
415
420
416
421
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);
429
424
}
430
425
}
431
426
@@ -444,18 +439,33 @@ Transform2D b2Transform_to_Transform2D(b2Transform transform) {
444
439
Vector2 box2d::b2Vec2_to_Vector2 (b2Vec2 vec) {
445
440
return Vector2 (vec.x , vec.y );
446
441
}
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
+ }
447
458
448
459
b2Vec2 xform_b2Vec2 (b2Vec2 vec, Transform2D transform) {
449
460
return Vector2_to_b2Vec2 (transform.xform (b2Vec2_to_Vector2 (vec)));
450
461
}
451
462
452
463
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
+ /*
459
469
b2Shape *shape_template = shape_info.handle.handles[i];
460
470
b2Shape *shape = handle->GetShape();
461
471
ERR_FAIL_COND(!shape);
@@ -509,12 +519,14 @@ void box2d::collider_set_transform(b2WorldId world_handle, FixtureHandle handles
509
519
ERR_FAIL_MSG("Invalid Shape Type");
510
520
}
511
521
}
522
+ */
512
523
}
513
524
}
514
525
515
526
Transform2D box2d::collider_get_transform (b2WorldId world_handle, FixtureHandle handle) {
516
527
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 ;
518
530
}
519
531
520
532
Transform2D box2d::collider_get_transform (b2WorldId world_handle, b2ShapeId handle) {
@@ -556,6 +568,7 @@ class AABBQueryCallback {
556
568
if (!b2Shape_IsSensor (shapeId) && !collide_with_body) {
557
569
return true ;
558
570
}
571
+ /*
559
572
if (!handle_excluded_callback(world, shapeId, fixture->GetUserData(), handle_excluded_info)) {
560
573
hit_info_array[count++] = PointHitInfo{
561
574
fixture,
@@ -567,6 +580,7 @@ class AABBQueryCallback {
567
580
return true;
568
581
}
569
582
}
583
+ */
570
584
return count < hit_info_length;
571
585
}
572
586
};
@@ -640,12 +654,13 @@ class RayCastQueryCallback {
640
654
// / closest hit, 1 to continue
641
655
float ReportFixture (b2ShapeId fixture, const b2Vec2 &point,
642
656
const b2Vec2 &normal , float fraction) {
643
- if (fixture-> IsSensor ( ) && !collide_with_area) {
657
+ if (b2Shape_IsSensor (fixture ) && !collide_with_area) {
644
658
return -1 ;
645
659
}
646
- if (!fixture-> IsSensor ( ) && !collide_with_body) {
660
+ if (!b2Shape_IsSensor (fixture ) && !collide_with_body) {
647
661
return -1 ;
648
662
}
663
+ /*
649
664
if (!handle_excluded_callback(world, fixture, fixture->GetUserData(), handle_excluded_info)) {
650
665
hit_info_array[0] = RayHitInfo{
651
666
point,
@@ -655,6 +670,7 @@ class RayCastQueryCallback {
655
670
};
656
671
count = 1;
657
672
}
673
+ */
658
674
return 1 ;
659
675
}
660
676
};
@@ -690,10 +706,7 @@ b2WorldId box2d::invalid_world_handle() {
690
706
return b2_nullWorldId;
691
707
}
692
708
FixtureHandle box2d::invalid_fixture_handle () {
693
- return FixtureHandle{
694
- nullptr ,
695
- 0
696
- };
709
+ return FixtureHandle{};
697
710
}
698
711
b2BodyId box2d::invalid_body_handle () {
699
712
return b2_nullBodyId;
@@ -719,7 +732,7 @@ bool box2d::is_user_data_valid(b2BodyUserData user_data) {
719
732
}
720
733
721
734
bool box2d::is_handle_valid (FixtureHandle handle) {
722
- return handle.count > 0 || handle. handles != nullptr ;
735
+ return handle.handles . size () > 0 ;
723
736
}
724
737
bool box2d::is_handle_valid (b2WorldId handle) {
725
738
return B2_IS_NON_NULL (handle);
@@ -751,7 +764,7 @@ void box2d::joint_change_revolute_params(b2JointId joint_handle,
751
764
// joint->SetLimits(angular_limit_lower, angular_limit_upper);
752
765
b2RevoluteJoint_EnableMotor (joint_handle, motor_enabled);
753
766
b2RevoluteJoint_SetMotorSpeed (joint_handle, motor_target_velocity);
754
- b2RevoluteJoint_EnableLimit (joint_handle, angular_limit_enabled)
767
+ b2RevoluteJoint_EnableLimit (joint_handle, angular_limit_enabled);
755
768
}
756
769
757
770
b2JointId box2d::joint_create_prismatic (b2WorldId world_handle,
@@ -850,6 +863,7 @@ ShapeCastResult box2d::shape_casting(b2WorldId world_handle,
850
863
bool first_time = true ;
851
864
b2Transform shape_transform = Transform2D_to_b2Transform (shape_info.body_transform * shape_info.transform );
852
865
b2Transform shape_transform_motion = shape_transform;
866
+ /*
853
867
shape_transform_motion.p += motion;
854
868
for (int j = 0; j < shape_info.handle.handles[i]->GetChildCount(); j++) {
855
869
shape_info.handle.handles[i]->ComputeAABB(&shape_aabb, shape_transform, j);
@@ -899,6 +913,7 @@ ShapeCastResult box2d::shape_casting(b2WorldId world_handle,
899
913
if (result.collided) {
900
914
return result;
901
915
}
916
+ */
902
917
}
903
918
return ShapeCastResult{
904
919
false
@@ -913,6 +928,7 @@ ShapeCollideResult box2d::shape_collide(const b2Vec2 motion1,
913
928
for (int j = 0 ; j < shape_info2.handle .handles .size (); j++) {
914
929
b2Transform transformA = Transform2D_to_b2Transform (shape_info1.body_transform * shape_info1.transform );
915
930
b2Transform transformB = Transform2D_to_b2Transform (shape_info2.body_transform * shape_info2.transform );
931
+ /*
916
932
b2TOIOutput toi_output = _time_of_impact(shape_info1.handle.handles[i],
917
933
transformA,
918
934
b2Vec2_zero,
@@ -932,6 +948,7 @@ ShapeCollideResult box2d::shape_collide(const b2Vec2 motion1,
932
948
result.witness1 = distance_output.pointA;
933
949
result.witness2 = distance_output.pointB;
934
950
return result;
951
+ */
935
952
}
936
953
}
937
954
return ShapeCollideResult{
@@ -1031,6 +1048,7 @@ ContactResult box2d::shapes_contact(b2WorldId world_handle,
1031
1048
ShapeInfo shape_info1,
1032
1049
ShapeInfo shape_info2,
1033
1050
real_t margin) {
1051
+ /*
1034
1052
for (int i = 0; i < shape_info1.handle.count; i++) {
1035
1053
for (int j = 0; j < shape_info2.handle.count; j++) {
1036
1054
b2Transform transform_A = Transform2D_to_b2Transform(shape_info1.body_transform * shape_info1.transform);
@@ -1090,42 +1108,44 @@ ContactResult box2d::shapes_contact(b2WorldId world_handle,
1090
1108
};
1091
1109
}
1092
1110
}
1111
+ */
1093
1112
return ContactResult{
1094
1113
false
1095
1114
};
1096
1115
}
1097
1116
1098
1117
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 );
1101
1120
}
1102
1121
1103
- void box2d::world_destroy (b2WorldId world_handle){
1104
- b2DestroyWorld (world_handle)
1122
+ void box2d::world_destroy (b2WorldId world_handle) {
1123
+ b2DestroyWorld (world_handle);
1105
1124
}
1106
1125
1107
1126
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) ];
1109
1128
}
1110
1129
1111
1130
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;
1113
1132
}
1114
1133
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 );
1118
1137
}
1119
1138
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);
1123
1142
}
1124
1143
1125
1144
void box2d::world_step (b2WorldId world_handle, SimulationSettings settings) {
1126
1145
// world_handle->SetGravity(settings->gravity);
1127
1146
// 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 );
1129
1149
int active_objects = 0 ;
1130
1150
/*
1131
1151
if (holder.active_body_callbacks.has(world_handle)) {
0 commit comments