python - Why is this way of finding the angle between two points so inaccurate? -


I use one another to try to get a phantom to pierce each other with angles. This function I have used, found online:

  def findAngle (x, y, x2, y2): deltaX = x2 - x Delta Y = y2 - y math.Tan 2 , Deltaax)   

However, it is very wrong when two sprites are around the same X, even then they are usually 100-200 pixels away from each other.

This is my complete program that you run for yourself.

  import pygame import math screen_size = screen_width, screen_height = 700,500 screen = pygame.display.set_mode (screen_size) clock = pygame.time.Clock () black = (0, 0, 0) white = (255, 255, 255) red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) square sprite (pygame.sprite.Sprite): def __init __ (self ): Pygame.sprite.Sprite .__ init __ (self) class particle (sprite): def __init __ (self, x, y, size): self.image = pygame.Surface ((size, shape)) self.rect = Pygame.Rect (x, y, shape, size) self.angle = 0 self.speed = 0 def Draw (self): self.rect.x - = math.cos (self.angle) * self.speed self. Rect.y - = Math.sin (self.angle) * self.speed pygame.draw.circle (screen, black, (auto. Correct. X, self. Net), df questengel (x, y, x2, y2): deltaX = x2 - x deltaY = y2 - y return math.atan2 (deltaY, deltaX) class main: p1 = Particle (100,100,16) p2 = Particle (600,400,5) p2.angle = findAngle (100,100,600,400) P2.speed = 2 done = Not being incorrect: For events in pygame.event.get (): If event .type = = Pygame.QUIT: did = true if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: did = true if event.key == pygame.K_r: pass pygame.display.init () Screen .fil (white) p1.draw () p2.draw () pygame.display.flip () clock.tick (60) pygame.display.quit () pygame.quit ()    

You get x-coordinate And Sin should use cos for y-coordinates. You are backward in the Attractive function.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -