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

Commit cdd251a

Browse files
committed
remove some errors and replace with if instead.
1 parent 7fa8d65 commit cdd251a

File tree

5 files changed

+150
-78
lines changed

5 files changed

+150
-78
lines changed

godot.code-workspace

+21
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,26 @@
2424
"string_view": "cpp",
2525
"__config": "cpp"
2626
}
27+
},
28+
"launch": {
29+
"version": "0.2.0",
30+
"configurations": [
31+
{
32+
"name": "Launch",
33+
"program": "/Users/dragosdaian/Documents/GitHub/godot/bin/godot.macos.editor.arm64",
34+
"type": "cppdbg",
35+
"request": "launch",
36+
"cwd": "/Users/dragosdaian/Documents/GitHub/godot-box2d",
37+
"osx": {
38+
"MIMode": "lldb"
39+
},
40+
"args": [
41+
"--path",
42+
"/Users/dragosdaian/Documents/GitHub/godot-box2d/Godot-Physics-Tests",
43+
"--debug-collisions",
44+
"test.tscn"
45+
]
46+
}
47+
]
2748
}
2849
}

src/bodies/box2d_body_2d.cpp

+93-45
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ void Box2DBody2D::_mass_properties_changed() {
1818
}
1919

2020
void Box2DBody2D::_apply_mass_properties(bool force_update) {
21-
ERR_FAIL_COND(mode < PhysicsServer2D::BODY_MODE_RIGID);
22-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
21+
if (mode < PhysicsServer2D::BODY_MODE_RIGID || !box2d::is_handle_valid(body_handle)) {
22+
return;
23+
}
2324

2425
real_t inertia_value = (mode == PhysicsServer2D::BODY_MODE_RIGID_LINEAR) ? 0.0 : inertia;
2526
box2d::body_set_mass_properties(body_handle, mass, inertia_value, { center_of_mass.x, center_of_mass.y });
2627
}
2728

