Check points is inside a polygon - doesn't work correct

I create a room based on any Array with points. Objects that can be moved are added to the room. The goal is to block the possibility of moving the object out of the room.

When the object is moved, I check if all 4 bottom points belong to the floor - if so, you can move the object further - if not, it means that the object would go behind the wall, so you have to restore it to the previous position.

The code can be seen here: https://stackoverflow.com/questions/55884693/check-points-is-inside-a-polygon-doesnt-work-correct.

Prisoner849 user - gave in response an algorithm that checks if points are inside a polygon. It works great until I add rotation to the polygon - this solution does not work. Ultimately, orbitControls will be added to the cameras.

The problem can be seen here:
https://codepen.io/jagodanat/pen/WWmqZm

I want the checkPoints function to show true or false depending on whether all 4 points are on the floor or not.

1 Like

Hi!
Are you looking for something like this?
https://codepen.io/prisoner849/pen/ROdXzw?editors=1010

Just another idea is to use polybool.js (related topic [Closed] How to split/separate a single ShapeGeometry into individual Geometries base on faces?) to work with polygons: find intersection of the polygon of your floor and the polygon of your four point rectangle, if the result (its area or coordinates of vertices or amount of veriteces) is not the same as your original rectangle, then your rectangle is out of the floor.

2 Likes

Thank you very much :slight_smile: I think that the codepen solution will work for me :slight_smile:

Checking the points works perfectly, but it does not quite solve my stackowerflow problem.
the idea is that the green square (in the original will be different models) could not go beyond the lines. Any attempt to modify the cube.position ends badly.
Look at:
https://codepen.io/jagodanat/pen/oORyLJ?editors=0000

You know position of the cube, you know its size, thus you have to check four points instead of one.

thanks for help - everything works as it should be :slight_smile:

1 Like

@blue
You’re welcome :slight_smile: :beers: