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

Commit

Permalink
fix one way dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu committed Nov 13, 2023
1 parent 14996a7 commit b6a2390
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 81 deletions.
2 changes: 1 addition & 1 deletion src/bodies/box2d_area_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ Box2DArea2D::Box2DArea2D() :
Box2DCollisionObject2D(TYPE_AREA),
monitor_query_list(this),
area_override_update_list(this) {
mode = PhysicsServer2D::BODY_MODE_STATIC;
mode = PhysicsServer2D::BODY_MODE_RIGID;
}

Box2DArea2D::~Box2DArea2D() {
Expand Down
10 changes: 8 additions & 2 deletions src/box2d-wrapper/box2d_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ FixtureHandle box2d::collider_create_solid(b2World *world_handle,
b2FixtureDef fixture_def;
fixture_def.shape = shape_handle.handles[i];
fixture_def.density = 1.0f;
fixture_def.restitution = mat->restitution;
fixture_def.friction = mat->friction;
fixture_def.isSensor = false;
fixture_def.userData = user_data;
b2Fixture *fixture = body_handle->CreateFixture(&fixture_def);
Expand Down Expand Up @@ -534,11 +536,15 @@ void box2d::collider_set_transform(b2World *world_handle, FixtureHandle handles,
b2PolygonShape *polygon_shape_template = (b2PolygonShape *)shape_template;
b2PolygonShape *polygon_shape = (b2PolygonShape *)shape;
b2Vec2 new_vertices[b2_maxPolygonVertices];
b2Hull hull;
for (int i = 0; i < polygon_shape->m_count; i++) {
new_vertices[i] = polygon_shape_template->m_vertices[i];
new_vertices[i] = xform_b2Vec2(new_vertices[i], transform);
hull.points[i] = new_vertices[i];
}
if (!polygon_shape->Set(new_vertices, polygon_shape->m_count)) {
hull.count = polygon_shape->m_count;
polygon_shape->Set(hull);
if (!polygon_shape->m_count) {
ERR_FAIL_MSG("Cannot update vertices");
}
} break;
Expand Down Expand Up @@ -1170,7 +1176,7 @@ void box2d::world_set_contact_listener(b2World *world_handle,

void box2d::world_step(b2World *world_handle, const SimulationSettings *settings) {
world_handle->SetGravity(settings->gravity);
world_handle->Step(settings->dt, 8, 3);
world_handle->Step(settings->dt, settings->max_velocity_iterations, settings->max_position_iterations);
int active_objects = 0;
if (holder.active_body_callbacks.has(world_handle)) {
ActiveBodyCallback callback = holder.active_body_callbacks[world_handle];
Expand Down
47 changes: 1 addition & 46 deletions src/box2d-wrapper/box2d_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,54 +166,9 @@ using CollisionModifyContactsCallback = OneWayDirection (*)(b2World *world_handl
const CollisionFilterInfo *filter_info);

struct SimulationSettings {
/// The timestep length (default: `1.0 / 60.0`)
real_t dt;
/// Minimum timestep size when using CCD with multiple substeps (default `1.0 / 60.0 / 100.0`)
///
/// When CCD with multiple substeps is enabled, the timestep is subdivided
/// into smaller pieces. This timestep subdivision won't generate timestep
/// lengths smaller than `min_ccd_dt`.
///
/// Setting this to a large value will reduce the opportunity to performing
/// CCD substepping, resulting in potentially more time dropped by the
/// motion-clamping mechanism. Setting this to an very small value may lead
/// to numerical instabilities.
real_t min_ccd_dt;
/// 0-1: multiplier for how much of the constraint violation (e.g. contact penetration)
/// will be compensated for during the velocity solve.
/// (default `0.8`).
real_t erp;
/// 0-1: the damping ratio used by the springs for Baumgarte constraints stabilization.
/// Lower values make the constraints more compliant (more "springy", allowing more visible penetrations
/// before stabilization).
/// (default `0.25`).
real_t damping_ratio;
/// 0-1: multiplier for how much of the joint violation
/// will be compensated for during the velocity solve.
/// (default `1.0`).
real_t joint_erp;
/// The fraction of critical damping applied to the joint for constraints regularization.
/// (default `0.25`).
real_t joint_damping_ratio;
/// Amount of penetration the engine wont attempt to correct (default: `0.001m`).
real_t allowed_linear_error;
/// Maximum amount of penetration the solver will attempt to resolve in one timestep.
real_t max_penetration_correction;
/// The maximal distance separating two objects that will generate predictive contacts (default: `0.002`).
real_t prediction_distance;
/// Maximum number of iterations performed to solve non-penetration and joint constraints (default: `4`).
size_t max_velocity_iterations;
/// Maximum number of iterations performed to solve friction constraints (default: `8`).
size_t max_velocity_friction_iterations;
/// Maximum number of iterations performed to remove the energy introduced by penetration corrections (default: `1`).
size_t max_stabilization_iterations;
/// If `false`, friction and non-penetration constraints will be solved in the same loop. Otherwise,
/// non-penetration constraints are solved first, and friction constraints are solved after (default: `true`).
bool interleave_restitution_and_friction_resolution;
/// Minimum number of dynamic bodies in each active island (default: `128`).
size_t min_island_size;
/// Maximum number of substeps performed by the solver (default: `1`).
size_t max_ccd_substeps;
size_t max_position_iterations;
b2Vec2 gravity;
};

Expand Down
4 changes: 2 additions & 2 deletions src/servers/box2d_body_utils_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

bool should_skip_collision_one_dir(box2d::ContactResult contact, Box2DShape2D *body_shape, Box2DBody2D *collision_body, int shape_index, const Transform2D &col_shape_transform, real_t p_margin, real_t last_step, Vector2 p_motion) {
real_t dist = contact.distance;
if (contact.collided && !contact.within_margin && body_shape->allows_one_way_collision() && collision_body->is_shape_set_as_one_way_collision(shape_index)) {
if (contact.collided && body_shape->allows_one_way_collision() && collision_body->is_shape_set_as_one_way_collision(shape_index)) {
real_t valid_depth = 10e20;
Vector2 valid_dir = col_shape_transform.columns[1].normalized();

Expand All @@ -34,7 +34,7 @@ bool should_skip_collision_one_dir(box2d::ContactResult contact, Box2DShape2D *b
Vector2 motion = p_motion;
real_t motion_len = motion.length();
valid_depth += motion_len * MAX(motion.normalized().dot(valid_dir), 0.0);
if ((dist < -valid_depth) || (p_motion.normalized().dot(valid_dir) < CMP_EPSILON * 10.0)) {
if ((dist > valid_depth) || (p_motion.normalized().dot(valid_dir) < CMP_EPSILON * 10.0)) {
return true;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/servers/box2d_physics_server_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,12 @@ void Box2DPhysicsServer2D::_body_set_state(const RID &p_body, BodyState p_state,

Variant Box2DPhysicsServer2D::_body_get_state(const RID &p_body, BodyState p_state) const {
Box2DBody2D *body = body_owner.get_or_null(p_body);
if (!body) {
Box2DArea2D *area = area_owner.get_or_null(p_body);
if (p_state == BodyState::BODY_STATE_TRANSFORM) {
return area->get_transform();
}
}
ERR_FAIL_COND_V(!body, Variant());

return body->get_state(p_state);
Expand Down
25 changes: 0 additions & 25 deletions src/servers/box2d_project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@

using namespace godot;

constexpr char DEFAULT_LINEAR_DAMP[] = "physics/2d/default_linear_damp";
constexpr char DEFAULT_ANGULAR_DAMP[] = "physics/2d/default_angular_damp";
constexpr char DEFAULT_GRAVITY_VECTOR[] = "physics/2d/default_gravity_vector";
constexpr char DEFAULT_GRAVITY[] = "physics/2d/default_gravity";

constexpr char PHYSICS_FPS[] = "physics/common/physics_ticks_per_second";
constexpr char RUN_ON_SEPARATE_THREAD[] = "physics/2d/run_on_separate_thread";
constexpr char MAX_THREADS[] = "threading/worker_pool/max_threads";
constexpr char POSITION_ITERATIONS[] = "physics/box_2d/solver/position_iterations";
constexpr char VELOCITY_ITERATIONS[] = "physics/box_2d/solver/velocity_iterations";
constexpr char SCALING_FACTOR[] = "physics/box_2d/scaling_factor";

void register_setting(
const String &p_name,
Expand Down Expand Up @@ -73,7 +66,6 @@ void register_setting_ranged(
void Box2DProjectSettings::register_settings() {
register_setting_ranged(VELOCITY_ITERATIONS, 8, U"2,16,or_greater");
register_setting_ranged(POSITION_ITERATIONS, 3, U"1,16,or_greater");
register_setting_ranged(SCALING_FACTOR, 100.0f, U"1,100,or_greater");
}

template <typename TType>
Expand Down Expand Up @@ -103,20 +95,3 @@ int Box2DProjectSettings::get_position_iterations() {
int Box2DProjectSettings::get_velocity_iterations() {
return get_setting<int>(VELOCITY_ITERATIONS);
}

int Box2DProjectSettings::get_physics_fps() {
return get_setting<int>(PHYSICS_FPS);
}

float Box2DProjectSettings::get_default_linear_damp() {
return get_setting<float>(DEFAULT_LINEAR_DAMP);
}
float Box2DProjectSettings::get_default_angular_damp() {
return get_setting<float>(DEFAULT_ANGULAR_DAMP);
}
float Box2DProjectSettings::get_default_gravity() {
return get_setting<float>(DEFAULT_GRAVITY);
}
Vector2 Box2DProjectSettings::get_default_gravity_vector() {
return get_setting<Vector2>(DEFAULT_GRAVITY_VECTOR);
}
5 changes: 0 additions & 5 deletions src/servers/box2d_project_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ class Box2DProjectSettings {
static int get_max_threads();
static int get_position_iterations();
static int get_velocity_iterations();
static int get_physics_fps();
static float get_default_linear_damp();
static float get_default_angular_damp();
static float get_default_gravity();
static Vector2 get_default_gravity_vector();
};
2 changes: 2 additions & 0 deletions src/spaces/box2d_space_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ void Box2DSpace2D::step(real_t p_step) {

box2d::SimulationSettings settings;
settings.dt = p_step;
settings.max_position_iterations = Box2DProjectSettings::get_position_iterations();
settings.max_velocity_iterations = Box2DProjectSettings::get_velocity_iterations();
settings.gravity.x = default_gravity_dir.x * default_gravity_value;
settings.gravity.y = default_gravity_dir.y * default_gravity_value;

Expand Down

0 comments on commit b6a2390

Please sign in to comment.