I try to move a set of balls in random movement, but with uniform speed. My code lets the balls move in random drections, but with different speeds. I think it is because the random.uniform function gives different values, some close to 0 which, multiplied with the speed and time, gives lower values than the ones closer to 1. But in the animation the speed doesn't seem to be tied with the direction the balls travel (which should be the case if the values of dx and dy are the cause). i know anout normalization, but i think the random.uniform gives the same values as a normalized vector?
my questions are:
- is the random value of dx and dy the culprit of the difference in speed?
- what is the way to make all the balls move in the same speed?
my code:
self.timer = pygame.time.Clock()
self.time_passed = self.timer.tick()
self.speed = 0.5
self.dx = random.uniform(-1, 1)
self.dy = random.uniform(-1, 1)
displacement = Vector2(
self.dx * (self.speed * self.time_passed),
self.dy * (self.speed * self.time_passed)
)
self.pos += displacement #both vector objects
Thanks for any help!
Aucun commentaire:
Enregistrer un commentaire