I'm currently developing a pygame based go to goal behavior. I approached the goal by Euclidean distance and vectorized mathematics. Here is the code

def move(roborect,i):

    dx = 8*test_x[i]-roborect.x
    dy = test_y[i]-roborect.y
    distance = math.sqrt(dx*dx + dy*dy)
    if distance > 70:
        vx = dx * 2/ distance 
        vy = dy * 2 / distance 
        roborect.x += vx
        roborect.y += vy
        return 1

But I could not understand to approach the orientation of the robot towards to goal.Please help.


What have you tried? This seems like it should simply be the inverse tangent of dy/dx.

  • $\begingroup$ I have tried try: grad=(test_x[i]-roborect.y)/(test_y[i]-roborect.x) angle=math.degrees(math.atan(grad)) print(angle) except: pass $\endgroup$ Jun 14 at 1:12
  • $\begingroup$ Theres is possibility grad = infinitie.. so in the exception you should handle that math error.. except : angle = 90 $\endgroup$
    – Albert H M
    Jun 14 at 1:20
  • $\begingroup$ Try atan2(dy, dx). $\endgroup$
    – SteveO
    Jun 14 at 1:25
  • $\begingroup$ Yes, I used dx/dy instead of dy/dx $\endgroup$ Jun 14 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.