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

Commit 7f8155b

Browse files
committed
update awake bodies get.
1 parent 417a19b commit 7f8155b

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

src/box2d-wrapper/box2d_wrapper.cpp

+24-29
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ using namespace godot;
1313
#define B2_DEBUG true
1414

1515
struct Box2DHolder {
16-
HashMap<b2WorldId, ActiveBodyCallback> active_body_callbacks;
17-
HashMap<int, int> active_objects;
16+
HashMap<int, ActiveBodyCallback> active_body_callbacks;
1817
};
1918

2019
Box2DHolder holder;
@@ -470,7 +469,9 @@ void box2d::body_wake_up(b2BodyId body_handle) {
470469
#ifdef B2_DEBUG
471470
UtilityFunctions::print("box2d::body_wake_up: ", rtos(body_handle.index));
472471
#endif
473-
b2Body_Wake(body_handle);
472+
if (b2Body_GetType(body_handle) == b2BodyType::b2_kinematicBody) {
473+
b2Body_Wake(body_handle);
474+
}
474475
}
475476

476477
b2ShapeId create_collider(b2BodyId body_handle, b2FixtureUserData *user_data, Material mat, ShapeData shape_data, bool isSensor) {
@@ -1352,13 +1353,6 @@ void box2d::world_destroy(b2WorldId world_handle) {
13521353
b2DestroyWorld(world_handle);
13531354
}
13541355

1355-
size_t box2d::world_get_active_objects_count(b2WorldId world_handle) {
1356-
#ifdef B2_DEBUG
1357-
UtilityFunctions::print("box2d::world_get_active_objects_count", rtos(world_handle.index));
1358-
#endif
1359-
return holder.active_objects[handle_hash(world_handle)];
1360-
}
1361-
13621356
void box2d::world_set_active_body_callback(b2WorldId world_handle, ActiveBodyCallback callback) {
13631357
#ifdef B2_DEBUG
13641358
UtilityFunctions::print("box2d::world_set_active_body_callback", rtos(world_handle.index));
@@ -1380,38 +1374,39 @@ void box2d::world_set_collision_filter_callback(b2WorldId world_handle,
13801374
}
13811375

13821376
// TODO
1383-
void box2d::world_step(b2WorldId world_handle, SimulationSettings settings) {
1377+
void box2d::world_step(b2WorldId world_handle, SimulationSettings settings, std::vector<b2BodyId> active_bodies) {
13841378
#ifdef B2_DEBUG
13851379
UtilityFunctions::print("box2d::world_step", rtos(world_handle.index));
13861380
#endif
13871381
//world_handle->SetGravity(settings->gravity);
13881382
// TODO set world gravity
13891383
b2World_Step(world_handle, settings.dt, settings.sub_step_count);
1390-
int active_objects = 0;
1391-
if (holder.active_body_callbacks.has(world_handle)) {
1392-
ActiveBodyCallback callback = holder.active_body_callbacks[world_handle];
1393-
for (b2BodyId body = world_handle->GetBodyList(); body != nullptr; body = body->GetNext()) {
1394-
if (body->GetType() == b2_kinematicBody) {
1395-
b2BodyUserData &userData = body->GetUserData();
1396-
userData.old_angular_velocity = body->GetAngularVelocity();
1397-
userData.old_linear_velocity = b2Vec2_to_Vector2(body->GetLinearVelocity());
1398-
body->SetLinearVelocity(b2Vec2_zero);
1399-
body->SetAngularVelocity(0);
1384+
if (holder.active_body_callbacks.has(handle_hash(world_handle))) {
1385+
ActiveBodyCallback callback = holder.active_body_callbacks[handle_hash(world_handle)];
1386+
b2BodyEvents bodyEvents = b2World_GetBodyEvents(world_handle);
1387+
b2SensorEvents sensorEvents = b2World_GetSensorEvents(world_handle);
1388+
// get bodies from active_bodies vector
1389+
for (b2BodyId body_handle : active_bodies) {
1390+
b2BodyType type = b2Body_GetType(body_handle);
1391+
b2BodyUserData *userData = get_body_user_data(body_handle);
1392+
if (type == b2_kinematicBody) {
1393+
userData->old_angular_velocity = b2Body_GetAngularVelocity(body_handle);
1394+
userData->old_linear_velocity = b2Vec2_to_Vector2(b2Body_GetLinearVelocity(body_handle));
1395+
b2Body_SetLinearVelocity(body_handle, b2Vec2_zero);
1396+
b2Body_SetAngularVelocity(body_handle, 0.0);
14001397
}
1401-
Vector2 constant_force = body->GetUserData().constant_force;
1398+
Vector2 constant_force = userData->constant_force;
14021399
if (constant_force != Vector2()) {
1403-
body->ApplyForceToCenter(Vector2_to_b2Vec2(constant_force), true);
1400+
b2Body_ApplyForceToCenter(body_handle, Vector2_to_b2Vec2(constant_force), true);
14041401
}
1405-
real_t constant_torque = body->GetUserData().constant_torque;
1402+
real_t constant_torque = userData->constant_torque;
14061403
if (constant_torque != 0.0) {
1407-
body->ApplyTorque(constant_torque, true);
1404+
b2Body_ApplyTorque(body_handle, constant_torque, true);
14081405
}
1409-
if (body->IsAwake() && body->GetUserData().collision_object->get_type() == Box2DCollisionObject2D::Type::TYPE_BODY) {
1410-
active_objects++;
1411-
ActiveBodyInfo info{ body, body->GetUserData() };
1406+
if (userData->collision_object->get_type() == Box2DCollisionObject2D::Type::TYPE_BODY) {
1407+
ActiveBodyInfo info{ body_handle, *userData };
14121408
callback(info);
14131409
}
14141410
}
14151411
}
1416-
//holder.active_objects[world_handle] = active_objects;
14171412
}

src/box2d-wrapper/box2d_wrapper.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -454,16 +454,14 @@ b2WorldId world_create(WorldSettings settings);
454454

455455
void world_destroy(b2WorldId world_handle);
456456

457-
size_t world_get_active_objects_count(b2WorldId world_handle);
458-
459457
void world_set_active_body_callback(b2WorldId world_handle, ActiveBodyCallback callback);
460458

461459
void world_set_collision_filter_callback(b2WorldId world_handle,
462460
b2ContactFilter *callback);
463461

464462
void collider_set_contact_force_events_enabled(FixtureHandle collider_handle, bool send_contacts);
465463

466-
void world_step(b2WorldId world_handle, const SimulationSettings settings);
464+
void world_step(b2WorldId world_handle, const SimulationSettings settings, std::vector<b2BodyId> active_bodies);
467465

468466
} // namespace box2d
469467

src/spaces/box2d_space_2d.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,10 @@ bool Box2DSpace2D::contact_point_callback(b2WorldId world_handle, const box2d::C
492492

493493
void Box2DSpace2D::step(real_t p_step) {
494494
last_step = p_step;
495+
std::vector<b2BodyId> active_bodies;
495496
for (SelfList<Box2DBody2D> *body_iterator = active_list.first(); body_iterator;) {
496497
Box2DBody2D *body = body_iterator->self();
498+
active_bodies.push_back(body->get_body_handle());
497499
body_iterator = body_iterator->next();
498500
body->reset_contact_count();
499501
}
@@ -538,7 +540,7 @@ void Box2DSpace2D::step(real_t p_step) {
538540
settings.gravity.y = default_gravity_dir.y * default_gravity_value;
539541

540542
ERR_FAIL_COND(!box2d::is_handle_valid(handle));
541-
box2d::world_step(handle, settings);
543+
box2d::world_step(handle, settings, active_bodies);
542544

543545
// Needed only for one physics step to retrieve lost info
544546
removed_colliders.clear();
@@ -548,7 +550,6 @@ void Box2DSpace2D::step(real_t p_step) {
548550
body_iterator = body_iterator->next();
549551
body->on_update_active();
550552
}
551-
active_objects = box2d::world_get_active_objects_count(handle);
552553
}
553554

554555
// TODO
@@ -747,7 +748,7 @@ Box2DSpace2D::Box2DSpace2D() {
747748

748749
handle = box2d::world_create(world_settings);
749750
ERR_FAIL_COND(!box2d::is_handle_valid(handle));
750-
751+
751752
box2d::world_set_active_body_callback(handle, active_body_callback);
752753
box2d::world_set_collision_filter_callback(handle, this);
753754
}

0 commit comments

Comments
 (0)