Skip to content
Advertisement

How to make sprite move to point along a curve in pygame

I’m doing a pygame project for practice and I need a sprite to move to some point on screen and I did it, but it moves in a straight line and I would like to learn how to make it move to the same point in a curve.

def move_to_point(self, dest_rect, speed, delta_time):

        #  Calculates relative rect of dest
        rel_x = self.rect.x - dest_rect[0]
        rel_y = self.rect.y - dest_rect[1]
        
        # Calculates diagonal distance and angle from entity rect to destination rect
        dist = math.sqrt(rel_x**2 + rel_y**2)
        angle =  math.atan2( - rel_y,  - rel_x)
        
        # Divides distance to value that later gives apropriate delta x and y for the given speed
        # there needs to be at least +2 at the end for it to work with all speeds
        delta_dist = dist / (speed * delta_time) + 5
        print(speed * delta_time)
        
        # If delta_dist is greater than dist entety movement is jittery
        if delta_dist > dist:
            delta_dist = dist
        
        # Calculates delta x and y
        delta_x = math.cos(angle) * (delta_dist)
        delta_y = math.sin(angle) * (delta_dist)
        

        if dist > 0:
            self.rect.x += delta_x 
            self.rect.y += delta_y 

This movement looks like

this

and I would like it to be like

this

Advertisement

Answer

There are many many ways to achieve what you want. One possibility is a Bézier curve:

def bezier(p0, p1, p2, t):
    px = p0[0]*(1-t)**2 + 2*(1-t)*t*p1[0] + p2[0]*t**2
    py = p0[1]*(1-t)**2 + 2*(1-t)*t*p1[1] + p2[1]*t**2   
    return px, py

p0, p1 and p2 are the control points and t is a value in the range [0,0, 1,0] indicating the position along the curve. p0 is the start of the curve and p2 is the end of the curve. If t = 0, the point returned by the bezier function is equal to p0. If t=1, the point returned is equal to p2.

Also see PyGameExamplesAndAnswers – Shape and contour – Bezier


Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock() 

def bezier(p0, p1, p2, t):
    px = p0[0]*(1-t)**2 + 2*(1-t)*t*p1[0] + p2[0]*t**2
    py = p0[1]*(1-t)**2 + 2*(1-t)*t*p1[1] + p2[1]*t**2   
    return px, py

dx = 0
run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    
    pts = [(100, 100), (100, 400), (400, 400)]

    window.fill(0)
    for p in pts:
        pygame.draw.circle(window, (255, 255, 255), p, 5)
    for i in range(101):
        x, y = bezier(*pts, i / 100)
        pygame.draw.rect(window, (255, 255, 0), (x, y, 1, 1))

    p = bezier(*pts, dx / 100)
    dx = (dx + 1) % 101
    pygame.draw.circle(window, (255, 0, 0), p, 5)
    pygame.display.update()

pygame.quit()
exit()
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement