summaryrefslogtreecommitdiff
path: root/servers/physics_2d/physics_server_2d_sw.cpp
blob: ffdaab0d9230d3bc4e2338b6500b08046db07909 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
/*************************************************************************/
/*  physics_server_2d_sw.cpp                                             */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "physics_server_2d_sw.h"

#include "broad_phase_2d_basic.h"
#include "broad_phase_2d_hash_grid.h"
#include "collision_solver_2d_sw.h"
#include "core/debugger/engine_debugger.h"
#include "core/os/os.h"
#include "core/project_settings.h"

#define FLUSH_QUERY_CHECK(m_object) \
	ERR_FAIL_COND_MSG(m_object->get_space() && flushing_queries, "Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.");

RID PhysicsServer2DSW::_shape_create(ShapeType p_shape) {
	Shape2DSW *shape = nullptr;
	switch (p_shape) {
		case SHAPE_LINE: {
			shape = memnew(LineShape2DSW);
		} break;
		case SHAPE_RAY: {
			shape = memnew(RayShape2DSW);
		} break;
		case SHAPE_SEGMENT: {
			shape = memnew(SegmentShape2DSW);
		} break;
		case SHAPE_CIRCLE: {
			shape = memnew(CircleShape2DSW);
		} break;
		case SHAPE_RECTANGLE: {
			shape = memnew(RectangleShape2DSW);
		} break;
		case SHAPE_CAPSULE: {
			shape = memnew(CapsuleShape2DSW);
		} break;
		case SHAPE_CONVEX_POLYGON: {
			shape = memnew(ConvexPolygonShape2DSW);
		} break;
		case SHAPE_CONCAVE_POLYGON: {
			shape = memnew(ConcavePolygonShape2DSW);
		} break;
		case SHAPE_CUSTOM: {
			ERR_FAIL_V(RID());

		} break;
	}

	RID id = shape_owner.make_rid(shape);
	shape->set_self(id);

	return id;
}

RID PhysicsServer2DSW::line_shape_create() {
	return _shape_create(SHAPE_LINE);
}

RID PhysicsServer2DSW::ray_shape_create() {
	return _shape_create(SHAPE_RAY);
}

RID PhysicsServer2DSW::segment_shape_create() {
	return _shape_create(SHAPE_SEGMENT);
}

RID PhysicsServer2DSW::circle_shape_create() {
	return _shape_create(SHAPE_CIRCLE);
}

RID PhysicsServer2DSW::rectangle_shape_create() {
	return _shape_create(SHAPE_RECTANGLE);
}

RID PhysicsServer2DSW::capsule_shape_create() {
	return _shape_create(SHAPE_CAPSULE);
}

RID PhysicsServer2DSW::convex_polygon_shape_create() {
	return _shape_create(SHAPE_CONVEX_POLYGON);
}

RID PhysicsServer2DSW::concave_polygon_shape_create() {
	return _shape_create(SHAPE_CONCAVE_POLYGON);
}

void PhysicsServer2DSW::shape_set_data(RID p_shape, const Variant &p_data) {
	Shape2DSW *shape = shape_owner.getornull(p_shape);
	ERR_FAIL_COND(!shape);
	shape->set_data(p_data);
};

void PhysicsServer2DSW::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
	Shape2DSW *shape = shape_owner.getornull(p_shape);
	ERR_FAIL_COND(!shape);
	shape->set_custom_bias(p_bias);
}

PhysicsServer2D::ShapeType PhysicsServer2DSW::shape_get_type(RID p_shape) const {
	const Shape2DSW *shape = shape_owner.getornull(p_shape);
	ERR_FAIL_COND_V(!shape, SHAPE_CUSTOM);
	return shape->get_type();
};

Variant PhysicsServer2DSW::shape_get_data(RID p_shape) const {
	const Shape2DSW *shape = shape_owner.getornull(p_shape);
	ERR_FAIL_COND_V(!shape, Variant());
	ERR_FAIL_COND_V(!shape->is_configured(), Variant());
	return shape->get_data();
};

real_t PhysicsServer2DSW::shape_get_custom_solver_bias(RID p_shape) const {
	const Shape2DSW *shape = shape_owner.getornull(p_shape);
	ERR_FAIL_COND_V(!shape, 0);
	return shape->get_custom_bias();
}

void PhysicsServer2DSW::_shape_col_cbk(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_userdata) {
	CollCbkData *cbk = (CollCbkData *)p_userdata;

	if (cbk->max == 0) {
		return;
	}

	if (cbk->valid_dir != Vector2()) {
		if (p_point_A.distance_squared_to(p_point_B) > cbk->valid_depth * cbk->valid_depth) {
			cbk->invalid_by_dir++;
			return;
		}
		Vector2 rel_dir = (p_point_A - p_point_B).normalized();

		if (cbk->valid_dir.dot(rel_dir) < Math_SQRT12) { //sqrt(2)/2.0 - 45 degrees
			cbk->invalid_by_dir++;

			/*
			print_line("A: "+p_point_A);
			print_line("B: "+p_point_B);
			print_line("discard too angled "+rtos(cbk->valid_dir.dot((p_point_A-p_point_B))));
			print_line("resnorm: "+(p_point_A-p_point_B).normalized());
			print_line("distance: "+rtos(p_point_A.distance_to(p_point_B)));
			*/
			return;
		}
	}

	if (cbk->amount == cbk->max) {
		//find least deep
		real_t min_depth = 1e20;
		int min_depth_idx = 0;
		for (int i = 0; i < cbk->amount; i++) {
			real_t d = cbk->ptr[i * 2 + 0].distance_squared_to(cbk->ptr[i * 2 + 1]);
			if (d < min_depth) {
				min_depth = d;
				min_depth_idx = i;
			}
		}

		real_t d = p_point_A.distance_squared_to(p_point_B);
		if (d < min_depth) {
			return;
		}
		cbk->ptr[min_depth_idx * 2 + 0] = p_point_A;
		cbk->ptr[min_depth_idx * 2 + 1] = p_point_B;
		cbk->passed++;

	} else {
		cbk->ptr[cbk->amount * 2 + 0] = p_point_A;
		cbk->ptr[cbk->amount * 2 + 1] = p_point_B;
		cbk->amount++;
		cbk->passed++;
	}
}

bool PhysicsServer2DSW::shape_collide(RID p_shape_A, const Transform2D &p_xform_A, const Vector2 &p_motion_A, RID p_shape_B, const Transform2D &p_xform_B, const Vector2 &p_motion_B, Vector2 *r_results, int p_result_max, int &r_result_count) {
	Shape2DSW *shape_A = shape_owner.getornull(p_shape_A);
	ERR_FAIL_COND_V(!shape_A, false);
	Shape2DSW *shape_B = shape_owner.getornull(p_shape_B);
	ERR_FAIL_COND_V(!shape_B, false);

	if (p_result_max == 0) {
		return CollisionSolver2DSW::solve(shape_A, p_xform_A, p_motion_A, shape_B, p_xform_B, p_motion_B, nullptr, nullptr);
	}

	CollCbkData cbk;
	cbk.max = p_result_max;
	cbk.amount = 0;
	cbk.passed = 0;
	cbk.ptr = r_results;

	bool res = CollisionSolver2DSW::solve(shape_A, p_xform_A, p_motion_A, shape_B, p_xform_B, p_motion_B, _shape_col_cbk, &cbk);
	r_result_count = cbk.amount;
	return res;
}

RID PhysicsServer2DSW::space_create() {
	Space2DSW *space = memnew(Space2DSW);
	RID id = space_owner.make_rid(space);
	space->set_self(id);
	RID area_id = area_create();
	Area2DSW *area = area_owner.getornull(area_id);
	ERR_FAIL_COND_V(!area, RID());
	space->set_default_area(area);
	area->set_space(space);
	area->set_priority(-1);

	return id;
};

void PhysicsServer2DSW::space_set_active(RID p_space, bool p_active) {
	Space2DSW *space = space_owner.getornull(p_space);
	ERR_FAIL_COND(!space);
	if (p_active) {
		active_spaces.insert(space);
	} else {
		active_spaces.erase(space);
	}
}

bool PhysicsServer2DSW::space_is_active(RID p_space) const {
	const Space2DSW *space = space_owner.getornull(p_space);
	ERR_FAIL_COND_V(!space, false);

	return active_spaces.has(space);
}

void PhysicsServer2DSW::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
	Space2DSW *space = space_owner.getornull(p_space);
	ERR_FAIL_COND(!space);

	space->set_param(p_param, p_value);
}

real_t PhysicsServer2DSW::space_get_param(RID p_space, SpaceParameter p_param) const {
	const Space2DSW *space = space_owner.getornull(p_space);
	ERR_FAIL_COND_V(!space, 0);
	return space->get_param(p_param);
}

void PhysicsServer2DSW::space_set_debug_contacts(RID p_space, int p_max_contacts) {
	Space2DSW *space = space_owner.getornull(p_space);
	ERR_FAIL_COND(!space);
	space->set_debug_contacts(p_max_contacts);
}

Vector<Vector2> PhysicsServer2DSW::space_get_contacts(RID p_space) const {
	Space2DSW *space = space_owner.getornull(p_space);
	ERR_FAIL_COND_V(!space, Vector<Vector2>());
	return space->get_debug_contacts();
}

int PhysicsServer2DSW::space_get_contact_count(RID p_space) const {
	Space2DSW *space = space_owner.getornull(p_space);
	ERR_FAIL_COND_V(!space, 0);
	return space->get_debug_contact_count();
}

PhysicsDirectSpaceState2D *PhysicsServer2DSW::space_get_direct_state(RID p_space) {
	Space2DSW *space = space_owner.getornull(p_space);
	ERR_FAIL_COND_V(!space, nullptr);
	ERR_FAIL_COND_V_MSG((using_threads && !doing_sync) || space->is_locked(), nullptr, "Space state is inaccessible right now, wait for iteration or physics process notification.");

	return space->get_direct_state();
}

RID PhysicsServer2DSW::area_create() {
	Area2DSW *area = memnew(Area2DSW);
	RID rid = area_owner.make_rid(area);
	area->set_self(rid);
	return rid;
};

void PhysicsServer2DSW::area_set_space(RID p_area, RID p_space) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);

	Space2DSW *space = nullptr;
	if (p_space.is_valid()) {
		space = space_owner.getornull(p_space);
		ERR_FAIL_COND(!space);
	}

	if (area->get_space() == space) {
		return; //pointless
	}

	area->clear_constraints();
	area->set_space(space);
};

RID PhysicsServer2DSW::area_get_space(RID p_area) const {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND_V(!area, RID());

	Space2DSW *space = area->get_space();
	if (!space) {
		return RID();
	}
	return space->get_self();
};

void PhysicsServer2DSW::area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);

	area->set_space_override_mode(p_mode);
}

PhysicsServer2D::AreaSpaceOverrideMode PhysicsServer2DSW::area_get_space_override_mode(RID p_area) const {
	const Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND_V(!area, AREA_SPACE_OVERRIDE_DISABLED);

	return area->get_space_override_mode();
}

void PhysicsServer2DSW::area_add_shape(RID p_area, RID p_shape, const Transform2D &p_transform, bool p_disabled) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);

	Shape2DSW *shape = shape_owner.getornull(p_shape);
	ERR_FAIL_COND(!shape);

	area->add_shape(shape, p_transform, p_disabled);
}

void PhysicsServer2DSW::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);

	Shape2DSW *shape = shape_owner.getornull(p_shape);
	ERR_FAIL_COND(!shape);
	ERR_FAIL_COND(!shape->is_configured());

	area->set_shape(p_shape_idx, shape);
}

void PhysicsServer2DSW::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform2D &p_transform) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);

	area->set_shape_transform(p_shape_idx, p_transform);
}

void PhysicsServer2DSW::area_set_shape_disabled(RID p_area, int p_shape, bool p_disabled) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);
	ERR_FAIL_INDEX(p_shape, area->get_shape_count());
	FLUSH_QUERY_CHECK(area);

	area->set_shape_as_disabled(p_shape, p_disabled);
}

int PhysicsServer2DSW::area_get_shape_count(RID p_area) const {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND_V(!area, -1);

	return area->get_shape_count();
}

RID PhysicsServer2DSW::area_get_shape(RID p_area, int p_shape_idx) const {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND_V(!area, RID());

	Shape2DSW *shape = area->get_shape(p_shape_idx);
	ERR_FAIL_COND_V(!shape, RID());

	return shape->get_self();
}

Transform2D PhysicsServer2DSW::area_get_shape_transform(RID p_area, int p_shape_idx) const {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND_V(!area, Transform2D());

	return area->get_shape_transform(p_shape_idx);
}

void PhysicsServer2DSW::area_remove_shape(RID p_area, int p_shape_idx) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);

	area->remove_shape(p_shape_idx);
}

void PhysicsServer2DSW::area_clear_shapes(RID p_area) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);

	while (area->get_shape_count()) {
		area->remove_shape(0);
	}
}

void PhysicsServer2DSW::area_attach_object_instance_id(RID p_area, ObjectID p_id) {
	if (space_owner.owns(p_area)) {
		Space2DSW *space = space_owner.getornull(p_area);
		p_area = space->get_default_area()->get_self();
	}
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);
	area->set_instance_id(p_id);
}

ObjectID PhysicsServer2DSW::area_get_object_instance_id(RID p_area) const {
	if (space_owner.owns(p_area)) {
		Space2DSW *space = space_owner.getornull(p_area);
		p_area = space->get_default_area()->get_self();
	}
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND_V(!area, ObjectID());
	return area->get_instance_id();
}

void PhysicsServer2DSW::area_attach_canvas_instance_id(RID p_area, ObjectID p_id) {
	if (space_owner.owns(p_area)) {
		Space2DSW *space = space_owner.getornull(p_area);
		p_area = space->get_default_area()->get_self();
	}
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);
	area->set_canvas_instance_id(p_id);
}

ObjectID PhysicsServer2DSW::area_get_canvas_instance_id(RID p_area) const {
	if (space_owner.owns(p_area)) {
		Space2DSW *space = space_owner.getornull(p_area);
		p_area = space->get_default_area()->get_self();
	}
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND_V(!area, ObjectID());
	return area->get_canvas_instance_id();
}

void PhysicsServer2DSW::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
	if (space_owner.owns(p_area)) {
		Space2DSW *space = space_owner.getornull(p_area);
		p_area = space->get_default_area()->get_self();
	}
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);
	area->set_param(p_param, p_value);
};

void PhysicsServer2DSW::area_set_transform(RID p_area, const Transform2D &p_transform) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);
	area->set_transform(p_transform);
};

