ray_cast.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*************************************************************************/
  2. /* ray_cast.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 "ray_cast.h"
  30. #include "servers/physics_server.h"
  31. #include "collision_object.h"
  32. void RayCast::set_cast_to(const Vector3& p_point) {
  33. cast_to=p_point;
  34. if (is_inside_tree() && (get_tree()->is_editor_hint() || get_tree()->is_debugging_collisions_hint()))
  35. update_gizmo();
  36. }
  37. Vector3 RayCast::get_cast_to() const{
  38. return cast_to;
  39. }
  40. bool RayCast::is_colliding() const{
  41. return collided;
  42. }
  43. Object *RayCast::get_collider() const{
  44. if (against==0)
  45. return NULL;
  46. return ObjectDB::get_instance(against);
  47. }
  48. int RayCast::get_collider_shape() const {
  49. return against_shape;
  50. }
  51. Vector3 RayCast::get_collision_point() const{
  52. return collision_point;
  53. }
  54. Vector3 RayCast::get_collision_normal() const{
  55. return collision_normal;
  56. }
  57. void RayCast::set_enabled(bool p_enabled) {
  58. enabled=p_enabled;
  59. if (is_inside_tree() && !get_tree()->is_editor_hint())
  60. set_fixed_process(p_enabled);
  61. if (!p_enabled)
  62. collided=false;
  63. }
  64. bool RayCast::is_enabled() const {
  65. return enabled;
  66. }
  67. void RayCast::_notification(int p_what) {
  68. switch(p_what) {
  69. case NOTIFICATION_ENTER_TREE: {
  70. if (enabled && !get_tree()->is_editor_hint()) {
  71. set_fixed_process(true);
  72. } else
  73. set_fixed_process(false);
  74. } break;
  75. case NOTIFICATION_EXIT_TREE: {
  76. if (enabled) {
  77. set_fixed_process(false);
  78. }
  79. } break;
  80. case NOTIFICATION_FIXED_PROCESS: {
  81. if (!enabled)
  82. break;
  83. Ref<World> w3d = get_world();
  84. ERR_BREAK( w3d.is_null() );
  85. PhysicsDirectSpaceState *dss = PhysicsServer::get_singleton()->space_get_direct_state(w3d->get_space());
  86. ERR_BREAK( !dss );
  87. Transform gt = get_global_transform();
  88. Vector3 to = cast_to;
  89. if (to==Vector3())
  90. to=Vector3(0,0.01,0);
  91. PhysicsDirectSpaceState::RayResult rr;
  92. if (dss->intersect_ray(gt.get_origin(),gt.xform(to),rr,exclude)) {
  93. collided=true;
  94. against=rr.collider_id;
  95. collision_point=rr.position;
  96. collision_normal=rr.normal;
  97. against_shape=rr.shape;
  98. } else {
  99. collided=false;
  100. }
  101. } break;
  102. }
  103. }
  104. void RayCast::add_exception_rid(const RID& p_rid) {
  105. exclude.insert(p_rid);
  106. }
  107. void RayCast::add_exception(const Object* p_object){
  108. ERR_FAIL_NULL(p_object);
  109. CollisionObject *co=((Object*)p_object)->cast_to<CollisionObject>();
  110. if (!co)
  111. return;
  112. add_exception_rid(co->get_rid());
  113. }
  114. void RayCast::remove_exception_rid(const RID& p_rid) {
  115. exclude.erase(p_rid);
  116. }
  117. void RayCast::remove_exception(const Object* p_object){
  118. ERR_FAIL_NULL(p_object);
  119. CollisionObject *co=((Object*)p_object)->cast_to<CollisionObject>();
  120. if (!co)
  121. return;
  122. remove_exception_rid(co->get_rid());
  123. }
  124. void RayCast::clear_exceptions(){
  125. exclude.clear();
  126. }
  127. void RayCast::_bind_methods() {
  128. ObjectTypeDB::bind_method(_MD("set_enabled","enabled"),&RayCast::set_enabled);
  129. ObjectTypeDB::bind_method(_MD("is_enabled"),&RayCast::is_enabled);
  130. ObjectTypeDB::bind_method(_MD("set_cast_to","local_point"),&RayCast::set_cast_to);
  131. ObjectTypeDB::bind_method(_MD("get_cast_to"),&RayCast::get_cast_to);
  132. ObjectTypeDB::bind_method(_MD("is_colliding"),&RayCast::is_colliding);
  133. ObjectTypeDB::bind_method(_MD("get_collider"),&RayCast::get_collider);
  134. ObjectTypeDB::bind_method(_MD("get_collider_shape"),&RayCast::get_collider_shape);
  135. ObjectTypeDB::bind_method(_MD("get_collision_point"),&RayCast::get_collision_point);
  136. ObjectTypeDB::bind_method(_MD("get_collision_normal"),&RayCast::get_collision_normal);
  137. ObjectTypeDB::bind_method(_MD("add_exception_rid","rid"),&RayCast::add_exception_rid);
  138. ObjectTypeDB::bind_method(_MD("add_exception","node"),&RayCast::add_exception);
  139. ObjectTypeDB::bind_method(_MD("remove_exception_rid","rid"),&RayCast::remove_exception_rid);
  140. ObjectTypeDB::bind_method(_MD("remove_exception","node"),&RayCast::remove_exception);
  141. ObjectTypeDB::bind_method(_MD("clear_exceptions"),&RayCast::clear_exceptions);
  142. ADD_PROPERTY(PropertyInfo(Variant::BOOL,"enabled"),_SCS("set_enabled"),_SCS("is_enabled"));
  143. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3,"cast_to"),_SCS("set_cast_to"),_SCS("get_cast_to"));
  144. }
  145. RayCast::RayCast() {
  146. enabled=false;
  147. against=0;
  148. collided=false;
  149. against_shape=0;
  150. cast_to=Vector3(0,-1,0);
  151. }