plane_shape.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*************************************************************************/
  2. /* plane_shape.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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::_gen_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. }
  58. void PlaneShape::set_plane(Plane p_plane) {
  59. plane = p_plane;
  60. _update_shape();
  61. notify_change_to_owners();
  62. _change_notify("plane");
  63. }
  64. Plane PlaneShape::get_plane() const {
  65. return plane;
  66. }
  67. void PlaneShape::_bind_methods() {
  68. ClassDB::bind_method(D_METHOD("set_plane", "plane"), &PlaneShape::set_plane);
  69. ClassDB::bind_method(D_METHOD("get_plane"), &PlaneShape::get_plane);
  70. ADD_PROPERTY(PropertyInfo(Variant::PLANE, "plane"), "set_plane", "get_plane");
  71. }
  72. PlaneShape::PlaneShape() :
  73. Shape(PhysicsServer::get_singleton()->shape_create(PhysicsServer::SHAPE_PLANE)) {
  74. set_plane(Plane(0, 1, 0, 0));
  75. }