Variant PhysicsServer2DSW::area_get_param(RID p_area, AreaParameter p_param) const {
	if (space_owner.owns(p_area)) {
		Space2DSW *space = space_owner.getornull(p_area);
		p_area = space->get_default_area()->get_self();
	}
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND_V(!area, Variant());

	return area->get_param(p_param);
};

Transform2D PhysicsServer2DSW::area_get_transform(RID p_area) const {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND_V(!area, Transform2D());

	return area->get_transform();
};

void PhysicsServer2DSW::area_set_pickable(RID p_area, bool p_pickable) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);
	area->set_pickable(p_pickable);
}

void PhysicsServer2DSW::area_set_monitorable(RID p_area, bool p_monitorable) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);
	FLUSH_QUERY_CHECK(area);

	area->set_monitorable(p_monitorable);
}

void PhysicsServer2DSW::area_set_collision_mask(RID p_area, uint32_t p_mask) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);

	area->set_collision_mask(p_mask);
}

void PhysicsServer2DSW::area_set_collision_layer(RID p_area, uint32_t p_layer) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);

	area->set_collision_layer(p_layer);
}

void PhysicsServer2DSW::area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);

	area->set_monitor_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(), p_method);
}

void PhysicsServer2DSW::area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
	Area2DSW *area = area_owner.getornull(p_area);
	ERR_FAIL_COND(!area);

	area->set_area_monitor_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(), p_method);
}

/* BODY API */

RID PhysicsServer2DSW::body_create() {
	Body2DSW *body = memnew(Body2DSW);
	RID rid = body_owner.make_rid(body);
	body->set_self(rid);
	return rid;
}

void PhysicsServer2DSW::body_set_space(RID p_body, RID p_space) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);
	Space2DSW *space = nullptr;
	if (p_space.is_valid()) {
		space = space_owner.getornull(p_space);
		ERR_FAIL_COND(!space);
	}

	if (body->get_space() == space) {
		return; //pointless
	}

	body->clear_constraint_map();
	body->set_space(space);
};

RID PhysicsServer2DSW::body_get_space(RID p_body) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, RID());

	Space2DSW *space = body->get_space();
	if (!space) {
		return RID();
	}
	return space->get_self();
};

void PhysicsServer2DSW::body_set_mode(RID p_body, BodyMode p_mode) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);
	FLUSH_QUERY_CHECK(body);

	body->set_mode(p_mode);
};

PhysicsServer2D::BodyMode PhysicsServer2DSW::body_get_mode(RID p_body) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, BODY_MODE_STATIC);

	return body->get_mode();
};

void PhysicsServer2DSW::body_add_shape(RID p_body, RID p_shape, const Transform2D &p_transform, bool p_disabled) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	Shape2DSW *shape = shape_owner.getornull(p_shape);
	ERR_FAIL_COND(!shape);

	body->add_shape(shape, p_transform, p_disabled);
}

void PhysicsServer2DSW::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	Shape2DSW *shape = shape_owner.getornull(p_shape);
	ERR_FAIL_COND(!shape);
	ERR_FAIL_COND(!shape->is_configured());

	body->set_shape(p_shape_idx, shape);
}

void PhysicsServer2DSW::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform2D &p_transform) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->set_shape_transform(p_shape_idx, p_transform);
}

void PhysicsServer2DSW::body_set_shape_metadata(RID p_body, int p_shape_idx, const Variant &p_metadata) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);
	body->set_shape_metadata(p_shape_idx, p_metadata);
}

Variant PhysicsServer2DSW::body_get_shape_metadata(RID p_body, int p_shape_idx) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, Variant());
	return body->get_shape_metadata(p_shape_idx);
}

int PhysicsServer2DSW::body_get_shape_count(RID p_body) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, -1);

	return body->get_shape_count();
}

RID PhysicsServer2DSW::body_get_shape(RID p_body, int p_shape_idx) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, RID());

	Shape2DSW *shape = body->get_shape(p_shape_idx);
	ERR_FAIL_COND_V(!shape, RID());

	return shape->get_self();
}

Transform2D PhysicsServer2DSW::body_get_shape_transform(RID p_body, int p_shape_idx) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, Transform2D());

	return body->get_shape_transform(p_shape_idx);
}

void PhysicsServer2DSW::body_remove_shape(RID p_body, int p_shape_idx) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->remove_shape(p_shape_idx);
}

void PhysicsServer2DSW::body_clear_shapes(RID p_body) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	while (body->get_shape_count()) {
		body->remove_shape(0);
	}
}

void PhysicsServer2DSW::body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);
	ERR_FAIL_INDEX(p_shape_idx, body->get_shape_count());
	FLUSH_QUERY_CHECK(body);

	body->set_shape_as_disabled(p_shape_idx, p_disabled);
}

void PhysicsServer2DSW::body_set_shape_as_one_way_collision(RID p_body, int p_shape_idx, bool p_enable, float p_margin) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);
	ERR_FAIL_INDEX(p_shape_idx, body->get_shape_count());
	FLUSH_QUERY_CHECK(body);

	body->set_shape_as_one_way_collision(p_shape_idx, p_enable, p_margin);
}

void PhysicsServer2DSW::body_set_continuous_collision_detection_mode(RID p_body, CCDMode p_mode) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);
	body->set_continuous_collision_detection_mode(p_mode);
}

PhysicsServer2DSW::CCDMode PhysicsServer2DSW::body_get_continuous_collision_detection_mode(RID p_body) const {
	const Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, CCD_MODE_DISABLED);

	return body->get_continuous_collision_detection_mode();
}

void PhysicsServer2DSW::body_attach_object_instance_id(RID p_body, ObjectID p_id) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->set_instance_id(p_id);
};

ObjectID PhysicsServer2DSW::body_get_object_instance_id(RID p_body) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, ObjectID());

	return body->get_instance_id();
};

void PhysicsServer2DSW::body_attach_canvas_instance_id(RID p_body, ObjectID p_id) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->set_canvas_instance_id(p_id);
};

ObjectID PhysicsServer2DSW::body_get_canvas_instance_id(RID p_body) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, ObjectID());

	return body->get_canvas_instance_id();
};

void PhysicsServer2DSW::body_set_collision_layer(RID p_body, uint32_t p_layer) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);
	body->set_collision_layer(p_layer);
};

uint32_t PhysicsServer2DSW::body_get_collision_layer(RID p_body) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, 0);

	return body->get_collision_layer();
};

void PhysicsServer2DSW::body_set_collision_mask(RID p_body, uint32_t p_mask) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);
	body->set_collision_mask(p_mask);
};

uint32_t PhysicsServer2DSW::body_get_collision_mask(RID p_body) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, 0);

	return body->get_collision_mask();
};

void PhysicsServer2DSW::body_set_param(RID p_body, BodyParameter p_param, real_t p_value) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->set_param(p_param, p_value);
};

real_t PhysicsServer2DSW::body_get_param(RID p_body, BodyParameter p_param) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, 0);

	return body->get_param(p_param);
};

void PhysicsServer2DSW::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->set_state(p_state, p_variant);
};

Variant PhysicsServer2DSW::body_get_state(RID p_body, BodyState p_state) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, Variant());

	return body->get_state(p_state);
};

void PhysicsServer2DSW::body_set_applied_force(RID p_body, const Vector2 &p_force) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->set_applied_force(p_force);
	body->wakeup();
};

Vector2 PhysicsServer2DSW::body_get_applied_force(RID p_body) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, Vector2());
	return body->get_applied_force();
};

void PhysicsServer2DSW::body_set_applied_torque(RID p_body, real_t p_torque) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->set_applied_torque(p_torque);
	body->wakeup();
};

real_t PhysicsServer2DSW::body_get_applied_torque(RID p_body) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, 0);

	return body->get_applied_torque();
};

void PhysicsServer2DSW::body_apply_central_impulse(RID p_body, const Vector2 &p_impulse) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->apply_central_impulse(p_impulse);
	body->wakeup();
}

void PhysicsServer2DSW::body_apply_torque_impulse(RID p_body, real_t p_torque) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	_update_shapes();

	body->apply_torque_impulse(p_torque);
	body->wakeup();
}

void PhysicsServer2DSW::body_apply_impulse(RID p_body, const Vector2 &p_impulse, const Vector2 &p_position) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	_update_shapes();

	body->apply_impulse(p_impulse, p_position);
	body->wakeup();
};

void PhysicsServer2DSW::body_add_central_force(RID p_body, const Vector2 &p_force) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->add_central_force(p_force);
	body->wakeup();
};

void PhysicsServer2DSW::body_add_force(RID p_body, const Vector2 &p_force, const Vector2 &p_position) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->add_force(p_force, p_position);
	body->wakeup();
};

void PhysicsServer2DSW::body_add_torque(RID p_body, real_t p_torque) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->add_torque(p_torque);
	body->wakeup();
};

void PhysicsServer2DSW::body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	_update_shapes();

	Vector2 v = body->get_linear_velocity();
	Vector2 axis = p_axis_velocity.normalized();
	v -= axis * axis.dot(v);
	v += p_axis_velocity;
	body->set_linear_velocity(v);
	body->wakeup();
};

void PhysicsServer2DSW::body_add_collision_exception(RID p_body, RID p_body_b) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->add_exception(p_body_b);
	body->wakeup();
};

void PhysicsServer2DSW::body_remove_collision_exception(RID p_body, RID p_body_b) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->remove_exception(p_body_b);
	body->wakeup();
};

void PhysicsServer2DSW::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	for (int i = 0; i < body->get_exceptions().size(); i++) {
		p_exceptions->push_back(body->get_exceptions()[i]);
	}
};

void PhysicsServer2DSW::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);
};

real_t PhysicsServer2DSW::body_get_contacts_reported_depth_threshold(RID p_body) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, 0);
	return 0;
};

