physics_server_sw.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  1. /*************************************************************************/
  2. /* physics_server_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_sw.h"
  31. #include "broad_phase_basic.h"
  32. #include "broad_phase_bvh.h"
  33. #include "broad_phase_octree.h"
  34. #include "core/os/os.h"
  35. #include "core/project_settings.h"
  36. #include "core/script_language.h"
  37. #include "joints/cone_twist_joint_sw.h"
  38. #include "joints/generic_6dof_joint_sw.h"
  39. #include "joints/hinge_joint_sw.h"
  40. #include "joints/pin_joint_sw.h"
  41. #include "joints/slider_joint_sw.h"
  42. #define FLUSH_QUERY_CHECK(m_object) \
  43. 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.");
  44. RID PhysicsServerSW::shape_create(ShapeType p_shape) {
  45. ShapeSW *shape = nullptr;
  46. switch (p_shape) {
  47. case SHAPE_PLANE: {
  48. shape = memnew(PlaneShapeSW);
  49. } break;
  50. case SHAPE_RAY: {
  51. shape = memnew(RayShapeSW);
  52. } break;
  53. case SHAPE_SPHERE: {
  54. shape = memnew(SphereShapeSW);
  55. } break;
  56. case SHAPE_BOX: {
  57. shape = memnew(BoxShapeSW);
  58. } break;
  59. case SHAPE_CAPSULE: {
  60. shape = memnew(CapsuleShapeSW);
  61. } break;
  62. case SHAPE_CYLINDER: {
  63. shape = memnew(CylinderShapeSW);
  64. } break;
  65. case SHAPE_CONVEX_POLYGON: {
  66. shape = memnew(ConvexPolygonShapeSW);
  67. } break;
  68. case SHAPE_CONCAVE_POLYGON: {
  69. shape = memnew(ConcavePolygonShapeSW);
  70. } break;
  71. case SHAPE_HEIGHTMAP: {
  72. shape = memnew(HeightMapShapeSW);
  73. } break;
  74. case SHAPE_CUSTOM: {
  75. ERR_FAIL_V(RID());
  76. } break;
  77. }
  78. RID id = shape_owner.make_rid(shape);
  79. shape->set_self(id);
  80. return id;
  81. };
  82. void PhysicsServerSW::shape_set_data(RID p_shape, const Variant &p_data) {
  83. ShapeSW *shape = shape_owner.get(p_shape);
  84. ERR_FAIL_COND(!shape);
  85. shape->set_data(p_data);
  86. };
  87. void PhysicsServerSW::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
  88. ShapeSW *shape = shape_owner.get(p_shape);
  89. ERR_FAIL_COND(!shape);
  90. shape->set_custom_bias(p_bias);
  91. }
  92. PhysicsServer::ShapeType PhysicsServerSW::shape_get_type(RID p_shape) const {
  93. const ShapeSW *shape = shape_owner.get(p_shape);
  94. ERR_FAIL_COND_V(!shape, SHAPE_CUSTOM);
  95. return shape->get_type();
  96. };
  97. Variant PhysicsServerSW::shape_get_data(RID p_shape) const {
  98. const ShapeSW *shape = shape_owner.get(p_shape);
  99. ERR_FAIL_COND_V(!shape, Variant());
  100. ERR_FAIL_COND_V(!shape->is_configured(), Variant());
  101. return shape->get_data();
  102. };
  103. void PhysicsServerSW::shape_set_margin(RID p_shape, real_t p_margin) {
  104. }
  105. real_t PhysicsServerSW::shape_get_margin(RID p_shape) const {
  106. return 0.0;
  107. }
  108. real_t PhysicsServerSW::shape_get_custom_solver_bias(RID p_shape) const {
  109. const ShapeSW *shape = shape_owner.get(p_shape);
  110. ERR_FAIL_COND_V(!shape, 0);
  111. return shape->get_custom_bias();
  112. }
  113. RID PhysicsServerSW::space_create() {
  114. SpaceSW *space = memnew(SpaceSW);
  115. RID id = space_owner.make_rid(space);
  116. space->set_self(id);
  117. RID area_id = area_create();
  118. AreaSW *area = area_owner.get(area_id);
  119. ERR_FAIL_COND_V(!area, RID());
  120. space->set_default_area(area);
  121. area->set_space(space);
  122. area->set_priority(-1);
  123. RID sgb = body_create();
  124. body_set_space(sgb, id);
  125. body_set_mode(sgb, BODY_MODE_STATIC);
  126. space->set_static_global_body(sgb);
  127. return id;
  128. };
  129. void PhysicsServerSW::space_set_active(RID p_space, bool p_active) {
  130. SpaceSW *space = space_owner.get(p_space);
  131. ERR_FAIL_COND(!space);
  132. if (p_active) {
  133. active_spaces.insert(space);
  134. } else {
  135. active_spaces.erase(space);
  136. }
  137. }
  138. bool PhysicsServerSW::space_is_active(RID p_space) const {
  139. const SpaceSW *space = space_owner.get(p_space);
  140. ERR_FAIL_COND_V(!space, false);
  141. return active_spaces.has(space);
  142. }
  143. void PhysicsServerSW::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
  144. SpaceSW *space = space_owner.get(p_space);
  145. ERR_FAIL_COND(!space);
  146. space->set_param(p_param, p_value);
  147. }
  148. real_t PhysicsServerSW::space_get_param(RID p_space, SpaceParameter p_param) const {
  149. const SpaceSW *space = space_owner.get(p_space);
  150. ERR_FAIL_COND_V(!space, 0);
  151. return space->get_param(p_param);
  152. }
  153. PhysicsDirectSpaceState *PhysicsServerSW::space_get_direct_state(RID p_space) {
  154. SpaceSW *space = space_owner.get(p_space);
  155. ERR_FAIL_COND_V(!space, nullptr);
  156. ERR_FAIL_COND_V_MSG(space->is_locked(), nullptr, "Space state is inaccessible right now, wait for iteration or physics process notification.");
  157. return space->get_direct_state();
  158. }
  159. void PhysicsServerSW::space_set_debug_contacts(RID p_space, int p_max_contacts) {
  160. SpaceSW *space = space_owner.get(p_space);
  161. ERR_FAIL_COND(!space);
  162. space->set_debug_contacts(p_max_contacts);
  163. }
  164. Vector<Vector3> PhysicsServerSW::space_get_contacts(RID p_space) const {
  165. SpaceSW *space = space_owner.get(p_space);
  166. ERR_FAIL_COND_V(!space, Vector<Vector3>());
  167. return space->get_debug_contacts();
  168. }
  169. int PhysicsServerSW::space_get_contact_count(RID p_space) const {
  170. SpaceSW *space = space_owner.get(p_space);
  171. ERR_FAIL_COND_V(!space, 0);
  172. return space->get_debug_contact_count();
  173. }
  174. RID PhysicsServerSW::area_create() {
  175. AreaSW *area = memnew(AreaSW);
  176. RID rid = area_owner.make_rid(area);
  177. area->set_self(rid);
  178. return rid;
  179. };
  180. void PhysicsServerSW::area_set_space(RID p_area, RID p_space) {
  181. AreaSW *area = area_owner.get(p_area);
  182. ERR_FAIL_COND(!area);
  183. SpaceSW *space = nullptr;
  184. if (p_space.is_valid()) {
  185. space = space_owner.get(p_space);
  186. ERR_FAIL_COND(!space);
  187. }
  188. if (area->get_space() == space) {
  189. return; //pointless
  190. }
  191. area->clear_constraints();
  192. area->set_space(space);
  193. };
  194. RID PhysicsServerSW::area_get_space(RID p_area) const {
  195. AreaSW *area = area_owner.get(p_area);
  196. ERR_FAIL_COND_V(!area, RID());
  197. SpaceSW *space = area->get_space();
  198. if (!space) {
  199. return RID();
  200. }
  201. return space->get_self();
  202. };
  203. void PhysicsServerSW::area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) {
  204. AreaSW *area = area_owner.get(p_area);
  205. ERR_FAIL_COND(!area);
  206. area->set_space_override_mode(p_mode);
  207. }
  208. PhysicsServer::AreaSpaceOverrideMode PhysicsServerSW::area_get_space_override_mode(RID p_area) const {
  209. const AreaSW *area = area_owner.get(p_area);
  210. ERR_FAIL_COND_V(!area, AREA_SPACE_OVERRIDE_DISABLED);
  211. return area->get_space_override_mode();
  212. }
  213. void PhysicsServerSW::area_add_shape(RID p_area, RID p_shape, const Transform &p_transform, bool p_disabled) {
  214. AreaSW *area = area_owner.get(p_area);
  215. ERR_FAIL_COND(!area);
  216. ShapeSW *shape = shape_owner.get(p_shape);
  217. ERR_FAIL_COND(!shape);
  218. area->add_shape(shape, p_transform, p_disabled);
  219. }
  220. void PhysicsServerSW::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
  221. AreaSW *area = area_owner.get(p_area);
  222. ERR_FAIL_COND(!area);
  223. ShapeSW *shape = shape_owner.get(p_shape);
  224. ERR_FAIL_COND(!shape);
  225. ERR_FAIL_COND(!shape->is_configured());
  226. area->set_shape(p_shape_idx, shape);
  227. }
  228. void PhysicsServerSW::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform &p_transform) {
  229. AreaSW *area = area_owner.get(p_area);
  230. ERR_FAIL_COND(!area);
  231. area->set_shape_transform(p_shape_idx, p_transform);
  232. }
  233. int PhysicsServerSW::area_get_shape_count(RID p_area) const {
  234. AreaSW *area = area_owner.get(p_area);
  235. ERR_FAIL_COND_V(!area, -1);
  236. return area->get_shape_count();
  237. }
  238. RID PhysicsServerSW::area_get_shape(RID p_area, int p_shape_idx) const {
  239. AreaSW *area = area_owner.get(p_area);
  240. ERR_FAIL_COND_V(!area, RID());
  241. ShapeSW *shape = area->get_shape(p_shape_idx);
  242. ERR_FAIL_COND_V(!shape, RID());
  243. return shape->get_self();
  244. }
  245. Transform PhysicsServerSW::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  246. AreaSW *area = area_owner.get(p_area);
  247. ERR_FAIL_COND_V(!area, Transform());
  248. return area->get_shape_transform(p_shape_idx);
  249. }
  250. void PhysicsServerSW::area_remove_shape(RID p_area, int p_shape_idx) {
  251. AreaSW *area = area_owner.get(p_area);
  252. ERR_FAIL_COND(!area);
  253. area->remove_shape(p_shape_idx);
  254. }
  255. void PhysicsServerSW::area_clear_shapes(RID p_area) {
  256. AreaSW *area = area_owner.get(p_area);
  257. ERR_FAIL_COND(!area);
  258. while (area->get_shape_count()) {
  259. area->remove_shape(0);
  260. }
  261. }
  262. void PhysicsServerSW::area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) {
  263. AreaSW *area = area_owner.get(p_area);
  264. ERR_FAIL_COND(!area);
  265. ERR_FAIL_INDEX(p_shape_idx, area->get_shape_count());
  266. FLUSH_QUERY_CHECK(area);
  267. area->set_shape_disabled(p_shape_idx, p_disabled);
  268. }
  269. void PhysicsServerSW::area_attach_object_instance_id(RID p_area, ObjectID p_id) {
  270. if (space_owner.owns(p_area)) {
  271. SpaceSW *space = space_owner.get(p_area);
  272. p_area = space->get_default_area()->get_self();
  273. }
  274. AreaSW *area = area_owner.get(p_area);
  275. ERR_FAIL_COND(!area);
  276. area->set_instance_id(p_id);
  277. }
  278. ObjectID PhysicsServerSW::area_get_object_instance_id(RID p_area) const {
  279. if (space_owner.owns(p_area)) {
  280. SpaceSW *space = space_owner.get(p_area);
  281. p_area = space->get_default_area()->get_self();
  282. }
  283. AreaSW *area = area_owner.get(p_area);
  284. ERR_FAIL_COND_V(!area, 0);
  285. return area->get_instance_id();
  286. }
  287. void PhysicsServerSW::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
  288. if (space_owner.owns(p_area)) {
  289. SpaceSW *space = space_owner.get(p_area);
  290. p_area = space->get_default_area()->get_self();
  291. }
  292. AreaSW *area = area_owner.get(p_area);
  293. ERR_FAIL_COND(!area);
  294. area->set_param(p_param, p_value);
  295. };
  296. void PhysicsServerSW::area_set_transform(RID p_area, const Transform &p_transform) {
  297. AreaSW *area = area_owner.get(p_area);
  298. ERR_FAIL_COND(!area);
  299. area->set_transform(p_transform);
  300. };
  301. Variant PhysicsServerSW::area_get_param(RID p_area, AreaParameter p_param) const {
  302. if (space_owner.owns(p_area)) {
  303. SpaceSW *space = space_owner.get(p_area);
  304. p_area = space->get_default_area()->get_self();
  305. }
  306. AreaSW *area = area_owner.get(p_area);
  307. ERR_FAIL_COND_V(!area, Variant());
  308. return area->get_param(p_param);
  309. };
  310. Transform PhysicsServerSW::area_get_transform(RID p_area) const {
  311. AreaSW *area = area_owner.get(p_area);
  312. ERR_FAIL_COND_V(!area, Transform());
  313. return area->get_transform();
  314. };
  315. void PhysicsServerSW::area_set_collision_layer(RID p_area, uint32_t p_layer) {
  316. AreaSW *area = area_owner.get(p_area);
  317. ERR_FAIL_COND(!area);
  318. area->set_collision_layer(p_layer);
  319. }
  320. void PhysicsServerSW::area_set_collision_mask(RID p_area, uint32_t p_mask) {
  321. AreaSW *area = area_owner.get(p_area);
  322. ERR_FAIL_COND(!area);
  323. area->set_collision_mask(p_mask);
  324. }
  325. void PhysicsServerSW::area_set_monitorable(RID p_area, bool p_monitorable) {
  326. AreaSW *area = area_owner.get(p_area);
  327. ERR_FAIL_COND(!area);
  328. FLUSH_QUERY_CHECK(area);
  329. area->set_monitorable(p_monitorable);
  330. }
  331. void PhysicsServerSW::area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  332. AreaSW *area = area_owner.get(p_area);
  333. ERR_FAIL_COND(!area);
  334. area->set_monitor_callback(p_receiver ? p_receiver->get_instance_id() : 0, p_method);
  335. }
  336. void PhysicsServerSW::area_set_ray_pickable(RID p_area, bool p_enable) {
  337. AreaSW *area = area_owner.get(p_area);
  338. ERR_FAIL_COND(!area);
  339. area->set_ray_pickable(p_enable);
  340. }
  341. bool PhysicsServerSW::area_is_ray_pickable(RID p_area) const {
  342. AreaSW *area = area_owner.get(p_area);
  343. ERR_FAIL_COND_V(!area, false);
  344. return area->is_ray_pickable();
  345. }
  346. void PhysicsServerSW::area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  347. AreaSW *area = area_owner.get(p_area);
  348. ERR_FAIL_COND(!area);
  349. area->set_area_monitor_callback(p_receiver ? p_receiver->get_instance_id() : 0, p_method);
  350. }
  351. /* BODY API */
  352. RID PhysicsServerSW::body_create(BodyMode p_mode, bool p_init_sleeping) {
  353. BodySW *body = memnew(BodySW);
  354. if (p_mode != BODY_MODE_RIGID) {
  355. body->set_mode(p_mode);
  356. }
  357. if (p_init_sleeping) {
  358. body->set_state(BODY_STATE_SLEEPING, p_init_sleeping);
  359. }
  360. RID rid = body_owner.make_rid(body);
  361. body->set_self(rid);
  362. return rid;
  363. };
  364. void PhysicsServerSW::body_set_space(RID p_body, RID p_space) {
  365. BodySW *body = body_owner.get(p_body);
  366. ERR_FAIL_COND(!body);
  367. SpaceSW *space = nullptr;
  368. if (p_space.is_valid()) {
  369. space = space_owner.get(p_space);
  370. ERR_FAIL_COND(!space);
  371. }
  372. if (body->get_space() == space) {
  373. return; //pointless
  374. }
  375. body->clear_constraint_map();
  376. body->set_space(space);
  377. };
  378. RID PhysicsServerSW::body_get_space(RID p_body) const {
  379. BodySW *body = body_owner.get(p_body);
  380. ERR_FAIL_COND_V(!body, RID());
  381. SpaceSW *space = body->get_space();
  382. if (!space) {
  383. return RID();
  384. }
  385. return space->get_self();
  386. };
  387. void PhysicsServerSW::body_set_mode(RID p_body, BodyMode p_mode) {
  388. BodySW *body = body_owner.get(p_body);
  389. ERR_FAIL_COND(!body);
  390. body->set_mode(p_mode);
  391. };
  392. PhysicsServer::BodyMode PhysicsServerSW::body_get_mode(RID p_body) const {
  393. BodySW *body = body_owner.get(p_body);
  394. ERR_FAIL_COND_V(!body, BODY_MODE_STATIC);
  395. return body->get_mode();
  396. };
  397. void PhysicsServerSW::body_add_shape(RID p_body, RID p_shape, const Transform &p_transform, bool p_disabled) {
  398. BodySW *body = body_owner.get(p_body);
  399. ERR_FAIL_COND(!body);
  400. ShapeSW *shape = shape_owner.get(p_shape);
  401. ERR_FAIL_COND(!shape);
  402. body->add_shape(shape, p_transform, p_disabled);
  403. }
  404. void PhysicsServerSW::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
  405. BodySW *body = body_owner.get(p_body);
  406. ERR_FAIL_COND(!body);
  407. ShapeSW *shape = shape_owner.get(p_shape);
  408. ERR_FAIL_COND(!shape);
  409. ERR_FAIL_COND(!shape->is_configured());
  410. body->set_shape(p_shape_idx, shape);
  411. }
  412. void PhysicsServerSW::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform &p_transform) {
  413. BodySW *body = body_owner.get(p_body);
  414. ERR_FAIL_COND(!body);
  415. body->set_shape_transform(p_shape_idx, p_transform);
  416. }
  417. int PhysicsServerSW::body_get_shape_count(RID p_body) const {
  418. BodySW *body = body_owner.get(p_body);
  419. ERR_FAIL_COND_V(!body, -1);
  420. return body->get_shape_count();
  421. }
  422. RID PhysicsServerSW::body_get_shape(RID p_body, int p_shape_idx) const {
  423. BodySW *body = body_owner.get(p_body);
  424. ERR_FAIL_COND_V(!body, RID());
  425. ShapeSW *shape = body->get_shape(p_shape_idx);
  426. ERR_FAIL_COND_V(!shape, RID());
  427. return shape->get_self();
  428. }
  429. void PhysicsServerSW::body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) {
  430. BodySW *body = body_owner.get(p_body);
  431. ERR_FAIL_COND(!body);
  432. ERR_FAIL_INDEX(p_shape_idx, body->get_shape_count());
  433. FLUSH_QUERY_CHECK(body);
  434. body->set_shape_disabled(p_shape_idx, p_disabled);
  435. }
  436. Transform PhysicsServerSW::body_get_shape_transform(RID p_body, int p_shape_idx) const {
  437. BodySW *body = body_owner.get(p_body);
  438. ERR_FAIL_COND_V(!body, Transform());
  439. return body->get_shape_transform(p_shape_idx);
  440. }
  441. void PhysicsServerSW::body_remove_shape(RID p_body, int p_shape_idx) {
  442. BodySW *body = body_owner.get(p_body);
  443. ERR_FAIL_COND(!body);
  444. body->remove_shape(p_shape_idx);
  445. }
  446. void PhysicsServerSW::body_clear_shapes(RID p_body) {
  447. BodySW *body = body_owner.get(p_body);
  448. ERR_FAIL_COND(!body);
  449. while (body->get_shape_count()) {
  450. body->remove_shape(0);
  451. }
  452. }
  453. void PhysicsServerSW::body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) {
  454. BodySW *body = body_owner.get(p_body);
  455. ERR_FAIL_COND(!body);
  456. body->set_continuous_collision_detection(p_enable);
  457. }
  458. bool PhysicsServerSW::body_is_continuous_collision_detection_enabled(RID p_body) const {
  459. BodySW *body = body_owner.get(p_body);
  460. ERR_FAIL_COND_V(!body, false);
  461. return body->is_continuous_collision_detection_enabled();
  462. }
  463. void PhysicsServerSW::body_set_collision_layer(RID p_body, uint32_t p_layer) {
  464. BodySW *body = body_owner.get(p_body);
  465. ERR_FAIL_COND(!body);
  466. body->set_collision_layer(p_layer);
  467. body->wakeup();
  468. }
  469. uint32_t PhysicsServerSW::body_get_collision_layer(RID p_body) const {
  470. const BodySW *body = body_owner.get(p_body);
  471. ERR_FAIL_COND_V(!body, 0);
  472. return body->get_collision_layer();
  473. }
  474. void PhysicsServerSW::body_set_collision_mask(RID p_body, uint32_t p_mask) {
  475. BodySW *body = body_owner.get(p_body);
  476. ERR_FAIL_COND(!body);
  477. body->set_collision_mask(p_mask);
  478. body->wakeup();
  479. }
  480. uint32_t PhysicsServerSW::body_get_collision_mask(RID p_body) const {
  481. const BodySW *body = body_owner.get(p_body);
  482. ERR_FAIL_COND_V(!body, 0);
  483. return body->get_collision_mask();
  484. }
  485. void PhysicsServerSW::body_attach_object_instance_id(RID p_body, uint32_t p_id) {
  486. BodySW *body = body_owner.get(p_body);
  487. ERR_FAIL_COND(!body);
  488. body->set_instance_id(p_id);
  489. };
  490. uint32_t PhysicsServerSW::body_get_object_instance_id(RID p_body) const {
  491. BodySW *body = body_owner.get(p_body);
  492. ERR_FAIL_COND_V(!body, 0);
  493. return body->get_instance_id();
  494. };
  495. void PhysicsServerSW::body_set_user_flags(RID p_body, uint32_t p_flags) {
  496. BodySW *body = body_owner.get(p_body);
  497. ERR_FAIL_COND(!body);
  498. };
  499. uint32_t PhysicsServerSW::body_get_user_flags(RID p_body) const {
  500. BodySW *body = body_owner.get(p_body);
  501. ERR_FAIL_COND_V(!body, 0);
  502. return 0;
  503. };
  504. void PhysicsServerSW::body_set_param(RID p_body, BodyParameter p_param, real_t p_value) {
  505. BodySW *body = body_owner.get(p_body);
  506. ERR_FAIL_COND(!body);
  507. body->set_param(p_param, p_value);
  508. };
  509. real_t PhysicsServerSW::body_get_param(RID p_body, BodyParameter p_param) const {
  510. BodySW *body = body_owner.get(p_body);
  511. ERR_FAIL_COND_V(!body, 0);
  512. return body->get_param(p_param);
  513. };
  514. void PhysicsServerSW::body_set_kinematic_safe_margin(RID p_body, real_t p_margin) {
  515. BodySW *body = body_owner.get(p_body);
  516. ERR_FAIL_COND(!body);
  517. body->set_kinematic_margin(p_margin);
  518. }
  519. real_t PhysicsServerSW::body_get_kinematic_safe_margin(RID p_body) const {
  520. BodySW *body = body_owner.get(p_body);
  521. ERR_FAIL_COND_V(!body, 0);
  522. return body->get_kinematic_margin();
  523. }
  524. void PhysicsServerSW::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  525. BodySW *body = body_owner.get(p_body);
  526. ERR_FAIL_COND(!body);
  527. body->set_state(p_state, p_variant);
  528. };
  529. Variant PhysicsServerSW::body_get_state(RID p_body, BodyState p_state) const {
  530. BodySW *body = body_owner.get(p_body);
  531. ERR_FAIL_COND_V(!body, Variant());
  532. return body->get_state(p_state);
  533. };
  534. void PhysicsServerSW::body_set_applied_force(RID p_body, const Vector3 &p_force) {
  535. BodySW *body = body_owner.get(p_body);
  536. ERR_FAIL_COND(!body);
  537. body->set_applied_force(p_force);
  538. body->wakeup();
  539. };
  540. Vector3 PhysicsServerSW::body_get_applied_force(RID p_body) const {
  541. BodySW *body = body_owner.get(p_body);
  542. ERR_FAIL_COND_V(!body, Vector3());
  543. return body->get_applied_force();
  544. };
  545. void PhysicsServerSW::body_set_applied_torque(RID p_body, const Vector3 &p_torque) {
  546. BodySW *body = body_owner.get(p_body);
  547. ERR_FAIL_COND(!body);
  548. body->set_applied_torque(p_torque);
  549. body->wakeup();
  550. };
  551. Vector3 PhysicsServerSW::body_get_applied_torque(RID p_body) const {
  552. BodySW *body = body_owner.get(p_body);
  553. ERR_FAIL_COND_V(!body, Vector3());
  554. return body->get_applied_torque();
  555. };
  556. void PhysicsServerSW::body_add_central_force(RID p_body, const Vector3 &p_force) {
  557. BodySW *body = body_owner.get(p_body);
  558. ERR_FAIL_COND(!body);
  559. body->add_central_force(p_force);
  560. body->wakeup();
  561. }
  562. void PhysicsServerSW::body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_pos) {
  563. BodySW *body = body_owner.get(p_body);
  564. ERR_FAIL_COND(!body);
  565. body->add_force(p_force, p_pos);
  566. body->wakeup();
  567. };
  568. void PhysicsServerSW::body_add_torque(RID p_body, const Vector3 &p_torque) {
  569. BodySW *body = body_owner.get(p_body);
  570. ERR_FAIL_COND(!body);
  571. body->add_torque(p_torque);
  572. body->wakeup();
  573. };
  574. void PhysicsServerSW::body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
  575. BodySW *body = body_owner.get(p_body);
  576. ERR_FAIL_COND(!body);
  577. _update_shapes();
  578. body->apply_central_impulse(p_impulse);
  579. body->wakeup();
  580. }
  581. void PhysicsServerSW::body_apply_impulse(RID p_body, const Vector3 &p_pos, const Vector3 &p_impulse) {
  582. BodySW *body = body_owner.get(p_body);
  583. ERR_FAIL_COND(!body);
  584. _update_shapes();
  585. body->apply_impulse(p_pos, p_impulse);
  586. body->wakeup();
  587. };
  588. void PhysicsServerSW::body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) {
  589. BodySW *body = body_owner.get(p_body);
  590. ERR_FAIL_COND(!body);
  591. _update_shapes();
  592. body->apply_torque_impulse(p_impulse);
  593. body->wakeup();
  594. };
  595. void PhysicsServerSW::body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) {
  596. BodySW *body = body_owner.get(p_body);
  597. ERR_FAIL_COND(!body);
  598. _update_shapes();
  599. Vector3 v = body->get_linear_velocity();
  600. Vector3 axis = p_axis_velocity.normalized();
  601. v -= axis * axis.dot(v);
  602. v += p_axis_velocity;
  603. body->set_linear_velocity(v);
  604. body->wakeup();
  605. };
  606. void PhysicsServerSW::body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) {
  607. BodySW *body = body_owner.get(p_body);
  608. ERR_FAIL_COND(!body);
  609. body->set_axis_lock(p_axis, p_lock);
  610. body->wakeup();
  611. }
  612. bool PhysicsServerSW::body_is_axis_locked(RID p_body, BodyAxis p_axis) const {
  613. const BodySW *body = body_owner.get(p_body);
  614. ERR_FAIL_COND_V(!body, 0);
  615. return body->is_axis_locked(p_axis);
  616. }
  617. void PhysicsServerSW::body_add_collision_exception(RID p_body, RID p_body_b) {
  618. BodySW *body = body_owner.get(p_body);
  619. ERR_FAIL_COND(!body);
  620. body->add_exception(p_body_b);
  621. body->wakeup();
  622. };
  623. void PhysicsServerSW::body_remove_collision_exception(RID p_body, RID p_body_b) {
  624. BodySW *body = body_owner.get(p_body);
  625. ERR_FAIL_COND(!body);
  626. body->remove_exception(p_body_b);
  627. body->wakeup();
  628. };
  629. void PhysicsServerSW::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  630. BodySW *body = body_owner.get(p_body);
  631. ERR_FAIL_COND(!body);
  632. for (int i = 0; i < body->get_exceptions().size(); i++) {
  633. p_exceptions->push_back(body->get_exceptions()[i]);
  634. }
  635. };
  636. void PhysicsServerSW::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
  637. BodySW *body = body_owner.get(p_body);
  638. ERR_FAIL_COND(!body);
  639. };
  640. real_t PhysicsServerSW::body_get_contacts_reported_depth_threshold(RID p_body) const {
  641. BodySW *body = body_owner.get(p_body);
  642. ERR_FAIL_COND_V(!body, 0);
  643. return 0;
  644. };
  645. void PhysicsServerSW::body_set_omit_force_integration(RID p_body, bool p_omit) {
  646. BodySW *body = body_owner.get(p_body);
  647. ERR_FAIL_COND(!body);
  648. body->set_omit_force_integration(p_omit);
  649. };
  650. bool PhysicsServerSW::body_is_omitting_force_integration(RID p_body) const {
  651. BodySW *body = body_owner.get(p_body);
  652. ERR_FAIL_COND_V(!body, false);
  653. return body->get_omit_force_integration();
  654. };
  655. void PhysicsServerSW::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  656. BodySW *body = body_owner.get(p_body);
  657. ERR_FAIL_COND(!body);
  658. body->set_max_contacts_reported(p_contacts);
  659. }
  660. int PhysicsServerSW::body_get_max_contacts_reported(RID p_body) const {
  661. BodySW *body = body_owner.get(p_body);
  662. ERR_FAIL_COND_V(!body, -1);
  663. return body->get_max_contacts_reported();
  664. }
  665. void PhysicsServerSW::body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata) {
  666. BodySW *body = body_owner.get(p_body);
  667. ERR_FAIL_COND(!body);
  668. body->set_force_integration_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(0), p_method, p_udata);
  669. }
  670. void PhysicsServerSW::body_set_ray_pickable(RID p_body, bool p_enable) {
  671. BodySW *body = body_owner.get(p_body);
  672. ERR_FAIL_COND(!body);
  673. body->set_ray_pickable(p_enable);
  674. }
  675. bool PhysicsServerSW::body_is_ray_pickable(RID p_body) const {
  676. BodySW *body = body_owner.get(p_body);
  677. ERR_FAIL_COND_V(!body, false);
  678. return body->is_ray_pickable();
  679. }
  680. bool PhysicsServerSW::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, const Set<RID> &p_exclude) {
  681. BodySW *body = body_owner.get(p_body);
  682. ERR_FAIL_COND_V(!body, false);
  683. ERR_FAIL_COND_V(!body->get_space(), false);
  684. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  685. _update_shapes();
  686. 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, p_exclude);
  687. }
  688. int PhysicsServerSW::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, float p_margin) {
  689. BodySW *body = body_owner.get(p_body);
  690. ERR_FAIL_COND_V(!body, false);
  691. ERR_FAIL_COND_V(!body->get_space(), false);
  692. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  693. _update_shapes();
  694. return body->get_space()->test_body_ray_separation(body, p_transform, p_infinite_inertia, r_recover_motion, r_results, p_result_max, p_margin);
  695. }
  696. PhysicsDirectBodyState *PhysicsServerSW::body_get_direct_state(RID p_body) {
  697. if (!body_owner.owns(p_body)) {
  698. return nullptr;
  699. }
  700. BodySW *body = body_owner.get(p_body);
  701. ERR_FAIL_COND_V(!body, nullptr);
  702. if (!body->get_space()) {
  703. return nullptr;
  704. }
  705. ERR_FAIL_COND_V_MSG(body->get_space()->is_locked(), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  706. direct_state->body = body;
  707. return direct_state;
  708. }
  709. /* JOINT API */
  710. RID PhysicsServerSW::joint_create_pin(RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) {
  711. BodySW *body_A = body_owner.get(p_body_A);
  712. ERR_FAIL_COND_V(!body_A, RID());
  713. if (!p_body_B.is_valid()) {
  714. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  715. p_body_B = body_A->get_space()->get_static_global_body();
  716. }
  717. BodySW *body_B = body_owner.get(p_body_B);
  718. ERR_FAIL_COND_V(!body_B, RID());
  719. ERR_FAIL_COND_V(body_A == body_B, RID());
  720. JointSW *joint = memnew(PinJointSW(body_A, p_local_A, body_B, p_local_B));
  721. RID rid = joint_owner.make_rid(joint);
  722. joint->set_self(rid);
  723. return rid;
  724. }
  725. void PhysicsServerSW::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
  726. JointSW *joint = joint_owner.get(p_joint);
  727. ERR_FAIL_COND(!joint);
  728. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  729. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  730. pin_joint->set_param(p_param, p_value);
  731. }
  732. real_t PhysicsServerSW::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  733. JointSW *joint = joint_owner.get(p_joint);
  734. ERR_FAIL_COND_V(!joint, 0);
  735. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, 0);
  736. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  737. return pin_joint->get_param(p_param);
  738. }
  739. void PhysicsServerSW::pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) {
  740. JointSW *joint = joint_owner.get(p_joint);
  741. ERR_FAIL_COND(!joint);
  742. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  743. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  744. pin_joint->set_pos_a(p_A);
  745. }
  746. Vector3 PhysicsServerSW::pin_joint_get_local_a(RID p_joint) const {
  747. JointSW *joint = joint_owner.get(p_joint);
  748. ERR_FAIL_COND_V(!joint, Vector3());
  749. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, Vector3());
  750. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  751. return pin_joint->get_position_a();
  752. }
  753. void PhysicsServerSW::pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) {
  754. JointSW *joint = joint_owner.get(p_joint);
  755. ERR_FAIL_COND(!joint);
  756. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  757. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  758. pin_joint->set_pos_b(p_B);
  759. }
  760. Vector3 PhysicsServerSW::pin_joint_get_local_b(RID p_joint) const {
  761. JointSW *joint = joint_owner.get(p_joint);
  762. ERR_FAIL_COND_V(!joint, Vector3());
  763. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, Vector3());
  764. PinJointSW *pin_joint = static_cast<PinJointSW *>(joint);
  765. return pin_joint->get_position_b();
  766. }
  767. RID PhysicsServerSW::joint_create_hinge(RID p_body_A, const Transform &p_frame_A, RID p_body_B, const Transform &p_frame_B) {
  768. BodySW *body_A = body_owner.get(p_body_A);
  769. ERR_FAIL_COND_V(!body_A, RID());
  770. if (!p_body_B.is_valid()) {
  771. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  772. p_body_B = body_A->get_space()->get_static_global_body();
  773. }
  774. BodySW *body_B = body_owner.get(p_body_B);
  775. ERR_FAIL_COND_V(!body_B, RID());
  776. ERR_FAIL_COND_V(body_A == body_B, RID());
  777. JointSW *joint = memnew(HingeJointSW(body_A, body_B, p_frame_A, p_frame_B));
  778. RID rid = joint_owner.make_rid(joint);
  779. joint->set_self(rid);
  780. return rid;
  781. }
  782. RID PhysicsServerSW::joint_create_hinge_simple(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) {
  783. BodySW *body_A = body_owner.get(p_body_A);
  784. ERR_FAIL_COND_V(!body_A, RID());
  785. if (!p_body_B.is_valid()) {
  786. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  787. p_body_B = body_A->get_space()->get_static_global_body();
  788. }
  789. BodySW *body_B = body_owner.get(p_body_B);
  790. ERR_FAIL_COND_V(!body_B, RID());
  791. ERR_FAIL_COND_V(body_A == body_B, RID());
  792. JointSW *joint = memnew(HingeJointSW(body_A, body_B, p_pivot_A, p_pivot_B, p_axis_A, p_axis_B));
  793. RID rid = joint_owner.make_rid(joint);
  794. joint->set_self(rid);
  795. return rid;
  796. }
  797. void PhysicsServerSW::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) {
  798. JointSW *joint = joint_owner.get(p_joint);
  799. ERR_FAIL_COND(!joint);
  800. ERR_FAIL_COND(joint->get_type() != JOINT_HINGE);
  801. HingeJointSW *hinge_joint = static_cast<HingeJointSW *>(joint);
  802. hinge_joint->set_param(p_param, p_value);
  803. }
  804. real_t PhysicsServerSW::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
  805. JointSW *joint = joint_owner.get(p_joint);
  806. ERR_FAIL_COND_V(!joint, 0);
  807. ERR_FAIL_COND_V(joint->get_type() != JOINT_HINGE, 0);
  808. HingeJointSW *hinge_joint = static_cast<HingeJointSW *>(joint);
  809. return hinge_joint->get_param(p_param);
  810. }
  811. void PhysicsServerSW::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) {
  812. JointSW *joint = joint_owner.get(p_joint);
  813. ERR_FAIL_COND(!joint);
  814. ERR_FAIL_COND(joint->get_type() != JOINT_HINGE);
  815. HingeJointSW *hinge_joint = static_cast<HingeJointSW *>(joint);
  816. hinge_joint->set_flag(p_flag, p_value);
  817. }
  818. bool PhysicsServerSW::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
  819. JointSW *joint = joint_owner.get(p_joint);
  820. ERR_FAIL_COND_V(!joint, false);
  821. ERR_FAIL_COND_V(joint->get_type() != JOINT_HINGE, false);
  822. HingeJointSW *hinge_joint = static_cast<HingeJointSW *>(joint);
  823. return hinge_joint->get_flag(p_flag);
  824. }
  825. void PhysicsServerSW::joint_set_solver_priority(RID p_joint, int p_priority) {
  826. JointSW *joint = joint_owner.get(p_joint);
  827. ERR_FAIL_COND(!joint);
  828. joint->set_priority(p_priority);
  829. }
  830. int PhysicsServerSW::joint_get_solver_priority(RID p_joint) const {
  831. JointSW *joint = joint_owner.get(p_joint);
  832. ERR_FAIL_COND_V(!joint, 0);
  833. return joint->get_priority();
  834. }
  835. void PhysicsServerSW::joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) {
  836. JointSW *joint = joint_owner.get(p_joint);
  837. ERR_FAIL_COND(!joint);
  838. joint->disable_collisions_between_bodies(p_disable);
  839. if (2 == joint->get_body_count()) {
  840. BodySW *body_a = *joint->get_body_ptr();
  841. BodySW *body_b = *(joint->get_body_ptr() + 1);
  842. if (p_disable) {
  843. body_add_collision_exception(body_a->get_self(), body_b->get_self());
  844. body_add_collision_exception(body_b->get_self(), body_a->get_self());
  845. } else {
  846. body_remove_collision_exception(body_a->get_self(), body_b->get_self());
  847. body_remove_collision_exception(body_b->get_self(), body_a->get_self());
  848. }
  849. }
  850. }
  851. bool PhysicsServerSW::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  852. JointSW *joint = joint_owner.get(p_joint);
  853. ERR_FAIL_COND_V(!joint, true);
  854. return joint->is_disabled_collisions_between_bodies();
  855. }
  856. PhysicsServerSW::JointType PhysicsServerSW::joint_get_type(RID p_joint) const {
  857. JointSW *joint = joint_owner.get(p_joint);
  858. ERR_FAIL_COND_V(!joint, JOINT_PIN);
  859. return joint->get_type();
  860. }
  861. RID PhysicsServerSW::joint_create_slider(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  862. BodySW *body_A = body_owner.get(p_body_A);
  863. ERR_FAIL_COND_V(!body_A, RID());
  864. if (!p_body_B.is_valid()) {
  865. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  866. p_body_B = body_A->get_space()->get_static_global_body();
  867. }
  868. BodySW *body_B = body_owner.get(p_body_B);
  869. ERR_FAIL_COND_V(!body_B, RID());
  870. ERR_FAIL_COND_V(body_A == body_B, RID());
  871. JointSW *joint = memnew(SliderJointSW(body_A, body_B, p_local_frame_A, p_local_frame_B));
  872. RID rid = joint_owner.make_rid(joint);
  873. joint->set_self(rid);
  874. return rid;
  875. }
  876. void PhysicsServerSW::slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) {
  877. JointSW *joint = joint_owner.get(p_joint);
  878. ERR_FAIL_COND(!joint);
  879. ERR_FAIL_COND(joint->get_type() != JOINT_SLIDER);
  880. SliderJointSW *slider_joint = static_cast<SliderJointSW *>(joint);
  881. slider_joint->set_param(p_param, p_value);
  882. }
  883. real_t PhysicsServerSW::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
  884. JointSW *joint = joint_owner.get(p_joint);
  885. ERR_FAIL_COND_V(!joint, 0);
  886. ERR_FAIL_COND_V(joint->get_type() != JOINT_CONE_TWIST, 0);
  887. SliderJointSW *slider_joint = static_cast<SliderJointSW *>(joint);
  888. return slider_joint->get_param(p_param);
  889. }
  890. RID PhysicsServerSW::joint_create_cone_twist(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  891. BodySW *body_A = body_owner.get(p_body_A);
  892. ERR_FAIL_COND_V(!body_A, RID());
  893. if (!p_body_B.is_valid()) {
  894. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  895. p_body_B = body_A->get_space()->get_static_global_body();
  896. }
  897. BodySW *body_B = body_owner.get(p_body_B);
  898. ERR_FAIL_COND_V(!body_B, RID());
  899. ERR_FAIL_COND_V(body_A == body_B, RID());
  900. JointSW *joint = memnew(ConeTwistJointSW(body_A, body_B, p_local_frame_A, p_local_frame_B));
  901. RID rid = joint_owner.make_rid(joint);
  902. joint->set_self(rid);
  903. return rid;
  904. }
  905. void PhysicsServerSW::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) {
  906. JointSW *joint = joint_owner.get(p_joint);
  907. ERR_FAIL_COND(!joint);
  908. ERR_FAIL_COND(joint->get_type() != JOINT_CONE_TWIST);
  909. ConeTwistJointSW *cone_twist_joint = static_cast<ConeTwistJointSW *>(joint);
  910. cone_twist_joint->set_param(p_param, p_value);
  911. }
  912. real_t PhysicsServerSW::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
  913. JointSW *joint = joint_owner.get(p_joint);
  914. ERR_FAIL_COND_V(!joint, 0);
  915. ERR_FAIL_COND_V(joint->get_type() != JOINT_CONE_TWIST, 0);
  916. ConeTwistJointSW *cone_twist_joint = static_cast<ConeTwistJointSW *>(joint);
  917. return cone_twist_joint->get_param(p_param);
  918. }
  919. RID PhysicsServerSW::joint_create_generic_6dof(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  920. BodySW *body_A = body_owner.get(p_body_A);
  921. ERR_FAIL_COND_V(!body_A, RID());
  922. if (!p_body_B.is_valid()) {
  923. ERR_FAIL_COND_V(!body_A->get_space(), RID());
  924. p_body_B = body_A->get_space()->get_static_global_body();
  925. }
  926. BodySW *body_B = body_owner.get(p_body_B);
  927. ERR_FAIL_COND_V(!body_B, RID());
  928. ERR_FAIL_COND_V(body_A == body_B, RID());
  929. JointSW *joint = memnew(Generic6DOFJointSW(body_A, body_B, p_local_frame_A, p_local_frame_B, true));
  930. RID rid = joint_owner.make_rid(joint);
  931. joint->set_self(rid);
  932. return rid;
  933. }
  934. void PhysicsServerSW::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param, real_t p_value) {
  935. JointSW *joint = joint_owner.get(p_joint);
  936. ERR_FAIL_COND(!joint);
  937. ERR_FAIL_COND(joint->get_type() != JOINT_6DOF);
  938. Generic6DOFJointSW *generic_6dof_joint = static_cast<Generic6DOFJointSW *>(joint);
  939. generic_6dof_joint->set_param(p_axis, p_param, p_value);
  940. }
  941. real_t PhysicsServerSW::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param) {
  942. JointSW *joint = joint_owner.get(p_joint);
  943. ERR_FAIL_COND_V(!joint, 0);
  944. ERR_FAIL_COND_V(joint->get_type() != JOINT_6DOF, 0);
  945. Generic6DOFJointSW *generic_6dof_joint = static_cast<Generic6DOFJointSW *>(joint);
  946. return generic_6dof_joint->get_param(p_axis, p_param);
  947. }
  948. void PhysicsServerSW::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag, bool p_enable) {
  949. JointSW *joint = joint_owner.get(p_joint);
  950. ERR_FAIL_COND(!joint);
  951. ERR_FAIL_COND(joint->get_type() != JOINT_6DOF);
  952. Generic6DOFJointSW *generic_6dof_joint = static_cast<Generic6DOFJointSW *>(joint);
  953. generic_6dof_joint->set_flag(p_axis, p_flag, p_enable);
  954. }
  955. bool PhysicsServerSW::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag) {
  956. JointSW *joint = joint_owner.get(p_joint);
  957. ERR_FAIL_COND_V(!joint, false);
  958. ERR_FAIL_COND_V(joint->get_type() != JOINT_6DOF, false);
  959. Generic6DOFJointSW *generic_6dof_joint = static_cast<Generic6DOFJointSW *>(joint);
  960. return generic_6dof_joint->get_flag(p_axis, p_flag);
  961. }
  962. void PhysicsServerSW::free(RID p_rid) {
  963. _update_shapes(); //just in case
  964. if (shape_owner.owns(p_rid)) {
  965. ShapeSW *shape = shape_owner.get(p_rid);
  966. while (shape->get_owners().size()) {
  967. ShapeOwnerSW *so = shape->get_owners().front()->key();
  968. so->remove_shape(shape);
  969. }
  970. shape_owner.free(p_rid);
  971. memdelete(shape);
  972. } else if (body_owner.owns(p_rid)) {
  973. BodySW *body = body_owner.get(p_rid);
  974. /*
  975. if (body->get_state_query())
  976. _clear_query(body->get_state_query());
  977. if (body->get_direct_state_query())
  978. _clear_query(body->get_direct_state_query());
  979. */
  980. body->set_space(nullptr);
  981. while (body->get_shape_count()) {
  982. body->remove_shape(0);
  983. }
  984. body_owner.free(p_rid);
  985. memdelete(body);
  986. } else if (area_owner.owns(p_rid)) {
  987. AreaSW *area = area_owner.get(p_rid);
  988. /*
  989. if (area->get_monitor_query())
  990. _clear_query(area->get_monitor_query());
  991. */
  992. area->set_space(nullptr);
  993. while (area->get_shape_count()) {
  994. area->remove_shape(0);
  995. }
  996. area_owner.free(p_rid);
  997. memdelete(area);
  998. } else if (space_owner.owns(p_rid)) {
  999. SpaceSW *space = space_owner.get(p_rid);
  1000. while (space->get_objects().size()) {
  1001. CollisionObjectSW *co = (CollisionObjectSW *)space->get_objects().front()->get();
  1002. co->set_space(nullptr);
  1003. }
  1004. active_spaces.erase(space);
  1005. free(space->get_default_area()->get_self());
  1006. free(space->get_static_global_body());
  1007. space_owner.free(p_rid);
  1008. memdelete(space);
  1009. } else if (joint_owner.owns(p_rid)) {
  1010. JointSW *joint = joint_owner.get(p_rid);
  1011. for (int i = 0; i < joint->get_body_count(); i++) {
  1012. joint->get_body_ptr()[i]->remove_constraint(joint);
  1013. }
  1014. joint_owner.free(p_rid);
  1015. memdelete(joint);
  1016. } else {
  1017. ERR_FAIL_MSG("Invalid ID.");
  1018. }
  1019. };
  1020. void PhysicsServerSW::set_active(bool p_active) {
  1021. active = p_active;
  1022. };
  1023. void PhysicsServerSW::set_collision_iterations(int p_iterations) {
  1024. iterations = p_iterations;
  1025. };
  1026. void PhysicsServerSW::init() {
  1027. last_step = 0.001;
  1028. iterations = 8; // 8?
  1029. stepper = memnew(StepSW);
  1030. direct_state = memnew(PhysicsDirectBodyStateSW);
  1031. };
  1032. void PhysicsServerSW::step(real_t p_step) {
  1033. #ifndef _3D_DISABLED
  1034. if (!active) {
  1035. return;
  1036. }
  1037. _update_shapes();
  1038. last_step = p_step;
  1039. PhysicsDirectBodyStateSW::singleton->step = p_step;
  1040. island_count = 0;
  1041. active_objects = 0;
  1042. collision_pairs = 0;
  1043. for (Set<const SpaceSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1044. stepper->step((SpaceSW *)E->get(), p_step, iterations);
  1045. island_count += E->get()->get_island_count();
  1046. active_objects += E->get()->get_active_objects();
  1047. collision_pairs += E->get()->get_collision_pairs();
  1048. }
  1049. #endif
  1050. }
  1051. void PhysicsServerSW::flush_queries() {
  1052. #ifndef _3D_DISABLED
  1053. if (!active) {
  1054. return;
  1055. }
  1056. flushing_queries = true;
  1057. uint64_t time_beg = OS::get_singleton()->get_ticks_usec();
  1058. for (Set<const SpaceSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1059. SpaceSW *space = (SpaceSW *)E->get();
  1060. space->call_queries();
  1061. }
  1062. flushing_queries = false;
  1063. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_profiling()) {
  1064. uint64_t total_time[SpaceSW::ELAPSED_TIME_MAX];
  1065. static const char *time_name[SpaceSW::ELAPSED_TIME_MAX] = {
  1066. "integrate_forces",
  1067. "generate_islands",
  1068. "setup_constraints",
  1069. "solve_constraints",
  1070. "integrate_velocities"
  1071. };
  1072. for (int i = 0; i < SpaceSW::ELAPSED_TIME_MAX; i++) {
  1073. total_time[i] = 0;
  1074. }
  1075. for (Set<const SpaceSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1076. for (int i = 0; i < SpaceSW::ELAPSED_TIME_MAX; i++) {
  1077. total_time[i] += E->get()->get_elapsed_time(SpaceSW::ElapsedTime(i));
  1078. }
  1079. }
  1080. Array values;
  1081. values.resize(SpaceSW::ELAPSED_TIME_MAX * 2);
  1082. for (int i = 0; i < SpaceSW::ELAPSED_TIME_MAX; i++) {
  1083. values[i * 2 + 0] = time_name[i];
  1084. values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
  1085. }
  1086. values.push_back("flush_queries");
  1087. values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));
  1088. ScriptDebugger::get_singleton()->add_profiling_frame_data("physics", values);
  1089. }
  1090. #endif
  1091. };
  1092. void PhysicsServerSW::finish() {
  1093. memdelete(stepper);
  1094. memdelete(direct_state);
  1095. };
  1096. int PhysicsServerSW::get_process_info(ProcessInfo p_info) {
  1097. switch (p_info) {
  1098. case INFO_ACTIVE_OBJECTS: {
  1099. return active_objects;
  1100. } break;
  1101. case INFO_COLLISION_PAIRS: {
  1102. return collision_pairs;
  1103. } break;
  1104. case INFO_ISLAND_COUNT: {
  1105. return island_count;
  1106. } break;
  1107. }
  1108. return 0;
  1109. }
  1110. void PhysicsServerSW::_update_shapes() {
  1111. while (pending_shape_update_list.first()) {
  1112. pending_shape_update_list.first()->self()->_shape_changed();
  1113. pending_shape_update_list.remove(pending_shape_update_list.first());
  1114. }
  1115. }
  1116. void PhysicsServerSW::_shape_col_cbk(const Vector3 &p_point_A, const Vector3 &p_point_B, void *p_userdata) {
  1117. CollCbkData *cbk = (CollCbkData *)p_userdata;
  1118. if (cbk->max == 0) {
  1119. return;
  1120. }
  1121. if (cbk->amount == cbk->max) {
  1122. //find least deep
  1123. real_t min_depth = 1e20;
  1124. int min_depth_idx = 0;
  1125. for (int i = 0; i < cbk->amount; i++) {
  1126. real_t d = cbk->ptr[i * 2 + 0].distance_squared_to(cbk->ptr[i * 2 + 1]);
  1127. if (d < min_depth) {
  1128. min_depth = d;
  1129. min_depth_idx = i;
  1130. }
  1131. }
  1132. real_t d = p_point_A.distance_squared_to(p_point_B);
  1133. if (d < min_depth) {
  1134. return;
  1135. }
  1136. cbk->ptr[min_depth_idx * 2 + 0] = p_point_A;
  1137. cbk->ptr[min_depth_idx * 2 + 1] = p_point_B;
  1138. } else {
  1139. cbk->ptr[cbk->amount * 2 + 0] = p_point_A;
  1140. cbk->ptr[cbk->amount * 2 + 1] = p_point_B;
  1141. cbk->amount++;
  1142. }
  1143. }
  1144. PhysicsServerSW *PhysicsServerSW::singleton = nullptr;
  1145. PhysicsServerSW::PhysicsServerSW() {
  1146. singleton = this;
  1147. bool use_bvh_or_octree = GLOBAL_GET("physics/3d/godot_physics/use_bvh");
  1148. if (use_bvh_or_octree) {
  1149. BroadPhaseSW::create_func = BroadPhaseBVH::_create;
  1150. } else {
  1151. BroadPhaseSW::create_func = BroadPhaseOctree::_create;
  1152. }
  1153. island_count = 0;
  1154. active_objects = 0;
  1155. collision_pairs = 0;
  1156. active = true;
  1157. flushing_queries = false;
  1158. };
  1159. PhysicsServerSW::~PhysicsServerSW(){
  1160. };