旋转后多边形的平移

Translation of polygon after rotation

我有两个函数,一个在 2D canvas 项目(多边形)中执行旋转,另一个函数平移 'robot'。移动是这样的:

.

如果进行旋转运动应该是这样的:

.

这里的函数:

def rotation(self, i):
    if i == '+':
        if self.angle == 360:
            self.angle = 0.0
        self.angle += 1
        delta = radians(1)
    elif i == '-':
        if self.angle == 0:
            self.angle = 360.0
        self.angle -= 1
        delta = -radians(1)
    x0, y0 = self.center[0], self.center[1]
    tmp = list()
    pts = self.polygon
    for i in range(0, len(pts), 2):
        xa, ya = pts[i], pts[i + 1]
        xl, yl = xa - x0, ya - y0
        xll, yll = cos(delta) * xl + sin(delta) * yl, -sin(delta) * xl + cos(delta) * yl
        xr, yr = xll + x0, yll + y0
        tmp.append(xr)
        tmp.append(yr)
    self.polygon = tmp.copy()
    self.arena.coords(self.item_canvas, self.polygon)

def move_polygon(self, x, y, switch=False):
    tmp = self.polygon
    for i in range(0, len(tmp), 2):
        if not switch:
            tmp[i] += cos(radians(self.angle)) * x * 5
            tmp[i + 1] += -sin(radians(self.angle)) * y * 5
        else:
            tmp[i] += -sin(radians(self.angle)) * x * 5
            tmp[i + 1] += cos(radians(self.angle)) * y * 5
        print('x:{} y:{}'.format(cos(radians(self.angle)), -sin(radians(self.angle))))
    self.polygon = tmp.copy()
    self.update_polygon()

def move(self, dir):
    if dir == 'f':
        x, y = cos(radians(self.angle)), sin(radians(self.angle))
        self._move_polygon(x, y)
    if dir == 'b':
        x, y = -cos(radians(self.angle)), -sin(radians(self.angle))
        self._move_polygon(x, y)
    if dir == 'l':
        x, y = -sin(radians(self.angle)), -cos(radians(self.angle))
        self._move_polygon(x, y, True)
    if dir == 'r':
        x, y = sin(radians(self.angle)), cos(radians(self.angle))
        self._move_polygon(x, y, True)

想法是将 x 和 y 随角度变化的函数传递给平移函数 (move_polygon)。当角度为零时向右运动(仅在第 1 和第 3 象限,方向相反...)。

我知道我需要添加 x 和 y 偏移量来执行平移,但是与角度、中心和旋转轴的关系令人困惑和沮丧。所以我需要帮助来理解数学和执行此任务的正确方法。

所以机器人有自己的方向 self.angle 并且您想将机器人朝这个方向移动,向后移动,然后左右移动(就像射击游戏中的扫射?)?

在这种情况下,您需要更改 move 中的符号:

 if dir == 'l':
      x, y = sin(radians(self.angle)), -cos(radians(self.angle))
      self._move_polygon(x, y, True)
 if dir == 'r':
      x, y = -sin(radians(self.angle)), cos(radians(self.angle))

但是为什么在 move_polygon 中再次应用 cossin 因素 ?看来您只需要像这样使用提供的位移(使用速度系数)更新坐标:

 tmp[i] += x * 5   
 tmp[i+1] += y * 5   

同时删除 True 作为 switch 参数