2829
void Box2DBody2D::update_mass_properties(bool force_update) {
29-
ERR_FAIL_COND(mode < PhysicsServer2D::BODY_MODE_RIGID);
30+
if (mode < PhysicsServer2D::BODY_MODE_RIGID) {
31+
return;
32+
}
3033

3134
mass_properties_update_list.remove_from_list();
3235

@@ -125,7 +128,9 @@ void Box2DBody2D::set_active(bool p_active) {
125128
}
126129

127130
void Box2DBody2D::set_can_sleep(bool p_can_sleep) {
128-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
131+
if (!box2d::is_handle_valid(body_handle)) {
132+
return;
133+
}
129134
box2d::body_set_can_sleep(body_handle, p_can_sleep);
130135
}
131136

@@ -174,8 +179,6 @@ void Box2DBody2D::on_update_active() {
174179
}
175180

176181
void Box2DBody2D::set_param(PhysicsServer2D::BodyParameter p_param, const Variant &p_value) {
177-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
178-
179182
switch (p_param) {
180183
case PhysicsServer2D::BODY_PARAM_BOUNCE:
181184
case PhysicsServer2D::BODY_PARAM_FRICTION: {
@@ -187,6 +190,9 @@ void Box2DBody2D::set_param(PhysicsServer2D::BodyParameter p_param, const Varian
187190
box2d::Material mat;
188191
mat.friction = friction;
189192
mat.restitution = bounce;
193+
if (!box2d::is_handle_valid(body_handle)) {
194+
return;
195+
}
190196
box2d::body_update_material(body_handle, mat);
191197
} break;
192198
case PhysicsServer2D::BODY_PARAM_MASS: {
@@ -320,17 +326,19 @@ void Box2DBody2D::set_mode(PhysicsServer2D::BodyMode p_mode) {
320326
}
321327
PhysicsServer2D::BodyMode prev_mode = mode;
322328
mode = p_mode;
323-
switch (p_mode) {
324-
case PhysicsServer2D::BODY_MODE_KINEMATIC: {
325-
box2d::body_change_mode(get_body_handle(), b2BodyType::b2_kinematicBody, false);
326-
} break;
327-
case PhysicsServer2D::BODY_MODE_STATIC: {
328-
box2d::body_change_mode(get_body_handle(), b2BodyType::b2_staticBody, false);
329-
} break;
330-
case PhysicsServer2D::BODY_MODE_RIGID:
331-
case PhysicsServer2D::BODY_MODE_RIGID_LINEAR: {
332-
box2d::body_change_mode(get_body_handle(), b2BodyType::b2_dynamicBody, p_mode == PhysicsServer2D::BODY_MODE_RIGID_LINEAR);
333-
} break;
329+
if (get_space()) {
330+
switch (p_mode) {
331+
case PhysicsServer2D::BODY_MODE_KINEMATIC: {
332+
box2d::body_change_mode(get_body_handle(), b2BodyType::b2_kinematicBody, false);
333+
} break;
334+
case PhysicsServer2D::BODY_MODE_STATIC: {
335+
box2d::body_change_mode(get_body_handle(), b2BodyType::b2_staticBody, false);
336+
} break;
337+
case PhysicsServer2D::BODY_MODE_RIGID:
338+
case PhysicsServer2D::BODY_MODE_RIGID_LINEAR: {
339+
box2d::body_change_mode(get_body_handle(), b2BodyType::b2_dynamicBody, p_mode == PhysicsServer2D::BODY_MODE_RIGID_LINEAR);
340+
} break;
341+
}
334342
}
335343
if (p_mode == PhysicsServer2D::BODY_MODE_STATIC) {
336344
force_sleep();
@@ -435,7 +443,9 @@ Variant Box2DBody2D::get_state(PhysicsServer2D::BodyState p_state) const {
435443
}
436444

437445
void Box2DBody2D::set_continuous_collision_detection_mode(PhysicsServer2D::CCDMode p_mode) {
438-
ERR_FAIL_COND(mode < PhysicsServer2D::BODY_MODE_RIGID);
446+
if (!box2d::is_handle_valid(body_handle) || mode < PhysicsServer2D::BODY_MODE_RIGID) {
447+
return;
448+
}
439449
if (ccd_mode == p_mode) {
440450
return;
441451
}
@@ -617,7 +627,9 @@ void Box2DBody2D::apply_central_impulse(const Vector2 &p_impulse) {
617627
// Force update internal mass properties to calculate proper impulse
618628
update_mass_properties(true);
619629
}
620-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
630+
if (!box2d::is_handle_valid(body_handle)) {
631+
return;
632+
}
621633
b2Vec2 impulse = { p_impulse.x, p_impulse.y };
622634
box2d::body_apply_impulse(body_handle, impulse);
623635
}
@@ -633,7 +645,9 @@ void Box2DBody2D::apply_impulse(const Vector2 &p_impulse, const Vector2 &p_posit
633645
// Force update internal mass properties to calculate proper impulse
634646
update_mass_properties(true);
635647
}
636-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
648+
if (!box2d::is_handle_valid(body_handle)) {
649+
return;
650+
}
637651
b2Vec2 impulse = { p_impulse.x, p_impulse.y };
638652
Vector2 point_centered = get_transform().get_origin() + p_position + get_center_of_mass();
639653
b2Vec2 pos = { point_centered.x, point_centered.y };
@@ -647,7 +661,9 @@ void Box2DBody2D::apply_torque_impulse(real_t p_torque) {
647661
// Force update internal mass properties to calculate proper impulse
648662
update_mass_properties(true);
649663
}
650-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
664+
if (!box2d::is_handle_valid(body_handle)) {
665+
return;
666+
}
651667
box2d::body_apply_torque_impulse(body_handle, p_torque);
652668
}
653669

@@ -664,7 +680,9 @@ void Box2DBody2D::apply_central_force(const Vector2 &p_force) {
664680
// Note: using last delta assuming constant physics time
665681
real_t last_delta = get_space()->get_last_step();
666682

667-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
683+
if (!box2d::is_handle_valid(body_handle)) {
684+
return;
685+
}
668686
b2Vec2 force = { p_force.x * last_delta, p_force.y * last_delta };
669687
box2d::body_apply_impulse(body_handle, force);
670688
}
@@ -681,7 +699,9 @@ void Box2DBody2D::apply_force(const Vector2 &p_force, const Vector2 &p_position)
681699
update_mass_properties(true);
682700
}
683701

684-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
702+
if (!box2d::is_handle_valid(body_handle)) {
703+
return;
704+
}
685705
// Note: using last delta assuming constant physics time
686706
real_t last_delta = get_space()->get_last_step();
687707
b2Vec2 force = { p_force.x * last_delta, p_force.y * last_delta };
@@ -700,7 +720,9 @@ void Box2DBody2D::apply_torque(real_t p_torque) {
700720
// Force update internal mass properties to calculate proper impulse
701721
update_mass_properties(true);
702722
}
703-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
723+
if (!box2d::is_handle_valid(body_handle)) {
724+
return;
725+
}
704726
// Note: using last delta assuming constant physics time
705727
real_t last_delta = get_space()->get_last_step();
706728
box2d::body_apply_torque_impulse(body_handle, p_torque * last_delta);
@@ -713,7 +735,9 @@ void Box2DBody2D::add_constant_central_force(const Vector2 &p_force) {
713735
// Force update internal mass properties to calculate proper impulse
714736
update_mass_properties(true);
715737
}
716-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
738+
if (!box2d::is_handle_valid(body_handle)) {
739+
return;
740+
}
717741
b2Vec2 force = { p_force.x, p_force.y };
718742
box2d::body_add_force(body_handle, force);
719743
}
@@ -726,7 +750,9 @@ void Box2DBody2D::add_constant_force(const Vector2 &p_force, const Vector2 &p_po
726750
// Force update internal mass properties to calculate proper impulse
727751
update_mass_properties(true);
728752
}
729-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
753+
if (!box2d::is_handle_valid(body_handle)) {
754+
return;
755+
}
730756
b2Vec2 force = { p_force.x, p_force.y };
731757
b2Vec2 pos = { p_position.x, p_position.y };
732758
box2d::body_add_force(body_handle, force);
@@ -739,7 +765,9 @@ void Box2DBody2D::add_constant_torque(real_t p_torque) {
739765
// Force update internal mass properties to calculate proper impulse
740766
update_mass_properties(true);
741767
}
742-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
768+
if (!box2d::is_handle_valid(body_handle)) {
769+
return;
770+
}
743771
box2d::body_add_torque(body_handle, p_torque);
744772
}
745773

@@ -749,14 +777,18 @@ void Box2DBody2D::set_constant_force(const Vector2 &p_force) {
749777
// Force update internal mass properties to calculate proper impulse
750778
update_mass_properties(true);
751779
}
752-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
780+
if (!box2d::is_handle_valid(body_handle)) {
781+
return;
782+
}
753783
b2Vec2 force = { p_force.x, p_force.y };
754784
box2d::body_reset_forces(body_handle);
755785
box2d::body_add_force(body_handle, force);
756786
}
757787

758788
Vector2 Box2DBody2D::get_constant_force() const {
759-
ERR_FAIL_COND_V(!box2d::is_handle_valid(body_handle), constant_force);
789+
if (!box2d::is_handle_valid(body_handle)) {
790+
return constant_force;
791+
}
760792

761793
b2Vec2 force = box2d::body_get_constant_force(body_handle);
762794

@@ -769,13 +801,17 @@ void Box2DBody2D::set_constant_torque(real_t p_torque) {
769801
// Force update internal mass properties to calculate proper impulse
770802
update_mass_properties(true);
771803
}
772-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
804+
if (!box2d::is_handle_valid(body_handle)) {
805+
return;
806+
}
773807
box2d::body_reset_torques(body_handle);
774808
box2d::body_add_torque(body_handle, p_torque);
775809
}
776810

777811
real_t Box2DBody2D::get_constant_torque() const {
778-
ERR_FAIL_COND_V(!box2d::is_handle_valid(body_handle), constant_torque);
812+
if (!box2d::is_handle_valid(body_handle)) {
813+
return constant_torque;
814+
}
779815
return box2d::body_get_constant_torque(body_handle);
780816
}
781817

@@ -784,13 +820,17 @@ void Box2DBody2D::wakeup() {
784820
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
785821
return;
786822
}
787-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
823+
if (!box2d::is_handle_valid(body_handle)) {
824+
return;
825+
}
788826
box2d::body_wake_up(body_handle);
789827
}
790828

791829
void Box2DBody2D::force_sleep() {
792830
sleep = true;
793-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
831+
if (!box2d::is_handle_valid(body_handle)) {
832+
return;
833+
}
794834
box2d::body_force_sleep(body_handle);
795835
}
796836

@@ -834,14 +874,17 @@ void Box2DBody2D::set_linear_velocity(const Vector2 &p_linear_velocity) {
834874
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
835875
return;
836876
}
837-
// TODO: apply delta
838-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
877+
if (!box2d::is_handle_valid(body_handle)) {
878+
return;
879+
}
839880
b2Vec2 velocity = { linear_velocity.x, linear_velocity.y };
840881
box2d::body_set_linear_velocity(body_handle, velocity);
841882
}
842883

