My goal is to rotate 4 points around a specific origin. Initially, the points appear in the correct position without any rotation. The code for this initial positioning is as follows:
let origin = this.transform.position;
for (let i in this._pointsOrig) {
let point = this._pointsOrig[i];
this._points[i] = new Vector2(
point.x + origin.x,
point.y + origin.y
);
}
This code produces the following output:
Note: In the visualization, the blue cross represents the origin, and the green rectangle outlines the points.
https://i.sstatic.net/lxj9y.png
However, when I attempt to introduce rotation to the rectangle with the following code:
// degrees is between 0 & 360, so convert to radians
let rotation = this.transform.rotation.degrees * (Math.PI / 180);
let cos = Math.cos(rotation);
let sin = Math.sin(rotation);
let origin = this.transform.position;
for (let i in this._pointsOrig) {
let point = this._pointsOrig[i];
this._points[i] = new Vector2(
(cos * (point.x - origin.x)) + (sin * (point.y - origin.y)) + origin.x,
(cos * (point.y - origin.y)) - (sin * (point.x - origin.x)) + origin.y
);
}
The result shifts the rectangle to the bottom left corner, as shown below:
https://i.sstatic.net/LbEFB.png
I am currently unsure about how to ensure that the points rotate effectively around the intended origin.