void PhysicsServer2DSW::body_set_omit_force_integration(RID p_body, bool p_omit) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);

	body->set_omit_force_integration(p_omit);
};

bool PhysicsServer2DSW::body_is_omitting_force_integration(RID p_body) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, false);
	return body->get_omit_force_integration();
};

void PhysicsServer2DSW::body_set_max_contacts_reported(RID p_body, int p_contacts) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);
	body->set_max_contacts_reported(p_contacts);
}

int PhysicsServer2DSW::body_get_max_contacts_reported(RID p_body) const {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, -1);
	return body->get_max_contacts_reported();
}

void PhysicsServer2DSW::body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);
	body->set_force_integration_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(), p_method, p_udata);
}

bool PhysicsServer2DSW::body_collide_shape(RID p_body, int p_body_shape, RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, Vector2 *r_results, int p_result_max, int &r_result_count) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, false);
	ERR_FAIL_INDEX_V(p_body_shape, body->get_shape_count(), false);

	return shape_collide(body->get_shape(p_body_shape)->get_self(), body->get_transform() * body->get_shape_transform(p_body_shape), Vector2(), p_shape, p_shape_xform, p_motion, r_results, p_result_max, r_result_count);
}

void PhysicsServer2DSW::body_set_pickable(RID p_body, bool p_pickable) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND(!body);
	body->set_pickable(p_pickable);
}

bool PhysicsServer2DSW::body_test_motion(RID p_body, const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, real_t p_margin, MotionResult *r_result, bool p_exclude_raycast_shapes) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, false);
	ERR_FAIL_COND_V(!body->get_space(), false);
	ERR_FAIL_COND_V(body->get_space()->is_locked(), false);

	_update_shapes();

	return body->get_space()->test_body_motion(body, p_from, p_motion, p_infinite_inertia, p_margin, r_result, p_exclude_raycast_shapes);
}

int PhysicsServer2DSW::body_test_ray_separation(RID p_body, const Transform2D &p_transform, bool p_infinite_inertia, Vector2 &r_recover_motion, SeparationResult *r_results, int p_result_max, float p_margin) {
	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, false);
	ERR_FAIL_COND_V(!body->get_space(), false);
	ERR_FAIL_COND_V(body->get_space()->is_locked(), false);

	return body->get_space()->test_body_ray_separation(body, p_transform, p_infinite_inertia, r_recover_motion, r_results, p_result_max, p_margin);
}

PhysicsDirectBodyState2D *PhysicsServer2DSW::body_get_direct_state(RID p_body) {
	ERR_FAIL_COND_V_MSG((using_threads && !doing_sync), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");

	if (!body_owner.owns(p_body)) {
		return nullptr;
	}

	Body2DSW *body = body_owner.getornull(p_body);
	ERR_FAIL_COND_V(!body, nullptr);
	ERR_FAIL_COND_V(!body->get_space(), nullptr);
	ERR_FAIL_COND_V_MSG(body->get_space()->is_locked(), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");

	direct_state->body = body;
	return direct_state;
}

/* JOINT API */

void PhysicsServer2DSW::joint_set_param(RID p_joint, JointParam p_param, real_t p_value) {
	Joint2DSW *joint = joint_owner.getornull(p_joint);
	ERR_FAIL_COND(!joint);

	switch (p_param) {
		case JOINT_PARAM_BIAS:
			joint->set_bias(p_value);
			break;
		case JOINT_PARAM_MAX_BIAS:
			joint->set_max_bias(p_value);
			break;
		case JOINT_PARAM_MAX_FORCE:
			joint->set_max_force(p_value);
			break;
	}
}

real_t PhysicsServer2DSW::joint_get_param(RID p_joint, JointParam p_param) const {
	const Joint2DSW *joint = joint_owner.getornull(p_joint);
	ERR_FAIL_COND_V(!joint, -1);

	switch (p_param) {
		case JOINT_PARAM_BIAS:
			return joint->get_bias();
			break;
		case JOINT_PARAM_MAX_BIAS:
			return joint->get_max_bias();
			break;
		case JOINT_PARAM_MAX_FORCE:
			return joint->get_max_force();
			break;
	}

	return 0;
}

void PhysicsServer2DSW::joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) {
	Joint2DSW *joint = joint_owner.getornull(p_joint);
	ERR_FAIL_COND(!joint);

	joint->disable_collisions_between_bodies(p_disable);

	if (2 == joint->get_body_count()) {
		Body2DSW *body_a = *joint->get_body_ptr();
		Body2DSW *body_b = *(joint->get_body_ptr() + 1);

		if (p_disable) {
			body_add_collision_exception(body_a->get_self(), body_b->get_self());
			body_add_collision_exception(body_b->get_self(), body_a->get_self());
		} else {
			body_remove_collision_exception(body_a->get_self(), body_b->get_self());
			body_remove_collision_exception(body_b->get_self(), body_a->get_self());
		}
	}
}

bool PhysicsServer2DSW::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
	const Joint2DSW *joint = joint_owner.getornull(p_joint);
	ERR_FAIL_COND_V(!joint, true);

	return joint->is_disabled_collisions_between_bodies();
}

