physics_body.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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-2014 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. PhysicsBody::PhysicsBody(PhysicsServer::BodyMode p_mode) : CollisionObject( PhysicsServer::get_singleton()->body_create(p_mode), false) {
  41. }
  42. void StaticBody::set_constant_linear_velocity(const Vector3& p_vel) {
  43. constant_linear_velocity=p_vel;
  44. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_LINEAR_VELOCITY,constant_linear_velocity);
  45. }
  46. void StaticBody::set_constant_angular_velocity(const Vector3& p_vel) {
  47. constant_angular_velocity=p_vel;
  48. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_ANGULAR_VELOCITY,constant_angular_velocity);
  49. }
  50. Vector3 StaticBody::get_constant_linear_velocity() const {
  51. return constant_linear_velocity;
  52. }
  53. Vector3 StaticBody::get_constant_angular_velocity() const {
  54. return constant_angular_velocity;
  55. }
  56. void StaticBody::_state_notify(Object *p_object) {
  57. if (!pre_xform)
  58. return;
  59. PhysicsDirectBodyState *p2d = (PhysicsDirectBodyState*)p_object;
  60. setting=true;
  61. Transform new_xform = p2d->get_transform();
  62. *pre_xform=new_xform;
  63. set_ignore_transform_notification(true);
  64. set_global_transform(new_xform);
  65. set_ignore_transform_notification(false);
  66. setting=false;
  67. }
  68. void StaticBody::_update_xform() {
  69. if (!pre_xform || !pending)
  70. return;
  71. setting=true;
  72. Transform new_xform = get_global_transform(); //obtain the new one
  73. //set_block_transform_notify(true);
  74. set_ignore_transform_notification(true);
  75. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_TRANSFORM,*pre_xform); //then simulate motion!
  76. set_global_transform(*pre_xform); //but restore state to previous one in both visual and physics
  77. set_ignore_transform_notification(false);
  78. PhysicsServer::get_singleton()->body_static_simulate_motion(get_rid(),new_xform); //then simulate motion!
  79. setting=false;
  80. pending=false;
  81. }
  82. void StaticBody::_notification(int p_what) {
  83. switch(p_what) {
  84. case NOTIFICATION_ENTER_SCENE: {
  85. if (pre_xform)
  86. *pre_xform = get_global_transform();
  87. pending=false;
  88. } break;
  89. case NOTIFICATION_TRANSFORM_CHANGED: {
  90. if (simulating_motion && !pending && is_inside_scene() && !setting && !get_scene()->is_editor_hint()) {
  91. call_deferred(SceneStringNames::get_singleton()->_update_xform);
  92. pending=true;
  93. }
  94. } break;
  95. }
  96. }
  97. void StaticBody::set_simulate_motion(bool p_enable) {
  98. if (p_enable==simulating_motion)
  99. return;
  100. simulating_motion=p_enable;
  101. if (p_enable) {
  102. pre_xform = memnew( Transform );
  103. if (is_inside_scene())
  104. *pre_xform=get_transform();
  105. // query = PhysicsServer::get_singleton()->query_create(this,"_state_notify",Variant());
  106. // PhysicsServer::get_singleton()->query_body_direct_state(query,get_rid());
  107. PhysicsServer::get_singleton()->body_set_force_integration_callback(get_rid(),this,"_state_notify");
  108. } else {
  109. memdelete( pre_xform );
  110. pre_xform=NULL;
  111. PhysicsServer::get_singleton()->body_set_force_integration_callback(get_rid(),NULL,StringName());
  112. pending=false;
  113. }
  114. }
  115. bool StaticBody::is_simulating_motion() const {
  116. return simulating_motion;
  117. }
  118. void StaticBody::_bind_methods() {
  119. ObjectTypeDB::bind_method(_MD("set_simulate_motion","enabled"),&StaticBody::set_simulate_motion);
  120. ObjectTypeDB::bind_method(_MD("is_simulating_motion"),&StaticBody::is_simulating_motion);
  121. ObjectTypeDB::bind_method(_MD("_update_xform"),&StaticBody::_update_xform);
  122. ObjectTypeDB::bind_method(_MD("_state_notify"),&StaticBody::_state_notify);
  123. ObjectTypeDB::bind_method(_MD("set_constant_linear_velocity","vel"),&StaticBody::set_constant_linear_velocity);
  124. ObjectTypeDB::bind_method(_MD("set_constant_angular_velocity","vel"),&StaticBody::set_constant_angular_velocity);
  125. ObjectTypeDB::bind_method(_MD("get_constant_linear_velocity"),&StaticBody::get_constant_linear_velocity);
  126. ObjectTypeDB::bind_method(_MD("get_constant_angular_velocity"),&StaticBody::get_constant_angular_velocity);
  127. ADD_PROPERTY(PropertyInfo(Variant::BOOL,"simulate_motion"),_SCS("set_simulate_motion"),_SCS("is_simulating_motion"));
  128. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3,"constant_linear_velocity"),_SCS("set_constant_linear_velocity"),_SCS("get_constant_linear_velocity"));
  129. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3,"constant_angular_velocity"),_SCS("set_constant_angular_velocity"),_SCS("get_constant_angular_velocity"));
  130. }
  131. StaticBody::StaticBody() : PhysicsBody(PhysicsServer::BODY_MODE_STATIC) {
  132. simulating_motion=false;
  133. pre_xform=NULL;
  134. setting=false;
  135. pending=false;
  136. //constant_angular_velocity=0;
  137. }
  138. StaticBody::~StaticBody() {
  139. if (pre_xform)
  140. memdelete(pre_xform);
  141. //if (query.is_valid())
  142. // PhysicsServer::get_singleton()->free(query);
  143. }
  144. void RigidBody::_body_enter_scene(ObjectID p_id) {
  145. Object *obj = ObjectDB::get_instance(p_id);
  146. Node *node = obj ? obj->cast_to<Node>() : NULL;
  147. ERR_FAIL_COND(!node);
  148. Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.find(p_id);
  149. ERR_FAIL_COND(!E);
  150. ERR_FAIL_COND(E->get().in_scene);
  151. E->get().in_scene=true;
  152. emit_signal(SceneStringNames::get_singleton()->body_enter,node);
  153. for(int i=0;i<E->get().shapes.size();i++) {
  154. emit_signal(SceneStringNames::get_singleton()->body_enter_shape,p_id,node,E->get().shapes[i].body_shape,E->get().shapes[i].local_shape);
  155. }
  156. }
  157. void RigidBody::_body_exit_scene(ObjectID p_id) {
  158. Object *obj = ObjectDB::get_instance(p_id);
  159. Node *node = obj ? obj->cast_to<Node>() : NULL;
  160. ERR_FAIL_COND(!node);
  161. Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.find(p_id);
  162. ERR_FAIL_COND(!E);
  163. ERR_FAIL_COND(!E->get().in_scene);
  164. E->get().in_scene=false;
  165. emit_signal(SceneStringNames::get_singleton()->body_exit,node);
  166. for(int i=0;i<E->get().shapes.size();i++) {
  167. emit_signal(SceneStringNames::get_singleton()->body_exit_shape,p_id,node,E->get().shapes[i].body_shape,E->get().shapes[i].local_shape);
  168. }
  169. }
  170. void RigidBody::_body_inout(int p_status, ObjectID p_instance, int p_body_shape,int p_local_shape) {
  171. bool body_in = p_status==1;
  172. ObjectID objid=p_instance;
  173. Object *obj = ObjectDB::get_instance(objid);
  174. Node *node = obj ? obj->cast_to<Node>() : NULL;
  175. Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.find(objid);
  176. ERR_FAIL_COND(!body_in && !E);
  177. if (body_in) {
  178. if (!E) {
  179. E = contact_monitor->body_map.insert(objid,BodyState());
  180. E->get().rc=0;
  181. E->get().in_scene=node && node->is_inside_scene();
  182. if (node) {
  183. node->connect(SceneStringNames::get_singleton()->enter_scene,this,SceneStringNames::get_singleton()->_body_enter_scene,make_binds(objid));
  184. node->connect(SceneStringNames::get_singleton()->exit_scene,this,SceneStringNames::get_singleton()->_body_exit_scene,make_binds(objid));
  185. if (E->get().in_scene) {
  186. emit_signal(SceneStringNames::get_singleton()->body_enter,node);
  187. }
  188. }
  189. }
  190. E->get().rc++;
  191. if (node)
  192. E->get().shapes.insert(ShapePair(p_body_shape,p_local_shape));
  193. if (E->get().in_scene) {
  194. emit_signal(SceneStringNames::get_singleton()->body_enter_shape,objid,node,p_body_shape,p_local_shape);
  195. }
  196. } else {
  197. E->get().rc--;
  198. if (node)
  199. E->get().shapes.erase(ShapePair(p_body_shape,p_local_shape));
  200. if (E->get().rc==0) {
  201. if (node) {
  202. node->disconnect(SceneStringNames::get_singleton()->enter_scene,this,SceneStringNames::get_singleton()->_body_enter_scene);
  203. node->disconnect(SceneStringNames::get_singleton()->exit_scene,this,SceneStringNames::get_singleton()->_body_exit_scene);
  204. if (E->get().in_scene)
  205. emit_signal(SceneStringNames::get_singleton()->body_exit,obj);
  206. }
  207. contact_monitor->body_map.erase(E);
  208. }
  209. if (node && E->get().in_scene) {
  210. emit_signal(SceneStringNames::get_singleton()->body_exit_shape,objid,obj,p_body_shape,p_local_shape);
  211. }
  212. }
  213. }
  214. struct _RigidBodyInOut {
  215. ObjectID id;
  216. int shape;
  217. int local_shape;
  218. };
  219. void RigidBody::_direct_state_changed(Object *p_state) {
  220. //eh.. fuck
  221. #ifdef DEBUG_ENABLED
  222. state=p_state->cast_to<PhysicsDirectBodyState>();
  223. #else
  224. state=(PhysicsDirectBodyState*)p_state; //trust it
  225. #endif
  226. if (contact_monitor) {
  227. //untag all
  228. int rc=0;
  229. for( Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.front();E;E=E->next()) {
  230. for(int i=0;i<E->get().shapes.size();i++) {
  231. E->get().shapes[i].tagged=false;
  232. rc++;
  233. }
  234. }
  235. _RigidBodyInOut *toadd=(_RigidBodyInOut*)alloca(state->get_contact_count()*sizeof(_RigidBodyInOut));
  236. int toadd_count=0;//state->get_contact_count();
  237. RigidBody_RemoveAction *toremove=(RigidBody_RemoveAction*)alloca(rc*sizeof(RigidBody_RemoveAction));
  238. int toremove_count=0;
  239. //put the ones to add
  240. for(int i=0;i<state->get_contact_count();i++) {
  241. ObjectID obj = state->get_contact_collider_id(i);
  242. int local_shape = state->get_contact_local_shape(i);
  243. int shape = state->get_contact_collider_shape(i);
  244. toadd[i].local_shape=local_shape;
  245. toadd[i].id=obj;
  246. toadd[i].shape=shape;
  247. bool found=false;
  248. Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.find(obj);
  249. if (!E) {
  250. toadd_count++;
  251. continue;
  252. }
  253. ShapePair sp( shape,local_shape );
  254. int idx = E->get().shapes.find(sp);
  255. if (idx==-1) {
  256. toadd_count++;
  257. continue;
  258. }
  259. E->get().shapes[idx].tagged=true;
  260. }
  261. //put the ones to remove
  262. for( Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.front();E;E=E->next()) {
  263. for(int i=0;i<E->get().shapes.size();i++) {
  264. if (!E->get().shapes[i].tagged) {
  265. toremove[toremove_count].body_id=E->key();
  266. toremove[toremove_count].pair=E->get().shapes[i];
  267. toremove_count++;
  268. }
  269. }
  270. }
  271. //process remotions
  272. for(int i=0;i<toremove_count;i++) {
  273. _body_inout(0,toremove[i].body_id,toremove[i].pair.body_shape,toremove[i].pair.local_shape);
  274. }
  275. //process aditions
  276. for(int i=0;i<toadd_count;i++) {
  277. _body_inout(1,toadd[i].id,toadd[i].shape,toadd[i].local_shape);
  278. }
  279. }
  280. set_ignore_transform_notification(true);
  281. set_global_transform(state->get_transform());
  282. linear_velocity=state->get_linear_velocity();
  283. angular_velocity=state->get_angular_velocity();
  284. active=!state->is_sleeping();
  285. if (get_script_instance())
  286. get_script_instance()->call("_integrate_forces",state);
  287. set_ignore_transform_notification(false);
  288. state=NULL;
  289. }
  290. void RigidBody::_notification(int p_what) {
  291. }
  292. void RigidBody::set_mode(Mode p_mode) {
  293. mode=p_mode;
  294. switch(p_mode) {
  295. case MODE_RIGID: {
  296. PhysicsServer::get_singleton()->body_set_mode(get_rid(),PhysicsServer::BODY_MODE_RIGID);
  297. } break;
  298. case MODE_STATIC: {
  299. PhysicsServer::get_singleton()->body_set_mode(get_rid(),PhysicsServer::BODY_MODE_STATIC);
  300. } break;
  301. case MODE_CHARACTER: {
  302. PhysicsServer::get_singleton()->body_set_mode(get_rid(),PhysicsServer::BODY_MODE_CHARACTER);
  303. } break;
  304. case MODE_KINEMATIC: {
  305. PhysicsServer::get_singleton()->body_set_mode(get_rid(),PhysicsServer::BODY_MODE_KINEMATIC);
  306. } break;
  307. }
  308. }
  309. RigidBody::Mode RigidBody::get_mode() const{
  310. return mode;
  311. }
  312. void RigidBody::set_mass(real_t p_mass){
  313. ERR_FAIL_COND(p_mass<=0);
  314. mass=p_mass;
  315. _change_notify("mass");
  316. _change_notify("weight");
  317. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_MASS,mass);
  318. }
  319. real_t RigidBody::get_mass() const{
  320. return mass;
  321. }
  322. void RigidBody::set_weight(real_t p_weight){
  323. set_mass(p_weight/9.8);
  324. }
  325. real_t RigidBody::get_weight() const{
  326. return mass*9.8;
  327. }
  328. void RigidBody::set_friction(real_t p_friction){
  329. ERR_FAIL_COND(p_friction<0 || p_friction>1);
  330. friction=p_friction;
  331. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_FRICTION,friction);
  332. }
  333. real_t RigidBody::get_friction() const{
  334. return friction;
  335. }
  336. void RigidBody::set_bounce(real_t p_bounce){
  337. ERR_FAIL_COND(p_bounce<0 || p_bounce>1);
  338. bounce=p_bounce;
  339. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_BOUNCE,bounce);
  340. }
  341. real_t RigidBody::get_bounce() const{
  342. return bounce;
  343. }
  344. void RigidBody::set_axis_velocity(const Vector3& p_axis) {
  345. Vector3 v = state? state->get_linear_velocity() : linear_velocity;
  346. Vector3 axis = p_axis.normalized();
  347. v-=axis*axis.dot(v);
  348. v+=p_axis;
  349. if (state) {
  350. set_linear_velocity(v);
  351. } else {
  352. PhysicsServer::get_singleton()->body_set_axis_velocity(get_rid(),p_axis);
  353. linear_velocity=v;
  354. }
  355. }
  356. void RigidBody::set_linear_velocity(const Vector3& p_velocity){
  357. linear_velocity=p_velocity;
  358. if (state)
  359. state->set_linear_velocity(linear_velocity);
  360. else
  361. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_LINEAR_VELOCITY,linear_velocity);
  362. }
  363. Vector3 RigidBody::get_linear_velocity() const{
  364. return linear_velocity;
  365. }
  366. void RigidBody::set_angular_velocity(const Vector3& p_velocity){
  367. angular_velocity=p_velocity;
  368. if (state)
  369. state->set_angular_velocity(angular_velocity);
  370. else
  371. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_ANGULAR_VELOCITY,angular_velocity);
  372. }
  373. Vector3 RigidBody::get_angular_velocity() const{
  374. return angular_velocity;
  375. }
  376. void RigidBody::set_use_custom_integrator(bool p_enable){
  377. if (custom_integrator==p_enable)
  378. return;
  379. custom_integrator=p_enable;
  380. PhysicsServer::get_singleton()->body_set_omit_force_integration(get_rid(),p_enable);
  381. }
  382. bool RigidBody::is_using_custom_integrator(){
  383. return custom_integrator;
  384. }
  385. void RigidBody::set_active(bool p_active) {
  386. active=p_active;
  387. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_SLEEPING,!active);
  388. }
  389. void RigidBody::set_can_sleep(bool p_active) {
  390. can_sleep=p_active;
  391. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_CAN_SLEEP,p_active);
  392. }
  393. bool RigidBody::is_able_to_sleep() const {
  394. return can_sleep;
  395. }
  396. bool RigidBody::is_active() const {
  397. return active;
  398. }
  399. void RigidBody::set_max_contacts_reported(int p_amount) {
  400. max_contacts_reported=p_amount;
  401. PhysicsServer::get_singleton()->body_set_max_contacts_reported(get_rid(),p_amount);
  402. }
  403. int RigidBody::get_max_contacts_reported() const{
  404. return max_contacts_reported;
  405. }
  406. void RigidBody::apply_impulse(const Vector3& p_pos, const Vector3& p_impulse) {
  407. PhysicsServer::get_singleton()->body_apply_impulse(get_rid(),p_pos,p_impulse);
  408. }
  409. void RigidBody::set_use_continuous_collision_detection(bool p_enable) {
  410. ccd=p_enable;
  411. PhysicsServer::get_singleton()->body_set_enable_continuous_collision_detection(get_rid(),p_enable);
  412. }
  413. bool RigidBody::is_using_continuous_collision_detection() const {
  414. return ccd;
  415. }
  416. void RigidBody::set_contact_monitor(bool p_enabled) {
  417. if (p_enabled==is_contact_monitor_enabled())
  418. return;
  419. if (!p_enabled) {
  420. for(Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.front();E;E=E->next()) {
  421. //clean up mess
  422. }
  423. memdelete( contact_monitor );
  424. contact_monitor=NULL;
  425. } else {
  426. contact_monitor = memnew( ContactMonitor );
  427. }
  428. }
  429. bool RigidBody::is_contact_monitor_enabled() const {
  430. return contact_monitor!=NULL;
  431. }
  432. void RigidBody::_bind_methods() {
  433. ObjectTypeDB::bind_method(_MD("set_mode","mode"),&RigidBody::set_mode);
  434. ObjectTypeDB::bind_method(_MD("get_mode"),&RigidBody::get_mode);
  435. ObjectTypeDB::bind_method(_MD("set_mass","mass"),&RigidBody::set_mass);
  436. ObjectTypeDB::bind_method(_MD("get_mass"),&RigidBody::get_mass);
  437. ObjectTypeDB::bind_method(_MD("set_weight","weight"),&RigidBody::set_weight);
  438. ObjectTypeDB::bind_method(_MD("get_weight"),&RigidBody::get_weight);
  439. ObjectTypeDB::bind_method(_MD("set_friction","friction"),&RigidBody::set_friction);
  440. ObjectTypeDB::bind_method(_MD("get_friction"),&RigidBody::get_friction);
  441. ObjectTypeDB::bind_method(_MD("set_bounce","bounce"),&RigidBody::set_bounce);
  442. ObjectTypeDB::bind_method(_MD("get_bounce"),&RigidBody::get_bounce);
  443. ObjectTypeDB::bind_method(_MD("set_linear_velocity","linear_velocity"),&RigidBody::set_linear_velocity);
  444. ObjectTypeDB::bind_method(_MD("get_linear_velocity"),&RigidBody::get_linear_velocity);
  445. ObjectTypeDB::bind_method(_MD("set_angular_velocity","angular_velocity"),&RigidBody::set_angular_velocity);
  446. ObjectTypeDB::bind_method(_MD("get_angular_velocity"),&RigidBody::get_angular_velocity);
  447. ObjectTypeDB::bind_method(_MD("set_max_contacts_reported","amount"),&RigidBody::set_max_contacts_reported);
  448. ObjectTypeDB::bind_method(_MD("get_max_contacts_reported"),&RigidBody::get_max_contacts_reported);
  449. ObjectTypeDB::bind_method(_MD("set_use_custom_integrator","enable"),&RigidBody::set_use_custom_integrator);
  450. ObjectTypeDB::bind_method(_MD("is_using_custom_integrator"),&RigidBody::is_using_custom_integrator);
  451. ObjectTypeDB::bind_method(_MD("set_contact_monitor","enabled"),&RigidBody::set_contact_monitor);
  452. ObjectTypeDB::bind_method(_MD("is_contact_monitor_enabled"),&RigidBody::is_contact_monitor_enabled);
  453. ObjectTypeDB::bind_method(_MD("set_use_continuous_collision_detection","enable"),&RigidBody::set_use_continuous_collision_detection);
  454. ObjectTypeDB::bind_method(_MD("is_using_continuous_collision_detection"),&RigidBody::is_using_continuous_collision_detection);
  455. ObjectTypeDB::bind_method(_MD("set_axis_velocity","axis_velocity"),&RigidBody::set_axis_velocity);
  456. ObjectTypeDB::bind_method(_MD("apply_impulse","pos","impulse"),&RigidBody::apply_impulse);
  457. ObjectTypeDB::bind_method(_MD("set_active","active"),&RigidBody::set_active);
  458. ObjectTypeDB::bind_method(_MD("is_active"),&RigidBody::is_active);
  459. ObjectTypeDB::bind_method(_MD("set_can_sleep","able_to_sleep"),&RigidBody::set_can_sleep);
  460. ObjectTypeDB::bind_method(_MD("is_able_to_sleep"),&RigidBody::is_able_to_sleep);
  461. ObjectTypeDB::bind_method(_MD("_direct_state_changed"),&RigidBody::_direct_state_changed);
  462. ObjectTypeDB::bind_method(_MD("_body_enter_scene"),&RigidBody::_body_enter_scene);
  463. ObjectTypeDB::bind_method(_MD("_body_exit_scene"),&RigidBody::_body_exit_scene);
  464. BIND_VMETHOD(MethodInfo("_integrate_forces",PropertyInfo(Variant::OBJECT,"state:PhysicsDirectBodyState")));
  465. ADD_PROPERTY( PropertyInfo(Variant::INT,"mode",PROPERTY_HINT_ENUM,"Rigid,Static,Character,Kinematic"),_SCS("set_mode"),_SCS("get_mode"));
  466. ADD_PROPERTY( PropertyInfo(Variant::REAL,"mass",PROPERTY_HINT_EXP_RANGE,"0.01,65535,0.01"),_SCS("set_mass"),_SCS("get_mass"));
  467. ADD_PROPERTY( PropertyInfo(Variant::REAL,"weight",PROPERTY_HINT_EXP_RANGE,"0.01,65535,0.01",PROPERTY_USAGE_EDITOR),_SCS("set_weight"),_SCS("get_weight"));
  468. ADD_PROPERTY( PropertyInfo(Variant::REAL,"friction",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_friction"),_SCS("get_friction"));
  469. ADD_PROPERTY( PropertyInfo(Variant::REAL,"bounce",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_bounce"),_SCS("get_bounce"));
  470. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"custom_integrator"),_SCS("set_use_custom_integrator"),_SCS("is_using_custom_integrator"));
  471. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"continuous_cd"),_SCS("set_use_continuous_collision_detection"),_SCS("is_using_continuous_collision_detection"));
  472. ADD_PROPERTY( PropertyInfo(Variant::INT,"contacts_reported"),_SCS("set_max_contacts_reported"),_SCS("get_max_contacts_reported"));
  473. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"contact_monitor"),_SCS("set_contact_monitor"),_SCS("is_contact_monitor_enabled"));
  474. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"active"),_SCS("set_active"),_SCS("is_active"));
  475. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"can_sleep"),_SCS("set_can_sleep"),_SCS("is_able_to_sleep"));
  476. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"velocity/linear"),_SCS("set_linear_velocity"),_SCS("get_linear_velocity"));
  477. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"velocity/angular"),_SCS("set_angular_velocity"),_SCS("get_angular_velocity"));
  478. 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")));
  479. 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")));
  480. ADD_SIGNAL( MethodInfo("body_enter",PropertyInfo(Variant::OBJECT,"body")));
  481. ADD_SIGNAL( MethodInfo("body_exit",PropertyInfo(Variant::OBJECT,"body")));
  482. BIND_CONSTANT( MODE_STATIC );
  483. BIND_CONSTANT( MODE_KINEMATIC );
  484. BIND_CONSTANT( MODE_RIGID );
  485. BIND_CONSTANT( MODE_CHARACTER );
  486. }
  487. RigidBody::RigidBody() : PhysicsBody(PhysicsServer::BODY_MODE_RIGID) {
  488. mode=MODE_RIGID;
  489. bounce=0;
  490. mass=1;
  491. friction=1;
  492. max_contacts_reported=0;
  493. state=NULL;
  494. //angular_velocity=0;
  495. active=true;
  496. ccd=false;
  497. custom_integrator=false;
  498. contact_monitor=NULL;
  499. can_sleep=true;
  500. PhysicsServer::get_singleton()->body_set_force_integration_callback(get_rid(),this,"_direct_state_changed");
  501. }
  502. RigidBody::~RigidBody() {
  503. if (contact_monitor)
  504. memdelete( contact_monitor );
  505. }