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

Commit

Permalink
update to state where objects fall/move
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu committed Mar 27, 2024
1 parent 7f8155b commit 630c927
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/bodies/box2d_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void Box2DBody2D::on_marked_active() {
ERR_FAIL_COND(!get_space());

// TODO: Check how that happens, probably not good for performance
//ERR_FAIL_COND(mode == PhysicsServer2D::BODY_MODE_STATIC);
ERR_FAIL_COND(mode == PhysicsServer2D::BODY_MODE_STATIC);
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
return;
}
Expand Down
197 changes: 136 additions & 61 deletions src/box2d-wrapper/box2d_wrapper.cpp

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion src/box2d-wrapper/box2d_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@

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

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

class Box2DCollisionObject2D;
Expand Down Expand Up @@ -450,7 +454,7 @@ ContactResult shapes_contact(b2WorldId world_handle,
ShapeInfo shape_info2,
real_t margin);

b2WorldId world_create(WorldSettings settings);
b2WorldId world_create(WorldSettings settings, SimulationSettings simulation_settings);

void world_destroy(b2WorldId world_handle);

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

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

int assert_callback(const char *condition, const char *fileName, int lineNumber);

void set_logging_enabled(bool enabled);

} // namespace box2d

#endif // BOX2D_WRAPPER_H
2 changes: 2 additions & 0 deletions src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ void initialize_box2d_module(ModuleInitializationLevel p_level) {
} break;
case MODULE_INITIALIZATION_LEVEL_SCENE: {
Box2DProjectSettings::register_settings();
b2SetAssertFcn(box2d::assert_callback);
box2d::set_logging_enabled(Box2DProjectSettings::get_logging_enabled());
} break;
default: {
} break;
Expand Down
8 changes: 7 additions & 1 deletion src/servers/box2d_project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ using namespace godot;

constexpr char RUN_ON_SEPARATE_THREAD[] = "physics/2d/run_on_separate_thread";
constexpr char MAX_THREADS[] = "threading/worker_pool/max_threads";
constexpr char SUB_STEP_COUNT[] = "physics/box_2d/solver/sub_step_count";
constexpr char SUB_STEP_COUNT[] = "physics/box_2d/sub_step_count";
constexpr char LOGGING_ENABLED[] = "physics/box_2d/logging";

void register_setting(
const String &p_name,
Expand Down Expand Up @@ -64,6 +65,7 @@ void register_setting_ranged(

void Box2DProjectSettings::register_settings() {
register_setting_ranged(SUB_STEP_COUNT, 4, U"1,16,or_greater");
register_setting_plain(LOGGING_ENABLED, false, true);
}

template <typename TType>
Expand All @@ -89,3 +91,7 @@ int Box2DProjectSettings::get_max_threads() {
int Box2DProjectSettings::get_sub_step_count() {
return get_setting<int>(SUB_STEP_COUNT);
}

bool Box2DProjectSettings::get_logging_enabled() {
return get_setting<bool>(LOGGING_ENABLED);
}
1 change: 1 addition & 0 deletions src/servers/box2d_project_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ class Box2DProjectSettings {

static bool should_run_on_separate_thread();
static int get_max_threads();
static bool get_logging_enabled();
static int get_sub_step_count();
};
22 changes: 12 additions & 10 deletions src/spaces/box2d_space_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,6 @@ void Box2DSpace2D::step(real_t p_step) {
}
contact_debug_count = 0;

ProjectSettings *project_settings = ProjectSettings::get_singleton();

default_gravity_dir = project_settings->get_setting_with_override("physics/2d/default_gravity_vector");
default_gravity_value = project_settings->get_setting_with_override("physics/2d/default_gravity");

default_linear_damping = project_settings->get_setting_with_override("physics/2d/default_linear_damp");
default_angular_damping = project_settings->get_setting_with_override("physics/2d/default_angular_damp");

for (SelfList<Box2DBody2D> *body_iterator = mass_properties_update_list.first(); body_iterator;) {
Box2DBody2D *body = body_iterator->self();
body_iterator = body_iterator->next();
Expand Down Expand Up @@ -736,6 +728,12 @@ Box2DSpace2D::Box2DSpace2D() {
contact_bias = project_settings->get_setting_with_override("physics/2d/solver/default_contact_bias");
constraint_bias = project_settings->get_setting_with_override("physics/2d/solver/default_constraint_bias");

default_gravity_dir = project_settings->get_setting_with_override("physics/2d/default_gravity_vector");
default_gravity_value = project_settings->get_setting_with_override("physics/2d/default_gravity");

default_linear_damping = project_settings->get_setting_with_override("physics/2d/default_linear_damp");
default_angular_damping = project_settings->get_setting_with_override("physics/2d/default_angular_damp");

direct_access = memnew(Box2DDirectSpaceState2D);
direct_access->space = this;

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

handle = box2d::world_create(world_settings);
box2d::SimulationSettings simulation_settings;
simulation_settings.sub_step_count = Box2DProjectSettings::get_sub_step_count();
simulation_settings.gravity.x = default_gravity_dir.x * default_gravity_value;
simulation_settings.gravity.y = default_gravity_dir.y * default_gravity_value;
handle = box2d::world_create(world_settings, simulation_settings);
ERR_FAIL_COND(!box2d::is_handle_valid(handle));

box2d::world_set_active_body_callback(handle, active_body_callback);
box2d::world_set_collision_filter_callback(handle, this);
}
Expand Down

0 comments on commit 630c927

Please sign in to comment.