Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
angle of rotation
#1
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()
   
Reply
#2
add
    if angle_radian < 0:
        angle_radian += math.tau
after the angle_radian = ... in line 28
Reply
#3
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()
Reply
#4
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.
Reply
#5
Thanks, I will definitely try your code!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Sprite image rotation problem XavierPlatinum 4 2,310 Jul-25-2022, 01:31 PM
Last Post: Ruslan8819
  [PyGame] Rotation Problem in PyGame thunderbird028 1 2,697 Nov-14-2019, 06:49 AM
Last Post: Windspar
  [PyGame] Converting PyGame 2 axis joystick float to 360 angle archieab 1 3,338 Sep-26-2018, 05:40 PM
Last Post: archieab
  What's the angle? microphone_head 0 1,881 Aug-13-2018, 08:38 PM
Last Post: microphone_head

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020