相关文章推荐
奋斗的竹笋  ·  Mac OS安装Python的pip - ...·  1 周前    · 
灰常酷的口罩  ·  带有Tomcat ...·  10 月前    · 
坚强的咖啡豆  ·  QT ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Can someone please show me an example of using Vector2.as_polar() to get the distance and azimuthal angle, and then plugging that information back into Vector2.from_polar() to get the cartesian coordinates?

Let's suppose we want the distance and azimuthal angle between (0, 0) and (15, 10) , and then plug that information into Vector2.from_polar() to generate (15, 10) again.

I would explain exactly what I am doing, but it might be easier for me and everyone else to just see a working example.

You can define a vector as a radius and an angle - the polar coordinates.

Let's use a point vector in the center of the screen as the pole of our polar coordinate system.

pole = Vector2(screen_rect.center)

To set the coordinates of a vector with the help of the from_polar method, first define a vector and then pass a tuple which consists of the radius and angle:

vec = Vector2()
# This updates the cartesian coordinates of vec.
vec.from_polar((90, 60))  # 90 pixels long, rotated 60 degrees.

Then use pygame.draw.line to draw it (add vec to pole to get the target coords):

pg.draw.line(screen, (200, 90, 20), pole, pole+vec, 3)

As a demonstration for as_polar, calculate the vector to the mouse position and unpack the polar coordinates into the variables r and phi which we render later to see the current radius and angle of this vector.

r, phi = (mouse_pos-pole).as_polar()

Note that pygame's y-axis is flipped, therefore the negative angles point up and the positive angles down.

import sys
import pygame as pg
from pygame.math import Vector2
def main():
    screen = pg.display.set_mode((640, 480))
    screen_rect = screen.get_rect()
    font = pg.font.Font(None, 30)
    color = pg.Color(150, 250, 100)
    clock = pg.time.Clock()
    pole = Vector2(screen_rect.center)
    # A zero vector.
    vec = Vector2()
    # To set the coordinates of `vec` we can pass the
    # polar coordinates (radius, angle) to `from_polar`.
    vec.from_polar((90, 60))
    print(vec)  # Now take a look at the updated cartesian coordinates.
    done = False
    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
        mouse_pos = pg.mouse.get_pos()
        # `as_polar` gives us the radius and angle of the vector that
        # points to the mouse. Unpack it into the `r` and `phi` variables.
        r, phi = (mouse_pos-pole).as_polar()
        screen.fill((30, 30, 30))
        # Draw the line to the mouse pos.
        pg.draw.line(screen, color, pole, mouse_pos, 3)
        # Add the vec to the pole to get the target coordinates.
        pg.draw.line(screen, (200, 90, 20), pole, pole+vec, 3)
        # Render the radius and angle.
        txt = font.render('r: {:.1f}'.format(r), True, color)
        screen.blit(txt, (30, 30))
        txt = font.render('phi: {:.1f}'.format(phi), True, color)
        screen.blit(txt, (30, 50))
        pg.display.flip()
        clock.tick(30)
if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()
    sys.exit()

Here's another example with a sprite that follows the mouse (press "w" or "s" to accelerate). I pass the self.speed as the radius and the angle that as_polar returns as the argument to from_polar to set the velocity of the player sprite.

import sys
import pygame as pg
class Player(pg.sprite.Sprite):
    def __init__(self, pos):
        super().__init__()
        self.image = pg.Surface((50, 30), pg.SRCALPHA)
        color = pg.Color('dodgerblue1')
        pg.draw.polygon(self.image, color, ((1, 1), (49, 15), (1, 29)))
        self.orig_img = self.image
        self.rect = self.image.get_rect(center=pos)
        self.pos = pg.math.Vector2(pos)
        self.vel = pg.math.Vector2(0, 0)
        self.speed = 0
    def update(self):
        self.rotate()
        self.pos += self.vel
        self.rect.center = self.pos
    def rotate(self):
        _, angle = (pg.mouse.get_pos()-self.pos).as_polar()
        self.vel.from_polar((self.speed, angle))
        self.image = pg.transform.rotozoom(self.orig_img, -angle, 1)
        self.rect = self.image.get_rect(center=self.rect.center)
def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    all_sprites = pg.sprite.Group()
    player = Player((300, 200))
    all_sprites.add(player)
    done = False
    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
        keys = pg.key.get_pressed()
        if keys[pg.K_w]:
            player.speed += .2
        elif keys[pg.K_s]:
            player.speed -= .2
        all_sprites.update()
        screen.fill((30, 30, 30))
        all_sprites.draw(screen)
        pg.display.flip()
        clock.tick(30)
if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()
    sys.exit()
                how could you do that if you have a player sprite? pygame and you want the object to face the player sprite and shoot at it?
– Habib Ismail
                Jun 19, 2020 at 16:42
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.