diff --git a/src/bodies/box2d_body_2d.cpp b/src/bodies/box2d_body_2d.cpp index e2869d1..511b4de 100644 --- a/src/bodies/box2d_body_2d.cpp +++ b/src/bodies/box2d_body_2d.cpp @@ -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; } diff --git a/src/box2d-wrapper/box2d_wrapper.cpp b/src/box2d-wrapper/box2d_wrapper.cpp index bf556f3..36f95eb 100644 --- a/src/box2d-wrapper/box2d_wrapper.cpp +++ b/src/box2d-wrapper/box2d_wrapper.cpp @@ -17,6 +17,7 @@ struct Box2DHolder { }; Box2DHolder holder; +bool logging_enabled = false; bool is_toi_intersected(b2TOIOutput output) { return output.state == b2TOIState::b2_toiStateFailed || output.state == b2TOIState::b2_toiStateOverlapped || output.state == b2TOIState::b2_toiStateHit; @@ -193,7 +194,8 @@ b2BodyUserData *get_body_user_data(b2BodyId body_handle) { void box2d::body_add_force(b2BodyId body_handle, b2Vec2 force) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_add_force: ", rtos(body_handle.index), ", ", rtos(force.x), ", ", rtos(force.y)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_add_force: ", rtos(body_handle.index), ", ", rtos(force.x), ", ", rtos(force.y)); #endif b2BodyUserData *body_user_data = get_body_user_data(body_handle); ERR_FAIL_NULL(body_user_data); @@ -202,7 +204,8 @@ void box2d::body_add_force(b2BodyId body_handle, b2Vec2 force) { void box2d::body_add_torque(b2BodyId body_handle, real_t torque) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_add_torque: ", rtos(body_handle.index), ", ", rtos(torque)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_add_torque: ", rtos(body_handle.index), ", ", rtos(torque)); #endif b2BodyUserData *body_user_data = get_body_user_data(body_handle); ERR_FAIL_NULL(body_user_data); @@ -211,28 +214,32 @@ void box2d::body_add_torque(b2BodyId body_handle, real_t torque) { void box2d::body_apply_impulse(b2BodyId body_handle, b2Vec2 impulse) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_apply_impulse: ", rtos(body_handle.index), ", ", rtos(impulse.x), ", ", rtos(impulse.y)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_apply_impulse: ", rtos(body_handle.index), ", ", rtos(impulse.x), ", ", rtos(impulse.y)); #endif b2Body_ApplyLinearImpulseToCenter(body_handle, impulse, true); } void box2d::body_apply_impulse_at_point(b2BodyId body_handle, b2Vec2 impulse, b2Vec2 point) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_apply_impulse_at_point: ", rtos(body_handle.index), ", ", rtos(impulse.x), ", ", rtos(impulse.y), ", ", rtos(point.x), ", ", rtos(point.y)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_apply_impulse_at_point: ", rtos(body_handle.index), ", ", rtos(impulse.x), ", ", rtos(impulse.y), ", ", rtos(point.x), ", ", rtos(point.y)); #endif b2Body_ApplyLinearImpulse(body_handle, impulse, point, true); } void box2d::body_apply_torque_impulse(b2BodyId body_handle, real_t torque_impulse) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_apply_torque_impulse: ", rtos(body_handle.index), ", ", rtos(torque_impulse)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_apply_torque_impulse: ", rtos(body_handle.index), ", ", rtos(torque_impulse)); #endif b2Body_ApplyAngularImpulse(body_handle, torque_impulse, true); } void box2d::body_change_mode(b2BodyId body_handle, b2BodyType body_type, bool fixed_rotation) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_change_mode: ", rtos(body_handle.index), ", ", rtos(body_type), ", ", fixed_rotation); + if (logging_enabled) + UtilityFunctions::print("box2d::body_change_mode: ", rtos(body_handle.index), ", ", rtos(body_type), ", ", fixed_rotation); #endif // this updates body mass too b2Body_SetType(body_handle, body_type); @@ -247,7 +254,8 @@ b2BodyId box2d::body_create(b2WorldId world_handle, b2BodyUserData *user_data, b2BodyType body_type) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_create: ", rtos(world_handle.index), ", ", rtos(pos.x), ", ", rtos(pos.y), ", ", rtos(rot), ", ", rtos(body_type)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_create: ", rtos(world_handle.index), ", ", rtos(pos.x), ", ", rtos(pos.y), ", ", rtos(rot), ", ", rtos(body_type)); #endif b2BodyDef body_def = { body_type, // bodyType @@ -262,6 +270,7 @@ b2BodyId box2d::body_create(b2WorldId world_handle, true, // enableSleep true, // isAwake false, // fixedRotation + false, // isBullet true, // isEnabled }; return b2CreateBody(world_handle, &body_def); @@ -269,7 +278,8 @@ b2BodyId box2d::body_create(b2WorldId world_handle, void box2d::body_destroy(b2BodyId body_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_destroy: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_destroy: ", rtos(body_handle.index)); #endif // TODO destroy user data too b2DestroyBody(body_handle); @@ -277,7 +287,8 @@ void box2d::body_destroy(b2BodyId body_handle) { void box2d::body_force_sleep(b2BodyId body_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_force_sleep: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_force_sleep: ", rtos(body_handle.index)); #endif // TODO no function yet //if (body_handle->IsSleepingAllowed()) { @@ -287,14 +298,16 @@ void box2d::body_force_sleep(b2BodyId body_handle) { real_t box2d::body_get_angle(b2BodyId body_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_get_angle: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_get_angle: ", rtos(body_handle.index)); #endif return b2Body_GetAngle(body_handle); } real_t box2d::body_get_angular_velocity(b2BodyId body_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_get_angular_velocity: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_get_angular_velocity: ", rtos(body_handle.index)); #endif if (b2Body_GetType(body_handle) == b2_kinematicBody) { b2BodyUserData *body_user_data = get_body_user_data(body_handle); @@ -306,7 +319,8 @@ real_t box2d::body_get_angular_velocity(b2BodyId body_handle) { b2Vec2 box2d::body_get_constant_force(b2BodyId body_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_get_constant_force: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_get_constant_force: ", rtos(body_handle.index)); #endif b2BodyUserData *body_user_data = get_body_user_data(body_handle); ERR_FAIL_NULL_V(body_user_data, b2Vec2_zero); @@ -315,7 +329,8 @@ b2Vec2 box2d::body_get_constant_force(b2BodyId body_handle) { real_t box2d::body_get_constant_torque(b2BodyId body_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_get_constant_torque: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_get_constant_torque: ", rtos(body_handle.index)); #endif b2BodyUserData *body_user_data = get_body_user_data(body_handle); ERR_FAIL_NULL_V(body_user_data, 0.0); @@ -324,7 +339,8 @@ real_t box2d::body_get_constant_torque(b2BodyId body_handle) { b2Vec2 box2d::body_get_linear_velocity(b2BodyId body_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_get_linear_velocity: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_get_linear_velocity: ", rtos(body_handle.index)); #endif if (b2Body_GetType(body_handle) == b2_kinematicBody) { b2BodyUserData *body_user_data = get_body_user_data(body_handle); @@ -336,14 +352,16 @@ b2Vec2 box2d::body_get_linear_velocity(b2BodyId body_handle) { b2Vec2 box2d::body_get_position(b2BodyId body_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_get_position: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_get_position: ", rtos(body_handle.index)); #endif return b2Body_GetPosition(body_handle); } bool box2d::body_is_ccd_enabled(b2BodyId body_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_is_ccd_enabled: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_is_ccd_enabled: ", rtos(body_handle.index)); #endif return false; // TODO no function yet @@ -352,7 +370,8 @@ bool box2d::body_is_ccd_enabled(b2BodyId body_handle) { void box2d::body_reset_forces(b2BodyId body_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_reset_forces: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_reset_forces: ", rtos(body_handle.index)); #endif b2BodyUserData *body_user_data = get_body_user_data(body_handle); ERR_FAIL_NULL(body_user_data); @@ -361,7 +380,8 @@ void box2d::body_reset_forces(b2BodyId body_handle) { void box2d::body_reset_torques(b2BodyId body_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_reset_torques: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_reset_torques: ", rtos(body_handle.index)); #endif b2BodyUserData *body_user_data = get_body_user_data(body_handle); ERR_FAIL_NULL(body_user_data); @@ -370,21 +390,24 @@ void box2d::body_reset_torques(b2BodyId body_handle) { void box2d::body_set_angular_damping(b2BodyId body_handle, real_t angular_damping) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_set_angular_damping: ", rtos(body_handle.index), ", ", rtos(angular_damping)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_set_angular_damping: ", rtos(body_handle.index), ", ", rtos(angular_damping)); #endif b2Body_SetAngularDamping(body_handle, angular_damping); } void box2d::body_set_angular_velocity(b2BodyId body_handle, real_t vel) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_set_angular_velocity: ", rtos(body_handle.index), ", ", rtos(vel)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_set_angular_velocity: ", rtos(body_handle.index), ", ", rtos(vel)); #endif b2Body_SetAngularVelocity(body_handle, vel); } void box2d::body_set_can_sleep(b2BodyId body_handle, bool can_sleep) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_set_can_sleep: ", rtos(body_handle.index), ", ", rtos(can_sleep)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_set_can_sleep: ", rtos(body_handle.index), ", ", rtos(can_sleep)); #endif // TODO no function yet //b2Body_SetSleepingAllowed(body_handle, can_sleep); @@ -392,7 +415,8 @@ void box2d::body_set_can_sleep(b2BodyId body_handle, bool can_sleep) { void box2d::body_set_ccd_enabled(b2BodyId body_handle, bool enable) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_set_ccd_enabled: ", rtos(body_handle.index), ", ", rtos(enable)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_set_ccd_enabled: ", rtos(body_handle.index), ", ", rtos(enable)); #endif // TODO no function yet // body_handle->SetBullet(enable); @@ -401,21 +425,24 @@ void box2d::body_set_ccd_enabled(b2BodyId body_handle, bool enable) { void box2d::body_set_gravity_scale(b2BodyId body_handle, real_t gravity_scale) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_set_gravity_scale: ", rtos(body_handle.index), ", ", rtos(gravity_scale)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_set_gravity_scale: ", rtos(body_handle.index), ", ", rtos(gravity_scale)); #endif b2Body_SetGravityScale(body_handle, gravity_scale); } void box2d::body_set_linear_damping(b2BodyId body_handle, real_t linear_damping) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_set_linear_damping: ", rtos(body_handle.index), ", ", rtos(linear_damping)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_set_linear_damping: ", rtos(body_handle.index), ", ", rtos(linear_damping)); #endif b2Body_SetLinearDamping(body_handle, linear_damping); } void box2d::body_set_linear_velocity(b2BodyId body_handle, b2Vec2 vel) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_set_linear_velocity: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_set_linear_velocity: ", rtos(body_handle.index)); #endif if (b2Body_GetType(body_handle) == b2_kinematicBody) { // for kinematic setting velocity is like moving the object, we want it to happen all the time @@ -427,7 +454,8 @@ void box2d::body_set_linear_velocity(b2BodyId body_handle, b2Vec2 vel) { void box2d::body_set_mass_properties(b2BodyId body_handle, real_t mass, real_t inertia, b2Vec2 local_com) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_set_mass_properties: ", rtos(body_handle.index), ", ", rtos(mass), ", ", rtos(inertia), ", ", rtos(local_com.x), ", ", rtos(local_com.y)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_set_mass_properties: ", rtos(body_handle.index), ", ", rtos(mass), ", ", rtos(inertia), ", ", rtos(local_com.x), ", ", rtos(local_com.y)); #endif b2MassData mass_data{ mass, local_com, inertia }; b2Body_SetMassData(body_handle, mass_data); @@ -435,7 +463,8 @@ void box2d::body_set_mass_properties(b2BodyId body_handle, real_t mass, real_t i void box2d::body_set_transform(b2BodyId body_handle, b2Vec2 pos, real_t rot, real_t step) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_set_transform: ", rtos(body_handle.index), ", ", rtos(pos.x), ", ", rtos(pos.y), ", ", rtos(rot), ", ", rtos(step)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_set_transform: ", rtos(body_handle.index), ", ", rtos(pos.x), ", ", rtos(pos.y), ", ", rtos(rot), ", ", rtos(step)); #endif b2Vec2 new_pos = b2Vec2_sub(pos, b2Body_GetPosition(body_handle)); new_pos.x /= step; @@ -457,7 +486,8 @@ void box2d::body_set_transform(b2BodyId body_handle, b2Vec2 pos, real_t rot, rea void box2d::body_update_material(b2BodyId body_handle, Material mat) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_update_material: ", rtos(body_handle.index), ", ", rtos(mat.friction), ", ", rtos(mat.restitution)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_update_material: ", rtos(body_handle.index), ", ", rtos(mat.friction), ", ", rtos(mat.restitution)); #endif for (b2ShapeId shape_handle = b2Body_GetFirstShape(body_handle); B2_IS_NON_NULL(shape_handle); shape_handle = b2Body_GetNextShape(shape_handle)) { b2Shape_SetFriction(shape_handle, mat.friction); @@ -467,7 +497,8 @@ void box2d::body_update_material(b2BodyId body_handle, Material mat) { void box2d::body_wake_up(b2BodyId body_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::body_wake_up: ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::body_wake_up: ", rtos(body_handle.index)); #endif if (b2Body_GetType(body_handle) == b2BodyType::b2_kinematicBody) { b2Body_Wake(body_handle); @@ -507,7 +538,8 @@ FixtureHandle box2d::collider_create_sensor(b2WorldId world_handle, b2BodyId body_handle, b2FixtureUserData *user_data) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::collider_create_sensor: ", rtos(world_handle.index), ", ", rtos(shape_handles.handles.size()), ", ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::collider_create_sensor: ", rtos(world_handle.index), ", ", rtos(shape_handles.handles.size()), ", ", rtos(body_handle.index)); #endif FixtureHandle fixture_handle{}; b2MassData mass_data = b2Body_GetMassData(body_handle); @@ -525,7 +557,8 @@ FixtureHandle box2d::collider_create_solid(b2WorldId world_handle, b2BodyId body_handle, b2FixtureUserData *user_data) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::collider_create_solid: ", rtos(world_handle.index), ", ", rtos(shape_handles.handles.size()), ", ", rtos(body_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::collider_create_solid: ", rtos(world_handle.index), ", ", rtos(shape_handles.handles.size()), ", ", rtos(body_handle.index)); #endif FixtureHandle fixture_handle{}; b2MassData mass_data = b2Body_GetMassData(body_handle); @@ -539,7 +572,8 @@ FixtureHandle box2d::collider_create_solid(b2WorldId world_handle, void box2d::collider_set_contact_force_events_enabled(FixtureHandle collider_handle, bool send_contacts) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::collider_set_contact_force_events_enabled: ", rtos(collider_handle.handles.size()), ", ", rtos(send_contacts)); + if (logging_enabled) + UtilityFunctions::print("box2d::collider_set_contact_force_events_enabled: ", rtos(collider_handle.handles.size()), ", ", rtos(send_contacts)); #endif for (int i = 0; i < collider_handle.handles.size(); i++) { b2ShapeId shape_id = collider_handle.handles[i]; @@ -550,7 +584,8 @@ void box2d::collider_set_contact_force_events_enabled(FixtureHandle collider_han void box2d::collider_destroy(FixtureHandle handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::collider_destroy: ", rtos(handle.handles.size())); + if (logging_enabled) + UtilityFunctions::print("box2d::collider_destroy: ", rtos(handle.handles.size())); #endif for (b2ShapeId shapeId : handle.handles) { b2DestroyShape(shapeId); @@ -596,7 +631,8 @@ b2Vec2 xform_b2Vec2(b2Vec2 vec, Transform2D transform) { // TODO void box2d::collider_set_transform(b2WorldId world_handle, FixtureHandle handles, ShapeInfo shape_info) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::collider_set_transform: ", rtos(world_handle.index), ", ", rtos(handles.handles.size())); + if (logging_enabled) + UtilityFunctions::print("box2d::collider_set_transform: ", rtos(world_handle.index), ", ", rtos(handles.handles.size())); #endif ERR_FAIL_COND(!is_handle_valid(shape_info.handle)); for (b2ShapeId handle : handles.handles) { @@ -662,7 +698,8 @@ void box2d::collider_set_transform(b2WorldId world_handle, FixtureHandle handles Transform2D box2d::collider_get_transform(b2WorldId world_handle, FixtureHandle handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::collider_get_transform: ", rtos(world_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::collider_get_transform: ", rtos(world_handle.index)); #endif ERR_FAIL_COND_V(!is_handle_valid(handle), Transform2D()); b2FixtureUserData *user_data = static_cast(b2Shape_GetUserData(handle.handles[0])); @@ -672,7 +709,8 @@ Transform2D box2d::collider_get_transform(b2WorldId world_handle, FixtureHandle Transform2D box2d::collider_get_transform(b2WorldId world_handle, b2ShapeId handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::collider_get_transform: ", rtos(world_handle.index), ", ", rtos(handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::collider_get_transform: ", rtos(world_handle.index), ", ", rtos(handle.index)); #endif ERR_FAIL_COND_V(!is_handle_valid(handle), Transform2D()); b2FixtureUserData *user_data = static_cast(b2Shape_GetUserData(handle)); @@ -744,7 +782,8 @@ size_t box2d::intersect_aabb(b2WorldId world_handle, QueryHandleExcludedCallback handle_excluded_callback, const QueryExcludedInfo *handle_excluded_info) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::intersect_aabb: ", rtos(world_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::intersect_aabb: ", rtos(world_handle.index)); #endif AABBQueryCallback callback; callback.world = world_handle; @@ -770,7 +809,8 @@ size_t box2d::intersect_point(b2WorldId world_handle, QueryHandleExcludedCallback handle_excluded_callback, const QueryExcludedInfo *handle_excluded_info) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::intersect_point: ", rtos(world_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::intersect_point: ", rtos(world_handle.index)); #endif AABBQueryCallback callback; callback.world = world_handle; @@ -844,7 +884,8 @@ bool box2d::intersect_ray(b2WorldId world_handle, QueryHandleExcludedCallback handle_excluded_callback, const QueryExcludedInfo *handle_excluded_info) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::intersect_ray: ", rtos(world_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::intersect_ray: ", rtos(world_handle.index)); #endif RayCastQueryCallback callback; callback.world = world_handle; @@ -925,7 +966,8 @@ void box2d::joint_change_revolute_params(b2JointId joint_handle, real_t motor_target_velocity, bool motor_enabled) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::joint_change_revolute_params: ", rtos(joint_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::joint_change_revolute_params: ", rtos(joint_handle.index)); #endif //joint->SetLimits(angular_limit_lower, angular_limit_upper); b2RevoluteJoint_EnableMotor(joint_handle, motor_enabled); @@ -942,7 +984,8 @@ b2JointId box2d::joint_create_prismatic(b2WorldId world_handle, const b2Vec2 limits, bool disable_collision) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::joint_create_prismatic: ", rtos(world_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::joint_create_prismatic: ", rtos(world_handle.index)); #endif b2PrismaticJointDef joint_def; joint_def.localAnchorA = anchor_1; @@ -967,7 +1010,8 @@ b2JointId box2d::joint_create_revolute(b2WorldId world_handle, bool motor_enabled, bool disable_collision) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::joint_create_revolute: ", rtos(world_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::joint_create_revolute: ", rtos(world_handle.index)); #endif b2RevoluteJointDef joint_def; joint_def.localAnchorA = anchor_1; @@ -987,7 +1031,8 @@ void box2d::joint_change_distance_joint(b2JointId joint_handle, real_t stiffness, real_t damping) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::joint_change_distance_joint: ", rtos(joint_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::joint_change_distance_joint: ", rtos(joint_handle.index)); #endif b2DistanceJoint_SetLength(joint_handle, rest_length); b2DistanceJoint_SetTuning(joint_handle, stiffness, damping); @@ -1004,7 +1049,8 @@ b2JointId box2d::joint_create_distance_joint(b2WorldId world_handle, real_t damping, bool disable_collision) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::joint_create_distance_joint: ", rtos(world_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::joint_create_distance_joint: ", rtos(world_handle.index)); #endif b2DistanceJointDef joint_def; joint_def.bodyIdA = body_handle_1; @@ -1022,7 +1068,8 @@ b2JointId box2d::joint_create_distance_joint(b2WorldId world_handle, void box2d::joint_destroy(b2JointId joint_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::joint_destroy: ", rtos(joint_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::joint_destroy: ", rtos(joint_handle.index)); #endif b2DestroyJoint(joint_handle); } @@ -1037,7 +1084,8 @@ ShapeCastResult box2d::shape_casting(b2WorldId world_handle, const QueryExcludedInfo *handle_excluded_info, double margin) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::shape_casting: ", rtos(world_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::shape_casting: ", rtos(world_handle.index)); #endif for (int i = 0; i < shape_info.handle.handles.size(); i++) { // TODO @@ -1111,7 +1159,8 @@ ShapeCollideResult box2d::shape_collide(const b2Vec2 motion1, const b2Vec2 motion2, ShapeInfo shape_info2) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::shape_collide: "); + if (logging_enabled) + UtilityFunctions::print("box2d::shape_collide: "); #endif for (int i = 0; i < shape_info1.handle.handles.size(); i++) { for (int j = 0; j < shape_info2.handle.handles.size(); j++) { @@ -1147,7 +1196,8 @@ ShapeCollideResult box2d::shape_collide(const b2Vec2 motion1, ShapeHandle box2d::shape_create_box(const b2Vec2 size) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::shape_create_box: "); + if (logging_enabled) + UtilityFunctions::print("box2d::shape_create_box: "); #endif ERR_FAIL_COND_V(size.x < CMP_EPSILON, invalid_shape_handle()); ERR_FAIL_COND_V(size.y < CMP_EPSILON, invalid_shape_handle()); @@ -1160,7 +1210,8 @@ ShapeHandle box2d::shape_create_box(const b2Vec2 size) { ShapeHandle box2d::shape_create_capsule(real_t half_height, real_t radius) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::shape_create_capsule: "); + if (logging_enabled) + UtilityFunctions::print("box2d::shape_create_capsule: "); #endif ERR_FAIL_COND_V(radius < CMP_EPSILON, invalid_shape_handle()); ERR_FAIL_COND_V(half_height < CMP_EPSILON, invalid_shape_handle()); @@ -1178,7 +1229,8 @@ ShapeHandle box2d::shape_create_capsule(real_t half_height, real_t radius) { ShapeHandle box2d::shape_create_circle(real_t radius, b2Vec2 pos) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::shape_create_circle: "); + if (logging_enabled) + UtilityFunctions::print("box2d::shape_create_circle: "); #endif ERR_FAIL_COND_V(radius < CMP_EPSILON, invalid_shape_handle()); b2Circle circle_shape = { @@ -1194,7 +1246,8 @@ ShapeHandle box2d::shape_create_circle(real_t radius, b2Vec2 pos) { // TODO ShapeHandle box2d::shape_create_concave_polyline(const b2Vec2 *points, size_t point_count) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::shape_create_concave_polyline: "); + if (logging_enabled) + UtilityFunctions::print("box2d::shape_create_concave_polyline: "); #endif b2Hull hull; ERR_FAIL_COND_V(point_count > b2_maxPolygonVertices, invalid_shape_handle()); @@ -1212,7 +1265,8 @@ ShapeHandle box2d::shape_create_concave_polyline(const b2Vec2 *points, size_t po // TODO ShapeHandle box2d::shape_create_convex_polyline(const b2Vec2 *points, size_t point_count) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::shape_create_convex_polyline: "); + if (logging_enabled) + UtilityFunctions::print("box2d::shape_create_convex_polyline: "); #endif b2Hull hull; ERR_FAIL_COND_V(point_count > b2_maxPolygonVertices, invalid_shape_handle()); @@ -1229,7 +1283,8 @@ ShapeHandle box2d::shape_create_convex_polyline(const b2Vec2 *points, size_t poi ShapeHandle box2d::shape_create_halfspace(const b2Vec2 normal, real_t distance) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::shape_create_halfspace: "); + if (logging_enabled) + UtilityFunctions::print("box2d::shape_create_halfspace: "); #endif real_t world_size = 1000000.0; b2Vec2 points[4]; @@ -1259,7 +1314,8 @@ ShapeHandle box2d::shape_create_halfspace(const b2Vec2 normal, real_t distance) void box2d::shape_destroy(ShapeHandle shape_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::shape_destroy: "); + if (logging_enabled) + UtilityFunctions::print("box2d::shape_destroy: "); #endif shape_handle.handles.clear(); } @@ -1270,7 +1326,8 @@ ContactResult box2d::shapes_contact(b2WorldId world_handle, ShapeInfo shape_info2, real_t margin) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::shapes_contact: ", rtos(world_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::shapes_contact: ", rtos(world_handle.index)); #endif /* for (int i = 0; i < shape_info1.handle.count; i++) { @@ -1338,24 +1395,28 @@ ContactResult box2d::shapes_contact(b2WorldId world_handle, }; } -b2WorldId box2d::world_create(WorldSettings settings) { +b2WorldId box2d::world_create(WorldSettings settings, SimulationSettings simulation_settings) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::world_create"); + if (logging_enabled) + UtilityFunctions::print("box2d::world_create"); #endif b2WorldDef world_def = b2DefaultWorldDef(); + world_def.gravity = simulation_settings.gravity; return b2CreateWorld(&world_def); } void box2d::world_destroy(b2WorldId world_handle) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::world_destroy", rtos(world_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::world_destroy", rtos(world_handle.index)); #endif b2DestroyWorld(world_handle); } void box2d::world_set_active_body_callback(b2WorldId world_handle, ActiveBodyCallback callback) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::world_set_active_body_callback", rtos(world_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::world_set_active_body_callback", rtos(world_handle.index)); #endif holder.active_body_callbacks[handle_hash(world_handle)] = callback; } @@ -1368,7 +1429,8 @@ bool presolve_fcn(b2ShapeId shapeIdA, b2ShapeId shapeIdB, b2Manifold *manifold, void box2d::world_set_collision_filter_callback(b2WorldId world_handle, b2ContactFilter *callback) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::world_set_collision_filter_callback", rtos(world_handle.index)); + if (logging_enabled) + UtilityFunctions::print("box2d::world_set_collision_filter_callback", rtos(world_handle.index)); #endif b2World_SetPreSolveCallback(world_handle, presolve_fcn, callback); } @@ -1376,8 +1438,11 @@ void box2d::world_set_collision_filter_callback(b2WorldId world_handle, // TODO void box2d::world_step(b2WorldId world_handle, SimulationSettings settings, std::vector active_bodies) { #ifdef B2_DEBUG - UtilityFunctions::print("box2d::world_step", rtos(world_handle.index)); + if (logging_enabled) { + UtilityFunctions::print("box2d::world_step", rtos(world_handle.index)); + } #endif + //world_handle->SetGravity(settings->gravity); // TODO set world gravity b2World_Step(world_handle, settings.dt, settings.sub_step_count); @@ -1410,3 +1475,13 @@ void box2d::world_step(b2WorldId world_handle, SimulationSettings settings, std: } } } + +int box2d::assert_callback(const char *condition, const char *fileName, int lineNumber) { + ERR_PRINT("Assertion failed: " + String(condition) + " at " + String(fileName) + ":" + rtos(lineNumber)); + return 0; +} + +// TODO maybe make a class if we have this exposed like so. +void box2d::set_logging_enabled(bool enabled) { + logging_enabled = enabled; +} \ No newline at end of file diff --git a/src/box2d-wrapper/box2d_wrapper.h b/src/box2d-wrapper/box2d_wrapper.h index c604915..09af4c9 100644 --- a/src/box2d-wrapper/box2d_wrapper.h +++ b/src/box2d-wrapper/box2d_wrapper.h @@ -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; @@ -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); @@ -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 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 diff --git a/src/register_types.cpp b/src/register_types.cpp index 7ef9d6e..139715b 100644 --- a/src/register_types.cpp +++ b/src/register_types.cpp @@ -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; diff --git a/src/servers/box2d_project_settings.cpp b/src/servers/box2d_project_settings.cpp index 6d505eb..ff1bb34 100644 --- a/src/servers/box2d_project_settings.cpp +++ b/src/servers/box2d_project_settings.cpp @@ -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, @@ -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 @@ -89,3 +91,7 @@ int Box2DProjectSettings::get_max_threads() { int Box2DProjectSettings::get_sub_step_count() { return get_setting(SUB_STEP_COUNT); } + +bool Box2DProjectSettings::get_logging_enabled() { + return get_setting(LOGGING_ENABLED); +} \ No newline at end of file diff --git a/src/servers/box2d_project_settings.h b/src/servers/box2d_project_settings.h index 2cde018..6923b4d 100644 --- a/src/servers/box2d_project_settings.h +++ b/src/servers/box2d_project_settings.h @@ -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(); }; diff --git a/src/spaces/box2d_space_2d.cpp b/src/spaces/box2d_space_2d.cpp index 1b75e67..8be0a56 100644 --- a/src/spaces/box2d_space_2d.cpp +++ b/src/spaces/box2d_space_2d.cpp @@ -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 *body_iterator = mass_properties_update_list.first(); body_iterator;) { Box2DBody2D *body = body_iterator->self(); body_iterator = body_iterator->next(); @@ -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; @@ -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); }