RID PhysicsServer2DSW::pin_joint_create(const Vector2 &p_pos, RID p_body_a, RID p_body_b) {
	Body2DSW *A = body_owner.getornull(p_body_a);
	ERR_FAIL_COND_V(!A, RID());
	Body2DSW *B = nullptr;
	if (body_owner.owns(p_body_b)) {
		B = body_owner.getornull(p_body_b);
		ERR_FAIL_COND_V(!B, RID());
	}

	Joint2DSW *joint = memnew(PinJoint2DSW(p_pos, A, B));
	RID self = joint_owner.make_rid(joint);
	joint->set_self(self);

	return self;
}

RID PhysicsServer2DSW::groove_joint_create(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, RID p_body_a, RID p_body_b) {
	Body2DSW *A = body_owner.getornull(p_body_a);
	ERR_FAIL_COND_V(!A, RID());

	Body2DSW *B = body_owner.getornull(p_body_b);
	ERR_FAIL_COND_V(!B, RID());

	Joint2DSW *joint = memnew(GrooveJoint2DSW(p_a_groove1, p_a_groove2, p_b_anchor, A, B));
	RID self = joint_owner.make_rid(joint);
	joint->set_self(self);
	return self;
}

RID PhysicsServer2DSW::damped_spring_joint_create(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, RID p_body_a, RID p_body_b) {
	Body2DSW *A = body_owner.getornull(p_body_a);
	ERR_FAIL_COND_V(!A, RID());

	Body2DSW *B = body_owner.getornull(p_body_b);
	ERR_FAIL_COND_V(!B, RID());

	Joint2DSW *joint = memnew(DampedSpringJoint2DSW(p_anchor_a, p_anchor_b, A, B));
	RID self = joint_owner.make_rid(joint);
	joint->set_self(self);
	return self;
}

void PhysicsServer2DSW::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
	Joint2DSW *j = joint_owner.getornull(p_joint);
	ERR_FAIL_COND(!j);
	ERR_FAIL_COND(j->get_type() != JOINT_PIN);

	PinJoint2DSW *pin_joint = static_cast<PinJoint2DSW *>(j);
	pin_joint->set_param(p_param, p_value);
}

real_t PhysicsServer2DSW::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
	Joint2DSW *j = joint_owner.getornull(p_joint);
	ERR_FAIL_COND_V(!j, 0);
	ERR_FAIL_COND_V(j->get_type() != JOINT_PIN, 0);

	PinJoint2DSW *pin_joint = static_cast<PinJoint2DSW *>(j);
	return pin_joint->get_param(p_param);
}

void PhysicsServer2DSW::damped_spring_joint_set_param(RID p_joint, DampedSpringParam p_param, real_t p_value) {
	Joint2DSW *j = joint_owner.getornull(p_joint);
	ERR_FAIL_COND(!j);
	ERR_FAIL_COND(j->get_type() != JOINT_DAMPED_SPRING);

	DampedSpringJoint2DSW *dsj = static_cast<DampedSpringJoint2DSW *>(j);
	dsj->set_param(p_param, p_value);
}

real_t PhysicsServer2DSW::damped_spring_joint_get_param(RID p_joint, DampedSpringParam p_param) const {
	Joint2DSW *j = joint_owner.getornull(p_joint);
	ERR_FAIL_COND_V(!j, 0);
	ERR_FAIL_COND_V(j->get_type() != JOINT_DAMPED_SPRING, 0);

	DampedSpringJoint2DSW *dsj = static_cast<DampedSpringJoint2DSW *>(j);
	return dsj->get_param(p_param);
}

PhysicsServer2D::JointType PhysicsServer2DSW::joint_get_type(RID p_joint) const {
	Joint2DSW *joint = joint_owner.getornull(p_joint);
	ERR_FAIL_COND_V(!joint, JOINT_PIN);

	return joint->get_type();
}

