Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotating a polygon around it's center #34

Closed
scofield-ua opened this issue May 25, 2016 · 4 comments
Closed

Rotating a polygon around it's center #34

scofield-ua opened this issue May 25, 2016 · 4 comments
Labels

Comments

@scofield-ua
Copy link
Contributor

I have a problem. Is it possible to rotate polygon around it's center using Polygon class functions?

I'm trying to use rotate function, but it's return not what I expected.

Here is how my polygon looks by default:
1

After rotate(1):
2

@jriecken
Copy link
Owner

How are you constructing the Polygon - do you have some example code?

The rotate method rotates using the pos of the Polygon as the origin of rotation (also it takes in the angle in radians, not degrees)

So if you use the pos as the center of the polygon and then define the points around/relative to that position it will rotate around that.

@scofield-ua
Copy link
Contributor Author

scofield-ua commented May 26, 2016

How are you constructing the Polygon - do you have some example code?

Sure, here is example code:

var P = SAT.Polygon; 
var V = SAT.Vector;
var points = [];

var angle = 0.05;
var r = 64;
while (angle < 2 * Math.PI / 2) {
    var x = r * -Math.cos(angle);
    var y = r * -Math.sin(angle);

    points.push(new V(x, y));

    angle += 0.5;
}

var polygon = new P(new V(100, 100), points);

Sorry about stupid question, I'm probably just don't understand something.

@jriecken
Copy link
Owner

Hm, that seems to work for me - take a look at this JSFiddle: https://jsfiddle.net/1d67zsaw/

I copied your code, drew the polygon, rotated it, and drew it again - it rotates around the center of the half-circle.

@scofield-ua
Copy link
Contributor Author

Yeah. Thanks man. Seems like odd behavior from engine polygon draw method.

Nice library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants