i got the rotating of the triangle down with this functions
def _rotate(self, center, scale, mouse_pos):
dx = mouse_pos[0] - center[0]
dy = mouse_pos[1] - center[1]
len = math.sqrt(dx * dx + dy * dy)
dx, dy = (dx * scale / len, dy * scale / len) if len > 0 else (1, 0)
pts = [(-0.5, -0.866), (-0.5, 0.866), (1.1, 0.0)]
pts = [(center[0] + p[0] * dx + p[1] * dy, center[1] + p[0] * dy - p[1] * dx) for p in pts]
return pts
but then i use another function to get the middle of the triangle with the formula (Ax + Bx + Cx)/3
def getCenter(self):
# formula: (Ax + Bx + Cx)/3
x = (self.x1 + self.x2 + self.x3)/3
y = (self.y1 + self.y2 + self.y3)/3
return (x, y)
here is where the two functions get called
def update(self):
# point to mouse
mouseP = pygame.mouse.get_pos()
center = self.getCenter(mouseP)
points = self._rotate(center, 12, mouseP)
self.x1 = points[0][0]
self.x2 = points[1][0]
self.x3 = points[2][0]
self.y1 = points[0][1]
self.y2 = points[1][1]
self.y3 = points[2][1]
and the move function
def move(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
self.x1 -= self.vel
self.x2 -= self.vel
self.x3 -= self.vel
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
self.x1 += self.vel
self.x2 += self.vel
self.x3 += self.vel
if keys[pygame.K_w] or keys[pygame.K_UP]:
self.y1 -= self.vel
self.y2 -= self.vel
self.y3 -= self.vel
if keys[pygame.K_s] or keys[pygame.K_DOWN]:
self.y1 += self.vel
self.y2 += self.vel
self.y3 += self.vel
with all of that this happens https://streamable.com/1mfibu im using pygame