Lennart Wijers
Lennart Wijers

Reputation: 235

How do I move sprites in random directions but with uniform speed python/pygame?

I try to move a set of balls in random movement, but with uniform speed. My code lets the balls move in random directions, 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 random.uniform gives the same values as a normalized vector?

My questions are:

  1. Is the random value of dx and dy the culprit of the difference in speed?
  2. 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!

Upvotes: 1

Views: 1103

Answers (2)

skrx
skrx

Reputation: 20438

Just start with a vector with the desired length (speed) and rotate it by a random angle.

self.velocity = Vector2(5, 0).rotate(random.randrange(360))

Regarding question 1, since you give the vector random x and y values, the resulting vectors will all have completely different lengths/magnitudes, therefore the sprites move with different speeds. For example you could get a Vector2(0, 0) and a Vector2(1, 1) (it's obvious that they don't have the same length).

You could actually normalize the vectors and then scale them, but better use the rotate method. (Vectors of length zero can't be normalized.)

Upvotes: 1

Josep Valls
Josep Valls

Reputation: 5560

Use an angle to derive your x and y.

self.angle = random.uniform(-1, 1)
self.dy = math.sin(self.angle * math.pi/2)
self.dx = math.cos(self.angle * math.pi/2)

Upvotes: 2

Related Questions