旋转矩阵缩小

时间:2014-09-16 16:58:55

标签: python python-3.x 3d rotational-matrices

我正在尝试构建一些微型" 3d引擎"了解这一领域的基础知识。 除旋转外,一切都很好。我使用标准旋转矩阵来做到这一点,但所有3个可能的旋转不仅旋转点,而且还缩小它们。此行为无意或无意。 以下是代码的相关部分(我认为):

class Point:
def __init__(self, x, y, z, ui):
    self.x = mpf(x)
    self.y = mpf(y)
    self.z = mpf(z)
    self.ui = ui

def subtractVectorFromPoint(self, vector):
    self.x -= vector.x
    self.y -= vector.y
    self.z -= vector.z
    return self

def subtractPointFromPoint(self, point):
    TempVector = Vector(0, 0, 0)
    TempVector.x = self.x - point.x
    TempVector.y = self.y - point.y
    TempVector.z = self.z - point.z
    return TempVector

def setPointToPoint(self, point):
    self.x = point.x
    self.y = point.y
    self.z = point.z
    return self


class Vector:
def __init__(self, x, y ,z):
    self.x = mpf(x)
    self.y = mpf(y)
    self.z = mpf(z)

def rotateXY(self, degrees):
    radians = mpf(math.radians(degrees))

    self.x = (math.cos(radians) * self.x) + (- math.sin(radians) * self.y)
    self.y = (math.sin(radians) * self.x) + (math.cos(radians) * self.y)
    return self


#rotate xy plane by 15 degrees on pressing r
if event.key == pygame.K_r:
    origin = Vector(0, 0, 0)
    UI.draw_background()
    for point in pointList:
        tempVector = point.subtractPointFromPoint(origin)
        point.setPointToPoint(origin)
        point.addVectorToPoint(tempVector.rotateXY(15))
        point.drawPoint()

这应该是我想要的全部。欢迎任何指向我出错的地方。 我知道这里的课程错过了一个缩进,我在这个网站上的布局很糟糕:)

P.S。我尝试使用" mpf()"函数提高精度,但结果为0。

1 个答案:

答案 0 :(得分:0)

问题出在这一部分:

def rotateXY(self, degrees):
    radians = mpf(math.radians(degrees))

    self.x = (math.cos(radians) * self.x) + (- math.sin(radians) * self.y)
    self.y = (math.sin(radians) * self.x) + (math.cos(radians) * self.y)
    return self

self.y正在使用已经调整过的self.x进行计算,从而导致出现问题。

对此的修复是使用如下的临时变量:

def rotateXY(self, degrees):
    radians = mpf(math.radians(degrees))

    tempX = (math.cos(radians) * self.x) + (- math.sin(radians) * self.y)
    self.y = (math.sin(radians) * self.x) + (math.cos(radians) * self.y)
    self.x = tempX
    return self