1

I'm trying to write a script (javascript) in an API of a Virtual Table Top program so I can manipulate some tokens (Car Wars :)).

I'm sort of finding the answer, but it seems like I'm struggling and reinventing the wheel so I thought I'd ask for help. One reason I'm getting confused is the program returns results based on +y is down and Deg go clockwise which is different than what all the trig formulas want (counter clockwise and +y is up).

Here is what I have access to. Rectangle rotates around centre, Centre point(x,y), width, height, and rotation. I've got the code working for moving the rectangle in the direction of the rotation, side to side, up and down, etc. Now I need to be able to rotate it around any of the four corners or any point would be nice, but four corners are all thats needed.

It won't let me include an image since I'm new so I hope the description is good enough. I had an image all done up. :(

In the API I can't actually draw the rectangle, I can only set its rotation, and centre value. So my thought was if I can find the x,y of one corner currently, then rotate it the desired degs around the centre (I can do this easily by setting the rectangles rotation), find the new x,y of that same corner. Then I will know the offset and apply that to the centre (thats how the rectangle is moved as well).

So I need to be able to find the x,y of any corner of a rectangle at any given starting angle, then again at a new angle rotated at its centre. This offset would then be easily applied to the centre x,y and the rectangle would see to have rotated along one of its corners.

Thanks for any help you can give. I'm hoping I will eventually figure it out, just writing this description out actually has helped me think it through. But I'm currently stuck!

Konrad

4

1 回答 1

1

围绕 2d 中的任意点(例如,矩形的四个角之一)旋转的技巧是首先平移形状的顶点,以便您要围绕其旋转的点位于原点(即 0, 0)。

要实现这一点:
1. 通过 (-x, -y) 平移您的矩形。
2. 将矩形旋转所需的角度。
3. 将矩形平移 (x, y) 以将其放回原来的位置。
其中 (x,y) 是要旋转的点的 x/y 坐标。

您可以使用负角来调整顺时针旋转。

网上有很多关于此的信息,例如: http ://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/2drota.htm

于 2013-05-06T09:35:04.783 回答