0

I've created a method for objects (enemies) in pygame that makes them move in 2D from current position to a destination given in x,y-coordinates. When executing this method the objects behave almost as intended, but during traversal the direction isn't stable. E.g., when moving in a positive x-direction and negative y-direction the object moves solely in the positive x-direction for the first half of the movement, then starts shifting its direction in the negative y-direction incrementally. Instead of the expected linear motion with a constant direction I get a curve.

I've expanded this method to accept multiple instructions for testing, giving the object four destinations to travel to sequentially. Reducing the sequence to one instruction still gives the curvey motion, so that doesn't seem the issue. What happens is that the curvey behaviour is present in 3 of the 4 instructions, the exception being the instruction where dy = - distance and dx = 0. What's peculiar is that the curving always seems to start halfway through the motion, as if correcting its trajectory.

The method accounts for combination of dx and dy being positive or negative, but for brevity sake I'll post the one example with positive dx and dy. Here's the method I've created to describe motion for the object:


    def move_to(self, destination, speed):
        distance = math.sqrt(pow((self.rect.centerx - destination[0]),2) + pow((self.rect.centery - destination[1]),2))
        if distance != 0:
            direction = pg.math.Vector2(((destination[0] - self.rect.centerx)/distance), ((destination[1] - self.rect.centery)/distance))
            self.direction = direction.normalize()
        else:
            pass

        if self.rect.centerx - destination[0] <= 0:
            if self.rect.centery - destination[1] <= 0:
                if not (self.rect.centerx >= destination[0] and self.rect.centery >= destination[1]):
                    self.rect.centerx += self.direction[0] * speed
                    self.rect.centery += self.direction[1] * speed
                else:
                    pass

Here's the object itself:

    def enemies(self):
        spawn_time = pg.time.get_ticks() / 10

        if self.enemy_spawn_switch1 == True:
            enemy = Enemy( 
            pos= (340, 350),
            groups=[self.visible_sprites, self.enemy_sprites], 
            speed=0, 
            direction=(0,1), 
            spawn_time=spawn_time, 
            health =1,
            movement_switch1 = True,
            movement_switch2 = True,
            movement_switch3 = True,
            movement_switch4 = True,
            )
            self.enemy_spawn_switch1 = False

        for enemy in self.enemy_sprites:
            destination = enemy.rect.center
            print(enemy.direction)
            if enemy.movement_switch1:
                destination = (360, 360)
                enemy.move_to(destination, speed = 1)
                if enemy.rect.center == destination:
                    enemy.movement_switch1 = False
            else: print('at target')

I've tried changing initial speed and direction to be 0 and (0,0), but that doesn't change the behaviour, as expected since those attributes get replaced when calling move_to().

When I print enemy.direction for this motion we get the following, to show that the direction isn't stable as I've expected:

    [0.83205, 0.5547]
    [0.834219, 0.551433]
    [0.836461, 0.548026]
    [0.83878, 0.544471]
    [0.841178, 0.540758]
    [0.843661, 0.536875]
    [0.846233, 0.532813]
    [0.848897, 0.528558]
    [0.851658, 0.524097]
    [0.854522, 0.519415]
    [0.857493, 0.514496]
    [0.860577, 0.509321]
    [0.863779, 0.503871]
    [0.867106, 0.498124]
    [0.862416, 0.506201]
    [0.865865, 0.500278]

etc.

Speed should be the fraction corresponding to the direction, so the position should be stable along the expected line, which is also what seemingly happens visually. However, the direction is janky, and sometimes quite radically so, only starting movement in one of the direction halfway during the motion.

It's weird to me how it almost works, yet it doesn't. I don't understand why movement sometimes should lag in one of the directions components.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

0 Answers0