Priya
Priya

Reputation: 339

Pygame for possible trajectories planning for a car and make the car follow one of that path/trajectory

I have to create a visualization where I have to create a car (I considered to represent it as a rectangle) and develop trajectories for that car depending on its lateral & longitudinal acceleration. My problem is I am unable to create trajectories. I am doing this as a part of own project. Hence I have no idea if I am in the right direction or not. Guide me if I am wrong.

Problem1 : How do I show predicted path trajectories (beginner step to create only 8 trajectories and not many) ? Is a possible way to also create trajectories also like a curve using Pygame? If not, can I use a mix pf pygame and tkinter to create a mix of curved and straight line trajectories?

Problem2 : Make my rectangular car run along the chosen trajectory (curved line or straight line) or make it follow the path defined in trajectory.

Solutions Tried:

I have taken a rectangular sprite as a car with an idea that I can make it move around the desired trajectory. I am successful with sprite creation and moving it. But I am unable to show display any trajectory curve and make the car follow the path. I am not sure if I have to take trajectories also as Sprite or only car as Sprite is sufficient and trajectories as lines/curves?

Code of the above same explanation is as follows:

My Sprite Class :

class Vehicle(pygame.sprite.Sprite):
def __init__(self,x,y,slip_angle,length=4):
    super().__init__() 
    width = 50
    height = 50
    self.image =  pygame.Surface([width,height])

    #Create a car
    self.image.fill(RED)
    self.image.set_colorkey(RED)
    pygame.draw.rect(self.image, WHITE, [0, 0, width, height])
    self.rect = self.image.get_rect()

    #Parameters of car
    self.position = position = Vector2(x, y)
    self.velocity = Vector2(0.0, 0.0)
    self.slip_angle = slip_angle
    self.length = length
    self.max_velocity = 20
    self.brake_deceleration = 10
    self.free_deceleration = 2
    self.initial_velocity = 0

    self.long_acceleration = 0.0
    self.lat_acceleration = 0.0
    self.acceleration = 0.0
    self.steering = 0.0

def update(self,dt):
    self.velocity += (self.acceleration * dt, 0)
    self.velocity.x = max(-self.max_velocity, min(self.velocity.x,          self.max_velocity))

    if self.steering:
        turning_radius = self.length / sin(radians(self.steering))
        angular_velocity = self.velocity.x / turning_radius
    else:
        angular_velocity = 0

    self.position += self.velocity.rotate(-self.angle) * dt
    self.slip_angle += degrees(angular_velocity) * dt 

Trajectory Class:

class trajectory(object):
def __init__(self,x,y):
    self.x = x
    self.y = y

def draw():
    x_value = []
    time = []
    for acc in range(10):
        for dt in range(0,10,0.05):
            x = vehicle1.initial_velocity + (vehicle1.velocity*dt) + (0.5 * vehicle1.long_acceleration * dt* dt)
            x_value.append(x)
            time.append(dt)
            coordinates = (x_value,time)

        canvas_1 = Canvas(root,700,600,background='pink')
        canvas_1.grid(row=0,column=1)
        x1 = coordinates[acc]
        y1 = coordinates[time]
        canvas_1.create_line(x1,y1)


        #pygame.draw.line(screen,GREEN,list(coordinates),(700, 600))
def update(self):
    #self.angle = vehicle1.slip_angle*pi / 180
    self.velocity_h = vehicle1.velocity*cos(angle)
    self.velocity_v = vehicle1.velocity*sin(angle)

Pygame Main Logic :

#main Logic
proceed = True

#Capturing events till exit
while proceed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            proceed = False

    vehicle1.update(dt)
    sprites_list.update(dt)
    screen.fill(WHITE)
    #screen.pygame.Surface.fill(color, rect=None, special_flags=0)
    pygame.draw.line(screen, BLACK, [0, 0],  [700, 300], 5)

    # self.screen.blit(rotated, [20,30])
    sprites_list.draw(screen)

    pygame.display.flip()

    clock.tick(60)

pygame.quit()

I thank you for your advices and support. Happy to accept any guidance

Upvotes: 0

Views: 1234

Answers (1)

Kingsley
Kingsley

Reputation: 14916

A simple re-working of your trajectory.draw() should sort it out. I noticed the initial x and y passed to the constructor don't seem to be used. Is it intended that the trajectory is always from ( 0, 0 ) or suchlike?

def draw( screen, initial_vel, vel, accel ):
    coordinates = []
    for acc in range(10):
        for dt in range( 0, 10, 0.05 ):
            x = initial_vel + ( vel * dt ) + ( 0.5 * accel * dt * dt )
            coordinates.append( ( x, dt ) )

    if ( len( coordinates ) > 1 ):
        PINK = ( 255, 192, 203 )
        pygame.draw.lines( screen, PINK, False, coordinates )    

Pass your Window and Vehicle Velocities as a parameters when calling trajectory.draw(), rather than relying on global variables.

my_trajectory.draw( screen, vehicle1.initial_velocity, vehicle1.velocity, vehicle1.long_acceleration )

Upvotes: 2

Related Questions