physics_server_3d_sw.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. /*************************************************************************/
  2. /* physics_server_3d_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "physics_server_3d_sw.h"
  31. #include "broad_phase_3d_basic.h"
  32. #include "broad_phase_octree.h"
  33. #include "core/debugger/engine_debugger.h"
  34. #include "core/os/os.h"
  35. #include "joints/cone_twist_joint_3d_sw.h"
  36. #include "joints/generic_6dof_joint_3d_sw.h"
  37. #include "joints/hinge_joint_3d_sw.h"
  38. #include "joints/pin_joint_3d_sw.h"
  39. #include "joints/slider_joint_3d_sw.h"
  40. #define FLUSH_QUERY_CHECK(m_object) \
  41. 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.");
  42. RID PhysicsServer3DSW::plane_shape_create() {
  43. Shape3DSW *shape = memnew(PlaneShape3DSW);
  44. RID rid = shape_owner.make_rid(shape);
  45. shape->set_self(rid);
  46. return rid;
  47. }
  48. RID PhysicsServer3DSW::ray_shape_create() {
  49. Shape3DSW *shape = memnew(RayShape3DSW);
  50. RID rid = shape_owner.make_rid(shape);
  51. shape->set_self(rid);
  52. return rid;
  53. }
  54. RID PhysicsServer3DSW::sphere_shape_create() {
  55. Shape3DSW *shape = memnew(SphereShape3DSW);
  56. RID rid = shape_owner.make_rid(shape);
  57. shape->set_self(rid);
  58. return rid;
  59. }
  60. RID PhysicsServer3DSW::box_shape_create() {
  61. Shape3DSW *shape = memnew(BoxShape3DSW);
  62. RID rid = shape_owner.make_rid(shape);
  63. shape->set_self(rid);
  64. return rid;
  65. }
  66. RID PhysicsServer3DSW::capsule_shape_create() {
  67. Shape3DSW *shape = memnew(CapsuleShape3DSW);
  68. RID rid = shape_owner.make_rid(shape);
  69. shape->set_self(rid);
  70. return rid;
  71. }
  72. RID PhysicsServer3DSW::cylinder_shape_create() {
  73. Shape3DSW *shape = memnew(CylinderShape3DSW);
  74. RID rid = shape_owner.make_rid(shape);
  75. shape->set_self(rid);
  76. return rid;
  77. }
  78. RID PhysicsServer3DSW::convex_polygon_shape_create() {
  79. Shape3DSW *shape = memnew(ConvexPolygonShape3DSW);
  80. RID rid = shape_owner.make_rid(shape);
  81. shape->set_self(rid);
  82. return rid;
  83. }
  84. RID PhysicsServer3DSW::concave_polygon_shape_create() {
  85. Shape3DSW *shape = memnew(ConcavePolygonShape3DSW);
  86. RID rid = shape_owner.make_rid(shape);
  87. shape->set_self(rid);
  88. return rid;
  89. }
  90. RID PhysicsServer3DSW::heightmap_shape_create() {
  91. Shape3DSW *shape = memnew(HeightMapShape3DSW);
  92. RID rid = shape_owner.make_rid(shape);
  93. shape->set_self(rid);
  94. return rid;
  95. }
  96. RID PhysicsServer3DSW::custom_shape_create() {
  97. ERR_FAIL_V(RID());
  98. }
  99. void PhysicsServer3DSW::shape_set_data(RID p_shape, const Variant &p_data) {
  100. Shape3DSW *shape = shape_owner.getornull(p_shape);
  101. ERR_FAIL_COND(!shape);
  102. shape->set_data(p_data);
  103. };
  104. void PhysicsServer3DSW::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
  105. Shape3DSW *shape = shape_owner.getornull(p_shape);
  106. ERR_FAIL_COND(!shape);
  107. shape->set_custom_bias(p_bias);
  108. }
  109. PhysicsServer3D::ShapeType PhysicsServer3DSW::shape_get_type(RID p_shape) const {
  110. const Shape3DSW *shape = shape_owner.getornull(p_shape);
  111. ERR_FAIL_COND_V(!shape, SHAPE_CUSTOM);
  112. return shape->get_type();
  113. };
  114. Variant PhysicsServer3DSW::shape_get_data(RID p_shape) const {
  115. const Shape3DSW *shape = shape_owner.getornull(p_shape);
  116. ERR_FAIL_COND_V(!shape, Variant());
  117. ERR_FAIL_COND_V(!shape->is_configured(), Variant());
  118. return shape->get_data();
  119. };
  120. void PhysicsServer3DSW::shape_set_margin(RID p_shape, real_t p_margin) {
  121. }
  122. real_t PhysicsServer3DSW::shape_get_margin(RID p_shape) const {
  123. return 0.0;
  124. }
  125. real_t PhysicsServer3DSW::shape_get_custom_solver_bias(RID p_shape) const {
  126. const Shape3DSW *shape = shape_owner.getornull(p_shape);
  127. ERR_FAIL_COND_V(!shape, 0);
  128. return shape->get_custom_bias();
  129. }
  130. RID PhysicsServer3DSW::space_create() {
  131. Space3DSW *space = memnew(Space3DSW);
  132. RID id = space_owner.make_rid(space);
  133. space->set_self(id);
  134. RID area_id = area_create();
  135. Area3DSW *area = area_owner.getornull(area_id);
  136. ERR_FAIL_COND_V(!area, RID());
  137. space->set_default_area(area);
  138. area->set_space(space);
  139. area->set_priority(-1);
  140. RID sgb = body_create();
  141. body_set_space(sgb, id);
  142. body_set_mode(sgb, BODY_MODE_STATIC);
  143. space->set_static_global_body(sgb);
  144. return id;
  145. };
  146. void PhysicsServer3DSW::space_set_active(RID p_space, bool p_active) {
  147. Space3DSW *space = space_owner.getornull(p_space);
  148. ERR_FAIL_COND(!space);
  149. if (p_active) {
  150. active_spaces.insert(space);
  151. } else {
  152. active_spaces.erase(space);
  153. }
  154. }
  155. bool PhysicsServer3DSW::space_is_active(RID p_space) const {
  156. const Space3DSW *space = space_owner.getornull(p_space);
  157. ERR_FAIL_COND_V(!space, false);
  158. return active_spaces.has(space);
  159. }
  160. void PhysicsServer3DSW::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
  161. Space3DSW *space = space_owner.getornull(p_space);
  162. ERR_FAIL_COND(!space);
  163. space->set_param(p_param, p_value);
  164. }
  165. real_t PhysicsServer3DSW::space_get_param(RID p_space, SpaceParameter p_param) const {
  166. const Space3DSW *space = space_owner.getornull(p_space);
  167. ERR_FAIL_COND_V(!space, 0);
  168. return space->get_param(p_param);
  169. }
  170. PhysicsDirectSpaceState3D *PhysicsServer3DSW::space_get_direct_state(RID p_space) {
  171. Space3DSW *space = space_owner.getornull(p_space);
  172. ERR_FAIL_COND_V(!space, nullptr);
  173. 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.");
  174. return space->get_direct_state();
  175. }
  176. void PhysicsServer3DSW::space_set_debug_contacts(RID p_space, int p_max_contacts) {
  177. Space3DSW *space = space_owner.getornull(p_space);
  178. ERR_FAIL_COND(!space);
  179. space->set_debug_contacts(p_max_contacts);
  180. }
  181. Vector<Vector3> PhysicsServer3DSW::space_get_contacts(RID p_space) const {
  182. Space3DSW *space = space_owner.getornull(p_space);
  183. ERR_FAIL_COND_V(!space, Vector<Vector3>());
  184. return space->get_debug_contacts();
  185. }
  186. int PhysicsServer3DSW::space_get_contact_count(RID p_space) const {
  187. Space3DSW *space = space_owner.getornull(p_space);
  188. ERR_FAIL_COND_V(!space, 0);
  189. return space->get_debug_contact_count();
  190. }
  191. RID PhysicsServer3DSW::area_create() {
  192. Area3DSW *area = memnew(Area3DSW);
  193. RID rid = area_owner.make_rid(area);
  194. area->set_self(rid);
  195. return rid;
  196. };
  197. void PhysicsServer3DSW::area_set_space(RID p_area, RID p_space) {
  198. Area3DSW *area = area_owner.getornull(p_area);
  199. ERR_FAIL_COND(!area);
  200. Space3DSW *space = nullptr;
  201. if (p_space.is_valid()) {
  202. space = space_owner.getornull(p_space);
  203. ERR_FAIL_COND(!space);
  204. }
  205. if (area->get_space() == space) {
  206. return; //pointless
  207. }
  208. area->clear_constraints();
  209. area->set_space(space);
  210. };
  211. RID PhysicsServer3DSW::area_get_space(RID p_area) const {
  212. Area3DSW *area = area_owner.getornull(p_area);
  213. ERR_FAIL_COND_V(!area, RID());
  214. Space3DSW *space = area->get_space();
  215. if (!space) {
  216. return RID();
  217. }
  218. return space->get_self();
  219. };
  220. void PhysicsServer3DSW::area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) {
  221. Area3DSW *area = area_owner.getornull(p_area);
  222. ERR_FAIL_COND(!area);
  223. area->set_space_override_mode(p_mode);
  224. }
  225. PhysicsServer3D::AreaSpaceOverrideMode PhysicsServer3DSW::area_get_space_override_mode(RID p_area) const {
  226. const Area3DSW *area = area_owner.getornull(p_area);
  227. ERR_FAIL_COND_V(!area, AREA_SPACE_OVERRIDE_DISABLED);
  228. return area->get_space_override_mode();
  229. }
  230. void PhysicsServer3DSW::area_add_shape(RID p_area, RID p_shape, const Transform &p_transform, bool p_disabled) {
  231. Area3DSW *area = area_owner.getornull(p_area);
  232. ERR_FAIL_COND(!area);
  233. Shape3DSW *shape = shape_owner.getornull(p_shape);
  234. ERR_FAIL_COND(!shape);
  235. area->add_shape(shape, p_transform, p_disabled);
  236. }
  237. void PhysicsServer3DSW::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
  238. Area3DSW *area = area_owner.getornull(p_area);
  239. ERR_FAIL_COND(!area);
  240. Shape3DSW *shape = shape_owner.getornull(p_shape);
  241. ERR_FAIL_COND(!shape);
  242. ERR_FAIL_COND(!shape->is_configured());
  243. area->set_shape(p_shape_idx, shape);
  244. }
  245. void PhysicsServer3DSW::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform &p_transform) {
  246. Area3DSW *area = area_owner.getornull(p_area);
  247. ERR_FAIL_COND(!area);
  248. area->set_shape_transform(p_shape_idx, p_transform);
  249. }
  250. int PhysicsServer3DSW::area_get_shape_count(RID p_area) const {
  251. Area3DSW *area = area_owner.getornull(p_area);
  252. ERR_FAIL_COND_V(!area, -1);
  253. return area->get_shape_count();
  254. }
  255. RID PhysicsServer3DSW::area_get_shape(RID p_area, int p_shape_idx) const {
  256. Area3DSW *area = area_owner.getornull(p_area);
  257. ERR_FAIL_COND_V(!area, RID());
  258. Shape3DSW *shape = area->get_shape(p_shape_idx);
  259. ERR_FAIL_COND_V(!shape, RID());
  260. return shape->get_self();
  261. }
  262. Transform PhysicsServer3DSW::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  263. Area3DSW *area = area_owner.getornull(p_area);
  264. ERR_FAIL_COND_V(!area, Transform());
  265. return area->get_shape_transform(p_shape_idx);
  266. }
  267. void PhysicsServer3DSW::area_remove_shape(RID p_area, int p_shape_idx) {
  268. Area3DSW *area = area_owner.getornull(p_area);
  269. ERR_FAIL_COND(!area);
  270. area->remove_shape(p_shape_idx);
  271. }
  272. void PhysicsServer3DSW::area_clear_shapes(RID p_area) {
  273. Area3DSW *area = area_owner.getornull(p_area);
  274. ERR_FAIL_COND(!area);
  275. while (area->get_shape_count()) {
  276. area->remove_shape(0);
  277. }
  278. }
  279. void PhysicsServer3DSW::area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) {
  280. Area3DSW *area = area_owner.getornull(p_area);
  281. ERR_FAIL_COND(!area);
  282. ERR_FAIL_INDEX(p_shape_idx, area->get_shape_count());
  283. FLUSH_QUERY_CHECK(area);
  284. area->set_shape_as_disabled(p_shape_idx, p_disabled);
  285. }
  286. void PhysicsServer3DSW::area_attach_object_instance_id(RID p_area, ObjectID p_id) {
  287. if (space_owner.owns(p_area)) {
  288. Space3DSW *space = space_owner.getornull(p_area);
  289. p_area = space->get_default_area()->get_self();
  290. }
  291. Area3DSW *area = area_owner.getornull(p_area);
  292. ERR_FAIL_COND(!area);
  293. area->set_instance_id(p_id);
  294. }
  295. ObjectID PhysicsServer3DSW::area_get_object_instance_id(RID p_area) const {
  296. if (space_owner.owns(p_area)) {
  297. Space3DSW *space = space_owner.getornull(p_area);
  298. p_area = space->get_default_area()->get_self();
  299. }
  300. Area3DSW *area = area_owner.getornull(p_area);
  301. ERR_FAIL_COND_V(!area, ObjectID());
  302. return area->get_instance_id();
  303. }
  304. void PhysicsServer3DSW::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
  305. if (space_owner.owns(p_area)) {
  306. Space3DSW *space = space_owner.getornull(p_area);
  307. p_area = space->get_default_area()->get_self();
  308. }
  309. Area3DSW *area = area_owner.getornull(p_area);
  310. ERR_FAIL_COND(!area);
  311. area->set_param(p_param, p_value);
  312. };
  313. void PhysicsServer3DSW::area_set_transform(RID p_area, const Transform &p_transform) {
  314. Area3DSW *area = area_owner.getornull(p_area);
  315. ERR_FAIL_COND(!area);
  316. area->set_transform(p_transform);
  317. };
  318. Variant PhysicsServer3DSW::area_get_param(RID p_area, AreaParameter p_param) const {
  319. if (space_owner.owns(p_area)) {
  320. Space3DSW *space = space_owner.getornull(p_area);
  321. p_area = space->get_default_area()->get_self();
  322. }
  323. Area3DSW *area = area_owner.getornull(p_area);
  324. ERR_FAIL_COND_V(!area, Variant());
  325. return area->get_param(p_param);
  326. };
  327. Transform PhysicsServer3DSW::area_get_transform(RID p_area) const {
  328. Area3DSW *area = area_owner.getornull(p_area);
  329. ERR_FAIL_COND_V(!area, Transform());
  330. return area->get_transform();
  331. };
  332. void PhysicsServer3DSW::area_set_collision_layer(RID p_area, uint32_t p_layer) {
  333. Area3DSW *area = area_owner.getornull(p_area);
  334. ERR_FAIL_COND(!area);
  335. area->set_collision_layer(p_layer);
  336. }
  337. void PhysicsServer3DSW::area_set_collision_mask(RID p_area, uint32_t p_mask) {
  338. Area3DSW *area = area_owner.getornull(p_area);
  339. ERR_FAIL_COND(!area);
  340. area->set_collision_mask(p_mask);
  341. }
  342. void PhysicsServer3DSW::area_set_monitorable(RID p_area, bool p_monitorable) {
  343. Area3DSW *area = area_owner.getornull(p_area);
  344. ERR_FAIL_COND(!area);
  345. FLUSH_QUERY_CHECK(area);
  346. area->set_monitorable(p_monitorable);
  347. }
  348. void PhysicsServer3DSW::area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  349. Area3DSW *area = area_owner.getornull(p_area);
  350. ERR_FAIL_COND(!area);
  351. area->set_monitor_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(), p_method);
  352. }
  353. void PhysicsServer3DSW::area_set_ray_pickable(RID p_area, bool p_enable) {
  354. Area3DSW *area = area_owner.getornull(p_area);
  355. ERR_FAIL_COND(!area);
  356. area->set_ray_pickable(p_enable);
  357. }
  358. void PhysicsServer3DSW::area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  359. Area3DSW *area = area_owner.getornull(p_area);
  360. ERR_FAIL_COND(!area);
  361. area->set_area_monitor_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(), p_method);
  362. }
  363. /* BODY API */
  364. RID PhysicsServer3DSW::body_create() {
  365. Body3DSW *body = memnew(Body3DSW);
  366. RID rid = body_owner.make_rid(body);
  367. body->set_self(rid);
  368. return rid;
  369. };
  370. void PhysicsServer3DSW::body_set_space(RID p_body, RID p_space) {
  371. Body3DSW *body = body_owner.getornull(p_body);
  372. ERR_FAIL_COND(!body);
  373. Space3DSW *space = nullptr;
  374. if (p_space.is_valid()) {
  375. space = space_owner.getornull(p_space);
  376. ERR_FAIL_COND(!space);
  377. }
  378. if (body->get_space() == space) {
  379. return; //pointless
  380. }
  381. body->clear_constraint_map();
  382. body->set_space(space);
  383. };
  384. RID PhysicsServer3DSW::body_get_space(RID p_body) const {
  385. Body3DSW *body = body_owner.getornull(p_body);
  386. ERR_FAIL_COND_V(!body, RID());
  387. Space3DSW *space = body->get_space();
  388. if (!space) {
  389. return RID();
  390. }
  391. return space->get_self();
  392. };
  393. void PhysicsServer3DSW::body_set_mode(RID p_body, BodyMode p_mode) {
  394. Body3DSW *body = body_owner.getornull(p_body);
  395. ERR_FAIL_COND(!body);
  396. body->set_mode(p_mode);
  397. };
  398. PhysicsServer3D::BodyMode PhysicsServer3DSW::body_get_mode(RID p_body) const {
  399. Body3DSW *body = body_owner.getornull(p_body);
  400. ERR_FAIL_COND_V(!body, BODY_MODE_STATIC);
  401. return body->get_mode();
  402. };
  403. void PhysicsServer3DSW::body_add_shape(RID p_body, RID p_shape, const Transform &p_transform, bool p_disabled) {
  404. Body3DSW *body = body_owner.getornull(p_body);
  405. ERR_FAIL_COND(!body);
  406. Shape3DSW *shape = shape_owner.getornull(p_shape);
  407. ERR_FAIL_COND(!shape);
  408. body->add_shape(shape, p_transform, p_disabled);
  409. }
  410. void PhysicsServer3DSW::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
  411. Body3DSW *body = body_owner.getornull(p_body);
  412. ERR_FAIL_COND(!body);
  413. Shape3DSW *shape = shape_owner.getornull(p_shape);
  414. ERR_FAIL_COND(!shape);
  415. ERR_FAIL_COND(!shape->is_configured());
  416. body->set_shape(p_shape_idx, shape);
  417. }
  418. void PhysicsServer3DSW::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform &p_transform) {
  419. Body3DSW *body = body_owner.getornull(p_body);
  420. ERR_FAIL_COND(!body);
  421. body->set_shape_transform(p_shape_idx, p_transform);
  422. }
  423. int PhysicsServer3DSW::body_get_shape_count(RID p_body) const {
  424. Body3DSW *body = body_owner.getornull(p_body);
  425. ERR_FAIL_COND_V(!body, -1);
  426. return body->get_shape_count();
  427. }
  428. RID PhysicsServer3DSW::body_get_shape(RID p_body, int p_shape_idx) const {
  429. Body3DSW *body = body_owner.getornull(p_body);
  430. ERR_FAIL_COND_V(!body, RID());
  431. Shape3DSW *shape = body->get_shape(p_shape_idx);
  432. ERR_FAIL_COND_V(!shape, RID());
  433. return shape->get_self();
  434. }
  435. void PhysicsServer3DSW::body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) {
  436. Body3DSW *body = body_owner.getornull(p_body);
  437. ERR_FAIL_COND(!body);
  438. ERR_FAIL_INDEX(p_shape_idx, body->get_shape_count());
  439. FLUSH_QUERY_CHECK(body);
  440. body->set_shape_as_disabled(p_shape_idx, p_disabled);
  441. }
  442. Transform PhysicsServer3DSW::body_get_shape_transform(RID p_body, int p_shape_idx) const {
  443. Body3DSW *body = body_owner.getornull(p_body);
  444. ERR_FAIL_COND_V(!body, Transform());
  445. return body->get_shape_transform(p_shape_idx);
  446. }
  447. void PhysicsServer3DSW::body_remove_shape(RID p_body, int p_shape_idx) {
  448. Body3DSW *body = body_owner.getornull(p_body);
  449. ERR_FAIL_COND(!body);
  450. body->remove_shape(p_shape_idx);
  451. }
  452. void PhysicsServer3DSW::body_clear_shapes(RID p_body) {
  453. Body3DSW *body = body_owner.getornull(p_body);
  454. ERR_FAIL_COND(!body);
  455. while (body->get_shape_count()) {
  456. body->remove_shape(0);
  457. }
  458. }
  459. void PhysicsServer3DSW::body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) {
  460. Body3DSW *body = body_owner.getornull(p_body);
  461. ERR_FAIL_COND(!body);
  462. body->set_continuous_collision_detection(p_enable);
  463. }
  464. bool PhysicsServer3DSW::body_is_continuous_collision_detection_enabled(RID p_body) const {
  465. Body3DSW *body = body_owner.getornull(p_body);
  466. ERR_FAIL_COND_V(!body, false);
  467. return body->is_continuous_collision_detection_enabled();
  468. }
  469. void PhysicsServer3DSW::body_set_collision_layer(RID p_body, uint32_t p_layer) {
  470. Body3DSW *body = body_owner.getornull(p_body);
  471. ERR_FAIL_COND(!body);
  472. body->set_collision_layer(p_layer);
  473. body->wakeup();
  474. }
  475. uint32_t PhysicsServer3DSW::body_get_collision_layer(RID p_body) const {
  476. const Body3DSW *body = body_owner.getornull(p_body);
  477. ERR_FAIL_COND_V(!body, 0);
  478. return body->get_collision_layer();
  479. }
  480. void PhysicsServer3DSW::body_set_collision_mask(RID p_body, uint32_t p_mask) {
  481. Body3DSW *body = body_owner.getornull(p_body);
  482. ERR_FAIL_COND(!body);
  483. body->set_collision_mask(p_mask);
  484. body->wakeup();
  485. }
  486. uint32_t PhysicsServer3DSW::body_get_collision_mask(RID p_body) const {
  487. const Body3DSW *body = body_owner.getornull(p_body);
  488. ERR_FAIL_COND_V(!body, 0);
  489. return body->get_collision_mask();
  490. }
  491. void PhysicsServer3DSW::body_attach_object_instance_id(RID p_body, ObjectID p_id) {
  492. Body3DSW *body = body_owner.getornull(p_body);
  493. if (body) {
  494. body->set_instance_id(p_id);
  495. return;
  496. }
  497. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  498. if (soft_body) {
  499. soft_body->set_instance_id(p_id);
  500. return;
  501. }
  502. ERR_FAIL_MSG("Invalid ID.");
  503. };
  504. ObjectID PhysicsServer3DSW::body_get_object_instance_id(RID p_body) const {
  505. Body3DSW *body = body_owner.getornull(p_body);
  506. ERR_FAIL_COND_V(!body, ObjectID());
  507. return body->get_instance_id();
  508. };
  509. void PhysicsServer3DSW::body_set_user_flags(RID p_body, uint32_t p_flags) {
  510. Body3DSW *body = body_owner.getornull(p_body);
  511. ERR_FAIL_COND(!body);
  512. };
  513. uint32_t PhysicsServer3DSW::body_get_user_flags(RID p_body) const {
  514. Body3DSW *body = body_owner.getornull(p_body);
  515. ERR_FAIL_COND_V(!body, 0);
  516. return 0;
  517. };
  518. void PhysicsServer3DSW::body_set_param(RID p_body, BodyParameter p_param, real_t p_value) {
  519. Body3DSW *body = body_owner.getornull(p_body);
  520. ERR_FAIL_COND(!body);
  521. body->set_param(p_param, p_value);
  522. };
  523. real_t PhysicsServer3DSW::body_get_param(RID p_body, BodyParameter p_param) const {
  524. Body3DSW *body = body_owner.getornull(p_body);
  525. ERR_FAIL_COND_V(!body, 0);
  526. return body->get_param(p_param);
  527. };
  528. void PhysicsServer3DSW::body_set_kinematic_safe_margin(RID p_body, real_t p_margin) {
  529. Body3DSW *body = body_owner.getornull(p_body);
  530. ERR_FAIL_COND(!body);
  531. body->set_kinematic_margin(p_margin);
  532. }
  533. real_t PhysicsServer3DSW::body_get_kinematic_safe_margin(RID p_body) const {
  534. Body3DSW *body = body_owner.getornull(p_body);
  535. ERR_FAIL_COND_V(!body, 0);
  536. return body->get_kinematic_margin();
  537. }
  538. void PhysicsServer3DSW::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  539. Body3DSW *body = body_owner.getornull(p_body);
  540. ERR_FAIL_COND(!body);
  541. body->set_state(p_state, p_variant);
  542. };
  543. Variant PhysicsServer3DSW::body_get_state(RID p_body, BodyState p_state) const {
  544. Body3DSW *body = body_owner.getornull(p_body);
  545. ERR_FAIL_COND_V(!body, Variant());
  546. return body->get_state(p_state);
  547. };
  548. void PhysicsServer3DSW::body_set_applied_force(RID p_body, const Vector3 &p_force) {
  549. Body3DSW *body = body_owner.getornull(p_body);
  550. ERR_FAIL_COND(!body);
  551. body->set_applied_force(p_force);
  552. body->wakeup();
  553. };
  554. Vector3 PhysicsServer3DSW::body_get_applied_force(RID p_body) const {
  555. Body3DSW *body = body_owner.getornull(p_body);
  556. ERR_FAIL_COND_V(!body, Vector3());
  557. return body->get_applied_force();
  558. };
  559. void PhysicsServer3DSW::body_set_applied_torque(RID p_body, const Vector3 &p_torque) {
  560. Body3DSW *body = body_owner.getornull(p_body);
  561. ERR_FAIL_COND(!body);
  562. body->set_applied_torque(p_torque);
  563. body->wakeup();
  564. };
  565. Vector3 PhysicsServer3DSW::body_get_applied_torque(RID p_body) const {
  566. Body3DSW *body = body_owner.getornull(p_body);
  567. ERR_FAIL_COND_V(!body, Vector3());
  568. return body->get_applied_torque();
  569. };
  570. void PhysicsServer3DSW::body_add_central_force(RID p_body, const Vector3 &p_force) {
  571. Body3DSW *body = body_owner.getornull(p_body);
  572. ERR_FAIL_COND(!body);
  573. body->add_central_force(p_force);
  574. body->wakeup();
  575. }
  576. void PhysicsServer3DSW::body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position) {
  577. Body3DSW *body = body_owner.getornull(p_body);
  578. ERR_FAIL_COND(!body);
  579. body->add_force(p_force, p_position);
  580. body->wakeup();
  581. };
  582. void PhysicsServer3DSW::body_add_torque(RID p_body, const Vector3 &p_torque) {
  583. Body3DSW *body = body_owner.getornull(p_body);
  584. ERR_FAIL_COND(!body);
  585. body->add_torque(p_torque);
  586. body->wakeup();
  587. };
  588. void PhysicsServer3DSW::body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
  589. Body3DSW *body = body_owner.getornull(p_body);
  590. ERR_FAIL_COND(!body);
  591. _update_shapes();
  592. body->apply_central_impulse(p_impulse);
  593. body->wakeup();
  594. }
  595. void PhysicsServer3DSW::body_apply_impulse(RID p_body, const Vector3 &p_impulse, const Vector3 &p_position) {
  596. Body3DSW *body = body_owner.getornull(p_body);
  597. ERR_FAIL_COND(!body);
  598. _update_shapes();
  599. body->apply_impulse(p_impulse, p_position);
  600. body->wakeup();
  601. };
  602. void PhysicsServer3DSW::body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) {
  603. Body3DSW *body = body_owner.getornull(p_body);
  604. ERR_FAIL_COND(!body);
  605. _update_shapes();
  606. body->apply_torque_impulse(p_impulse);
  607. body->wakeup();
  608. };
  609. void PhysicsServer3DSW::body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) {
  610. Body3DSW *body = body_owner.getornull(p_body);
  611. ERR_FAIL_COND(!body);
  612. _update_shapes();
  613. Vector3 v = body->get_linear_velocity();
  614. Vector3 axis = p_axis_velocity.normalized();
  615. v -= axis * axis.dot(v);
  616. v += p_axis_velocity;
  617. body->set_linear_velocity(v);
  618. body->wakeup();
  619. };
  620. void PhysicsServer3DSW::body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) {
  621. Body3DSW *body = body_owner.getornull(p_body);
  622. ERR_FAIL_COND(!body);
  623. body->set_axis_lock(p_axis, p_lock);
  624. body->wakeup();
  625. }
  626. bool PhysicsServer3DSW::body_is_axis_locked(RID p_body, BodyAxis p_axis) const {
  627. const Body3DSW *body = body_owner.getornull(p_body);
  628. ERR_FAIL_COND_V(!body, 0);
  629. return body->is_axis_locked(p_axis);
  630. }
  631. void PhysicsServer3DSW::body_add_collision_exception(RID p_body, RID p_body_b) {
  632. Body3DSW *body = body_owner.getornull(p_body);
  633. ERR_FAIL_COND(!body);
  634. body->add_exception(p_body_b);
  635. body->wakeup();
  636. };
  637. void PhysicsServer3DSW::body_remove_collision_exception(RID p_body, RID p_body_b) {
  638. Body3DSW *body = body_owner.getornull(p_body);
  639. ERR_FAIL_COND(!body);
  640. body->remove_exception(p_body_b);
  641. body->wakeup();
  642. };
  643. void PhysicsServer3DSW::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  644. Body3DSW *body = body_owner.getornull(p_body);
  645. ERR_FAIL_COND(!body);
  646. for (int i = 0; i < body->get_exceptions().size(); i++) {
  647. p_exceptions->push_back(body->get_exceptions()[i]);
  648. }
  649. };
  650. void PhysicsServer3DSW::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
  651. Body3DSW *body = body_owner.getornull(p_body);
  652. ERR_FAIL_COND(!body);
  653. };
  654. real_t PhysicsServer3DSW::body_get_contacts_reported_depth_threshold(RID p_body) const {
  655. Body3DSW *body = body_owner.getornull(p_body);
  656. ERR_FAIL_COND_V(!body, 0);
  657. return 0;
  658. };
  659. void PhysicsServer3DSW::body_set_omit_force_integration(RID p_body, bool p_omit) {
  660. Body3DSW *body = body_owner.getornull(p_body);
  661. ERR_FAIL_COND(!body);
  662. body->set_omit_force_integration(p_omit);
  663. };
  664. bool PhysicsServer3DSW::body_is_omitting_force_integration(RID p_body) const {
  665. Body3DSW *body = body_owner.getornull(p_body);
  666. ERR_FAIL_COND_V(!body, false);
  667. return body->get_omit_force_integration();
  668. };
  669. void PhysicsServer3DSW::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  670. Body3DSW *body = body_owner.getornull(p_body);
  671. ERR_FAIL_COND(!body);
  672. body->set_max_contacts_reported(p_contacts);
  673. }
  674. int PhysicsServer3DSW::body_get_max_contacts_reported(RID p_body) const {
  675. Body3DSW *body = body_owner.getornull(p_body);
  676. ERR_FAIL_COND_V(!body, -1);
  677. return body->get_max_contacts_reported();
  678. }
  679. void PhysicsServer3DSW::body_set_force_integration_callback(RID p_body, const Callable &p_callable, const Variant &p_udata) {
  680. Body3DSW *body = body_owner.getornull(p_body);
  681. ERR_FAIL_COND(!body);
  682. body->set_force_integration_callback(p_callable, p_udata);
  683. }
  684. void PhysicsServer3DSW::body_set_ray_pickable(RID p_body, bool p_enable) {
  685. Body3DSW *body = body_owner.getornull(p_body);
  686. ERR_FAIL_COND(!body);
  687. body->set_ray_pickable(p_enable);
  688. }
  689. bool PhysicsServer3DSW::body_test_motion(RID p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, MotionResult *r_result, bool p_exclude_raycast_shapes) {
  690. Body3DSW *body = body_owner.getornull(p_body);
  691. ERR_FAIL_COND_V(!body, false);
  692. ERR_FAIL_COND_V(!body->get_space(), false);
  693. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  694. _update_shapes();
  695. return body->get_space()->test_body_motion(body, p_from, p_motion, p_infinite_inertia, body->get_kinematic_margin(), r_result, p_exclude_raycast_shapes);
  696. }
  697. int PhysicsServer3DSW::body_test_ray_separation(RID p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, SeparationResult *r_results, int p_result_max, real_t p_margin) {
  698. Body3DSW *body = body_owner.getornull(p_body);
  699. ERR_FAIL_COND_V(!body, false);
  700. ERR_FAIL_COND_V(!body->get_space(), false);
  701. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  702. _update_shapes();
  703. return body->get_space()->test_body_ray_separation(body, p_transform, p_infinite_inertia, r_recover_motion, r_results, p_result_max, p_margin);
  704. }
  705. PhysicsDirectBodyState3D *PhysicsServer3DSW::body_get_direct_state(RID p_body) {
  706. ERR_FAIL_COND_V_MSG((using_threads && !doing_sync), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  707. Body3DSW *body = body_owner.getornull(p_body);
  708. ERR_FAIL_COND_V(!body, nullptr);
  709. ERR_FAIL_COND_V_MSG(body->get_space()->is_locked(), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  710. direct_state->body = body;
  711. return direct_state;
  712. }
  713. /* SOFT BODY */
  714. RID PhysicsServer3DSW::soft_body_create() {
  715. SoftBody3DSW *soft_body = memnew(SoftBody3DSW);
  716. RID rid = soft_body_owner.make_rid(soft_body);
  717. soft_body->set_self(rid);
  718. return rid;
  719. }
  720. void PhysicsServer3DSW::soft_body_update_rendering_server(RID p_body, RenderingServerHandler *p_rendering_server_handler) {
  721. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  722. ERR_FAIL_COND(!soft_body);
  723. soft_body->update_rendering_server(p_rendering_server_handler);
  724. }
  725. void PhysicsServer3DSW::soft_body_set_space(RID p_body, RID p_space) {
  726. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  727. ERR_FAIL_COND(!soft_body);
  728. Space3DSW *space = nullptr;
  729. if (p_space.is_valid()) {
  730. space = space_owner.getornull(p_space);
  731. ERR_FAIL_COND(!space);
  732. }
  733. if (soft_body->get_space() == space) {
  734. return;
  735. }
  736. soft_body->set_space(space);
  737. }
  738. RID PhysicsServer3DSW::soft_body_get_space(RID p_body) const {
  739. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  740. ERR_FAIL_COND_V(!soft_body, RID());
  741. Space3DSW *space = soft_body->get_space();
  742. if (!space) {
  743. return RID();
  744. }
  745. return space->get_self();
  746. }
  747. void PhysicsServer3DSW::soft_body_set_collision_layer(RID p_body, uint32_t p_layer) {
  748. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  749. ERR_FAIL_COND(!soft_body);
  750. soft_body->set_collision_layer(p_layer);
  751. }
  752. uint32_t PhysicsServer3DSW::soft_body_get_collision_layer(RID p_body) const {
  753. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  754. ERR_FAIL_COND_V(!soft_body, 0);
  755. return soft_body->get_collision_layer();
  756. }
  757. void PhysicsServer3DSW::soft_body_set_collision_mask(RID p_body, uint32_t p_mask) {
  758. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  759. ERR_FAIL_COND(!soft_body);
  760. soft_body->set_collision_mask(p_mask);
  761. }
  762. uint32_t PhysicsServer3DSW::soft_body_get_collision_mask(RID p_body) const {
  763. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  764. ERR_FAIL_COND_V(!soft_body, 0);
  765. return soft_body->get_collision_mask();
  766. }
  767. void PhysicsServer3DSW::soft_body_add_collision_exception(RID p_body, RID p_body_b) {
  768. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  769. ERR_FAIL_COND(!soft_body);
  770. soft_body->add_exception(p_body_b);
  771. }
  772. void PhysicsServer3DSW::soft_body_remove_collision_exception(RID p_body, RID p_body_b) {
  773. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  774. ERR_FAIL_COND(!soft_body);
  775. soft_body->remove_exception(p_body_b);
  776. }
  777. void PhysicsServer3DSW::soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  778. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  779. ERR_FAIL_COND(!soft_body);
  780. for (int i = 0; i < soft_body->get_exceptions().size(); i++) {
  781. p_exceptions->push_back(soft_body->get_exceptions()[i]);
  782. }
  783. }
  784. void PhysicsServer3DSW::soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  785. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  786. ERR_FAIL_COND(!soft_body);
  787. soft_body->set_state(p_state, p_variant);
  788. }
  789. Variant PhysicsServer3DSW::soft_body_get_state(RID p_body, BodyState p_state) const {
  790. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  791. ERR_FAIL_COND_V(!soft_body, Variant());
  792. return soft_body->get_state(p_state);
  793. }
  794. void PhysicsServer3DSW::soft_body_set_transform(RID p_body, const Transform &p_transform) {
  795. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  796. ERR_FAIL_COND(!soft_body);
  797. soft_body->set_state(BODY_STATE_TRANSFORM, p_transform);
  798. }
  799. void PhysicsServer3DSW::soft_body_set_ray_pickable(RID p_body, bool p_enable) {
  800. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  801. ERR_FAIL_COND(!soft_body);
  802. soft_body->set_ray_pickable(p_enable);
  803. }
  804. void PhysicsServer3DSW::soft_body_set_simulation_precision(RID p_body, int p_simulation_precision) {
  805. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  806. ERR_FAIL_COND(!soft_body);
  807. soft_body->set_iteration_count(p_simulation_precision);
  808. }
  809. int PhysicsServer3DSW::soft_body_get_simulation_precision(RID p_body) const {
  810. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  811. ERR_FAIL_COND_V(!soft_body, 0.f);
  812. return soft_body->get_iteration_count();
  813. }
  814. void PhysicsServer3DSW::soft_body_set_total_mass(RID p_body, real_t p_total_mass) {
  815. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  816. ERR_FAIL_COND(!soft_body);
  817. soft_body->set_total_mass(p_total_mass);
  818. }
  819. real_t PhysicsServer3DSW::soft_body_get_total_mass(RID p_body) const {
  820. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  821. ERR_FAIL_COND_V(!soft_body, 0.f);
  822. return soft_body->get_total_mass();
  823. }
  824. void PhysicsServer3DSW::soft_body_set_linear_stiffness(RID p_body, real_t p_stiffness) {
  825. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  826. ERR_FAIL_COND(!soft_body);
  827. soft_body->set_linear_stiffness(p_stiffness);
  828. }
  829. real_t PhysicsServer3DSW::soft_body_get_linear_stiffness(RID p_body) const {
  830. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  831. ERR_FAIL_COND_V(!soft_body, 0.f);
  832. return soft_body->get_linear_stiffness();
  833. }
  834. void PhysicsServer3DSW::soft_body_set_pressure_coefficient(RID p_body, real_t p_pressure_coefficient) {
  835. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  836. ERR_FAIL_COND(!soft_body);
  837. soft_body->set_pressure_coefficient(p_pressure_coefficient);
  838. }
  839. real_t PhysicsServer3DSW::soft_body_get_pressure_coefficient(RID p_body) const {
  840. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  841. ERR_FAIL_COND_V(!soft_body, 0.f);
  842. return soft_body->get_pressure_coefficient();
  843. }
  844. void PhysicsServer3DSW::soft_body_set_damping_coefficient(RID p_body, real_t p_damping_coefficient) {
  845. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  846. ERR_FAIL_COND(!soft_body);
  847. soft_body->set_damping_coefficient(p_damping_coefficient);
  848. }
  849. real_t PhysicsServer3DSW::soft_body_get_damping_coefficient(RID p_body) const {
  850. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  851. ERR_FAIL_COND_V(!soft_body, 0.f);
  852. return soft_body->get_damping_coefficient();
  853. }
  854. void PhysicsServer3DSW::soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient) {
  855. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  856. ERR_FAIL_COND(!soft_body);
  857. soft_body->set_drag_coefficient(p_drag_coefficient);
  858. }
  859. real_t PhysicsServer3DSW::soft_body_get_drag_coefficient(RID p_body) const {
  860. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  861. ERR_FAIL_COND_V(!soft_body, 0.f);
  862. return soft_body->get_drag_coefficient();
  863. }
  864. void PhysicsServer3DSW::soft_body_set_mesh(RID p_body, const REF &p_mesh) {
  865. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  866. ERR_FAIL_COND(!soft_body);
  867. soft_body->set_mesh(p_mesh);
  868. }
  869. AABB PhysicsServer3DSW::soft_body_get_bounds(RID p_body) const {
  870. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  871. ERR_FAIL_COND_V(!soft_body, AABB());
  872. return soft_body->get_bounds();
  873. }
  874. void PhysicsServer3DSW::soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) {
  875. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  876. ERR_FAIL_COND(!soft_body);
  877. soft_body->set_vertex_position(p_point_index, p_global_position);
  878. }
  879. Vector3 PhysicsServer3DSW::soft_body_get_point_global_position(RID p_body, int p_point_index) const {
  880. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  881. ERR_FAIL_COND_V(!soft_body, Vector3());
  882. return soft_body->get_vertex_position(p_point_index);
  883. }
  884. void PhysicsServer3DSW::soft_body_remove_all_pinned_points(RID p_body) {
  885. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  886. ERR_FAIL_COND(!soft_body);
  887. soft_body->unpin_all_vertices();
  888. }
  889. void PhysicsServer3DSW::soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) {
  890. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  891. ERR_FAIL_COND(!soft_body);
  892. if (p_pin) {
  893. soft_body->pin_vertex(p_point_index);
  894. } else {
  895. soft_body->unpin_vertex(p_point_index);
  896. }
  897. }
  898. bool PhysicsServer3DSW::soft_body_is_point_pinned(RID p_body, int p_point_index) const {
  899. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_body);
  900. ERR_FAIL_COND_V(!soft_body, false);
  901. return soft_body->is_vertex_pinned(p_point_index);
  902. }
  903. /* JOINT API */
  904. RID PhysicsServer3DSW::joint_create() {
  905. Joint3DSW *joint = memnew(Joint3DSW);
  906. RID rid = joint_owner.make_rid(joint);
  907. joint->set_self(rid);
  908. return rid;
  909. }
  910. void PhysicsServer3DSW::joint_clear(RID p_joint) {
  911. Joint3DSW *joint = joint_owner.getornull(p_joint);
  912. if (joint->get_type() != JOINT_TYPE_MAX) {
  913. Joint3DSW *empty_joint = memnew(Joint3DSW);
  914. empty_joint->copy_settings_from(joint);
  915. joint_owner.replace(p_joint, empty_joint);
  916. memdelete(joint);
  917. }
  918. }
  919. void PhysicsServer3DSW::joint_make_pin(RID p_joint, RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) {
  920. Body3DSW *body_A = body_owner.getornull(p_body_A);
  921. ERR_FAIL_COND(!body_A);
  922. if (!p_body_B.is_valid()) {
  923. ERR_FAIL_COND(!body_A->get_space());
  924. p_body_B = body_A->get_space()->get_static_global_body();
  925. }
  926. Body3DSW *body_B = body_owner.getornull(p_body_B);
  927. ERR_FAIL_COND(!body_B);
  928. ERR_FAIL_COND(body_A == body_B);
  929. Joint3DSW *prev_joint = joint_owner.getornull(p_joint);
  930. ERR_FAIL_COND(prev_joint == nullptr);
  931. Joint3DSW *joint = memnew(PinJoint3DSW(body_A, p_local_A, body_B, p_local_B));
  932. joint->copy_settings_from(prev_joint);
  933. joint_owner.replace(p_joint, joint);
  934. memdelete(prev_joint);
  935. }
  936. void PhysicsServer3DSW::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
  937. Joint3DSW *joint = joint_owner.getornull(p_joint);
  938. ERR_FAIL_COND(!joint);
  939. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  940. PinJoint3DSW *pin_joint = static_cast<PinJoint3DSW *>(joint);
  941. pin_joint->set_param(p_param, p_value);
  942. }
  943. real_t PhysicsServer3DSW::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  944. Joint3DSW *joint = joint_owner.getornull(p_joint);
  945. ERR_FAIL_COND_V(!joint, 0);
  946. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, 0);
  947. PinJoint3DSW *pin_joint = static_cast<PinJoint3DSW *>(joint);
  948. return pin_joint->get_param(p_param);
  949. }
  950. void PhysicsServer3DSW::pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) {
  951. Joint3DSW *joint = joint_owner.getornull(p_joint);
  952. ERR_FAIL_COND(!joint);
  953. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  954. PinJoint3DSW *pin_joint = static_cast<PinJoint3DSW *>(joint);
  955. pin_joint->set_pos_a(p_A);
  956. }
  957. Vector3 PhysicsServer3DSW::pin_joint_get_local_a(RID p_joint) const {
  958. Joint3DSW *joint = joint_owner.getornull(p_joint);
  959. ERR_FAIL_COND_V(!joint, Vector3());
  960. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
  961. PinJoint3DSW *pin_joint = static_cast<PinJoint3DSW *>(joint);
  962. return pin_joint->get_position_a();
  963. }
  964. void PhysicsServer3DSW::pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) {
  965. Joint3DSW *joint = joint_owner.getornull(p_joint);
  966. ERR_FAIL_COND(!joint);
  967. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  968. PinJoint3DSW *pin_joint = static_cast<PinJoint3DSW *>(joint);
  969. pin_joint->set_pos_b(p_B);
  970. }
  971. Vector3 PhysicsServer3DSW::pin_joint_get_local_b(RID p_joint) const {
  972. Joint3DSW *joint = joint_owner.getornull(p_joint);
  973. ERR_FAIL_COND_V(!joint, Vector3());
  974. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
  975. PinJoint3DSW *pin_joint = static_cast<PinJoint3DSW *>(joint);
  976. return pin_joint->get_position_b();
  977. }
  978. void PhysicsServer3DSW::joint_make_hinge(RID p_joint, RID p_body_A, const Transform &p_frame_A, RID p_body_B, const Transform &p_frame_B) {
  979. Body3DSW *body_A = body_owner.getornull(p_body_A);
  980. ERR_FAIL_COND(!body_A);
  981. if (!p_body_B.is_valid()) {
  982. ERR_FAIL_COND(!body_A->get_space());
  983. p_body_B = body_A->get_space()->get_static_global_body();
  984. }
  985. Body3DSW *body_B = body_owner.getornull(p_body_B);
  986. ERR_FAIL_COND(!body_B);
  987. ERR_FAIL_COND(body_A == body_B);
  988. Joint3DSW *prev_joint = joint_owner.getornull(p_joint);
  989. ERR_FAIL_COND(prev_joint == nullptr);
  990. Joint3DSW *joint = memnew(HingeJoint3DSW(body_A, body_B, p_frame_A, p_frame_B));
  991. joint->copy_settings_from(prev_joint);
  992. joint_owner.replace(p_joint, joint);
  993. memdelete(prev_joint);
  994. }
  995. void PhysicsServer3DSW::joint_make_hinge_simple(RID p_joint, RID p_body_A, const Vector3 &p_pivot_A, const Vector3 &p_axis_A, RID p_body_B, const Vector3 &p_pivot_B, const Vector3 &p_axis_B) {
  996. Body3DSW *body_A = body_owner.getornull(p_body_A);
  997. ERR_FAIL_COND(!body_A);
  998. if (!p_body_B.is_valid()) {
  999. ERR_FAIL_COND(!body_A->get_space());
  1000. p_body_B = body_A->get_space()->get_static_global_body();
  1001. }
  1002. Body3DSW *body_B = body_owner.getornull(p_body_B);
  1003. ERR_FAIL_COND(!body_B);
  1004. ERR_FAIL_COND(body_A == body_B);
  1005. Joint3DSW *prev_joint = joint_owner.getornull(p_joint);
  1006. ERR_FAIL_COND(prev_joint == nullptr);
  1007. Joint3DSW *joint = memnew(HingeJoint3DSW(body_A, body_B, p_pivot_A, p_pivot_B, p_axis_A, p_axis_B));
  1008. joint->copy_settings_from(prev_joint);
  1009. joint_owner.replace(p_joint, joint);
  1010. memdelete(prev_joint);
  1011. }
  1012. void PhysicsServer3DSW::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) {
  1013. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1014. ERR_FAIL_COND(!joint);
  1015. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1016. HingeJoint3DSW *hinge_joint = static_cast<HingeJoint3DSW *>(joint);
  1017. hinge_joint->set_param(p_param, p_value);
  1018. }
  1019. real_t PhysicsServer3DSW::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
  1020. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1021. ERR_FAIL_COND_V(!joint, 0);
  1022. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0);
  1023. HingeJoint3DSW *hinge_joint = static_cast<HingeJoint3DSW *>(joint);
  1024. return hinge_joint->get_param(p_param);
  1025. }
  1026. void PhysicsServer3DSW::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) {
  1027. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1028. ERR_FAIL_COND(!joint);
  1029. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
  1030. HingeJoint3DSW *hinge_joint = static_cast<HingeJoint3DSW *>(joint);
  1031. hinge_joint->set_flag(p_flag, p_value);
  1032. }
  1033. bool PhysicsServer3DSW::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
  1034. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1035. ERR_FAIL_COND_V(!joint, false);
  1036. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, false);
  1037. HingeJoint3DSW *hinge_joint = static_cast<HingeJoint3DSW *>(joint);
  1038. return hinge_joint->get_flag(p_flag);
  1039. }
  1040. void PhysicsServer3DSW::joint_set_solver_priority(RID p_joint, int p_priority) {
  1041. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1042. ERR_FAIL_COND(!joint);
  1043. joint->set_priority(p_priority);
  1044. }
  1045. int PhysicsServer3DSW::joint_get_solver_priority(RID p_joint) const {
  1046. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1047. ERR_FAIL_COND_V(!joint, 0);
  1048. return joint->get_priority();
  1049. }
  1050. void PhysicsServer3DSW::joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) {
  1051. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1052. ERR_FAIL_COND(!joint);
  1053. joint->disable_collisions_between_bodies(p_disable);
  1054. if (2 == joint->get_body_count()) {
  1055. Body3DSW *body_a = *joint->get_body_ptr();
  1056. Body3DSW *body_b = *(joint->get_body_ptr() + 1);
  1057. if (p_disable) {
  1058. body_add_collision_exception(body_a->get_self(), body_b->get_self());
  1059. body_add_collision_exception(body_b->get_self(), body_a->get_self());
  1060. } else {
  1061. body_remove_collision_exception(body_a->get_self(), body_b->get_self());
  1062. body_remove_collision_exception(body_b->get_self(), body_a->get_self());
  1063. }
  1064. }
  1065. }
  1066. bool PhysicsServer3DSW::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  1067. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1068. ERR_FAIL_COND_V(!joint, true);
  1069. return joint->is_disabled_collisions_between_bodies();
  1070. }
  1071. PhysicsServer3DSW::JointType PhysicsServer3DSW::joint_get_type(RID p_joint) const {
  1072. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1073. ERR_FAIL_COND_V(!joint, JOINT_TYPE_PIN);
  1074. return joint->get_type();
  1075. }
  1076. void PhysicsServer3DSW::joint_make_slider(RID p_joint, RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  1077. Body3DSW *body_A = body_owner.getornull(p_body_A);
  1078. ERR_FAIL_COND(!body_A);
  1079. if (!p_body_B.is_valid()) {
  1080. ERR_FAIL_COND(!body_A->get_space());
  1081. p_body_B = body_A->get_space()->get_static_global_body();
  1082. }
  1083. Body3DSW *body_B = body_owner.getornull(p_body_B);
  1084. ERR_FAIL_COND(!body_B);
  1085. ERR_FAIL_COND(body_A == body_B);
  1086. Joint3DSW *prev_joint = joint_owner.getornull(p_joint);
  1087. ERR_FAIL_COND(prev_joint == nullptr);
  1088. Joint3DSW *joint = memnew(SliderJoint3DSW(body_A, body_B, p_local_frame_A, p_local_frame_B));
  1089. joint->copy_settings_from(prev_joint);
  1090. joint_owner.replace(p_joint, joint);
  1091. memdelete(prev_joint);
  1092. }
  1093. void PhysicsServer3DSW::slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) {
  1094. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1095. ERR_FAIL_COND(!joint);
  1096. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_SLIDER);
  1097. SliderJoint3DSW *slider_joint = static_cast<SliderJoint3DSW *>(joint);
  1098. slider_joint->set_param(p_param, p_value);
  1099. }
  1100. real_t PhysicsServer3DSW::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
  1101. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1102. ERR_FAIL_COND_V(!joint, 0);
  1103. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0);
  1104. SliderJoint3DSW *slider_joint = static_cast<SliderJoint3DSW *>(joint);
  1105. return slider_joint->get_param(p_param);
  1106. }
  1107. void PhysicsServer3DSW::joint_make_cone_twist(RID p_joint, RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  1108. Body3DSW *body_A = body_owner.getornull(p_body_A);
  1109. ERR_FAIL_COND(!body_A);
  1110. if (!p_body_B.is_valid()) {
  1111. ERR_FAIL_COND(!body_A->get_space());
  1112. p_body_B = body_A->get_space()->get_static_global_body();
  1113. }
  1114. Body3DSW *body_B = body_owner.getornull(p_body_B);
  1115. ERR_FAIL_COND(!body_B);
  1116. ERR_FAIL_COND(body_A == body_B);
  1117. Joint3DSW *prev_joint = joint_owner.getornull(p_joint);
  1118. ERR_FAIL_COND(prev_joint == nullptr);
  1119. Joint3DSW *joint = memnew(ConeTwistJoint3DSW(body_A, body_B, p_local_frame_A, p_local_frame_B));
  1120. joint->copy_settings_from(prev_joint);
  1121. joint_owner.replace(p_joint, joint);
  1122. memdelete(prev_joint);
  1123. }
  1124. void PhysicsServer3DSW::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) {
  1125. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1126. ERR_FAIL_COND(!joint);
  1127. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_CONE_TWIST);
  1128. ConeTwistJoint3DSW *cone_twist_joint = static_cast<ConeTwistJoint3DSW *>(joint);
  1129. cone_twist_joint->set_param(p_param, p_value);
  1130. }
  1131. real_t PhysicsServer3DSW::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
  1132. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1133. ERR_FAIL_COND_V(!joint, 0);
  1134. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0);
  1135. ConeTwistJoint3DSW *cone_twist_joint = static_cast<ConeTwistJoint3DSW *>(joint);
  1136. return cone_twist_joint->get_param(p_param);
  1137. }
  1138. void PhysicsServer3DSW::joint_make_generic_6dof(RID p_joint, RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  1139. Body3DSW *body_A = body_owner.getornull(p_body_A);
  1140. ERR_FAIL_COND(!body_A);
  1141. if (!p_body_B.is_valid()) {
  1142. ERR_FAIL_COND(!body_A->get_space());
  1143. p_body_B = body_A->get_space()->get_static_global_body();
  1144. }
  1145. Body3DSW *body_B = body_owner.getornull(p_body_B);
  1146. ERR_FAIL_COND(!body_B);
  1147. ERR_FAIL_COND(body_A == body_B);
  1148. Joint3DSW *prev_joint = joint_owner.getornull(p_joint);
  1149. ERR_FAIL_COND(prev_joint == nullptr);
  1150. Joint3DSW *joint = memnew(Generic6DOFJoint3DSW(body_A, body_B, p_local_frame_A, p_local_frame_B, true));
  1151. joint->copy_settings_from(prev_joint);
  1152. joint_owner.replace(p_joint, joint);
  1153. memdelete(prev_joint);
  1154. }
  1155. void PhysicsServer3DSW::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param, real_t p_value) {
  1156. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1157. ERR_FAIL_COND(!joint);
  1158. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1159. Generic6DOFJoint3DSW *generic_6dof_joint = static_cast<Generic6DOFJoint3DSW *>(joint);
  1160. generic_6dof_joint->set_param(p_axis, p_param, p_value);
  1161. }
  1162. real_t PhysicsServer3DSW::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param) const {
  1163. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1164. ERR_FAIL_COND_V(!joint, 0);
  1165. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0);
  1166. Generic6DOFJoint3DSW *generic_6dof_joint = static_cast<Generic6DOFJoint3DSW *>(joint);
  1167. return generic_6dof_joint->get_param(p_axis, p_param);
  1168. }
  1169. void PhysicsServer3DSW::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag, bool p_enable) {
  1170. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1171. ERR_FAIL_COND(!joint);
  1172. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
  1173. Generic6DOFJoint3DSW *generic_6dof_joint = static_cast<Generic6DOFJoint3DSW *>(joint);
  1174. generic_6dof_joint->set_flag(p_axis, p_flag, p_enable);
  1175. }
  1176. bool PhysicsServer3DSW::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag) const {
  1177. Joint3DSW *joint = joint_owner.getornull(p_joint);
  1178. ERR_FAIL_COND_V(!joint, false);
  1179. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, false);
  1180. Generic6DOFJoint3DSW *generic_6dof_joint = static_cast<Generic6DOFJoint3DSW *>(joint);
  1181. return generic_6dof_joint->get_flag(p_axis, p_flag);
  1182. }
  1183. void PhysicsServer3DSW::free(RID p_rid) {
  1184. _update_shapes(); //just in case
  1185. if (shape_owner.owns(p_rid)) {
  1186. Shape3DSW *shape = shape_owner.getornull(p_rid);
  1187. while (shape->get_owners().size()) {
  1188. ShapeOwner3DSW *so = shape->get_owners().front()->key();
  1189. so->remove_shape(shape);
  1190. }
  1191. shape_owner.free(p_rid);
  1192. memdelete(shape);
  1193. } else if (body_owner.owns(p_rid)) {
  1194. Body3DSW *body = body_owner.getornull(p_rid);
  1195. /*
  1196. if (body->get_state_query())
  1197. _clear_query(body->get_state_query());
  1198. if (body->get_direct_state_query())
  1199. _clear_query(body->get_direct_state_query());
  1200. */
  1201. body->set_space(nullptr);
  1202. while (body->get_shape_count()) {
  1203. body->remove_shape(0);
  1204. }
  1205. body_owner.free(p_rid);
  1206. memdelete(body);
  1207. } else if (soft_body_owner.owns(p_rid)) {
  1208. SoftBody3DSW *soft_body = soft_body_owner.getornull(p_rid);
  1209. soft_body->set_space(nullptr);
  1210. soft_body_owner.free(p_rid);
  1211. memdelete(soft_body);
  1212. } else if (area_owner.owns(p_rid)) {
  1213. Area3DSW *area = area_owner.getornull(p_rid);
  1214. /*
  1215. if (area->get_monitor_query())
  1216. _clear_query(area->get_monitor_query());
  1217. */
  1218. area->set_space(nullptr);
  1219. while (area->get_shape_count()) {
  1220. area->remove_shape(0);
  1221. }
  1222. area_owner.free(p_rid);
  1223. memdelete(area);
  1224. } else if (space_owner.owns(p_rid)) {
  1225. Space3DSW *space = space_owner.getornull(p_rid);
  1226. while (space->get_objects().size()) {
  1227. CollisionObject3DSW *co = (CollisionObject3DSW *)space->get_objects().front()->get();
  1228. co->set_space(nullptr);
  1229. }
  1230. active_spaces.erase(space);
  1231. free(space->get_default_area()->get_self());
  1232. free(space->get_static_global_body());
  1233. space_owner.free(p_rid);
  1234. memdelete(space);
  1235. } else if (joint_owner.owns(p_rid)) {
  1236. Joint3DSW *joint = joint_owner.getornull(p_rid);
  1237. joint_owner.free(p_rid);
  1238. memdelete(joint);
  1239. } else {
  1240. ERR_FAIL_MSG("Invalid ID.");
  1241. }
  1242. };
  1243. void PhysicsServer3DSW::set_active(bool p_active) {
  1244. active = p_active;
  1245. };
  1246. void PhysicsServer3DSW::init() {
  1247. last_step = 0.001;
  1248. iterations = 8; // 8?
  1249. stepper = memnew(Step3DSW);
  1250. direct_state = memnew(PhysicsDirectBodyState3DSW);
  1251. };
  1252. void PhysicsServer3DSW::step(real_t p_step) {
  1253. #ifndef _3D_DISABLED
  1254. if (!active) {
  1255. return;
  1256. }
  1257. _update_shapes();
  1258. last_step = p_step;
  1259. PhysicsDirectBodyState3DSW::singleton->step = p_step;
  1260. island_count = 0;
  1261. active_objects = 0;
  1262. collision_pairs = 0;
  1263. for (Set<const Space3DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1264. stepper->step((Space3DSW *)E->get(), p_step, iterations);
  1265. island_count += E->get()->get_island_count();
  1266. active_objects += E->get()->get_active_objects();
  1267. collision_pairs += E->get()->get_collision_pairs();
  1268. }
  1269. #endif
  1270. }
  1271. void PhysicsServer3DSW::sync() {
  1272. doing_sync = true;
  1273. };
  1274. void PhysicsServer3DSW::flush_queries() {
  1275. #ifndef _3D_DISABLED
  1276. if (!active) {
  1277. return;
  1278. }
  1279. flushing_queries = true;
  1280. uint64_t time_beg = OS::get_singleton()->get_ticks_usec();
  1281. for (Set<const Space3DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1282. Space3DSW *space = (Space3DSW *)E->get();
  1283. space->call_queries();
  1284. }
  1285. flushing_queries = false;
  1286. if (EngineDebugger::is_profiling("servers")) {
  1287. uint64_t total_time[Space3DSW::ELAPSED_TIME_MAX];
  1288. static const char *time_name[Space3DSW::ELAPSED_TIME_MAX] = {
  1289. "integrate_forces",
  1290. "generate_islands",
  1291. "setup_constraints",
  1292. "solve_constraints",
  1293. "integrate_velocities"
  1294. };
  1295. for (int i = 0; i < Space3DSW::ELAPSED_TIME_MAX; i++) {
  1296. total_time[i] = 0;
  1297. }
  1298. for (Set<const Space3DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1299. for (int i = 0; i < Space3DSW::ELAPSED_TIME_MAX; i++) {
  1300. total_time[i] += E->get()->get_elapsed_time(Space3DSW::ElapsedTime(i));
  1301. }
  1302. }
  1303. Array values;
  1304. values.resize(Space3DSW::ELAPSED_TIME_MAX * 2);
  1305. for (int i = 0; i < Space3DSW::ELAPSED_TIME_MAX; i++) {
  1306. values[i * 2 + 0] = time_name[i];
  1307. values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
  1308. }
  1309. values.push_back("flush_queries");
  1310. values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));
  1311. values.push_front("physics");
  1312. EngineDebugger::profiler_add_frame_data("servers", values);
  1313. }
  1314. #endif
  1315. };
  1316. void PhysicsServer3DSW::end_sync() {
  1317. doing_sync = false;
  1318. };
  1319. void PhysicsServer3DSW::finish() {
  1320. memdelete(stepper);
  1321. memdelete(direct_state);
  1322. };
  1323. int PhysicsServer3DSW::get_process_info(ProcessInfo p_info) {
  1324. switch (p_info) {
  1325. case INFO_ACTIVE_OBJECTS: {
  1326. return active_objects;
  1327. } break;
  1328. case INFO_COLLISION_PAIRS: {
  1329. return collision_pairs;
  1330. } break;
  1331. case INFO_ISLAND_COUNT: {
  1332. return island_count;
  1333. } break;
  1334. }
  1335. return 0;
  1336. }
  1337. void PhysicsServer3DSW::_update_shapes() {
  1338. while (pending_shape_update_list.first()) {
  1339. pending_shape_update_list.first()->self()->_shape_changed();
  1340. pending_shape_update_list.remove(pending_shape_update_list.first());
  1341. }
  1342. }
  1343. void PhysicsServer3DSW::_shape_col_cbk(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, void *p_userdata) {
  1344. CollCbkData *cbk = (CollCbkData *)p_userdata;
  1345. if (cbk->max == 0) {
  1346. return;
  1347. }
  1348. if (cbk->amount == cbk->max) {
  1349. //find least deep
  1350. real_t min_depth = 1e20;
  1351. int min_depth_idx = 0;
  1352. for (int i = 0; i < cbk->amount; i++) {
  1353. real_t d = cbk->ptr[i * 2 + 0].distance_squared_to(cbk->ptr[i * 2 + 1]);
  1354. if (d < min_depth) {
  1355. min_depth = d;
  1356. min_depth_idx = i;
  1357. }
  1358. }
  1359. real_t d = p_point_A.distance_squared_to(p_point_B);
  1360. if (d < min_depth) {
  1361. return;
  1362. }
  1363. cbk->ptr[min_depth_idx * 2 + 0] = p_point_A;
  1364. cbk->ptr[min_depth_idx * 2 + 1] = p_point_B;
  1365. } else {
  1366. cbk->ptr[cbk->amount * 2 + 0] = p_point_A;
  1367. cbk->ptr[cbk->amount * 2 + 1] = p_point_B;
  1368. cbk->amount++;
  1369. }
  1370. }
  1371. PhysicsServer3DSW *PhysicsServer3DSW::singletonsw = nullptr;
  1372. PhysicsServer3DSW::PhysicsServer3DSW(bool p_using_threads) {
  1373. singletonsw = this;
  1374. BroadPhase3DSW::create_func = BroadPhaseOctree::_create;
  1375. island_count = 0;
  1376. active_objects = 0;
  1377. collision_pairs = 0;
  1378. using_threads = p_using_threads;
  1379. active = true;
  1380. flushing_queries = false;
  1381. doing_sync = false;
  1382. };