* Compute a 2D convex hull polygon from the current projected vertices. * Useful for creating collision shapes. * @returns {Polygon} a convex hull polygon in local coordinates
()
| 876 | * @returns {Polygon} a convex hull polygon in local coordinates |
| 877 | */ |
| 878 | toPolygon() { |
| 879 | // update cached points from projected vertices |
| 880 | for (let i = 0; i < this.vertexCount; i++) { |
| 881 | this._hullPoints[i].set(this.vertices[i * 3], this.vertices[i * 3 + 1]); |
| 882 | } |
| 883 | |
| 884 | // Graham scan convex hull (sorts in place, returns a subset) |
| 885 | const hull = convexHull(this._hullPoints); |
| 886 | |
| 887 | if (this._hullPolygon === null) { |
| 888 | this._hullPolygon = new Polygon(0, 0, hull); |
| 889 | } else { |
| 890 | this._hullPolygon.setVertices(hull); |
| 891 | } |
| 892 | |
| 893 | return this._hullPolygon; |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * Render the mesh at its current state (transforms, projection, tint) to an offscreen canvas. |