Reputation: 23
i am working on a little Game with Pygame and i stumbled across a little thing i couldnt fix by myself. So here is the Problem:
px, py = player_rect.center
sx, sy = snail_rect.center
direction = (px - sx, py - sy)
dx, dy = (px - sx, py - sy)
snail_rect.center = (sx + dx / 60, sy + dy / 60)
When i now execute it the snail will speed up the farther it is away from the player. But i now i want that the snail have the same speed no matter how far it is away. Im pretty new and i hope this snipped is enought so you guys can help me ^^
Upvotes: 0
Views: 98
Reputation: 94
I believe the reason why it's speeding up is because your direction isn't normalized. This is reinforced by your statement of
the snail will speed up the farther it is away from the player
Normalizing your direction should help resolve this issue for you.
Upvotes: 0
Reputation: 2921
You need to normalize your vector to a unit vector and then properly scale it by a constant speed factor. Try this:
import math
dx, dy = (px - sx, py - sy)
length = math.sqrt(dx*dx + dy*dy)
dx /= length
dy /= length
# you can use this already
# or you can do
c = 5
dx = c * dx
dy = c * dy
Upvotes: 2