plane_shape.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**************************************************************************/
  2. /* plane_shape.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "plane_shape.h"
  31. #include "servers/physics_server.h"
  32. Vector<Vector3> PlaneShape::get_debug_mesh_lines() {
  33. Plane p = get_plane();
  34. Vector<Vector3> points;
  35. Vector3 n1 = p.get_any_perpendicular_normal();
  36. Vector3 n2 = p.normal.cross(n1).normalized();
  37. Vector3 pface[4] = {
  38. p.normal * p.d + n1 * 10.0 + n2 * 10.0,
  39. p.normal * p.d + n1 * 10.0 + n2 * -10.0,
  40. p.normal * p.d + n1 * -10.0 + n2 * -10.0,
  41. p.normal * p.d + n1 * -10.0 + n2 * 10.0,
  42. };
  43. points.push_back(pface[0]);
  44. points.push_back(pface[1]);
  45. points.push_back(pface[1]);
  46. points.push_back(pface[2]);
  47. points.push_back(pface[2]);
  48. points.push_back(pface[3]);
  49. points.push_back(pface[3]);
  50. points.push_back(pface[0]);
  51. points.push_back(p.normal * p.d);
  52. points.push_back(p.normal * p.d + p.normal * 3);
  53. return points;
  54. }
  55. void PlaneShape::_update_shape() {
  56. PhysicsServer::get_singleton()->shape_set_data(get_shape(), plane);
  57. Shape::_update_shape();
  58. }
  59. void PlaneShape::set_plane(Plane p_plane) {
  60. plane = p_plane;
  61. _update_shape();
  62. notify_change_to_owners();
  63. _change_notify("plane");
  64. }
  65. Plane PlaneShape::get_plane() const {
  66. return plane;
  67. }
  68. void PlaneShape::_bind_methods() {
  69. ClassDB::bind_method(D_METHOD("set_plane", "plane"), &PlaneShape::set_plane);
  70. ClassDB::bind_method(D_METHOD("get_plane"), &PlaneShape::get_plane);
  71. ADD_PROPERTY(PropertyInfo(Variant::PLANE, "plane"), "set_plane", "get_plane");
  72. }
  73. PlaneShape::PlaneShape() :
  74. Shape(RID_PRIME(PhysicsServer::get_singleton()->shape_create(PhysicsServer::SHAPE_PLANE))) {
  75. set_plane(Plane(0, 1, 0, 0));
  76. }