0
$\begingroup$

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):

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

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

$\endgroup$
1
$\begingroup$

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

$\endgroup$
4
  • $\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.