0

Hi im tryin to make simulator for parking with use of genetic alghorythm, and from the begining im having trouble with colliding with mask i dont what im doing wrong but my track mask and car mask are colliding very wierdly.

Below is how im drawing and rotating my car

    def draw(self, screen):  # 3
        self.car_rect.topleft = (int(self.x), int(self.y))
        rotated_image = pygame.transform.rotate(self.car_img, self.angle)
        new_rect = rotated_image.get_rect(center=self.car_img.get_rect(topleft=self.car_rect.topleft).center)
        self.car_mask = pygame.mask.from_surface(rotated_image)
        screen.blit(rotated_image, new_rect.topleft)

Theres how im using collision

    def collide(self ,mask):
        self.car_mask = pygame.mask.from_surface(self.car_img)
        offset = (int(self.x),int(self.y))
        poi = mask.overlap(self.car_mask,offset)
        return poi

Below is my game loop where im moving with car and drawing it

        screen.screen.fill((175, 174, 174))  # 9
        screen.screen.blit(track.track_img, (0, 0))
        car1.draw(screen.screen)
        pygame.display.flip()
        pygame.display.update()
        clock.tick(60)
        events = pygame.event.get()
        pressed = pygame.key.get_pressed()
        car1.speed *= 0.9
        if pressed[pygame.K_UP]: car1.speed += 0.5  # 6
        if pressed[pygame.K_DOWN]: car1.speed -= 0.5  # 6
        if pressed[pygame.K_LEFT]: car1.angle += car1.speed / 4  # 7
        if pressed[pygame.K_RIGHT]: car1.angle -= car1.speed / 4  # 7
        car1.x += car1.speed * math.cos(math.radians(+car1.angle))  # 8
        car1.y -= car1.speed * math.sin(math.radians(car1.angle))  # 8
        offset = (int(car1.x), int(car1.y))
        if car1.collide(track.track_mask) != None:
            print("collide")

i dont know where problem is because mask i moving with car but on corners it stuck even if its now on the border. For offset im using just position of car bcs map is drawing at 0,0.

Kajstrl
  • 3
  • 2
  • You have to get `car_mask` after `car1.angle` changed and before the collision test. – Rabbid76 Apr 16 '22 at 06:25
  • yeah but it seem like itsnot turning mask at all but when i draw outline of it , it is changing good that what you said is right but it would make problem with just one frame delay – Kajstrl Apr 16 '22 at 08:47
  • The offset ist car_rect.topleft – Rabbid76 Apr 16 '22 at 09:20
  • didnt work theres video how its working (not working) https://youtu.be/w9as62rihc0 it seems like its not turning but as you can see the outline of mask is turning – Kajstrl Apr 16 '22 at 09:58

0 Answers0