python - Angular movement in pygame -
python - Angular movement in pygame -
i've made function allowes me move object diagonal:
if(myx > targetx): dx = myx - targetx else: dx = targetx - myx if(myy > targety): dy = myy - targety else: dy = targety - myy if(dy == 0): dy = 1 if(dx == 0): dx = 1 #calc motion if(dx < dy): speedy = dy/dx speedx = 1 if(dx > dy): speedy = 1 speedx = dx/dy elif(dx == dy): speedx = 1 speedy = 1 if(myx < targetx): speedx = speedx * -1 if(myy < targety): speedy = speedy * -1 homecoming speedx,speedy
the code perfectly, problem dosen't work want to. right object speeds if move closer it, looks rather odd. aware why this, there easy way prepare speed constant, not direction?
if way dx , dy scalars of vector points guy target. split magnitude of both of them, here represented dz. dx , dy represent unit vector. 1 time multiply them speed object moving @ constant speed, varrying direction.
import math #set speed how fast want guy move speed = 1 dx = myx - targetx dy = myy - targety dz = math.sqrt(dx**2 + dy**2) speedx = dx/dz * speed speedy = dy/dz * speed
python pygame movement diagonal
Comments
Post a Comment