How to calculate the location of points in a rotating form AS3

I am creating a game in which I draw a series of polygons, creating points around the radius of the cricket.

Later, I rotate the shapes, and I need to calculate the new location (X, Y) of the points based on the rotation. I have an old XY of each point, XY of the center of the shape, radius of the shape and rotation.

Look at my problem diagram.

Diagram of problem

+4
source share
2 answers

It should be possible to use matrix transformations for this, but you can also do it manually: http://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/2drota.htm

In this way,

newX = initialX * Math.cos(angle) - initialY * Math.sin(angle); newY = initialY * Math.cos(angle) + initialX * Math.sin(angle); //Angle is in radians btw 

This assumes that the starting X / Y refers to the center of rotation, so before starting you will need to subtract the center point and then add it again after calculation to correctly position it.

Hope this helps!

+3
source

For each do point:

 alpha = arctan2(x, y) len = sqrt(x^2 + y^2) newX = len * cos(alpha + rotation) newy = len * sin(alpha + rotation) 

The original [x, y] and new [newX, newY] coordinates are relative to the center of your rotation. If your original [x, y] is absolute, you need to calculate the relative first:

 x = xAbs - xCenter y = yAbs - yCenter 

Make sure your arctan2 function provides a PI / 2 or -PI / 2 result if x = 0. The primitive arctan functions do not allow x = 0.

+1
source

Source: https://habr.com/ru/post/1390064/


All Articles