Python Forum

Full Version: angle of rotation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone!
Tell me, please - how can I determine the angle between the bot and the click? I wrote this in the program, but if the click is on the left and higher relative to the bot, the angle is negative. How to calculate the angle in the range from 0 to 360 degrees?

import tkinter as tk
from tkinter import*
import math


root = tk.Tk()
root.geometry('500x500+2+10')



def target(event):
    
    bot_center_x = canvas1.coords(bot)[0] + 15
    bot_center_y = canvas1.coords(bot)[1] + 15
    
    clik_x = event.x
    clik_y = event.y
    
    #print('bot:  '+str((int(bot_center_x), int(bot_center_y))))
    #print('clik: '+str((clik_x, clik_y)))
    
    
    angle(bot_center_x, bot_center_y, clik_x, clik_y)


def angle(bot_center_x, bot_center_y, clik_x, clik_y):
    
    angle_radian = math.atan2(clik_y - bot_center_y, clik_x - bot_center_x)
    angle_gradus = math.degrees(angle_radian)
    
    print('angle_radian: '+str(round(angle_radian, 2)))
    print('angle_gradus: '+str(round(angle_gradus, 2)))
    print()



canvas1 = Canvas(root, width = 500, height = 500)
canvas1.place(x = 0, y = 0)

bot = canvas1.create_oval(200, 200, 230, 230)



root.bind('<Button-1>', target)

root.mainloop()
[attachment=837]
add
    if angle_radian < 0:
        angle_radian += math.tau
after the angle_radian = ... in line 28
I entered into the program correction bot coefficients from the current bot direction. Now shows the exact value of the angle.

import tkinter as tk
from tkinter import*
import math


root = tk.Tk()
root.geometry('500x500+2+10')



def target(event):
    
    bot_center_x = canvas1.coords(bot)[0] + 15
    bot_center_y = canvas1.coords(bot)[1] + 15
    
    clik_x = event.x
    clik_y = event.y
    
    angle(bot_center_x, bot_center_y, clik_x, clik_y)


def angle(bot_center_x, bot_center_y, clik_x, clik_y):
    
    if  angle_mov == 'nord':  #current bot direction
        k1 = 90
        k2 = 270
    elif angle_mov == 'east': #current bot direction
        pass
    elif angle_mov == 'south':#current bot direction
        pass
    elif angle_mov == 'west': #current bot direction
        pass
    
    if clic_x >= bot_x:
        angle_grad = int(math.degrees(math.atan((clic_y - bot_center_y) / (clic_x - bot_center_x))) + k1)
    else:
        angle_grad = int(math.degrees(math.atan((clic_y - bot_center_y) / (clic_x - bot_center_x))) + k2)
        
        print('angle: '+str(angle_grad))



canvas1 = Canvas(root, width = 500, height = 500)
canvas1.place(x = 0, y = 0)

bot = canvas1.create_oval(200, 200, 230, 230)

angle_mov = 'nord' #current bot direction

root.bind('<Button-1>', target)

root.mainloop()
This is the code I use to get enemies shoot at the player:
self.gun_rot = (vec(self.game.player.current_ship.rect.center) - self.position).angle_to(vec(1, 0))
It should work for you after modification like:
vec = pygame.math.Vector2

angle = (vec(mouse_pos) - vec(bot_position)).angle_to(vec(1, 0))
angle will be the angle to the bot from the mouse pointer relative the to the x axis.
Thanks, I will definitely try your code!