void PhysicsServer2DSW::free(RID p_rid) {
	_update_shapes(); // just in case

	if (shape_owner.owns(p_rid)) {
		Shape2DSW *shape = shape_owner.getornull(p_rid);

		while (shape->get_owners().size()) {
			ShapeOwner2DSW *so = shape->get_owners().front()->key();
			so->remove_shape(shape);
		}

		shape_owner.free(p_rid);
		memdelete(shape);
	} else if (body_owner.owns(p_rid)) {
		Body2DSW *body = body_owner.getornull(p_rid);

		/*
		if (body->get_state_query())
			_clear_query(body->get_state_query());

		if (body->get_direct_state_query())
			_clear_query(body->get_direct_state_query());
		*/

		body_set_space(p_rid, RID());

		while (body->get_shape_count()) {
			body->remove_shape(0);
		}

		body_owner.free(p_rid);
		memdelete(body);

	} else if (area_owner.owns(p_rid)) {
		Area2DSW *area = area_owner.getornull(p_rid);

		/*
		if (area->get_monitor_query())
			_clear_query(area->get_monitor_query());
		*/

		area->set_space(nullptr);

		while (area->get_shape_count()) {
			area->remove_shape(0);
		}

		area_owner.free(p_rid);
		memdelete(area);
	} else if (space_owner.owns(p_rid)) {
		Space2DSW *space = space_owner.getornull(p_rid);

		while (space->get_objects().size()) {
			CollisionObject2DSW *co = (CollisionObject2DSW *)space->get_objects().front()->get();
			co->set_space(nullptr);
		}

		active_spaces.erase(space);
		free(space->get_default_area()->get_self());
		space_owner.free(p_rid);
		memdelete(space);
	} else if (joint_owner.owns(p_rid)) {
		Joint2DSW *joint = joint_owner.getornull(p_rid);

		joint_owner.free(p_rid);
		memdelete(joint);

	} else {
		ERR_FAIL_MSG("Invalid ID.");
	}
};

void PhysicsServer2DSW::set_active(bool p_active) {
	active = p_active;
};

void PhysicsServer2DSW::init() {
	doing_sync = false;
	last_step = 0.001;
	iterations = 8; // 8?
	stepper = memnew(Step2DSW);
	direct_state = memnew(PhysicsDirectBodyState2DSW);
};

void PhysicsServer2DSW::step(real_t p_step) {
	if (!active) {
		return;
	}

	_update_shapes();

	doing_sync = false;

	last_step = p_step;
	PhysicsDirectBodyState2DSW::singleton->step = p_step;
	island_count = 0;
	active_objects = 0;
	collision_pairs = 0;
	for (Set<const Space2DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
		stepper->step((Space2DSW *)E->get(), p_step, iterations);
		island_count += E->get()->get_island_count();
		active_objects += E->get()->get_active_objects();
		collision_pairs += E->get()->get_collision_pairs();
	}
};

void PhysicsServer2DSW::sync() {
	doing_sync = true;
};

void PhysicsServer2DSW::flush_queries() {
	if (!active) {
		return;
	}

	flushing_queries = true;

	uint64_t time_beg = OS::get_singleton()->get_ticks_usec();

	for (Set<const Space2DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
		Space2DSW *space = (Space2DSW *)E->get();
		space->call_queries();
	}

	flushing_queries = false;

	if (EngineDebugger::is_profiling("servers")) {
		uint64_t total_time[Space2DSW::ELAPSED_TIME_MAX];
		static const char *time_name[Space2DSW::ELAPSED_TIME_MAX] = {
			"integrate_forces",
			"generate_islands",
			"setup_constraints",
			"solve_constraints",
			"integrate_velocities"
		};

		for (int i = 0; i < Space2DSW::ELAPSED_TIME_MAX; i++) {
			total_time[i] = 0;
		}

		for (Set<const Space2DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
			for (int i = 0; i < Space2DSW::ELAPSED_TIME_MAX; i++) {
				total_time[i] += E->get()->get_elapsed_time(Space2DSW::ElapsedTime(i));
			}
		}

		Array values;
		values.resize(Space2DSW::ELAPSED_TIME_MAX * 2);
		for (int i = 0; i < Space2DSW::ELAPSED_TIME_MAX; i++) {
			values[i * 2 + 0] = time_name[i];
			values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
		}
		values.push_back("flush_queries");
		values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));

		values.push_front("physics_2d");
		EngineDebugger::profiler_add_frame_data("servers", values);
	}
}

void PhysicsServer2DSW::end_sync() {
	doing_sync = false;
}

void PhysicsServer2DSW::finish() {
	memdelete(stepper);
	memdelete(direct_state);
};

void PhysicsServer2DSW::_update_shapes() {
	while (pending_shape_update_list.first()) {
		pending_shape_update_list.first()->self()->_shape_changed();
		pending_shape_update_list.remove(pending_shape_update_list.first());
	}
}

int PhysicsServer2DSW::get_process_info(ProcessInfo p_info) {
	switch (p_info) {
		case INFO_ACTIVE_OBJECTS: {
			return active_objects;
		} break;
		case INFO_COLLISION_PAIRS: {
			return collision_pairs;
		} break;
		case INFO_ISLAND_COUNT: {
			return island_count;
		} break;
	}

	return 0;
}

PhysicsServer2DSW *PhysicsServer2DSW::singletonsw = nullptr;

PhysicsServer2DSW::PhysicsServer2DSW() {
	singletonsw = this;
	BroadPhase2DSW::create_func = BroadPhase2DHashGrid::_create;
	//BroadPhase2DSW::create_func=BroadPhase2DBasic::_create;

	active = true;
	island_count = 0;
	active_objects = 0;
	collision_pairs = 0;
	using_threads = int(ProjectSettings::get_singleton()->get("physics/2d/thread_model")) == 2;
	flushing_queries = false;
};