capsule_shape.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*************************************************************************/
  2. /* capsule_shape.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 "capsule_shape.h"
  30. #include "servers/physics_server.h"
  31. Vector<Vector3> CapsuleShape::_gen_debug_mesh_lines() {
  32. float radius = get_radius();
  33. float height = get_height();
  34. Vector<Vector3> points;
  35. Vector3 d(0,0,height*0.5);
  36. for(int i=0;i<360;i++) {
  37. float ra=Math::deg2rad(i);
  38. float rb=Math::deg2rad(i+1);
  39. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*radius;
  40. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*radius;
  41. points.push_back(Vector3(a.x,a.y,0)+d);
  42. points.push_back(Vector3(b.x,b.y,0)+d);
  43. points.push_back(Vector3(a.x,a.y,0)-d);
  44. points.push_back(Vector3(b.x,b.y,0)-d);
  45. if (i%90==0) {
  46. points.push_back(Vector3(a.x,a.y,0)+d);
  47. points.push_back(Vector3(a.x,a.y,0)-d);
  48. }
  49. Vector3 dud = i<180?d:-d;
  50. points.push_back(Vector3(0,a.y,a.x)+dud);
  51. points.push_back(Vector3(0,b.y,b.x)+dud);
  52. points.push_back(Vector3(a.y,0,a.x)+dud);
  53. points.push_back(Vector3(b.y,0,b.x)+dud);
  54. }
  55. return points;
  56. }
  57. void CapsuleShape::_update_shape() {
  58. Dictionary d;
  59. d["radius"]=radius;
  60. d["height"]=height;
  61. PhysicsServer::get_singleton()->shape_set_data(get_shape(),d);
  62. }
  63. void CapsuleShape::set_radius(float p_radius) {
  64. radius=p_radius;
  65. _update_shape();
  66. notify_change_to_owners();
  67. _change_notify("radius");
  68. }
  69. float CapsuleShape::get_radius() const {
  70. return radius;
  71. }
  72. void CapsuleShape::set_height(float p_height) {
  73. height=p_height;
  74. _update_shape();
  75. notify_change_to_owners();
  76. _change_notify("height");
  77. }
  78. float CapsuleShape::get_height() const {
  79. return height;
  80. }
  81. void CapsuleShape::_bind_methods() {
  82. ObjectTypeDB::bind_method(_MD("set_radius","radius"),&CapsuleShape::set_radius);
  83. ObjectTypeDB::bind_method(_MD("get_radius"),&CapsuleShape::get_radius);
  84. ObjectTypeDB::bind_method(_MD("set_height","height"),&CapsuleShape::set_height);
  85. ObjectTypeDB::bind_method(_MD("get_height"),&CapsuleShape::get_height);
  86. ADD_PROPERTY( PropertyInfo(Variant::REAL,"radius",PROPERTY_HINT_RANGE,"0.01,4096,0.01"), _SCS("set_radius"),_SCS("get_radius") );
  87. ADD_PROPERTY( PropertyInfo(Variant::REAL,"height",PROPERTY_HINT_RANGE,"0.01,4096,0.01"), _SCS("set_height"),_SCS("get_height") );
  88. }
  89. CapsuleShape::CapsuleShape() : Shape( PhysicsServer::get_singleton()->shape_create(PhysicsServer::SHAPE_CAPSULE)) {
  90. radius=1.0;
  91. height=1.0;
  92. _update_shape();
  93. }