physics_2d_server.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*************************************************************************/
  2. /* physics_2d_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "physics_2d_server.h"
  30. #include "print_string.h"
  31. Physics2DServer * Physics2DServer::singleton=NULL;
  32. void Physics2DDirectBodyState::integrate_forces() {
  33. real_t step = get_step();
  34. Vector2 lv = get_linear_velocity();
  35. lv+=get_total_gravity() * step;
  36. real_t av = get_angular_velocity();
  37. float damp = 1.0 - step * get_total_linear_damp();
  38. if (damp<0) // reached zero in the given time
  39. damp=0;
  40. lv*=damp;
  41. damp = 1.0 - step * get_total_angular_damp();
  42. if (damp<0) // reached zero in the given time
  43. damp=0;
  44. av*=damp;
  45. set_linear_velocity(lv);
  46. set_angular_velocity(av);
  47. }
  48. Object* Physics2DDirectBodyState::get_contact_collider_object(int p_contact_idx) const {
  49. ObjectID objid = get_contact_collider_id(p_contact_idx);
  50. Object *obj = ObjectDB::get_instance( objid );
  51. return obj;
  52. }
  53. Physics2DServer * Physics2DServer::get_singleton() {
  54. return singleton;
  55. }
  56. void Physics2DDirectBodyState::_bind_methods() {
  57. ObjectTypeDB::bind_method(_MD("get_total_gravity"),&Physics2DDirectBodyState::get_total_gravity);
  58. ObjectTypeDB::bind_method(_MD("get_total_linear_damp"),&Physics2DDirectBodyState::get_total_linear_damp);
  59. ObjectTypeDB::bind_method(_MD("get_total_angular_damp"),&Physics2DDirectBodyState::get_total_angular_damp);
  60. ObjectTypeDB::bind_method(_MD("get_inverse_mass"),&Physics2DDirectBodyState::get_inverse_mass);
  61. ObjectTypeDB::bind_method(_MD("get_inverse_inertia"),&Physics2DDirectBodyState::get_inverse_inertia);
  62. ObjectTypeDB::bind_method(_MD("set_linear_velocity","velocity"),&Physics2DDirectBodyState::set_linear_velocity);
  63. ObjectTypeDB::bind_method(_MD("get_linear_velocity"),&Physics2DDirectBodyState::get_linear_velocity);
  64. ObjectTypeDB::bind_method(_MD("set_angular_velocity","velocity"),&Physics2DDirectBodyState::set_angular_velocity);
  65. ObjectTypeDB::bind_method(_MD("get_angular_velocity"),&Physics2DDirectBodyState::get_angular_velocity);
  66. ObjectTypeDB::bind_method(_MD("set_transform","transform"),&Physics2DDirectBodyState::set_transform);
  67. ObjectTypeDB::bind_method(_MD("get_transform"),&Physics2DDirectBodyState::get_transform);
  68. ObjectTypeDB::bind_method(_MD("set_sleep_state","enabled"),&Physics2DDirectBodyState::set_sleep_state);
  69. ObjectTypeDB::bind_method(_MD("is_sleeping"),&Physics2DDirectBodyState::is_sleeping);
  70. ObjectTypeDB::bind_method(_MD("get_contact_count"),&Physics2DDirectBodyState::get_contact_count);
  71. ObjectTypeDB::bind_method(_MD("get_contact_local_pos","contact_idx"),&Physics2DDirectBodyState::get_contact_local_pos);
  72. ObjectTypeDB::bind_method(_MD("get_contact_local_normal","contact_idx"),&Physics2DDirectBodyState::get_contact_local_normal);
  73. ObjectTypeDB::bind_method(_MD("get_contact_local_shape","contact_idx"),&Physics2DDirectBodyState::get_contact_local_shape);
  74. ObjectTypeDB::bind_method(_MD("get_contact_collider","contact_idx"),&Physics2DDirectBodyState::get_contact_collider);
  75. ObjectTypeDB::bind_method(_MD("get_contact_collider_pos","contact_idx"),&Physics2DDirectBodyState::get_contact_collider_pos);
  76. ObjectTypeDB::bind_method(_MD("get_contact_collider_id","contact_idx"),&Physics2DDirectBodyState::get_contact_collider_id);
  77. ObjectTypeDB::bind_method(_MD("get_contact_collider_object","contact_idx"),&Physics2DDirectBodyState::get_contact_collider_object);
  78. ObjectTypeDB::bind_method(_MD("get_contact_collider_shape","contact_idx"),&Physics2DDirectBodyState::get_contact_collider_shape);
  79. ObjectTypeDB::bind_method(_MD("get_contact_collider_shape_metadata:var","contact_idx"),&Physics2DDirectBodyState::get_contact_collider_shape_metadata);
  80. ObjectTypeDB::bind_method(_MD("get_contact_collider_velocity_at_pos","contact_idx"),&Physics2DDirectBodyState::get_contact_collider_velocity_at_pos);
  81. ObjectTypeDB::bind_method(_MD("get_step"),&Physics2DDirectBodyState::get_step);
  82. ObjectTypeDB::bind_method(_MD("integrate_forces"),&Physics2DDirectBodyState::integrate_forces);
  83. ObjectTypeDB::bind_method(_MD("get_space_state:Physics2DDirectSpaceState"),&Physics2DDirectBodyState::get_space_state);
  84. }
  85. Physics2DDirectBodyState::Physics2DDirectBodyState() {}
  86. ///////////////////////////////////////////////////////
  87. void Physics2DShapeQueryParameters::set_shape(const RES &p_shape) {
  88. ERR_FAIL_COND(p_shape.is_null());
  89. shape=p_shape->get_rid();
  90. }
  91. void Physics2DShapeQueryParameters::set_shape_rid(const RID& p_shape) {
  92. shape=p_shape;
  93. }
  94. RID Physics2DShapeQueryParameters::get_shape_rid() const {
  95. return shape;
  96. }
  97. void Physics2DShapeQueryParameters::set_transform(const Matrix32& p_transform){
  98. transform=p_transform;
  99. }
  100. Matrix32 Physics2DShapeQueryParameters::get_transform() const{
  101. return transform;
  102. }
  103. void Physics2DShapeQueryParameters::set_motion(const Vector2& p_motion){
  104. motion=p_motion;
  105. }
  106. Vector2 Physics2DShapeQueryParameters::get_motion() const{
  107. return motion;
  108. }
  109. void Physics2DShapeQueryParameters::set_margin(float p_margin){
  110. margin=p_margin;
  111. }
  112. float Physics2DShapeQueryParameters::get_margin() const{
  113. return margin;
  114. }
  115. void Physics2DShapeQueryParameters::set_layer_mask(int p_layer_mask){
  116. layer_mask=p_layer_mask;
  117. }
  118. int Physics2DShapeQueryParameters::get_layer_mask() const{
  119. return layer_mask;
  120. }
  121. void Physics2DShapeQueryParameters::set_object_type_mask(int p_object_type_mask){
  122. object_type_mask=p_object_type_mask;
  123. }
  124. int Physics2DShapeQueryParameters::get_object_type_mask() const{
  125. return object_type_mask;
  126. }
  127. void Physics2DShapeQueryParameters::set_exclude(const Vector<RID>& p_exclude) {
  128. exclude.clear();;
  129. for(int i=0;i<p_exclude.size();i++)
  130. exclude.insert(p_exclude[i]);
  131. }
  132. Vector<RID> Physics2DShapeQueryParameters::get_exclude() const{
  133. Vector<RID> ret;
  134. ret.resize(exclude.size());
  135. int idx=0;
  136. for(Set<RID>::Element *E=exclude.front();E;E=E->next()) {
  137. ret[idx]=E->get();
  138. }
  139. return ret;
  140. }
  141. void Physics2DShapeQueryParameters::_bind_methods() {
  142. ObjectTypeDB::bind_method(_MD("set_shape","shape:Shape2D"),&Physics2DShapeQueryParameters::set_shape);
  143. ObjectTypeDB::bind_method(_MD("set_shape_rid","shape"),&Physics2DShapeQueryParameters::set_shape_rid);
  144. ObjectTypeDB::bind_method(_MD("get_shape_rid"),&Physics2DShapeQueryParameters::get_shape_rid);
  145. ObjectTypeDB::bind_method(_MD("set_transform","transform"),&Physics2DShapeQueryParameters::set_transform);
  146. ObjectTypeDB::bind_method(_MD("get_transform"),&Physics2DShapeQueryParameters::get_transform);
  147. ObjectTypeDB::bind_method(_MD("set_motion","motion"),&Physics2DShapeQueryParameters::set_motion);
  148. ObjectTypeDB::bind_method(_MD("get_motion"),&Physics2DShapeQueryParameters::get_motion);
  149. ObjectTypeDB::bind_method(_MD("set_margin","margin"),&Physics2DShapeQueryParameters::set_margin);
  150. ObjectTypeDB::bind_method(_MD("get_margin"),&Physics2DShapeQueryParameters::get_margin);
  151. ObjectTypeDB::bind_method(_MD("set_layer_mask","layer_mask"),&Physics2DShapeQueryParameters::set_layer_mask);
  152. ObjectTypeDB::bind_method(_MD("get_layer_mask"),&Physics2DShapeQueryParameters::get_layer_mask);
  153. ObjectTypeDB::bind_method(_MD("set_object_type_mask","object_type_mask"),&Physics2DShapeQueryParameters::set_object_type_mask);
  154. ObjectTypeDB::bind_method(_MD("get_object_type_mask"),&Physics2DShapeQueryParameters::get_object_type_mask);
  155. ObjectTypeDB::bind_method(_MD("set_exclude","exclude"),&Physics2DShapeQueryParameters::set_exclude);
  156. ObjectTypeDB::bind_method(_MD("get_exclude"),&Physics2DShapeQueryParameters::get_exclude);
  157. }
  158. Physics2DShapeQueryParameters::Physics2DShapeQueryParameters() {
  159. margin=0;
  160. layer_mask=0x7FFFFFFF;
  161. object_type_mask=Physics2DDirectSpaceState::TYPE_MASK_COLLISION;
  162. }
  163. Dictionary Physics2DDirectSpaceState::_intersect_ray(const Vector2& p_from, const Vector2& p_to,const Vector<RID>& p_exclude,uint32_t p_layers,uint32_t p_object_type_mask) {
  164. RayResult inters;
  165. Set<RID> exclude;
  166. for(int i=0;i<p_exclude.size();i++)
  167. exclude.insert(p_exclude[i]);
  168. bool res = intersect_ray(p_from,p_to,inters,exclude,p_layers,p_object_type_mask);
  169. if (!res)
  170. return Dictionary(true);
  171. Dictionary d(true);
  172. d["position"]=inters.position;
  173. d["normal"]=inters.normal;
  174. d["collider_id"]=inters.collider_id;
  175. d["collider"]=inters.collider;
  176. d["shape"]=inters.shape;
  177. d["rid"]=inters.rid;
  178. d["metadata"]=inters.metadata;
  179. return d;
  180. }
  181. Array Physics2DDirectSpaceState::_intersect_shape(const Ref<Physics2DShapeQueryParameters> &psq, int p_max_results) {
  182. Vector<ShapeResult> sr;
  183. sr.resize(p_max_results);
  184. int rc = intersect_shape(psq->shape,psq->transform,psq->motion,psq->margin,sr.ptr(),sr.size(),psq->exclude,psq->layer_mask,psq->object_type_mask);
  185. Array ret;
  186. ret.resize(rc);
  187. for(int i=0;i<rc;i++) {
  188. Dictionary d;
  189. d["rid"]=sr[i].rid;
  190. d["collider_id"]=sr[i].collider_id;
  191. d["collider"]=sr[i].collider;
  192. d["shape"]=sr[i].shape;
  193. d["metadata"]=sr[i].metadata;
  194. ret[i]=d;
  195. }
  196. return ret;
  197. }
  198. Array Physics2DDirectSpaceState::_cast_motion(const Ref<Physics2DShapeQueryParameters> &psq){
  199. float closest_safe,closest_unsafe;
  200. bool res = cast_motion(psq->shape,psq->transform,psq->motion,psq->margin,closest_safe,closest_unsafe,psq->exclude,psq->layer_mask,psq->object_type_mask);
  201. if (!res)
  202. return Array();
  203. Array ret(true);
  204. ret.resize(2);
  205. ret[0]=closest_safe;
  206. ret[1]=closest_unsafe;
  207. return ret;
  208. }
  209. Array Physics2DDirectSpaceState::_intersect_point(const Vector2& p_point,int p_max_results,const Vector<RID>& p_exclude,uint32_t p_layers,uint32_t p_object_type_mask) {
  210. Set<RID> exclude;
  211. for(int i=0;i<p_exclude.size();i++)
  212. exclude.insert(p_exclude[i]);
  213. Vector<ShapeResult> ret;
  214. ret.resize(p_max_results);
  215. int rc = intersect_point(p_point,ret.ptr(),ret.size(),exclude,p_layers,p_object_type_mask);
  216. if (rc==0)
  217. return Array();
  218. Array r;
  219. r.resize(rc);
  220. for(int i=0;i<rc;i++) {
  221. Dictionary d;
  222. d["rid"]=ret[i].rid;
  223. d["collider_id"]=ret[i].collider_id;
  224. d["collider"]=ret[i].collider;
  225. d["shape"]=ret[i].shape;
  226. d["metadata"]=ret[i].metadata;
  227. r[i]=d;
  228. }
  229. return r;
  230. }
  231. Array Physics2DDirectSpaceState::_collide_shape(const Ref<Physics2DShapeQueryParameters> &psq, int p_max_results){
  232. Vector<Vector2> ret;
  233. ret.resize(p_max_results*2);
  234. int rc=0;
  235. bool res = collide_shape(psq->shape,psq->transform,psq->motion,psq->margin,ret.ptr(),p_max_results,rc,psq->exclude,psq->layer_mask,psq->object_type_mask);
  236. if (!res)
  237. return Array();
  238. Array r;
  239. r.resize(rc*2);
  240. for(int i=0;i<rc*2;i++)
  241. r[i]=ret[i];
  242. return r;
  243. }
  244. Dictionary Physics2DDirectSpaceState::_get_rest_info(const Ref<Physics2DShapeQueryParameters> &psq){
  245. ShapeRestInfo sri;
  246. bool res = rest_info(psq->shape,psq->transform,psq->motion,psq->margin,&sri,psq->exclude,psq->layer_mask,psq->object_type_mask);
  247. Dictionary r(true);
  248. if (!res)
  249. return r;
  250. r["point"]=sri.point;
  251. r["normal"]=sri.normal;
  252. r["rid"]=sri.rid;
  253. r["collider_id"]=sri.collider_id;
  254. r["shape"]=sri.shape;
  255. r["linear_velocity"]=sri.linear_velocity;
  256. r["metadata"]=sri.metadata;
  257. return r;
  258. }
  259. Physics2DDirectSpaceState::Physics2DDirectSpaceState() {
  260. }
  261. void Physics2DDirectSpaceState::_bind_methods() {
  262. ObjectTypeDB::bind_method(_MD("intersect_point","point","max_results","exclude","layer_mask","type_mask"),&Physics2DDirectSpaceState::_intersect_point,DEFVAL(32),DEFVAL(Array()),DEFVAL(0x7FFFFFFF),DEFVAL(TYPE_MASK_COLLISION));
  263. ObjectTypeDB::bind_method(_MD("intersect_ray:Dictionary","from","to","exclude","layer_mask","type_mask"),&Physics2DDirectSpaceState::_intersect_ray,DEFVAL(Array()),DEFVAL(0x7FFFFFFF),DEFVAL(TYPE_MASK_COLLISION));
  264. ObjectTypeDB::bind_method(_MD("intersect_shape","shape:Physics2DShapeQueryParameters","max_results"),&Physics2DDirectSpaceState::_intersect_shape,DEFVAL(32));
  265. ObjectTypeDB::bind_method(_MD("cast_motion","shape:Physics2DShapeQueryParameters"),&Physics2DDirectSpaceState::_cast_motion);
  266. ObjectTypeDB::bind_method(_MD("collide_shape","shape:Physics2DShapeQueryParameters","max_results"),&Physics2DDirectSpaceState::_collide_shape,DEFVAL(32));
  267. ObjectTypeDB::bind_method(_MD("get_rest_info","shape:Physics2DShapeQueryParameters"),&Physics2DDirectSpaceState::_get_rest_info);
  268. //ObjectTypeDB::bind_method(_MD("cast_motion","shape","xform","motion","exclude","umask"),&Physics2DDirectSpaceState::_intersect_shape,DEFVAL(Array()),DEFVAL(0));
  269. BIND_CONSTANT( TYPE_MASK_STATIC_BODY );
  270. BIND_CONSTANT( TYPE_MASK_KINEMATIC_BODY );
  271. BIND_CONSTANT( TYPE_MASK_RIGID_BODY );
  272. BIND_CONSTANT( TYPE_MASK_CHARACTER_BODY );
  273. BIND_CONSTANT( TYPE_MASK_AREA );
  274. BIND_CONSTANT( TYPE_MASK_COLLISION );
  275. }
  276. int Physics2DShapeQueryResult::get_result_count() const {
  277. return result.size();
  278. }
  279. RID Physics2DShapeQueryResult::get_result_rid(int p_idx) const {
  280. return result[p_idx].rid;
  281. }
  282. ObjectID Physics2DShapeQueryResult::get_result_object_id(int p_idx) const {
  283. return result[p_idx].collider_id;
  284. }
  285. Object* Physics2DShapeQueryResult::get_result_object(int p_idx) const {
  286. return result[p_idx].collider;
  287. }
  288. int Physics2DShapeQueryResult::get_result_object_shape(int p_idx) const {
  289. return result[p_idx].shape;
  290. }
  291. Physics2DShapeQueryResult::Physics2DShapeQueryResult() {
  292. }
  293. void Physics2DShapeQueryResult::_bind_methods() {
  294. ObjectTypeDB::bind_method(_MD("get_result_count"),&Physics2DShapeQueryResult::get_result_count);
  295. ObjectTypeDB::bind_method(_MD("get_result_rid","idx"),&Physics2DShapeQueryResult::get_result_rid);
  296. ObjectTypeDB::bind_method(_MD("get_result_object_id","idx"),&Physics2DShapeQueryResult::get_result_object_id);
  297. ObjectTypeDB::bind_method(_MD("get_result_object","idx"),&Physics2DShapeQueryResult::get_result_object);
  298. ObjectTypeDB::bind_method(_MD("get_result_object_shape","idx"),&Physics2DShapeQueryResult::get_result_object_shape);
  299. }
  300. ///////////////////////////////
  301. /*bool Physics2DTestMotionResult::is_colliding() const {
  302. return colliding;
  303. }*/
  304. Vector2 Physics2DTestMotionResult::get_motion() const{
  305. return result.motion;
  306. }
  307. Vector2 Physics2DTestMotionResult::get_motion_remainder() const{
  308. return result.remainder;
  309. }
  310. Vector2 Physics2DTestMotionResult::get_collision_point() const{
  311. return result.collision_point;
  312. }
  313. Vector2 Physics2DTestMotionResult::get_collision_normal() const{
  314. return result.collision_normal;
  315. }
  316. Vector2 Physics2DTestMotionResult::get_collider_velocity() const{
  317. return result.collider_velocity;
  318. }
  319. ObjectID Physics2DTestMotionResult::get_collider_id() const{
  320. return result.collider_id;
  321. }
  322. RID Physics2DTestMotionResult::get_collider_rid() const{
  323. return result.collider;
  324. }
  325. Object* Physics2DTestMotionResult::get_collider() const {
  326. return ObjectDB::get_instance(result.collider_id);
  327. }
  328. int Physics2DTestMotionResult::get_collider_shape() const{
  329. return result.collider_shape;
  330. }
  331. void Physics2DTestMotionResult::_bind_methods() {
  332. //ObjectTypeDB::bind_method(_MD("is_colliding"),&Physics2DTestMotionResult::is_colliding);
  333. ObjectTypeDB::bind_method(_MD("get_motion"),&Physics2DTestMotionResult::get_motion);
  334. ObjectTypeDB::bind_method(_MD("get_motion_remainder"),&Physics2DTestMotionResult::get_motion_remainder);
  335. ObjectTypeDB::bind_method(_MD("get_collision_point"),&Physics2DTestMotionResult::get_collision_point);
  336. ObjectTypeDB::bind_method(_MD("get_collision_normal"),&Physics2DTestMotionResult::get_collision_normal);
  337. ObjectTypeDB::bind_method(_MD("get_collider_velocity"),&Physics2DTestMotionResult::get_collider_velocity);
  338. ObjectTypeDB::bind_method(_MD("get_collider_id"),&Physics2DTestMotionResult::get_collider_id);
  339. ObjectTypeDB::bind_method(_MD("get_collider_rid"),&Physics2DTestMotionResult::get_collider_rid);
  340. ObjectTypeDB::bind_method(_MD("get_collider"),&Physics2DTestMotionResult::get_collider);
  341. ObjectTypeDB::bind_method(_MD("get_collider_shape"),&Physics2DTestMotionResult::get_collider_shape);
  342. }
  343. Physics2DTestMotionResult::Physics2DTestMotionResult(){
  344. colliding=false;
  345. result.collider_id=0;
  346. result.collider_shape=0;
  347. }
  348. ///////////////////////////////////////
  349. bool Physics2DServer::_body_test_motion(RID p_body,const Vector2& p_motion,float p_margin,const Ref<Physics2DTestMotionResult>& p_result) {
  350. MotionResult *r=NULL;
  351. if (p_result.is_valid())
  352. r=p_result->get_result_ptr();
  353. return body_test_motion(p_body,p_motion,p_margin,r);
  354. }
  355. void Physics2DServer::_bind_methods() {
  356. ObjectTypeDB::bind_method(_MD("shape_create","type"),&Physics2DServer::shape_create);
  357. ObjectTypeDB::bind_method(_MD("shape_set_data","shape","data"),&Physics2DServer::shape_set_data);
  358. ObjectTypeDB::bind_method(_MD("shape_get_type","shape"),&Physics2DServer::shape_get_type);
  359. ObjectTypeDB::bind_method(_MD("shape_get_data","shape"),&Physics2DServer::shape_get_data);
  360. ObjectTypeDB::bind_method(_MD("space_create"),&Physics2DServer::space_create);
  361. ObjectTypeDB::bind_method(_MD("space_set_active","space","active"),&Physics2DServer::space_set_active);
  362. ObjectTypeDB::bind_method(_MD("space_is_active","space"),&Physics2DServer::space_is_active);
  363. ObjectTypeDB::bind_method(_MD("space_set_param","space","param","value"),&Physics2DServer::space_set_param);
  364. ObjectTypeDB::bind_method(_MD("space_get_param","space","param"),&Physics2DServer::space_get_param);
  365. ObjectTypeDB::bind_method(_MD("space_get_direct_state:Physics2DDirectSpaceState","space"),&Physics2DServer::space_get_direct_state);
  366. ObjectTypeDB::bind_method(_MD("area_create"),&Physics2DServer::area_create);
  367. ObjectTypeDB::bind_method(_MD("area_set_space","area","space"),&Physics2DServer::area_set_space);
  368. ObjectTypeDB::bind_method(_MD("area_get_space","area"),&Physics2DServer::area_get_space);
  369. ObjectTypeDB::bind_method(_MD("area_set_space_override_mode","area","mode"),&Physics2DServer::area_set_space_override_mode);
  370. ObjectTypeDB::bind_method(_MD("area_get_space_override_mode","area"),&Physics2DServer::area_get_space_override_mode);
  371. ObjectTypeDB::bind_method(_MD("area_add_shape","area","shape","transform"),&Physics2DServer::area_add_shape,DEFVAL(Matrix32()));
  372. ObjectTypeDB::bind_method(_MD("area_set_shape","area","shape_idx","shape"),&Physics2DServer::area_set_shape);
  373. ObjectTypeDB::bind_method(_MD("area_set_shape_transform","area","shape_idx","transform"),&Physics2DServer::area_set_shape_transform);
  374. ObjectTypeDB::bind_method(_MD("area_get_shape_count","area"),&Physics2DServer::area_get_shape_count);
  375. ObjectTypeDB::bind_method(_MD("area_get_shape","area","shape_idx"),&Physics2DServer::area_get_shape);
  376. ObjectTypeDB::bind_method(_MD("area_get_shape_transform","area","shape_idx"),&Physics2DServer::area_get_shape_transform);
  377. ObjectTypeDB::bind_method(_MD("area_remove_shape","area","shape_idx"),&Physics2DServer::area_remove_shape);
  378. ObjectTypeDB::bind_method(_MD("area_clear_shapes","area"),&Physics2DServer::area_clear_shapes);
  379. ObjectTypeDB::bind_method(_MD("area_set_layer_mask","area","mask"),&Physics2DServer::area_set_layer_mask);
  380. ObjectTypeDB::bind_method(_MD("area_set_collision_mask","area","mask"),&Physics2DServer::area_set_collision_mask);
  381. ObjectTypeDB::bind_method(_MD("area_set_param","area","param","value"),&Physics2DServer::area_set_param);
  382. ObjectTypeDB::bind_method(_MD("area_set_transform","area","transform"),&Physics2DServer::area_set_transform);
  383. ObjectTypeDB::bind_method(_MD("area_get_param","area","param"),&Physics2DServer::area_get_param);
  384. ObjectTypeDB::bind_method(_MD("area_get_transform","area"),&Physics2DServer::area_get_transform);
  385. ObjectTypeDB::bind_method(_MD("area_attach_object_instance_ID","area","id"),&Physics2DServer::area_attach_object_instance_ID);
  386. ObjectTypeDB::bind_method(_MD("area_get_object_instance_ID","area"),&Physics2DServer::area_get_object_instance_ID);
  387. ObjectTypeDB::bind_method(_MD("area_set_monitor_callback","receiver","method"),&Physics2DServer::area_set_monitor_callback);
  388. ObjectTypeDB::bind_method(_MD("body_create","mode","init_sleeping"),&Physics2DServer::body_create,DEFVAL(BODY_MODE_RIGID),DEFVAL(false));
  389. ObjectTypeDB::bind_method(_MD("body_set_space","body","space"),&Physics2DServer::body_set_space);
  390. ObjectTypeDB::bind_method(_MD("body_get_space","body"),&Physics2DServer::body_get_space);
  391. ObjectTypeDB::bind_method(_MD("body_set_mode","body","mode"),&Physics2DServer::body_set_mode);
  392. ObjectTypeDB::bind_method(_MD("body_get_mode","body"),&Physics2DServer::body_get_mode);
  393. ObjectTypeDB::bind_method(_MD("body_add_shape","body","shape","transform"),&Physics2DServer::body_add_shape,DEFVAL(Matrix32()));
  394. ObjectTypeDB::bind_method(_MD("body_set_shape","body","shape_idx","shape"),&Physics2DServer::body_set_shape);
  395. ObjectTypeDB::bind_method(_MD("body_set_shape_transform","body","shape_idx","transform"),&Physics2DServer::body_set_shape_transform);
  396. ObjectTypeDB::bind_method(_MD("body_set_shape_metadata","body","shape_idx","metadata"),&Physics2DServer::body_set_shape_metadata);
  397. ObjectTypeDB::bind_method(_MD("body_get_shape_count","body"),&Physics2DServer::body_get_shape_count);
  398. ObjectTypeDB::bind_method(_MD("body_get_shape","body","shape_idx"),&Physics2DServer::body_get_shape);
  399. ObjectTypeDB::bind_method(_MD("body_get_shape_transform","body","shape_idx"),&Physics2DServer::body_get_shape_transform);
  400. ObjectTypeDB::bind_method(_MD("body_get_shape_metadata","body","shape_idx"),&Physics2DServer::body_get_shape_metadata);
  401. ObjectTypeDB::bind_method(_MD("body_remove_shape","body","shape_idx"),&Physics2DServer::body_remove_shape);
  402. ObjectTypeDB::bind_method(_MD("body_clear_shapes","body"),&Physics2DServer::body_clear_shapes);
  403. ObjectTypeDB::bind_method(_MD("body_set_shape_as_trigger","body","shape_idx","enable"),&Physics2DServer::body_set_shape_as_trigger);
  404. ObjectTypeDB::bind_method(_MD("body_is_shape_set_as_trigger","body","shape_idx"),&Physics2DServer::body_is_shape_set_as_trigger);
  405. ObjectTypeDB::bind_method(_MD("body_attach_object_instance_ID","body","id"),&Physics2DServer::body_attach_object_instance_ID);
  406. ObjectTypeDB::bind_method(_MD("body_get_object_instance_ID","body"),&Physics2DServer::body_get_object_instance_ID);
  407. ObjectTypeDB::bind_method(_MD("body_set_continuous_collision_detection_mode","body","mode"),&Physics2DServer::body_set_continuous_collision_detection_mode);
  408. ObjectTypeDB::bind_method(_MD("body_get_continuous_collision_detection_mode","body"),&Physics2DServer::body_get_continuous_collision_detection_mode);
  409. ObjectTypeDB::bind_method(_MD("body_set_layer_mask","body","mask"),&Physics2DServer::body_set_layer_mask);
  410. ObjectTypeDB::bind_method(_MD("body_get_layer_mask","body"),&Physics2DServer::body_get_layer_mask);
  411. ObjectTypeDB::bind_method(_MD("body_set_collision_mask","body","mask"),&Physics2DServer::body_set_collision_mask);
  412. ObjectTypeDB::bind_method(_MD("body_get_collision_mask","body"),&Physics2DServer::body_get_collision_mask);
  413. ObjectTypeDB::bind_method(_MD("body_set_param","body","param","value"),&Physics2DServer::body_set_param);
  414. ObjectTypeDB::bind_method(_MD("body_get_param","body","param"),&Physics2DServer::body_get_param);
  415. ObjectTypeDB::bind_method(_MD("body_set_state","body","state","value"),&Physics2DServer::body_set_state);
  416. ObjectTypeDB::bind_method(_MD("body_get_state","body","state"),&Physics2DServer::body_get_state);
  417. ObjectTypeDB::bind_method(_MD("body_apply_impulse","body","pos","impulse"),&Physics2DServer::body_apply_impulse);
  418. ObjectTypeDB::bind_method(_MD("body_set_axis_velocity","body","axis_velocity"),&Physics2DServer::body_set_axis_velocity);
  419. ObjectTypeDB::bind_method(_MD("body_add_collision_exception","body","excepted_body"),&Physics2DServer::body_add_collision_exception);
  420. ObjectTypeDB::bind_method(_MD("body_remove_collision_exception","body","excepted_body"),&Physics2DServer::body_remove_collision_exception);
  421. // virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions)=0;
  422. ObjectTypeDB::bind_method(_MD("body_set_max_contacts_reported","body","amount"),&Physics2DServer::body_set_max_contacts_reported);
  423. ObjectTypeDB::bind_method(_MD("body_get_max_contacts_reported","body"),&Physics2DServer::body_get_max_contacts_reported);
  424. ObjectTypeDB::bind_method(_MD("body_set_one_way_collision_direction","normal"),&Physics2DServer::body_set_one_way_collision_direction);
  425. ObjectTypeDB::bind_method(_MD("body_get_one_way_collision_direction"),&Physics2DServer::body_get_one_way_collision_direction);
  426. ObjectTypeDB::bind_method(_MD("body_set_one_way_collision_max_depth","normal"),&Physics2DServer::body_set_one_way_collision_max_depth);
  427. ObjectTypeDB::bind_method(_MD("body_get_one_way_collision_max_depth"),&Physics2DServer::body_get_one_way_collision_max_depth);
  428. ObjectTypeDB::bind_method(_MD("body_set_omit_force_integration","body","enable"),&Physics2DServer::body_set_omit_force_integration);
  429. ObjectTypeDB::bind_method(_MD("body_is_omitting_force_integration","body"),&Physics2DServer::body_is_omitting_force_integration);
  430. ObjectTypeDB::bind_method(_MD("body_set_force_integration_callback","body","receiver","method"),&Physics2DServer::body_set_force_integration_callback);
  431. ObjectTypeDB::bind_method(_MD("body_test_motion","body","motion","margin","result:Physics2DTestMotionResult"),&Physics2DServer::_body_test_motion,DEFVAL(0.08),DEFVAL(Variant()));
  432. /* JOINT API */
  433. ObjectTypeDB::bind_method(_MD("joint_set_param","joint","param","value"),&Physics2DServer::joint_set_param);
  434. ObjectTypeDB::bind_method(_MD("joint_get_param","joint","param"),&Physics2DServer::joint_get_param);
  435. ObjectTypeDB::bind_method(_MD("pin_joint_create","anchor","body_a","body_b"),&Physics2DServer::pin_joint_create,DEFVAL(RID()));
  436. ObjectTypeDB::bind_method(_MD("groove_joint_create","groove1_a","groove2_a","anchor_b","body_a","body_b"),&Physics2DServer::groove_joint_create,DEFVAL(RID()),DEFVAL(RID()));
  437. ObjectTypeDB::bind_method(_MD("damped_spring_joint_create","anchor_a","anchor_b","body_a","body_b"),&Physics2DServer::damped_spring_joint_create,DEFVAL(RID()));
  438. ObjectTypeDB::bind_method(_MD("damped_string_joint_set_param","joint","param","value"),&Physics2DServer::damped_string_joint_set_param,DEFVAL(RID()));
  439. ObjectTypeDB::bind_method(_MD("damped_string_joint_get_param","joint","param"),&Physics2DServer::damped_string_joint_get_param);
  440. ObjectTypeDB::bind_method(_MD("joint_get_type","joint"),&Physics2DServer::joint_get_type);
  441. ObjectTypeDB::bind_method(_MD("free_rid","rid"),&Physics2DServer::free);
  442. ObjectTypeDB::bind_method(_MD("set_active","active"),&Physics2DServer::set_active);
  443. ObjectTypeDB::bind_method(_MD("get_process_info"),&Physics2DServer::get_process_info);
  444. // ObjectTypeDB::bind_method(_MD("init"),&Physics2DServer::init);
  445. // ObjectTypeDB::bind_method(_MD("step"),&Physics2DServer::step);
  446. // ObjectTypeDB::bind_method(_MD("sync"),&Physics2DServer::sync);
  447. //ObjectTypeDB::bind_method(_MD("flush_queries"),&Physics2DServer::flush_queries);
  448. BIND_CONSTANT( SHAPE_LINE );
  449. BIND_CONSTANT( SHAPE_SEGMENT );
  450. BIND_CONSTANT( SHAPE_CIRCLE );
  451. BIND_CONSTANT( SHAPE_RECTANGLE );
  452. BIND_CONSTANT( SHAPE_CAPSULE );
  453. BIND_CONSTANT( SHAPE_CONVEX_POLYGON );
  454. BIND_CONSTANT( SHAPE_CONCAVE_POLYGON );
  455. BIND_CONSTANT( SHAPE_CUSTOM );
  456. BIND_CONSTANT( AREA_PARAM_GRAVITY );
  457. BIND_CONSTANT( AREA_PARAM_GRAVITY_VECTOR );
  458. BIND_CONSTANT( AREA_PARAM_GRAVITY_IS_POINT );
  459. BIND_CONSTANT( AREA_PARAM_GRAVITY_POINT_ATTENUATION );
  460. BIND_CONSTANT( AREA_PARAM_LINEAR_DAMP);
  461. BIND_CONSTANT( AREA_PARAM_ANGULAR_DAMP);
  462. BIND_CONSTANT( AREA_PARAM_PRIORITY );
  463. BIND_CONSTANT( AREA_SPACE_OVERRIDE_COMBINE );
  464. BIND_CONSTANT( AREA_SPACE_OVERRIDE_DISABLED );
  465. BIND_CONSTANT( AREA_SPACE_OVERRIDE_REPLACE );
  466. BIND_CONSTANT( BODY_MODE_STATIC );
  467. BIND_CONSTANT( BODY_MODE_KINEMATIC );
  468. BIND_CONSTANT( BODY_MODE_RIGID );
  469. BIND_CONSTANT( BODY_MODE_CHARACTER );
  470. BIND_CONSTANT( BODY_PARAM_BOUNCE );
  471. BIND_CONSTANT( BODY_PARAM_FRICTION );
  472. BIND_CONSTANT( BODY_PARAM_MASS );
  473. BIND_CONSTANT( BODY_PARAM_GRAVITY_SCALE );
  474. BIND_CONSTANT( BODY_PARAM_LINEAR_DAMP);
  475. BIND_CONSTANT( BODY_PARAM_ANGULAR_DAMP);
  476. BIND_CONSTANT( BODY_PARAM_MAX );
  477. BIND_CONSTANT( BODY_STATE_TRANSFORM );
  478. BIND_CONSTANT( BODY_STATE_LINEAR_VELOCITY );
  479. BIND_CONSTANT( BODY_STATE_ANGULAR_VELOCITY );
  480. BIND_CONSTANT( BODY_STATE_SLEEPING );
  481. BIND_CONSTANT( BODY_STATE_CAN_SLEEP );
  482. BIND_CONSTANT( JOINT_PIN );
  483. BIND_CONSTANT( JOINT_GROOVE );
  484. BIND_CONSTANT( JOINT_DAMPED_SPRING );
  485. BIND_CONSTANT( DAMPED_STRING_REST_LENGTH );
  486. BIND_CONSTANT( DAMPED_STRING_STIFFNESS );
  487. BIND_CONSTANT( DAMPED_STRING_DAMPING );
  488. BIND_CONSTANT( CCD_MODE_DISABLED );
  489. BIND_CONSTANT( CCD_MODE_CAST_RAY );
  490. BIND_CONSTANT( CCD_MODE_CAST_SHAPE );
  491. // BIND_CONSTANT( TYPE_BODY );
  492. // BIND_CONSTANT( TYPE_AREA );
  493. BIND_CONSTANT( AREA_BODY_ADDED );
  494. BIND_CONSTANT( AREA_BODY_REMOVED );
  495. BIND_CONSTANT( INFO_ACTIVE_OBJECTS );
  496. BIND_CONSTANT( INFO_COLLISION_PAIRS );
  497. BIND_CONSTANT( INFO_ISLAND_COUNT );
  498. }
  499. Physics2DServer::Physics2DServer() {
  500. ERR_FAIL_COND( singleton!=NULL );
  501. singleton=this;
  502. }
  503. Physics2DServer::~Physics2DServer() {
  504. singleton=NULL;
  505. }