Python/Pygame 中的矩形旋转

发布于 2024-12-28 15:02:11 字数 860 浏览 2 评论 0原文

嘿,我正在尝试绕其中心旋转一个矩形,当我尝试旋转该矩形时,它同时向上和向左移动。有人对如何解决这个问题有任何想法吗?

def rotatePoint(self, angle, point, origin):
    sinT = sin(radians(angle))
    cosT = cos(radians(angle))
    return (origin[0] + (cosT * (point[0] - origin[0]) - sinT * (point[1] - origin[1])),
                  origin[1] + (sinT * (point[0] - origin[0]) + cosT * (point[1] - origin[1])))

def rotateRect(self, degrees):
    center = (self.collideRect.centerx, self.collideRect.centery)
    self.collideRect.topleft = self.rotatePoint(degrees, self.collideRect.topleft, center)
    self.collideRect.topright = self.rotatePoint(degrees, self.collideRect.topright, center)
    self.collideRect.bottomleft = self.rotatePoint(degrees, self.collideRect.bottomleft, center)
    self.collideRect.bottomright = self.rotatePoint(degrees, self.collideRect.bottomright, center)

Hey I'm trying to rotate a rectangle around its center and when I try to rotate the rectangle, it moves up and to the left at the same time. Does anyone have any ideas on how to fix this?

def rotatePoint(self, angle, point, origin):
    sinT = sin(radians(angle))
    cosT = cos(radians(angle))
    return (origin[0] + (cosT * (point[0] - origin[0]) - sinT * (point[1] - origin[1])),
                  origin[1] + (sinT * (point[0] - origin[0]) + cosT * (point[1] - origin[1])))

def rotateRect(self, degrees):
    center = (self.collideRect.centerx, self.collideRect.centery)
    self.collideRect.topleft = self.rotatePoint(degrees, self.collideRect.topleft, center)
    self.collideRect.topright = self.rotatePoint(degrees, self.collideRect.topright, center)
    self.collideRect.bottomleft = self.rotatePoint(degrees, self.collideRect.bottomleft, center)
    self.collideRect.bottomright = self.rotatePoint(degrees, self.collideRect.bottomright, center)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽你眉间 2025-01-04 15:02:11

旋转代码看起来不错 - 但是,您知道 pygame 的内部结构不适用于旋转的矩形,是吗?

除非您自己编写了一些带有新矩形角的代码,否则它的作用是定义一个新矩形,其边平行于 Surface 边缘,其中原始矩形在旋转时可以内切,而不是内切于 Surface 的矩形。与原始尺寸相同但倾斜角度相同。旋转后向其传递“self.collideRect”对象的任何 Pygame 函数都会执行以下操作:将矩形视为与表面对齐,
就像它是用现在的角创建的一样。

如果您的代码要求您在旋转的矩形内检查某些内容,甚至进行绘制,则必须在旋转之前执行所有计算,并且仅在显示所需内容时执行坐标旋转。也就是说,您使用全局坐标变换,该变换应用于渲染的最后一步。

The rotation code looks to be fine - but, you are aware that pygame's internals don't work with rotated rectangles, do you?

Unless you have some code you wrote yourself with the new rectangle corners, what this does is to define a new rectangle, with sides parallel to the Surface edges, where the original rectangle, when rotated, could be inscribed to, not a rectangle at the same size than the original at a skewed angle. Any Pygame function to which you pass the "self.collideRect" object after the rotation will just do that: treat the rectangle as aligned to the surface,
just as if it has been created with the corners it has now.

If your code requires you to check for things, or even draw, inside a rotated rectangle, you have to perform all the calculations as they where prior to the rotation, and just perform the coordinate rotation at the time of displaying what you want. That is, you work with a global coordinate transform, that is applied in the last step of rendering.

九歌凝 2025-01-04 15:02:11

也许这可以帮助你:

#load image
image1 = pygame.image.load(file)
#get width and height of unrotated image
width1,height1 = image1.get_size()
#rotate image
image2 = pygame.transform.rotate(image1, angle)
#get width,height of rotated image
width2,height2 = image2.get_size()
#blit rotated image (positon - difference of width or height /2)
display.blit(image2,[round(x - (width1 - width2)/2),round(y - (height1 - height2)/2)])

Maybe this can help you:

#load image
image1 = pygame.image.load(file)
#get width and height of unrotated image
width1,height1 = image1.get_size()
#rotate image
image2 = pygame.transform.rotate(image1, angle)
#get width,height of rotated image
width2,height2 = image2.get_size()
#blit rotated image (positon - difference of width or height /2)
display.blit(image2,[round(x - (width1 - width2)/2),round(y - (height1 - height2)/2)])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文