843884
Vector2 Box2DBody2D::get_linear_velocity() const {
844-
ERR_FAIL_COND_V(!box2d::is_handle_valid(body_handle), linear_velocity);
885+
if (!box2d::is_handle_valid(body_handle)) {
886+
return linear_velocity;
887+
}
845888
b2Vec2 vel = box2d::body_get_linear_velocity(body_handle);
846889
return Vector2(vel.x, vel.y);
847890
}
@@ -852,19 +895,16 @@ Vector2 Box2DBody2D::get_static_linear_velocity() const {
852895

853896
void Box2DBody2D::set_angular_velocity(real_t p_angular_velocity) {
854897
angular_velocity = p_angular_velocity;
855-
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
898+
if (mode == PhysicsServer2D::BODY_MODE_STATIC || !box2d::is_handle_valid(body_handle)) {
856899
return;
857900
}
858-
// TODO: apply delta
859-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
860901
box2d::body_set_angular_velocity(body_handle, angular_velocity);
861902
}
862903

863904
real_t Box2DBody2D::get_angular_velocity() const {
864-
if (!get_space()) {
905+
if (!get_space() || !box2d::is_handle_valid(body_handle)) {
865906
return angular_velocity;
866907
}
867-
ERR_FAIL_COND_V(!box2d::is_handle_valid(body_handle), angular_velocity);
868908
return box2d::body_get_angular_velocity(body_handle);
869909
}
870910

@@ -882,7 +922,9 @@ void Box2DBody2D::_apply_linear_damping(real_t new_value, bool apply_default) {
882922
if (apply_default) {
883923
total_linear_damping += (real_t)get_space()->get_default_area_param(PhysicsServer2D::AREA_PARAM_LINEAR_DAMP);
884924
}
885-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
925+
if (!box2d::is_handle_valid(body_handle)) {
926+
return;
927+
}
886928
box2d::body_set_linear_damping(body_handle, total_linear_damping);
887929
}
888930

@@ -896,15 +938,19 @@ void Box2DBody2D::_apply_angular_damping(real_t new_value, bool apply_default) {
896938
if (apply_default) {
897939
total_angular_damping += (real_t)get_space()->get_default_area_param(PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP);
898940
}
899-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
941+
if (!box2d::is_handle_valid(body_handle)) {
942+
return;
943+
}
900944
box2d::body_set_angular_damping(body_handle, total_angular_damping);
901945
}
902946

903947
void Box2DBody2D::_apply_gravity_scale(real_t new_value) {
904948
if (!get_space()) {
905949
return;
906950
}
907-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
951+
if (!box2d::is_handle_valid(body_handle)) {
952+
return;
953+
}
908954
box2d::body_set_gravity_scale(body_handle, new_value);
909955
}
910956

@@ -1041,7 +1087,9 @@ void Box2DBody2D::update_gravity(real_t p_step) {
10411087
ERR_FAIL_COND(!using_area_gravity);
10421088

10431089
Vector2 gravity_impulse = total_gravity * mass * p_step;
1044-
ERR_FAIL_COND(!box2d::is_handle_valid(body_handle));
1090+
if (!box2d::is_handle_valid(body_handle)) {
1091+
return;
1092+
}
10451093
b2Vec2 impulse = { gravity_impulse.x, gravity_impulse.y };
10461094
box2d::body_apply_impulse(body_handle, impulse);
10471095
}

src/bodies/box2d_collision_object_2d.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void Box2DCollisionObject2D::_create_shape(Shape &shape, uint32_t p_shape_index)
171171

172172
switch (type) {
173173
case TYPE_BODY: {
174-
shape.collider_handle = box2d::collider_create_solid(space_handle, shape_handle, &mat, body_handle, user_data);
174+
shape.collider_handle = box2d::collider_create_solid(space_handle, shape_handle, mat, body_handle, user_data);
175175
} break;
176176
case TYPE_AREA: {
177177
shape.collider_handle = box2d::collider_create_sensor(space_handle, shape_handle, body_handle, user_data);

0 commit comments

Comments
 (0)