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

Commit 630c927

Browse files
committed
update to state where objects fall/move
1 parent 7f8155b commit 630c927

7 files changed

+168
-74
lines changed

src/bodies/box2d_body_2d.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void Box2DBody2D::on_marked_active() {
139139
ERR_FAIL_COND(!get_space());
140140

141141
// TODO: Check how that happens, probably not good for performance
142-
//ERR_FAIL_COND(mode == PhysicsServer2D::BODY_MODE_STATIC);
142+
ERR_FAIL_COND(mode == PhysicsServer2D::BODY_MODE_STATIC);
143143
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
144144
return;
145145
}

src/box2d-wrapper/box2d_wrapper.cpp

+136-61
Large diffs are not rendered by default.

src/box2d-wrapper/box2d_wrapper.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414

1515
// You can use this to change the length scale used by your game.
1616
// For example for inches you could use 39.4.
17+
#ifdef b2_lengthUnitsPerMeter
1718
#undef b2_lengthUnitsPerMeter
19+
#endif
1820
#define b2_lengthUnitsPerMeter 100.0f
1921

2022
// The maximum number of vertices on a convex polygon. You cannot increase
2123
// this too much because b2BlockAllocator has a maximum object size.
24+
#ifdef b2_maxPolygonVertices
2225
#undef b2_maxPolygonVertices
26+
#endif
2327
#define b2_maxPolygonVertices 64
2428

2529
class Box2DCollisionObject2D;
@@ -450,7 +454,7 @@ ContactResult shapes_contact(b2WorldId world_handle,
450454
ShapeInfo shape_info2,
451455
real_t margin);
452456

453-
b2WorldId world_create(WorldSettings settings);
457+
b2WorldId world_create(WorldSettings settings, SimulationSettings simulation_settings);
454458

455459
void world_destroy(b2WorldId world_handle);
456460

@@ -463,6 +467,10 @@ void collider_set_contact_force_events_enabled(FixtureHandle collider_handle, bo
463467

464468
void world_step(b2WorldId world_handle, const SimulationSettings settings, std::vector<b2BodyId> active_bodies);
465469

470+
int assert_callback(const char *condition, const char *fileName, int lineNumber);
471+
472+
void set_logging_enabled(bool enabled);
473+
466474
} // namespace box2d
467475

468476
#endif // BOX2D_WRAPPER_H

src/register_types.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ void initialize_box2d_module(ModuleInitializationLevel p_level) {
4040
} break;
4141
case MODULE_INITIALIZATION_LEVEL_SCENE: {
4242
Box2DProjectSettings::register_settings();
43+
b2SetAssertFcn(box2d::assert_callback);
44+
box2d::set_logging_enabled(Box2DProjectSettings::get_logging_enabled());
4345
} break;
4446
default: {
4547
} break;

src/servers/box2d_project_settings.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ using namespace godot;
66

77
constexpr char RUN_ON_SEPARATE_THREAD[] = "physics/2d/run_on_separate_thread";
88
constexpr char MAX_THREADS[] = "threading/worker_pool/max_threads";
9-
constexpr char SUB_STEP_COUNT[] = "physics/box_2d/solver/sub_step_count";
9+
constexpr char SUB_STEP_COUNT[] = "physics/box_2d/sub_step_count";
10+
constexpr char LOGGING_ENABLED[] = "physics/box_2d/logging";
1011

1112
void register_setting(
1213
const String &p_name,
@@ -64,6 +65,7 @@ void register_setting_ranged(
6465

6566
void Box2DProjectSettings::register_settings() {
6667
register_setting_ranged(SUB_STEP_COUNT, 4, U"1,16,or_greater");
68+
register_setting_plain(LOGGING_ENABLED, false, true);
6769
}
6870

6971
template <typename TType>
@@ -89,3 +91,7 @@ int Box2DProjectSettings::get_max_threads() {
8991
int Box2DProjectSettings::get_sub_step_count() {
9092
return get_setting<int>(SUB_STEP_COUNT);
9193
}
94+
95+
bool Box2DProjectSettings::get_logging_enabled() {
96+
return get_setting<bool>(LOGGING_ENABLED);
97+
}

src/servers/box2d_project_settings.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ class Box2DProjectSettings {
1010

1111
static bool should_run_on_separate_thread();
1212
static int get_max_threads();
13+
static bool get_logging_enabled();
1314
static int get_sub_step_count();
1415
};

src/spaces/box2d_space_2d.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,6 @@ void Box2DSpace2D::step(real_t p_step) {
501501
}
502502
contact_debug_count = 0;
503503

504-
ProjectSettings *project_settings = ProjectSettings::get_singleton();
505-
506-
default_gravity_dir = project_settings->get_setting_with_override("physics/2d/default_gravity_vector");
507-
default_gravity_value = project_settings->get_setting_with_override("physics/2d/default_gravity");
508-
509-
default_linear_damping = project_settings->get_setting_with_override("physics/2d/default_linear_damp");
510-
default_angular_damping = project_settings->get_setting_with_override("physics/2d/default_angular_damp");
511-
512504
for (SelfList<Box2DBody2D> *body_iterator = mass_properties_update_list.first(); body_iterator;) {
513505
Box2DBody2D *body = body_iterator->self();
514506
body_iterator = body_iterator->next();
@@ -736,6 +728,12 @@ Box2DSpace2D::Box2DSpace2D() {
736728
contact_bias = project_settings->get_setting_with_override("physics/2d/solver/default_contact_bias");
737729
constraint_bias = project_settings->get_setting_with_override("physics/2d/solver/default_constraint_bias");
738730

731+
default_gravity_dir = project_settings->get_setting_with_override("physics/2d/default_gravity_vector");
732+
default_gravity_value = project_settings->get_setting_with_override("physics/2d/default_gravity");
733+
734+
default_linear_damping = project_settings->get_setting_with_override("physics/2d/default_linear_damp");
735+
default_angular_damping = project_settings->get_setting_with_override("physics/2d/default_angular_damp");
736+
739737
direct_access = memnew(Box2DDirectSpaceState2D);
740738
direct_access->space = this;
741739

@@ -746,9 +744,13 @@ Box2DSpace2D::Box2DSpace2D() {
746744
world_settings.sleep_angular_threshold = body_angular_velocity_sleep_threshold;
747745
world_settings.sleep_time_until_sleep = body_time_to_sleep;
748746

749-
handle = box2d::world_create(world_settings);
747+
box2d::SimulationSettings simulation_settings;
748+
simulation_settings.sub_step_count = Box2DProjectSettings::get_sub_step_count();
749+
simulation_settings.gravity.x = default_gravity_dir.x * default_gravity_value;
750+
simulation_settings.gravity.y = default_gravity_dir.y * default_gravity_value;
751+
handle = box2d::world_create(world_settings, simulation_settings);
750752
ERR_FAIL_COND(!box2d::is_handle_valid(handle));
751-
753+
752754
box2d::world_set_active_body_callback(handle, active_body_callback);
753755
box2d::world_set_collision_filter_callback(handle, this);
754756
}

0 commit comments

Comments
 (0)