physics_body.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. /*************************************************************************/
  2. /* physics_body.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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_body.h"
  30. #include "scene/scene_string_names.h"
  31. void PhysicsBody::_notification(int p_what) {
  32. /*
  33. switch(p_what) {
  34. case NOTIFICATION_TRANSFORM_CHANGED: {
  35. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_TRANSFORM,get_global_transform());
  36. } break;
  37. }
  38. */
  39. }
  40. Vector3 PhysicsBody::get_linear_velocity() const {
  41. return Vector3();
  42. }
  43. Vector3 PhysicsBody::get_angular_velocity() const {
  44. return Vector3();
  45. }
  46. float PhysicsBody::get_inverse_mass() const {
  47. return 0;
  48. }
  49. void PhysicsBody::set_layer_mask(uint32_t p_mask) {
  50. layer_mask=p_mask;
  51. PhysicsServer::get_singleton()->body_set_layer_mask(get_rid(),p_mask);
  52. }
  53. uint32_t PhysicsBody::get_layer_mask() const {
  54. return layer_mask;
  55. }
  56. void PhysicsBody::add_collision_exception_with(Node* p_node) {
  57. ERR_FAIL_NULL(p_node);
  58. PhysicsBody *physics_body = p_node->cast_to<PhysicsBody>();
  59. if (!physics_body) {
  60. ERR_EXPLAIN("Collision exception only works between two objects of PhysicsBody type");
  61. }
  62. ERR_FAIL_COND(!physics_body);
  63. PhysicsServer::get_singleton()->body_add_collision_exception(get_rid(),physics_body->get_rid());
  64. }
  65. void PhysicsBody::remove_collision_exception_with(Node* p_node) {
  66. ERR_FAIL_NULL(p_node);
  67. PhysicsBody *physics_body = p_node->cast_to<PhysicsBody>();
  68. if (!physics_body) {
  69. ERR_EXPLAIN("Collision exception only works between two objects of PhysicsBody type");
  70. }
  71. ERR_FAIL_COND(!physics_body);
  72. PhysicsServer::get_singleton()->body_remove_collision_exception(get_rid(),physics_body->get_rid());
  73. }
  74. void PhysicsBody::_bind_methods() {
  75. ObjectTypeDB::bind_method(_MD("set_layer_mask","mask"),&PhysicsBody::set_layer_mask);
  76. ObjectTypeDB::bind_method(_MD("get_layer_mask"),&PhysicsBody::get_layer_mask);
  77. ADD_PROPERTY(PropertyInfo(Variant::INT,"layers",PROPERTY_HINT_ALL_FLAGS),_SCS("set_layer_mask"),_SCS("get_layer_mask"));
  78. }
  79. PhysicsBody::PhysicsBody(PhysicsServer::BodyMode p_mode) : CollisionObject( PhysicsServer::get_singleton()->body_create(p_mode), false) {
  80. layer_mask=1;
  81. }
  82. void StaticBody::set_friction(real_t p_friction){
  83. ERR_FAIL_COND(p_friction<0 || p_friction>1);
  84. friction=p_friction;
  85. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_FRICTION,friction);
  86. }
  87. real_t StaticBody::get_friction() const{
  88. return friction;
  89. }
  90. void StaticBody::set_bounce(real_t p_bounce){
  91. ERR_FAIL_COND(p_bounce<0 || p_bounce>1);
  92. bounce=p_bounce;
  93. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_BOUNCE,bounce);
  94. }
  95. real_t StaticBody::get_bounce() const{
  96. return bounce;
  97. }
  98. void StaticBody::set_constant_linear_velocity(const Vector3& p_vel) {
  99. constant_linear_velocity=p_vel;
  100. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_LINEAR_VELOCITY,constant_linear_velocity);
  101. }
  102. void StaticBody::set_constant_angular_velocity(const Vector3& p_vel) {
  103. constant_angular_velocity=p_vel;
  104. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_ANGULAR_VELOCITY,constant_angular_velocity);
  105. }
  106. Vector3 StaticBody::get_constant_linear_velocity() const {
  107. return constant_linear_velocity;
  108. }
  109. Vector3 StaticBody::get_constant_angular_velocity() const {
  110. return constant_angular_velocity;
  111. }
  112. void StaticBody::_bind_methods() {
  113. ObjectTypeDB::bind_method(_MD("set_constant_linear_velocity","vel"),&StaticBody::set_constant_linear_velocity);
  114. ObjectTypeDB::bind_method(_MD("set_constant_angular_velocity","vel"),&StaticBody::set_constant_angular_velocity);
  115. ObjectTypeDB::bind_method(_MD("get_constant_linear_velocity"),&StaticBody::get_constant_linear_velocity);
  116. ObjectTypeDB::bind_method(_MD("get_constant_angular_velocity"),&StaticBody::get_constant_angular_velocity);
  117. ObjectTypeDB::bind_method(_MD("set_friction","friction"),&StaticBody::set_friction);
  118. ObjectTypeDB::bind_method(_MD("get_friction"),&StaticBody::get_friction);
  119. ObjectTypeDB::bind_method(_MD("set_bounce","bounce"),&StaticBody::set_bounce);
  120. ObjectTypeDB::bind_method(_MD("get_bounce"),&StaticBody::get_bounce);
  121. ObjectTypeDB::bind_method(_MD("add_collision_exception_with","body:PhysicsBody"),&PhysicsBody::add_collision_exception_with);
  122. ObjectTypeDB::bind_method(_MD("remove_collision_exception_with","body:PhysicsBody"),&PhysicsBody::remove_collision_exception_with);
  123. ADD_PROPERTY( PropertyInfo(Variant::REAL,"friction",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_friction"),_SCS("get_friction"));
  124. ADD_PROPERTY( PropertyInfo(Variant::REAL,"bounce",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_bounce"),_SCS("get_bounce"));
  125. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3,"constant_linear_velocity"),_SCS("set_constant_linear_velocity"),_SCS("get_constant_linear_velocity"));
  126. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3,"constant_angular_velocity"),_SCS("set_constant_angular_velocity"),_SCS("get_constant_angular_velocity"));
  127. }
  128. StaticBody::StaticBody() : PhysicsBody(PhysicsServer::BODY_MODE_STATIC) {
  129. bounce=0;
  130. friction=1;
  131. }
  132. StaticBody::~StaticBody() {
  133. }
  134. void RigidBody::_body_enter_tree(ObjectID p_id) {
  135. Object *obj = ObjectDB::get_instance(p_id);
  136. Node *node = obj ? obj->cast_to<Node>() : NULL;
  137. ERR_FAIL_COND(!node);
  138. Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.find(p_id);
  139. ERR_FAIL_COND(!E);
  140. ERR_FAIL_COND(E->get().in_tree);
  141. E->get().in_tree=true;
  142. contact_monitor->locked=true;
  143. emit_signal(SceneStringNames::get_singleton()->body_enter,node);
  144. for(int i=0;i<E->get().shapes.size();i++) {
  145. emit_signal(SceneStringNames::get_singleton()->body_enter_shape,p_id,node,E->get().shapes[i].body_shape,E->get().shapes[i].local_shape);
  146. }
  147. contact_monitor->locked=false;
  148. }
  149. void RigidBody::_body_exit_tree(ObjectID p_id) {
  150. Object *obj = ObjectDB::get_instance(p_id);
  151. Node *node = obj ? obj->cast_to<Node>() : NULL;
  152. ERR_FAIL_COND(!node);
  153. Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.find(p_id);
  154. ERR_FAIL_COND(!E);
  155. ERR_FAIL_COND(!E->get().in_tree);
  156. E->get().in_tree=false;
  157. contact_monitor->locked=true;
  158. emit_signal(SceneStringNames::get_singleton()->body_exit,node);
  159. for(int i=0;i<E->get().shapes.size();i++) {
  160. emit_signal(SceneStringNames::get_singleton()->body_exit_shape,p_id,node,E->get().shapes[i].body_shape,E->get().shapes[i].local_shape);
  161. }
  162. contact_monitor->locked=false;
  163. }
  164. void RigidBody::_body_inout(int p_status, ObjectID p_instance, int p_body_shape,int p_local_shape) {
  165. bool body_in = p_status==1;
  166. ObjectID objid=p_instance;
  167. Object *obj = ObjectDB::get_instance(objid);
  168. Node *node = obj ? obj->cast_to<Node>() : NULL;
  169. Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.find(objid);
  170. ERR_FAIL_COND(!body_in && !E);
  171. if (body_in) {
  172. if (!E) {
  173. E = contact_monitor->body_map.insert(objid,BodyState());
  174. //E->get().rc=0;
  175. E->get().in_tree=node && node->is_inside_tree();
  176. if (node) {
  177. node->connect(SceneStringNames::get_singleton()->enter_tree,this,SceneStringNames::get_singleton()->_body_enter_tree,make_binds(objid));
  178. node->connect(SceneStringNames::get_singleton()->exit_tree,this,SceneStringNames::get_singleton()->_body_exit_tree,make_binds(objid));
  179. if (E->get().in_tree) {
  180. emit_signal(SceneStringNames::get_singleton()->body_enter,node);
  181. }
  182. }
  183. }
  184. //E->get().rc++;
  185. if (node)
  186. E->get().shapes.insert(ShapePair(p_body_shape,p_local_shape));
  187. if (E->get().in_tree) {
  188. emit_signal(SceneStringNames::get_singleton()->body_enter_shape,objid,node,p_body_shape,p_local_shape);
  189. }
  190. } else {
  191. //E->get().rc--;
  192. if (node)
  193. E->get().shapes.erase(ShapePair(p_body_shape,p_local_shape));
  194. bool in_tree = E->get().in_tree;
  195. if (E->get().shapes.empty()) {
  196. if (node) {
  197. node->disconnect(SceneStringNames::get_singleton()->enter_tree,this,SceneStringNames::get_singleton()->_body_enter_tree);
  198. node->disconnect(SceneStringNames::get_singleton()->exit_tree,this,SceneStringNames::get_singleton()->_body_exit_tree);
  199. if (in_tree)
  200. emit_signal(SceneStringNames::get_singleton()->body_exit,obj);
  201. }
  202. contact_monitor->body_map.erase(E);
  203. }
  204. if (node && in_tree) {
  205. emit_signal(SceneStringNames::get_singleton()->body_exit_shape,objid,obj,p_body_shape,p_local_shape);
  206. }
  207. }
  208. }
  209. struct _RigidBodyInOut {
  210. ObjectID id;
  211. int shape;
  212. int local_shape;
  213. };
  214. void RigidBody::_direct_state_changed(Object *p_state) {
  215. //eh.. fuck
  216. #ifdef DEBUG_ENABLED
  217. state=p_state->cast_to<PhysicsDirectBodyState>();
  218. #else
  219. state=(PhysicsDirectBodyState*)p_state; //trust it
  220. #endif
  221. set_ignore_transform_notification(true);
  222. set_global_transform(state->get_transform());
  223. linear_velocity=state->get_linear_velocity();
  224. angular_velocity=state->get_angular_velocity();
  225. if(sleeping!=state->is_sleeping()) {
  226. sleeping=state->is_sleeping();
  227. emit_signal(SceneStringNames::get_singleton()->sleeping_state_changed);
  228. }
  229. if (get_script_instance())
  230. get_script_instance()->call("_integrate_forces",state);
  231. set_ignore_transform_notification(false);
  232. if (contact_monitor) {
  233. contact_monitor->locked=true;
  234. //untag all
  235. int rc=0;
  236. for( Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.front();E;E=E->next()) {
  237. for(int i=0;i<E->get().shapes.size();i++) {
  238. E->get().shapes[i].tagged=false;
  239. rc++;
  240. }
  241. }
  242. _RigidBodyInOut *toadd=(_RigidBodyInOut*)alloca(state->get_contact_count()*sizeof(_RigidBodyInOut));
  243. int toadd_count=0;//state->get_contact_count();
  244. RigidBody_RemoveAction *toremove=(RigidBody_RemoveAction*)alloca(rc*sizeof(RigidBody_RemoveAction));
  245. int toremove_count=0;
  246. //put the ones to add
  247. for(int i=0;i<state->get_contact_count();i++) {
  248. ObjectID obj = state->get_contact_collider_id(i);
  249. int local_shape = state->get_contact_local_shape(i);
  250. int shape = state->get_contact_collider_shape(i);
  251. // bool found=false;
  252. Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.find(obj);
  253. if (!E) {
  254. toadd[toadd_count].local_shape=local_shape;
  255. toadd[toadd_count].id=obj;
  256. toadd[toadd_count].shape=shape;
  257. toadd_count++;
  258. continue;
  259. }
  260. ShapePair sp( shape,local_shape );
  261. int idx = E->get().shapes.find(sp);
  262. if (idx==-1) {
  263. toadd[toadd_count].local_shape=local_shape;
  264. toadd[toadd_count].id=obj;
  265. toadd[toadd_count].shape=shape;
  266. toadd_count++;
  267. continue;
  268. }
  269. E->get().shapes[idx].tagged=true;
  270. }
  271. //put the ones to remove
  272. for( Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.front();E;E=E->next()) {
  273. for(int i=0;i<E->get().shapes.size();i++) {
  274. if (!E->get().shapes[i].tagged) {
  275. toremove[toremove_count].body_id=E->key();
  276. toremove[toremove_count].pair=E->get().shapes[i];
  277. toremove_count++;
  278. }
  279. }
  280. }
  281. //process remotions
  282. for(int i=0;i<toremove_count;i++) {
  283. _body_inout(0,toremove[i].body_id,toremove[i].pair.body_shape,toremove[i].pair.local_shape);
  284. }
  285. //process aditions
  286. for(int i=0;i<toadd_count;i++) {
  287. _body_inout(1,toadd[i].id,toadd[i].shape,toadd[i].local_shape);
  288. }
  289. contact_monitor->locked=false;
  290. }
  291. state=NULL;
  292. }
  293. void RigidBody::_notification(int p_what) {
  294. }
  295. void RigidBody::set_mode(Mode p_mode) {
  296. mode=p_mode;
  297. switch(p_mode) {
  298. case MODE_RIGID: {
  299. PhysicsServer::get_singleton()->body_set_mode(get_rid(),PhysicsServer::BODY_MODE_RIGID);
  300. } break;
  301. case MODE_STATIC: {
  302. PhysicsServer::get_singleton()->body_set_mode(get_rid(),PhysicsServer::BODY_MODE_STATIC);
  303. } break;
  304. case MODE_CHARACTER: {
  305. PhysicsServer::get_singleton()->body_set_mode(get_rid(),PhysicsServer::BODY_MODE_CHARACTER);
  306. } break;
  307. case MODE_KINEMATIC: {
  308. PhysicsServer::get_singleton()->body_set_mode(get_rid(),PhysicsServer::BODY_MODE_KINEMATIC);
  309. } break;
  310. }
  311. }
  312. RigidBody::Mode RigidBody::get_mode() const{
  313. return mode;
  314. }
  315. void RigidBody::set_mass(real_t p_mass){
  316. ERR_FAIL_COND(p_mass<=0);
  317. mass=p_mass;
  318. _change_notify("mass");
  319. _change_notify("weight");
  320. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_MASS,mass);
  321. }
  322. real_t RigidBody::get_mass() const{
  323. return mass;
  324. }
  325. void RigidBody::set_weight(real_t p_weight){
  326. set_mass(p_weight/9.8);
  327. }
  328. real_t RigidBody::get_weight() const{
  329. return mass*9.8;
  330. }
  331. void RigidBody::set_friction(real_t p_friction){
  332. ERR_FAIL_COND(p_friction<0 || p_friction>1);
  333. friction=p_friction;
  334. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_FRICTION,friction);
  335. }
  336. real_t RigidBody::get_friction() const{
  337. return friction;
  338. }
  339. void RigidBody::set_bounce(real_t p_bounce){
  340. ERR_FAIL_COND(p_bounce<0 || p_bounce>1);
  341. bounce=p_bounce;
  342. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_BOUNCE,bounce);
  343. }
  344. real_t RigidBody::get_bounce() const{
  345. return bounce;
  346. }
  347. void RigidBody::set_gravity_scale(real_t p_gravity_scale){
  348. gravity_scale=p_gravity_scale;
  349. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_GRAVITY_SCALE,gravity_scale);
  350. }
  351. real_t RigidBody::get_gravity_scale() const{
  352. return gravity_scale;
  353. }
  354. void RigidBody::set_linear_damp(real_t p_linear_damp){
  355. ERR_FAIL_COND(p_linear_damp<-1);
  356. linear_damp=p_linear_damp;
  357. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_LINEAR_DAMP,linear_damp);
  358. }
  359. real_t RigidBody::get_linear_damp() const{
  360. return linear_damp;
  361. }
  362. void RigidBody::set_angular_damp(real_t p_angular_damp){
  363. ERR_FAIL_COND(p_angular_damp<-1);
  364. angular_damp=p_angular_damp;
  365. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_ANGULAR_DAMP,angular_damp);
  366. }
  367. real_t RigidBody::get_angular_damp() const{
  368. return angular_damp;
  369. }
  370. void RigidBody::set_axis_velocity(const Vector3& p_axis) {
  371. Vector3 v = state? state->get_linear_velocity() : linear_velocity;
  372. Vector3 axis = p_axis.normalized();
  373. v-=axis*axis.dot(v);
  374. v+=p_axis;
  375. if (state) {
  376. set_linear_velocity(v);
  377. } else {
  378. PhysicsServer::get_singleton()->body_set_axis_velocity(get_rid(),p_axis);
  379. linear_velocity=v;
  380. }
  381. }
  382. void RigidBody::set_linear_velocity(const Vector3& p_velocity){
  383. linear_velocity=p_velocity;
  384. if (state)
  385. state->set_linear_velocity(linear_velocity);
  386. else
  387. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_LINEAR_VELOCITY,linear_velocity);
  388. }
  389. Vector3 RigidBody::get_linear_velocity() const{
  390. return linear_velocity;
  391. }
  392. void RigidBody::set_angular_velocity(const Vector3& p_velocity){
  393. angular_velocity=p_velocity;
  394. if (state)
  395. state->set_angular_velocity(angular_velocity);
  396. else
  397. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_ANGULAR_VELOCITY,angular_velocity);
  398. }
  399. Vector3 RigidBody::get_angular_velocity() const{
  400. return angular_velocity;
  401. }
  402. void RigidBody::set_use_custom_integrator(bool p_enable){
  403. if (custom_integrator==p_enable)
  404. return;
  405. custom_integrator=p_enable;
  406. PhysicsServer::get_singleton()->body_set_omit_force_integration(get_rid(),p_enable);
  407. }
  408. bool RigidBody::is_using_custom_integrator(){
  409. return custom_integrator;
  410. }
  411. void RigidBody::set_sleeping(bool p_sleeping) {
  412. sleeping=p_sleeping;
  413. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_SLEEPING,sleeping);
  414. }
  415. void RigidBody::set_can_sleep(bool p_active) {
  416. can_sleep=p_active;
  417. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_CAN_SLEEP,p_active);
  418. }
  419. bool RigidBody::is_able_to_sleep() const {
  420. return can_sleep;
  421. }
  422. bool RigidBody::is_sleeping() const {
  423. return sleeping;
  424. }
  425. void RigidBody::set_max_contacts_reported(int p_amount) {
  426. max_contacts_reported=p_amount;
  427. PhysicsServer::get_singleton()->body_set_max_contacts_reported(get_rid(),p_amount);
  428. }
  429. int RigidBody::get_max_contacts_reported() const{
  430. return max_contacts_reported;
  431. }
  432. void RigidBody::apply_impulse(const Vector3& p_pos, const Vector3& p_impulse) {
  433. PhysicsServer::get_singleton()->body_apply_impulse(get_rid(),p_pos,p_impulse);
  434. }
  435. void RigidBody::set_use_continuous_collision_detection(bool p_enable) {
  436. ccd=p_enable;
  437. PhysicsServer::get_singleton()->body_set_enable_continuous_collision_detection(get_rid(),p_enable);
  438. }
  439. bool RigidBody::is_using_continuous_collision_detection() const {
  440. return ccd;
  441. }
  442. void RigidBody::set_contact_monitor(bool p_enabled) {
  443. if (p_enabled==is_contact_monitor_enabled())
  444. return;
  445. if (!p_enabled) {
  446. if (contact_monitor->locked) {
  447. ERR_EXPLAIN("Can't disable contact monitoring during in/out callback. Use call_deferred(\"set_contact_monitor\",false) instead");
  448. }
  449. ERR_FAIL_COND(contact_monitor->locked);
  450. for(Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.front();E;E=E->next()) {
  451. //clean up mess
  452. }
  453. memdelete( contact_monitor );
  454. contact_monitor=NULL;
  455. } else {
  456. contact_monitor = memnew( ContactMonitor );
  457. contact_monitor->locked=false;
  458. }
  459. }
  460. bool RigidBody::is_contact_monitor_enabled() const {
  461. return contact_monitor!=NULL;
  462. }
  463. void RigidBody::set_axis_lock(AxisLock p_lock) {
  464. axis_lock=p_lock;
  465. PhysicsServer::get_singleton()->body_set_axis_lock(get_rid(),PhysicsServer::BodyAxisLock(axis_lock));
  466. }
  467. RigidBody::AxisLock RigidBody::get_axis_lock() const {
  468. return axis_lock;
  469. }
  470. Array RigidBody::get_colliding_bodies() const {
  471. ERR_FAIL_COND_V(!contact_monitor,Array());
  472. Array ret;
  473. ret.resize(contact_monitor->body_map.size());
  474. int idx=0;
  475. for (const Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.front();E;E=E->next()) {
  476. Object *obj = ObjectDB::get_instance(E->key());
  477. if (!obj) {
  478. ret.resize( ret.size() -1 ); //ops
  479. } else {
  480. ret[idx++]=obj;
  481. }
  482. }
  483. return ret;
  484. }
  485. void RigidBody::_bind_methods() {
  486. ObjectTypeDB::bind_method(_MD("set_mode","mode"),&RigidBody::set_mode);
  487. ObjectTypeDB::bind_method(_MD("get_mode"),&RigidBody::get_mode);
  488. ObjectTypeDB::bind_method(_MD("set_mass","mass"),&RigidBody::set_mass);
  489. ObjectTypeDB::bind_method(_MD("get_mass"),&RigidBody::get_mass);
  490. ObjectTypeDB::bind_method(_MD("set_weight","weight"),&RigidBody::set_weight);
  491. ObjectTypeDB::bind_method(_MD("get_weight"),&RigidBody::get_weight);
  492. ObjectTypeDB::bind_method(_MD("set_friction","friction"),&RigidBody::set_friction);
  493. ObjectTypeDB::bind_method(_MD("get_friction"),&RigidBody::get_friction);
  494. ObjectTypeDB::bind_method(_MD("set_bounce","bounce"),&RigidBody::set_bounce);
  495. ObjectTypeDB::bind_method(_MD("get_bounce"),&RigidBody::get_bounce);
  496. ObjectTypeDB::bind_method(_MD("set_linear_velocity","linear_velocity"),&RigidBody::set_linear_velocity);
  497. ObjectTypeDB::bind_method(_MD("get_linear_velocity"),&RigidBody::get_linear_velocity);
  498. ObjectTypeDB::bind_method(_MD("set_angular_velocity","angular_velocity"),&RigidBody::set_angular_velocity);
  499. ObjectTypeDB::bind_method(_MD("get_angular_velocity"),&RigidBody::get_angular_velocity);
  500. ObjectTypeDB::bind_method(_MD("set_gravity_scale","gravity_scale"),&RigidBody::set_gravity_scale);
  501. ObjectTypeDB::bind_method(_MD("get_gravity_scale"),&RigidBody::get_gravity_scale);
  502. ObjectTypeDB::bind_method(_MD("set_linear_damp","linear_damp"),&RigidBody::set_linear_damp);
  503. ObjectTypeDB::bind_method(_MD("get_linear_damp"),&RigidBody::get_linear_damp);
  504. ObjectTypeDB::bind_method(_MD("set_angular_damp","angular_damp"),&RigidBody::set_angular_damp);
  505. ObjectTypeDB::bind_method(_MD("get_angular_damp"),&RigidBody::get_angular_damp);
  506. ObjectTypeDB::bind_method(_MD("set_max_contacts_reported","amount"),&RigidBody::set_max_contacts_reported);
  507. ObjectTypeDB::bind_method(_MD("get_max_contacts_reported"),&RigidBody::get_max_contacts_reported);
  508. ObjectTypeDB::bind_method(_MD("set_use_custom_integrator","enable"),&RigidBody::set_use_custom_integrator);
  509. ObjectTypeDB::bind_method(_MD("is_using_custom_integrator"),&RigidBody::is_using_custom_integrator);
  510. ObjectTypeDB::bind_method(_MD("set_contact_monitor","enabled"),&RigidBody::set_contact_monitor);
  511. ObjectTypeDB::bind_method(_MD("is_contact_monitor_enabled"),&RigidBody::is_contact_monitor_enabled);
  512. ObjectTypeDB::bind_method(_MD("set_use_continuous_collision_detection","enable"),&RigidBody::set_use_continuous_collision_detection);
  513. ObjectTypeDB::bind_method(_MD("is_using_continuous_collision_detection"),&RigidBody::is_using_continuous_collision_detection);
  514. ObjectTypeDB::bind_method(_MD("set_axis_velocity","axis_velocity"),&RigidBody::set_axis_velocity);
  515. ObjectTypeDB::bind_method(_MD("apply_impulse","pos","impulse"),&RigidBody::apply_impulse);
  516. ObjectTypeDB::bind_method(_MD("set_sleeping","sleeping"),&RigidBody::set_sleeping);
  517. ObjectTypeDB::bind_method(_MD("is_sleeping"),&RigidBody::is_sleeping);
  518. ObjectTypeDB::bind_method(_MD("set_can_sleep","able_to_sleep"),&RigidBody::set_can_sleep);
  519. ObjectTypeDB::bind_method(_MD("is_able_to_sleep"),&RigidBody::is_able_to_sleep);
  520. ObjectTypeDB::bind_method(_MD("_direct_state_changed"),&RigidBody::_direct_state_changed);
  521. ObjectTypeDB::bind_method(_MD("_body_enter_tree"),&RigidBody::_body_enter_tree);
  522. ObjectTypeDB::bind_method(_MD("_body_exit_tree"),&RigidBody::_body_exit_tree);
  523. ObjectTypeDB::bind_method(_MD("set_axis_lock","axis_lock"),&RigidBody::set_axis_lock);
  524. ObjectTypeDB::bind_method(_MD("get_axis_lock"),&RigidBody::get_axis_lock);
  525. ObjectTypeDB::bind_method(_MD("get_colliding_bodies"),&RigidBody::get_colliding_bodies);
  526. BIND_VMETHOD(MethodInfo("_integrate_forces",PropertyInfo(Variant::OBJECT,"state:PhysicsDirectBodyState")));
  527. ADD_PROPERTY( PropertyInfo(Variant::INT,"mode",PROPERTY_HINT_ENUM,"Rigid,Static,Character,Kinematic"),_SCS("set_mode"),_SCS("get_mode"));
  528. ADD_PROPERTY( PropertyInfo(Variant::REAL,"mass",PROPERTY_HINT_EXP_RANGE,"0.01,65535,0.01"),_SCS("set_mass"),_SCS("get_mass"));
  529. ADD_PROPERTY( PropertyInfo(Variant::REAL,"weight",PROPERTY_HINT_EXP_RANGE,"0.01,65535,0.01",PROPERTY_USAGE_EDITOR),_SCS("set_weight"),_SCS("get_weight"));
  530. ADD_PROPERTY( PropertyInfo(Variant::REAL,"friction",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_friction"),_SCS("get_friction"));
  531. ADD_PROPERTY( PropertyInfo(Variant::REAL,"bounce",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_bounce"),_SCS("get_bounce"));
  532. ADD_PROPERTY( PropertyInfo(Variant::REAL,"gravity_scale",PROPERTY_HINT_RANGE,"-128,128,0.01"),_SCS("set_gravity_scale"),_SCS("get_gravity_scale"));
  533. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"custom_integrator"),_SCS("set_use_custom_integrator"),_SCS("is_using_custom_integrator"));
  534. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"continuous_cd"),_SCS("set_use_continuous_collision_detection"),_SCS("is_using_continuous_collision_detection"));
  535. ADD_PROPERTY( PropertyInfo(Variant::INT,"contacts_reported"),_SCS("set_max_contacts_reported"),_SCS("get_max_contacts_reported"));
  536. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"contact_monitor"),_SCS("set_contact_monitor"),_SCS("is_contact_monitor_enabled"));
  537. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"sleeping"),_SCS("set_sleeping"),_SCS("is_sleeping"));
  538. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"can_sleep"),_SCS("set_can_sleep"),_SCS("is_able_to_sleep"));
  539. ADD_PROPERTY( PropertyInfo(Variant::INT,"axis_lock",PROPERTY_HINT_ENUM,"Disabled,Lock X,Lock Y,Lock Z"),_SCS("set_axis_lock"),_SCS("get_axis_lock"));
  540. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"velocity/linear"),_SCS("set_linear_velocity"),_SCS("get_linear_velocity"));
  541. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"velocity/angular"),_SCS("set_angular_velocity"),_SCS("get_angular_velocity"));
  542. ADD_PROPERTY( PropertyInfo(Variant::REAL,"damp_override/linear",PROPERTY_HINT_RANGE,"-1,128,0.01"),_SCS("set_linear_damp"),_SCS("get_linear_damp"));
  543. ADD_PROPERTY( PropertyInfo(Variant::REAL,"damp_override/angular",PROPERTY_HINT_RANGE,"-1,128,0.01"),_SCS("set_angular_damp"),_SCS("get_angular_damp"));
  544. ADD_SIGNAL( MethodInfo("body_enter_shape",PropertyInfo(Variant::INT,"body_id"),PropertyInfo(Variant::OBJECT,"body"),PropertyInfo(Variant::INT,"body_shape"),PropertyInfo(Variant::INT,"local_shape")));
  545. ADD_SIGNAL( MethodInfo("body_exit_shape",PropertyInfo(Variant::INT,"body_id"),PropertyInfo(Variant::OBJECT,"body"),PropertyInfo(Variant::INT,"body_shape"),PropertyInfo(Variant::INT,"local_shape")));
  546. ADD_SIGNAL( MethodInfo("body_enter",PropertyInfo(Variant::OBJECT,"body")));
  547. ADD_SIGNAL( MethodInfo("body_exit",PropertyInfo(Variant::OBJECT,"body")));
  548. ADD_SIGNAL( MethodInfo("sleeping_state_changed"));
  549. BIND_CONSTANT( MODE_STATIC );
  550. BIND_CONSTANT( MODE_KINEMATIC );
  551. BIND_CONSTANT( MODE_RIGID );
  552. BIND_CONSTANT( MODE_CHARACTER );
  553. }
  554. RigidBody::RigidBody() : PhysicsBody(PhysicsServer::BODY_MODE_RIGID) {
  555. mode=MODE_RIGID;
  556. bounce=0;
  557. mass=1;
  558. friction=1;
  559. max_contacts_reported=0;
  560. state=NULL;
  561. gravity_scale=1;
  562. linear_damp=-1;
  563. angular_damp=-1;
  564. //angular_velocity=0;
  565. sleeping=false;
  566. ccd=false;
  567. custom_integrator=false;
  568. contact_monitor=NULL;
  569. can_sleep=true;
  570. axis_lock = AXIS_LOCK_DISABLED;
  571. PhysicsServer::get_singleton()->body_set_force_integration_callback(get_rid(),this,"_direct_state_changed");
  572. }
  573. RigidBody::~RigidBody() {
  574. if (contact_monitor)
  575. memdelete( contact_monitor );
  576. }
  577. //////////////////////////////////////////////////////
  578. //////////////////////////
  579. Variant KinematicBody::_get_collider() const {
  580. ObjectID oid=get_collider();
  581. if (oid==0)
  582. return Variant();
  583. Object *obj = ObjectDB::get_instance(oid);
  584. if (!obj)
  585. return Variant();
  586. Reference *ref = obj->cast_to<Reference>();
  587. if (ref) {
  588. return Ref<Reference>(ref);
  589. }
  590. return obj;
  591. }
  592. bool KinematicBody::_ignores_mode(PhysicsServer::BodyMode p_mode) const {
  593. switch(p_mode) {
  594. case PhysicsServer::BODY_MODE_STATIC: return !collide_static;
  595. case PhysicsServer::BODY_MODE_KINEMATIC: return !collide_kinematic;
  596. case PhysicsServer::BODY_MODE_RIGID: return !collide_rigid;
  597. case PhysicsServer::BODY_MODE_CHARACTER: return !collide_character;
  598. }
  599. return true;
  600. }
  601. Vector3 KinematicBody::move(const Vector3& p_motion) {
  602. //give me back regular physics engine logic
  603. //this is madness
  604. //and most people using this function will think
  605. //what it does is simpler than using physics
  606. //this took about a week to get right..
  607. //but is it right? who knows at this point..
  608. colliding=false;
  609. ERR_FAIL_COND_V(!is_inside_tree(),Vector3());
  610. PhysicsDirectSpaceState *dss = PhysicsServer::get_singleton()->space_get_direct_state(get_world()->get_space());
  611. ERR_FAIL_COND_V(!dss,Vector3());
  612. const int max_shapes=32;
  613. Vector3 sr[max_shapes*2];
  614. int res_shapes;
  615. Set<RID> exclude;
  616. exclude.insert(get_rid());
  617. //recover first
  618. int recover_attempts=4;
  619. bool collided=false;
  620. uint32_t mask=0;
  621. if (collide_static)
  622. mask|=PhysicsDirectSpaceState::TYPE_MASK_STATIC_BODY;
  623. if (collide_kinematic)
  624. mask|=PhysicsDirectSpaceState::TYPE_MASK_KINEMATIC_BODY;
  625. if (collide_rigid)
  626. mask|=PhysicsDirectSpaceState::TYPE_MASK_RIGID_BODY;
  627. if (collide_character)
  628. mask|=PhysicsDirectSpaceState::TYPE_MASK_CHARACTER_BODY;
  629. // print_line("motion: "+p_motion+" margin: "+rtos(margin));
  630. //print_line("margin: "+rtos(margin));
  631. float m = margin;
  632. //m=0.001;
  633. do {
  634. //motion recover
  635. for(int i=0;i<get_shape_count();i++) {
  636. if (is_shape_set_as_trigger(i))
  637. continue;
  638. if (dss->collide_shape(get_shape(i)->get_rid(), get_global_transform() * get_shape_transform(i),m,sr,max_shapes,res_shapes,exclude,get_layer_mask(),mask)) {
  639. collided=true;
  640. }
  641. }
  642. if (!collided)
  643. break;
  644. //print_line("have to recover");
  645. Vector3 recover_motion;
  646. bool all_outside=true;
  647. for(int j=0;j<8;j++) {
  648. for(int i=0;i<res_shapes;i++) {
  649. Vector3 a = sr[i*2+0];
  650. Vector3 b = sr[i*2+1];
  651. //print_line(String()+a+" -> "+b);
  652. #if 0
  653. float d = a.distance_to(b);
  654. //if (d<margin)
  655. /// continue;
  656. ///
  657. ///
  658. recover_motion+=(b-a)*0.2;
  659. #else
  660. float dist = a.distance_to(b);
  661. if (dist>CMP_EPSILON) {
  662. Vector3 norm = (b-a).normalized();
  663. if (dist>margin*0.5)
  664. all_outside=false;
  665. float adv = norm.dot(recover_motion);
  666. //print_line(itos(i)+" dist: "+rtos(dist)+" adv: "+rtos(adv));
  667. recover_motion+=norm*MAX(dist-adv,0)*0.4;
  668. }
  669. #endif
  670. }
  671. }
  672. if (recover_motion==Vector3()) {
  673. collided=false;
  674. break;
  675. }
  676. //print_line("**** RECOVER: "+recover_motion);
  677. Transform gt = get_global_transform();
  678. gt.origin+=recover_motion;
  679. set_global_transform(gt);
  680. recover_attempts--;
  681. if (all_outside)
  682. break;
  683. } while (recover_attempts);
  684. //move second
  685. float safe = 1.0;
  686. float unsafe = 1.0;
  687. int best_shape=-1;
  688. PhysicsDirectSpaceState::ShapeRestInfo rest;
  689. //print_line("pos: "+get_global_transform().origin);
  690. //print_line("motion: "+p_motion);
  691. for(int i=0;i<get_shape_count();i++) {
  692. if (is_shape_set_as_trigger(i))
  693. continue;
  694. float lsafe,lunsafe;
  695. PhysicsDirectSpaceState::ShapeRestInfo lrest;
  696. bool valid = dss->cast_motion(get_shape(i)->get_rid(), get_global_transform() * get_shape_transform(i), p_motion,0, lsafe,lunsafe,exclude,get_layer_mask(),mask,&lrest);
  697. //print_line("shape: "+itos(i)+" travel:"+rtos(ltravel));
  698. if (!valid) {
  699. safe=0;
  700. unsafe=0;
  701. best_shape=i; //sadly it's the best
  702. //print_line("initial stuck");
  703. break;
  704. }
  705. if (lsafe==1.0) {
  706. //print_line("initial free");
  707. continue;
  708. }
  709. if (lsafe < safe) {
  710. //print_line("initial at "+rtos(lsafe));
  711. safe=lsafe;
  712. safe=MAX(0,lsafe-0.01);
  713. unsafe=lunsafe;
  714. best_shape=i;
  715. rest=lrest;
  716. }
  717. }
  718. //print_line("best shape: "+itos(best_shape)+" motion "+p_motion);
  719. if (safe>=1) {
  720. //not collided
  721. colliding=false;
  722. } else {
  723. colliding=true;
  724. if (true || (safe==0 && unsafe==0)) { //use it always because it's more precise than GJK
  725. //no advance, use rest info from collision
  726. Transform ugt = get_global_transform();
  727. ugt.origin+=p_motion*unsafe;
  728. PhysicsDirectSpaceState::ShapeRestInfo rest_info;
  729. bool c2 = dss->rest_info(get_shape(best_shape)->get_rid(), ugt*get_shape_transform(best_shape), m,&rest,exclude,get_layer_mask(),mask);
  730. if (!c2) {
  731. //should not happen, but floating point precision is so weird..
  732. colliding=false;
  733. }
  734. // print_line("Rest Travel: "+rest.normal);
  735. }
  736. if (colliding) {
  737. collision=rest.point;
  738. normal=rest.normal;
  739. collider=rest.collider_id;
  740. collider_vel=rest.linear_velocity;
  741. collider_shape=rest.shape;
  742. }
  743. }
  744. Vector3 motion=p_motion*safe;
  745. //if (colliding)
  746. // motion+=normal*0.001;
  747. Transform gt = get_global_transform();
  748. gt.origin+=motion;
  749. set_global_transform(gt);
  750. return p_motion-motion;
  751. }
  752. Vector3 KinematicBody::move_to(const Vector3& p_position) {
  753. return move(p_position-get_global_transform().origin);
  754. }
  755. bool KinematicBody::can_teleport_to(const Vector3& p_position) {
  756. ERR_FAIL_COND_V(!is_inside_tree(),false);
  757. PhysicsDirectSpaceState *dss = PhysicsServer::get_singleton()->space_get_direct_state(get_world()->get_space());
  758. ERR_FAIL_COND_V(!dss,false);
  759. uint32_t mask=0;
  760. if (collide_static)
  761. mask|=PhysicsDirectSpaceState::TYPE_MASK_STATIC_BODY;
  762. if (collide_kinematic)
  763. mask|=PhysicsDirectSpaceState::TYPE_MASK_KINEMATIC_BODY;
  764. if (collide_rigid)
  765. mask|=PhysicsDirectSpaceState::TYPE_MASK_RIGID_BODY;
  766. if (collide_character)
  767. mask|=PhysicsDirectSpaceState::TYPE_MASK_CHARACTER_BODY;
  768. Transform xform=get_global_transform();
  769. xform.origin=p_position;
  770. Set<RID> exclude;
  771. exclude.insert(get_rid());
  772. for(int i=0;i<get_shape_count();i++) {
  773. if (is_shape_set_as_trigger(i))
  774. continue;
  775. bool col = dss->intersect_shape(get_shape(i)->get_rid(), xform * get_shape_transform(i),0,NULL,1,exclude,get_layer_mask(),mask);
  776. if (col)
  777. return false;
  778. }
  779. return true;
  780. }
  781. bool KinematicBody::is_colliding() const {
  782. ERR_FAIL_COND_V(!is_inside_tree(),false);
  783. return colliding;
  784. }
  785. Vector3 KinematicBody::get_collision_pos() const {
  786. ERR_FAIL_COND_V(!colliding,Vector3());
  787. return collision;
  788. }
  789. Vector3 KinematicBody::get_collision_normal() const {
  790. ERR_FAIL_COND_V(!colliding,Vector3());
  791. return normal;
  792. }
  793. Vector3 KinematicBody::get_collider_velocity() const {
  794. return collider_vel;
  795. }
  796. ObjectID KinematicBody::get_collider() const {
  797. ERR_FAIL_COND_V(!colliding,0);
  798. return collider;
  799. }
  800. int KinematicBody::get_collider_shape() const {
  801. ERR_FAIL_COND_V(!colliding,-1);
  802. return collider_shape;
  803. }
  804. void KinematicBody::set_collide_with_static_bodies(bool p_enable) {
  805. collide_static=p_enable;
  806. }
  807. bool KinematicBody::can_collide_with_static_bodies() const {
  808. return collide_static;
  809. }
  810. void KinematicBody::set_collide_with_rigid_bodies(bool p_enable) {
  811. collide_rigid=p_enable;
  812. }
  813. bool KinematicBody::can_collide_with_rigid_bodies() const {
  814. return collide_rigid;
  815. }
  816. void KinematicBody::set_collide_with_kinematic_bodies(bool p_enable) {
  817. collide_kinematic=p_enable;
  818. }
  819. bool KinematicBody::can_collide_with_kinematic_bodies() const {
  820. return collide_kinematic;
  821. }
  822. void KinematicBody::set_collide_with_character_bodies(bool p_enable) {
  823. collide_character=p_enable;
  824. }
  825. bool KinematicBody::can_collide_with_character_bodies() const {
  826. return collide_character;
  827. }
  828. void KinematicBody::set_collision_margin(float p_margin) {
  829. margin=p_margin;
  830. }
  831. float KinematicBody::get_collision_margin() const{
  832. return margin;
  833. }
  834. void KinematicBody::_bind_methods() {
  835. ObjectTypeDB::bind_method(_MD("move","rel_vec"),&KinematicBody::move);
  836. ObjectTypeDB::bind_method(_MD("move_to","position"),&KinematicBody::move_to);
  837. ObjectTypeDB::bind_method(_MD("can_teleport_to","position"),&KinematicBody::can_teleport_to);
  838. ObjectTypeDB::bind_method(_MD("is_colliding"),&KinematicBody::is_colliding);
  839. ObjectTypeDB::bind_method(_MD("get_collision_pos"),&KinematicBody::get_collision_pos);
  840. ObjectTypeDB::bind_method(_MD("get_collision_normal"),&KinematicBody::get_collision_normal);
  841. ObjectTypeDB::bind_method(_MD("get_collider_velocity"),&KinematicBody::get_collider_velocity);
  842. ObjectTypeDB::bind_method(_MD("get_collider:Object"),&KinematicBody::_get_collider);
  843. ObjectTypeDB::bind_method(_MD("get_collider_shape"),&KinematicBody::get_collider_shape);
  844. ObjectTypeDB::bind_method(_MD("set_collide_with_static_bodies","enable"),&KinematicBody::set_collide_with_static_bodies);
  845. ObjectTypeDB::bind_method(_MD("can_collide_with_static_bodies"),&KinematicBody::can_collide_with_static_bodies);
  846. ObjectTypeDB::bind_method(_MD("set_collide_with_kinematic_bodies","enable"),&KinematicBody::set_collide_with_kinematic_bodies);
  847. ObjectTypeDB::bind_method(_MD("can_collide_with_kinematic_bodies"),&KinematicBody::can_collide_with_kinematic_bodies);
  848. ObjectTypeDB::bind_method(_MD("set_collide_with_rigid_bodies","enable"),&KinematicBody::set_collide_with_rigid_bodies);
  849. ObjectTypeDB::bind_method(_MD("can_collide_with_rigid_bodies"),&KinematicBody::can_collide_with_rigid_bodies);
  850. ObjectTypeDB::bind_method(_MD("set_collide_with_character_bodies","enable"),&KinematicBody::set_collide_with_character_bodies);
  851. ObjectTypeDB::bind_method(_MD("can_collide_with_character_bodies"),&KinematicBody::can_collide_with_character_bodies);
  852. ObjectTypeDB::bind_method(_MD("set_collision_margin","pixels"),&KinematicBody::set_collision_margin);
  853. ObjectTypeDB::bind_method(_MD("get_collision_margin","pixels"),&KinematicBody::get_collision_margin);
  854. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"collide_with/static"),_SCS("set_collide_with_static_bodies"),_SCS("can_collide_with_static_bodies"));
  855. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"collide_with/kinematic"),_SCS("set_collide_with_kinematic_bodies"),_SCS("can_collide_with_kinematic_bodies"));
  856. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"collide_with/rigid"),_SCS("set_collide_with_rigid_bodies"),_SCS("can_collide_with_rigid_bodies"));
  857. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"collide_with/character"),_SCS("set_collide_with_character_bodies"),_SCS("can_collide_with_character_bodies"));
  858. ADD_PROPERTY( PropertyInfo(Variant::REAL,"collision/margin",PROPERTY_HINT_RANGE,"0.001,256,0.001"),_SCS("set_collision_margin"),_SCS("get_collision_margin"));
  859. }
  860. KinematicBody::KinematicBody() : PhysicsBody(PhysicsServer::BODY_MODE_KINEMATIC){
  861. collide_static=true;
  862. collide_rigid=true;
  863. collide_kinematic=true;
  864. collide_character=true;
  865. colliding=false;
  866. collider=0;
  867. margin=0.001;
  868. collider_shape=0;
  869. }
  870. KinematicBody::~KinematicBody() {
  871. }