physics_body.cpp 23 KB

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