1

First, I calculated the Euclidean distance then I tried to iterated using loop to go to certain point using slope but the moving path doesn't like a straight line. Please help me to slove.

def move(roborect,grad):
   power=(x_cord[0]-roborect.x)**2+(y_cord[0]-roborect.y)**2
     distance=math.sqrt(power)
     if(distance>15):
        roborect.y=int(grad*roborect.x)+roborect.y
        roborect.x=roborect.x+5
        print(distance)
   

1 Answers1

0

Compute the direction vector from the object to the target. Normalize the direction vector (Unit vector). Scale the vector to a certain length and add the vector to the position:

dx = x_cord[0]-roborect.x
dy = y_cord[0]-roborect.y
distance = math.sqrt(dx*dy + dy*dy)
if distance > 15:
    vx = dx * 5 / distance 
    vy = dy * 5 / distance 
    roborect.x += vx
    roborect.x += vy
Rabbid76
  • 202,892
  • 27
  • 131
  • 174