Aug-24-2021, 09:54 AM
I think the pygame rotations are rotations about the Z axis (axis pointing out of the screen). Sounds like you want to rotate about the Y axis instead. This would make appear like the rectangle is coming out of the screen. Like this?
import math import tkinter as tk class Point(): '''A point in space''' def __init__(self, x, y, z): self.x = x self.y = y self.z = z def rotate_y(self, angle, center=(0, 0, 0)): '''Rotate self around center.y. Angle is in radians''' cx, cy, cz = center x = self.x - cx z = self.z - cz d = math.hypot(x, z) theta = math.atan2(x, z) + angle self.z = cz + d * math.cos(theta) self.x = cx + d * math.sin(theta) def __str__(self): return f'({self.x}, {self.y}, {self.z})' class Rectangle(): def __init__(self, width, height): self.points = [ Point(-width/2, height/2, 0), Point(width/2, height/2, 0), Point(width/2, -height/2, 0), Point(-width/2, -height/2, 0), ] def rotate_y(self, angle, center=(0, 0, 0)): '''Rotate self around center.y. Angle is in radians''' for point in self.points: point.rotate_y(angle, center) def draw(self, canvas, center=(0, 0)): '''Draw self on canvas''' cx, cy = center for i in range(4): a = rectangle.points[i] b = rectangle.points[(i+1)%4] canvas.create_line(a.x+cx, a.y+cy, b.x+cx, b.y+cy) def __str__(self): return f'{self.points[0]}, {self.points[1]}, {self.points[2]}, {self.points[3]}' rectangle = [] def rotate_y(rectangle, angle, center=(0, 0, 0)): canvas.delete("all") rectangle.rotate_y(angle, center) rectangle.draw(canvas, (140, 100)) canvas.after(10, lambda: rotate_y(rectangle, angle, center)) root = tk.Tk() canvas = tk.Canvas(root, width=200, height=200) canvas.pack() rectangle = Rectangle(80, 80) rotate_y(rectangle, 0.02, (-40, 0, 0)) root.mainloop()For a more realistic view the rectangle should be drawn in perspective instead of drawing the projection.