Added CapsuleShape2D support for StaticBody2DNavigationGeometryParser2D.

This commit is contained in:
Relintai 2023-06-07 00:36:53 +02:00
parent 683feba2e1
commit 91f365b183
3 changed files with 20 additions and 7 deletions

View File

@ -92,10 +92,22 @@ void StaticBody2DNavigationGeometryParser2D::parse_geometry(Node *p_node, Ref<Na
CapsuleShape2D *capsule_shape = Object::cast_to<CapsuleShape2D>(*s); CapsuleShape2D *capsule_shape = Object::cast_to<CapsuleShape2D>(*s);
if (capsule_shape) { if (capsule_shape) {
//const real_t capsule_height = capsule_shape->get_height(); PoolVector<Vector2> shape_outline;
//const real_t capsule_radius = capsule_shape->get_radius();
//p_source_geometry->add_mesh_array(arr, transform); Vector<Vector2> points = capsule_shape->get_points();
shape_outline.resize(points.size() + 1);
PoolVector<Vector2>::Write shape_outline_write = shape_outline.write();
for (int i = 0; i < points.size(); ++i) {
shape_outline_write[i] = transform.xform(points[i]);
}
if (points.size() > 0) {
shape_outline_write[points.size()] = transform.xform(points[0]);
}
p_source_geometry->add_obstruction_outline(shape_outline);
} }
CircleShape2D *circle_shape = Object::cast_to<CircleShape2D>(*s); CircleShape2D *circle_shape = Object::cast_to<CircleShape2D>(*s);

View File

@ -33,7 +33,7 @@
#include "servers/physics_2d_server.h" #include "servers/physics_2d_server.h"
#include "servers/rendering_server.h" #include "servers/rendering_server.h"
Vector<Vector2> CapsuleShape2D::_get_points() const { Vector<Vector2> CapsuleShape2D::get_points() const {
Vector<Vector2> points; Vector<Vector2> points;
for (int i = 0; i < 24; i++) { for (int i = 0; i < 24; i++) {
Vector2 ofs = Vector2(0, (i > 6 && i <= 18) ? -get_height() * 0.5 : get_height() * 0.5); Vector2 ofs = Vector2(0, (i > 6 && i <= 18) ? -get_height() * 0.5 : get_height() * 0.5);
@ -48,7 +48,7 @@ Vector<Vector2> CapsuleShape2D::_get_points() const {
} }
bool CapsuleShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { bool CapsuleShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
return Geometry::is_point_in_polygon(p_point, _get_points()); return Geometry::is_point_in_polygon(p_point, get_points());
} }
void CapsuleShape2D::_update_shape() { void CapsuleShape2D::_update_shape() {
@ -79,7 +79,7 @@ real_t CapsuleShape2D::get_height() const {
} }
void CapsuleShape2D::draw(const RID &p_to_rid, const Color &p_color) { void CapsuleShape2D::draw(const RID &p_to_rid, const Color &p_color) {
Vector<Vector2> points = _get_points(); Vector<Vector2> points = get_points();
Vector<Color> col; Vector<Color> col;
col.push_back(p_color); col.push_back(p_color);
RenderingServer::get_singleton()->canvas_item_add_polygon(p_to_rid, points, col); RenderingServer::get_singleton()->canvas_item_add_polygon(p_to_rid, points, col);

View File

@ -39,7 +39,6 @@ class CapsuleShape2D : public Shape2D {
real_t radius; real_t radius;
void _update_shape(); void _update_shape();
Vector<Vector2> _get_points() const;
protected: protected:
static void _bind_methods(); static void _bind_methods();
@ -57,6 +56,8 @@ public:
virtual Rect2 get_rect() const; virtual Rect2 get_rect() const;
virtual real_t get_enclosing_radius() const; virtual real_t get_enclosing_radius() const;
Vector<Vector2> get_points() const;
CapsuleShape2D(); CapsuleShape2D();
}; };