@@ -18,15 +18,18 @@ void Box2DBody2D::_mass_properties_changed() {
18
18
}
19
19
20
20
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
+ }
23
24
24
25
real_t inertia_value = (mode == PhysicsServer2D::BODY_MODE_RIGID_LINEAR) ? 0.0 : inertia;
25
26
box2d::body_set_mass_properties (body_handle, mass, inertia_value, { center_of_mass.x , center_of_mass.y });
26
27
}
27
28
28
29
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
+ }
30
33
31
34
mass_properties_update_list.remove_from_list ();
32
35
@@ -125,7 +128,9 @@ void Box2DBody2D::set_active(bool p_active) {
125
128
}
126
129
127
130
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
+ }
129
134
box2d::body_set_can_sleep (body_handle, p_can_sleep);
130
135
}
131
136
@@ -174,8 +179,6 @@ void Box2DBody2D::on_update_active() {
174
179
}
175
180
176
181
void Box2DBody2D::set_param (PhysicsServer2D::BodyParameter p_param, const Variant &p_value) {
177
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
178
-
179
182
switch (p_param) {
180
183
case PhysicsServer2D::BODY_PARAM_BOUNCE:
181
184
case PhysicsServer2D::BODY_PARAM_FRICTION: {
@@ -187,6 +190,9 @@ void Box2DBody2D::set_param(PhysicsServer2D::BodyParameter p_param, const Varian
187
190
box2d::Material mat;
188
191
mat.friction = friction;
189
192
mat.restitution = bounce;
193
+ if (!box2d::is_handle_valid (body_handle)) {
194
+ return ;
195
+ }
190
196
box2d::body_update_material (body_handle, mat);
191
197
} break ;
192
198
case PhysicsServer2D::BODY_PARAM_MASS: {
@@ -320,17 +326,19 @@ void Box2DBody2D::set_mode(PhysicsServer2D::BodyMode p_mode) {
320
326
}
321
327
PhysicsServer2D::BodyMode prev_mode = mode;
322
328
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
+ }
334
342
}
335
343
if (p_mode == PhysicsServer2D::BODY_MODE_STATIC) {
336
344
force_sleep ();
@@ -435,7 +443,9 @@ Variant Box2DBody2D::get_state(PhysicsServer2D::BodyState p_state) const {
435
443
}
436
444
437
445
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
+ }
439
449
if (ccd_mode == p_mode) {
440
450
return ;
441
451
}
@@ -617,7 +627,9 @@ void Box2DBody2D::apply_central_impulse(const Vector2 &p_impulse) {
617
627
// Force update internal mass properties to calculate proper impulse
618
628
update_mass_properties (true );
619
629
}
620
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
630
+ if (!box2d::is_handle_valid (body_handle)) {
631
+ return ;
632
+ }
621
633
b2Vec2 impulse = { p_impulse.x , p_impulse.y };
622
634
box2d::body_apply_impulse (body_handle, impulse);
623
635
}
@@ -633,7 +645,9 @@ void Box2DBody2D::apply_impulse(const Vector2 &p_impulse, const Vector2 &p_posit
633
645
// Force update internal mass properties to calculate proper impulse
634
646
update_mass_properties (true );
635
647
}
636
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
648
+ if (!box2d::is_handle_valid (body_handle)) {
649
+ return ;
650
+ }
637
651
b2Vec2 impulse = { p_impulse.x , p_impulse.y };
638
652
Vector2 point_centered = get_transform ().get_origin () + p_position + get_center_of_mass ();
639
653
b2Vec2 pos = { point_centered.x , point_centered.y };
@@ -647,7 +661,9 @@ void Box2DBody2D::apply_torque_impulse(real_t p_torque) {
647
661
// Force update internal mass properties to calculate proper impulse
648
662
update_mass_properties (true );
649
663
}
650
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
664
+ if (!box2d::is_handle_valid (body_handle)) {
665
+ return ;
666
+ }
651
667
box2d::body_apply_torque_impulse (body_handle, p_torque);
652
668
}
653
669
@@ -664,7 +680,9 @@ void Box2DBody2D::apply_central_force(const Vector2 &p_force) {
664
680
// Note: using last delta assuming constant physics time
665
681
real_t last_delta = get_space ()->get_last_step ();
666
682
667
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
683
+ if (!box2d::is_handle_valid (body_handle)) {
684
+ return ;
685
+ }
668
686
b2Vec2 force = { p_force.x * last_delta, p_force.y * last_delta };
669
687
box2d::body_apply_impulse (body_handle, force);
670
688
}
@@ -681,7 +699,9 @@ void Box2DBody2D::apply_force(const Vector2 &p_force, const Vector2 &p_position)
681
699
update_mass_properties (true );
682
700
}
683
701
684
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
702
+ if (!box2d::is_handle_valid (body_handle)) {
703
+ return ;
704
+ }
685
705
// Note: using last delta assuming constant physics time
686
706
real_t last_delta = get_space ()->get_last_step ();
687
707
b2Vec2 force = { p_force.x * last_delta, p_force.y * last_delta };
@@ -700,7 +720,9 @@ void Box2DBody2D::apply_torque(real_t p_torque) {
700
720
// Force update internal mass properties to calculate proper impulse
701
721
update_mass_properties (true );
702
722
}
703
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
723
+ if (!box2d::is_handle_valid (body_handle)) {
724
+ return ;
725
+ }
704
726
// Note: using last delta assuming constant physics time
705
727
real_t last_delta = get_space ()->get_last_step ();
706
728
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) {
713
735
// Force update internal mass properties to calculate proper impulse
714
736
update_mass_properties (true );
715
737
}
716
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
738
+ if (!box2d::is_handle_valid (body_handle)) {
739
+ return ;
740
+ }
717
741
b2Vec2 force = { p_force.x , p_force.y };
718
742
box2d::body_add_force (body_handle, force);
719
743
}
@@ -726,7 +750,9 @@ void Box2DBody2D::add_constant_force(const Vector2 &p_force, const Vector2 &p_po
726
750
// Force update internal mass properties to calculate proper impulse
727
751
update_mass_properties (true );
728
752
}
729
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
753
+ if (!box2d::is_handle_valid (body_handle)) {
754
+ return ;
755
+ }
730
756
b2Vec2 force = { p_force.x , p_force.y };
731
757
b2Vec2 pos = { p_position.x , p_position.y };
732
758
box2d::body_add_force (body_handle, force);
@@ -739,7 +765,9 @@ void Box2DBody2D::add_constant_torque(real_t p_torque) {
739
765
// Force update internal mass properties to calculate proper impulse
740
766
update_mass_properties (true );
741
767
}
742
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
768
+ if (!box2d::is_handle_valid (body_handle)) {
769
+ return ;
770
+ }
743
771
box2d::body_add_torque (body_handle, p_torque);
744
772
}
745
773
@@ -749,14 +777,18 @@ void Box2DBody2D::set_constant_force(const Vector2 &p_force) {
749
777
// Force update internal mass properties to calculate proper impulse
750
778
update_mass_properties (true );
751
779
}
752
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
780
+ if (!box2d::is_handle_valid (body_handle)) {
781
+ return ;
782
+ }
753
783
b2Vec2 force = { p_force.x , p_force.y };
754
784
box2d::body_reset_forces (body_handle);
755
785
box2d::body_add_force (body_handle, force);
756
786
}
757
787
758
788
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
+ }
760
792
761
793
b2Vec2 force = box2d::body_get_constant_force (body_handle);
762
794
@@ -769,13 +801,17 @@ void Box2DBody2D::set_constant_torque(real_t p_torque) {
769
801
// Force update internal mass properties to calculate proper impulse
770
802
update_mass_properties (true );
771
803
}
772
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
804
+ if (!box2d::is_handle_valid (body_handle)) {
805
+ return ;
806
+ }
773
807
box2d::body_reset_torques (body_handle);
774
808
box2d::body_add_torque (body_handle, p_torque);
775
809
}
776
810
777
811
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
+ }
779
815
return box2d::body_get_constant_torque (body_handle);
780
816
}
781
817
@@ -784,13 +820,17 @@ void Box2DBody2D::wakeup() {
784
820
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
785
821
return ;
786
822
}
787
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
823
+ if (!box2d::is_handle_valid (body_handle)) {
824
+ return ;
825
+ }
788
826
box2d::body_wake_up (body_handle);
789
827
}
790
828
791
829
void Box2DBody2D::force_sleep () {
792
830
sleep = true ;
793
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
831
+ if (!box2d::is_handle_valid (body_handle)) {
832
+ return ;
833
+ }
794
834
box2d::body_force_sleep (body_handle);
795
835
}
796
836
@@ -834,14 +874,17 @@ void Box2DBody2D::set_linear_velocity(const Vector2 &p_linear_velocity) {
834
874
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
835
875
return ;
836
876
}
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
+ }
839
880
b2Vec2 velocity = { linear_velocity.x , linear_velocity.y };
840
881
box2d::body_set_linear_velocity (body_handle, velocity);
841
882
}
842
883
843
884
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
+ }
845
888
b2Vec2 vel = box2d::body_get_linear_velocity (body_handle);
846
889
return Vector2 (vel.x , vel.y );
847
890
}
@@ -852,19 +895,16 @@ Vector2 Box2DBody2D::get_static_linear_velocity() const {
852
895
853
896
void Box2DBody2D::set_angular_velocity (real_t p_angular_velocity) {
854
897
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) ) {
856
899
return ;
857
900
}
858
- // TODO: apply delta
859
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
860
901
box2d::body_set_angular_velocity (body_handle, angular_velocity);
861
902
}
862
903
863
904
real_t Box2DBody2D::get_angular_velocity () const {
864
- if (!get_space ()) {
905
+ if (!get_space () || ! box2d::is_handle_valid (body_handle) ) {
865
906
return angular_velocity;
866
907
}
867
- ERR_FAIL_COND_V (!box2d::is_handle_valid (body_handle), angular_velocity);
868
908
return box2d::body_get_angular_velocity (body_handle);
869
909
}
870
910
@@ -882,7 +922,9 @@ void Box2DBody2D::_apply_linear_damping(real_t new_value, bool apply_default) {
882
922
if (apply_default) {
883
923
total_linear_damping += (real_t )get_space ()->get_default_area_param (PhysicsServer2D::AREA_PARAM_LINEAR_DAMP);
884
924
}
885
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
925
+ if (!box2d::is_handle_valid (body_handle)) {
926
+ return ;
927
+ }
886
928
box2d::body_set_linear_damping (body_handle, total_linear_damping);
887
929
}
888
930
@@ -896,15 +938,19 @@ void Box2DBody2D::_apply_angular_damping(real_t new_value, bool apply_default) {
896
938
if (apply_default) {
897
939
total_angular_damping += (real_t )get_space ()->get_default_area_param (PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP);
898
940
}
899
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
941
+ if (!box2d::is_handle_valid (body_handle)) {
942
+ return ;
943
+ }
900
944
box2d::body_set_angular_damping (body_handle, total_angular_damping);
901
945
}
902
946
903
947
void Box2DBody2D::_apply_gravity_scale (real_t new_value) {
904
948
if (!get_space ()) {
905
949
return ;
906
950
}
907
- ERR_FAIL_COND (!box2d::is_handle_valid (body_handle));
951
+ if (!box2d::is_handle_valid (body_handle)) {
952
+ return ;
953
+ }
908
954
box2d::body_set_gravity_scale (body_handle, new_value);
909
955
}
910
956
@@ -1041,7 +1087,9 @@ void Box2DBody2D::update_gravity(real_t p_step) {
1041
1087
ERR_FAIL_COND (!using_area_gravity);
1042
1088
1043
1089
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
+ }
1045
1093
b2Vec2 impulse = { gravity_impulse.x , gravity_impulse.y };
1046
1094
box2d::body_apply_impulse (body_handle, impulse);
1047
1095
}
0 commit comments