Can anyone please tell that how will I check if a point is in a Polygon2D or not

in my case the Polygon is a rhombus with one diagonal 128 and other 64. The longer diagonal is horizontal and smaller diagonal is vertical

+4 votes

Best answer

Hi...A technique is explained in the godot manual:

https://docs.godotengine.org/en/stable/tutorials/math/vectors_advanced.html

Godot has a class called Geometry and one of its methods is just what you are looking for:

https://docs.godotengine.org/en/stable/classes/class_geometry.html?highlight=geometry#class-geometry-method-is-point-in-polygon

In godot it would be used like this, passing it an array with the points of the polygon:

```
var polygon = [Vector2(0,0), Vector2(1,0), Vector2(1,1), Vector2(0,1)]
print(Geometry.is_point_in_polygon(Vector2(0.5,0.5),polygon)) #print true
print(Geometry.is_point_in_polygon(Vector2(2.5,0.5),polygon)) #print false
```

I forgot, to know the points of the polygon use the polygon property of Polygon2D:

https://docs.godotengine.org/en/stable/classes/class_polygon2d.html#class-polygon2d-property-polygon

Thanks!

But I have doubt in second parameter of this method `is_point_in_polygon`

Can it be `$Polygon2D.get_polygon()`

?

- All categories
- Engine 32,374