physics_server_sw.cpp 43 KB

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