volume.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*************************************************************************/
  2. /* volume.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 "volume.h"
  30. #if 0
  31. void Volume::_set(const String& p_name, const Variant& p_value) {
  32. if (p_name.begins_with("shapes/")) {
  33. int idx=p_name.get_slice("/",1).to_int()-1;
  34. ERR_FAIL_COND( idx != get_shape_count() );
  35. Dictionary shape = p_value;
  36. ERR_FAIL_COND( !shape.has("type") || !shape.has("data"));
  37. String type = shape["type"];
  38. Variant data=shape["data"];
  39. Transform transform;
  40. if (shape.has("transform"))
  41. transform=shape["transform"];
  42. if (type=="plane")
  43. add_shape(SHAPE_PLANE,data,transform);
  44. else if (type=="sphere")
  45. add_shape(SHAPE_SPHERE,data,transform);
  46. else if (type=="box")
  47. add_shape(SHAPE_BOX,data,transform);
  48. else if (type=="cylinder")
  49. add_shape(SHAPE_CYLINDER,data,transform);
  50. else if (type=="capsule")
  51. add_shape(SHAPE_CAPSULE,data,transform);
  52. else if (type=="convex_polygon")
  53. add_shape(SHAPE_CONVEX_POLYGON,data,transform);
  54. else if (type=="concave_polygon")
  55. add_shape(SHAPE_CONCAVE_POLYGON,data,transform);
  56. else {
  57. ERR_FAIL();
  58. }
  59. }
  60. }
  61. Variant Volume::_get(const String& p_name) const {
  62. if (p_name.begins_with("shapes/")) {
  63. int idx=p_name.get_slice("/",1).to_int()-1;
  64. ERR_FAIL_INDEX_V( idx, get_shape_count(), Variant() );
  65. Dictionary shape;
  66. switch( get_shape_type(idx) ) {
  67. case SHAPE_PLANE: shape["type"]="plane"; break;
  68. case SHAPE_SPHERE: shape["type"]="sphere"; break;
  69. case SHAPE_BOX: shape["type"]="box"; break;
  70. case SHAPE_CYLINDER: shape["type"]="cylinder"; break;
  71. case SHAPE_CAPSULE: shape["type"]="capsule"; break;
  72. case SHAPE_CONVEX_POLYGON: shape["type"]="convex_polygon"; break;
  73. case SHAPE_CONCAVE_POLYGON: shape["type"]="concave_polygon"; break;
  74. }
  75. shape["transform"]=get_shape_transform(idx);
  76. shape["data"]=get_shape(idx);
  77. return shape;
  78. }
  79. return Variant();
  80. }
  81. void Volume::_get_property_list( List<PropertyInfo> *p_list) const {
  82. int count=get_shape_count();
  83. for(int i=0;i<count;i++) {
  84. p_list->push_back( PropertyInfo( Variant::DICTIONARY, "shapes/"+itos(i+1)) );
  85. }
  86. }
  87. void Volume::add_shape(ShapeType p_shape_type, const Variant& p_data, const Transform& p_transform) {
  88. PhysicsServer::get_singleton()->volume_add_shape(volume,(PhysicsServer::ShapeType)p_shape_type,p_data,p_transform);
  89. _change_notify();
  90. }
  91. void Volume::add_plane_shape(const Plane& p_plane,const Transform& p_transform) {
  92. add_shape(SHAPE_PLANE, p_plane, p_transform );
  93. }
  94. void Volume::add_sphere_shape(float p_radius,const Transform& p_transform) {
  95. add_shape(SHAPE_SPHERE, p_radius, p_transform );
  96. }
  97. void Volume::add_box_shape(const Vector3& p_half_extents,const Transform& p_transform) {
  98. add_shape(SHAPE_BOX, p_half_extents, p_transform );
  99. }
  100. void Volume::add_cylinder_shape(float p_radius, float p_height,const Transform& p_transform) {
  101. Dictionary d;
  102. d["radius"]=p_radius;
  103. d["height"]=p_height;
  104. add_shape(SHAPE_CYLINDER,d,p_transform);
  105. }
  106. void Volume::add_capsule_shape(float p_radius, float p_height,const Transform& p_transform) {
  107. Dictionary d;
  108. d["radius"]=p_radius;
  109. d["height"]=p_height;
  110. add_shape(SHAPE_CAPSULE,d,p_transform);
  111. }
  112. int Volume::get_shape_count() const {
  113. return PhysicsServer::get_singleton()->volume_get_shape_count(volume);
  114. }
  115. Volume::ShapeType Volume::get_shape_type(int p_shape) const {
  116. return (ShapeType)PhysicsServer::get_singleton()->volume_get_shape_type(volume,p_shape);
  117. }
  118. Transform Volume::get_shape_transform(int p_shape) const {
  119. return PhysicsServer::get_singleton()->volume_get_shape_transform(volume,p_shape);
  120. }
  121. Variant Volume::get_shape(int p_shape) const {
  122. return PhysicsServer::get_singleton()->volume_get_shape(volume,p_shape);
  123. }
  124. void Volume::_bind_methods() {
  125. ObjectTypeDB::bind_method(_MD("add_shape","type","data","transform"),&Volume::add_shape,DEFVAL( Transform() ));
  126. ObjectTypeDB::bind_method(_MD("add_plane_shape","plane","transform"),&Volume::add_plane_shape,DEFVAL( Transform() ));
  127. ObjectTypeDB::bind_method(_MD("add_sphere_shape"),&Volume::add_sphere_shape,DEFVAL( Transform() ));
  128. ObjectTypeDB::bind_method(_MD("add_box_shape","radius","transform"),&Volume::add_box_shape,DEFVAL( Transform() ));
  129. ObjectTypeDB::bind_method(_MD("add_cylinder_shape","radius","height","transform"),&Volume::add_cylinder_shape,DEFVAL( Transform() ));
  130. ObjectTypeDB::bind_method(_MD("add_capsule_shape","radius","height","transform"),&Volume::add_capsule_shape,DEFVAL( Transform() ));
  131. ObjectTypeDB::bind_method(_MD("get_shape_count"),&Volume::get_shape_count);
  132. ObjectTypeDB::bind_method(_MD("get_shape_type","shape_idx"),&Volume::get_shape_type);
  133. ObjectTypeDB::bind_method(_MD("get_shape_transform","shape_idx"),&Volume::get_shape_transform);
  134. ObjectTypeDB::bind_method(_MD("get_shape","shape_idx"),&Volume::get_shape);
  135. BIND_CONSTANT( SHAPE_PLANE );
  136. BIND_CONSTANT( SHAPE_SPHERE );
  137. BIND_CONSTANT( SHAPE_BOX );
  138. BIND_CONSTANT( SHAPE_CYLINDER );
  139. BIND_CONSTANT( SHAPE_CAPSULE );
  140. BIND_CONSTANT( SHAPE_CONVEX_POLYGON );
  141. BIND_CONSTANT( SHAPE_CONCAVE_POLYGON );
  142. }
  143. RID Volume::get_rid() {
  144. return volume;
  145. }
  146. Volume::Volume() {
  147. volume= PhysicsServer::get_singleton()->volume_create();
  148. }
  149. Volume::~Volume() {
  150. PhysicsServer::get_singleton()->free(volume);
  151. }
  152. #endif