0

I've been working on a basic game using Pygame in Python, but I can't for the life of me figure out a formula to rotate a shape by changing it's base coordinates. Here's what I have so far:

pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Singleplayer Mode")
done = False
clock = pygame.time.Clock()

p11 = 100
p12 = 100
p21 = 100 
p22 = 150
p31 = 150
p32 = 125

upspeed = 0
rightspeed = 0
topspeed = 25

pause = "false"

while not done:
    screen.fill(WHITE)        
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == K_w:
                upspeed -= 1
            if event.key == K_s:
                upspeed += 1
            if event.key == K_d:
                rightspeed += 1
            if event.key == K_a:
                rightspeed -= 1
            if event.key == K_SPACE:
                pass
            if pause == "true":
                if event.key == K_p:
                    pause = "false"
                    print("Game is unpaused.")
                if event.key == K_q:
                    done = True
            if event.key == K_p:
                pause = "true"
                print("Game is paused.")

    if rightspeed > 0:
        rightspeed -= 0.01
    if rightspeed < 0:
        rightspeed += 0.01
    if upspeed > 0:
        upspeed -= 0.01
    if upspeed < 0:
        upspeed += 0.01

    if pause != "true":
        p11 += rightspeed
        p21 += rightspeed
        p31 += rightspeed
        p12 += upspeed
        p22 += upspeed
        p32 += upspeed

    if p12 < 0:
        upspeed = 0
    if p11 < 0:
        rightspeed = 0
    if p31 > w:
        rightspeed = 0
    if p22 > h:
        upspeed = 0

    pygame.draw.polygon(screen, BLACK, [[p11, p12], [p21, p22], [p31, p32]], 0)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Essentially what I want to do is take that triangle that I create, and rotate it instead of making it move right or left when I press the d and a keys. Is there any formula I could use to change the values of p11,p12,p21,p22,p31, and p32 to accomplish this?

1adog1
  • 52
  • 10
  • Have you tried anything? For example, find your triangles centroid, then draw and rotate the points around that? – Shawnic Hedgehog Feb 23 '15 at 18:00
  • See [this](http://stackoverflow.com/questions/20023209/python-function-for-rotating-2d-objects/20025232#20025232) answer. – martineau Feb 23 '15 at 18:23
  • Sorry about it taking so long to get back to this, I tried finding the triangle's centroid and creating a formula, but it ended up just making the object move diagonally. I'm just having trouble finding a formula that will work for all 6 numbers. Here's what I tried: **2 * math.pi * 25 * (turnspeed / 360)** (turnspeed is just a variable I used to define how fast the object was turning.) – 1adog1 Feb 25 '15 at 17:22

1 Answers1

1

If you represent your coordinates as 2d coordinates [100,100], [100, 150], and [150, 125], you can rotate these coordinates by applying a matrix

[  0, -1, 
   1,  0   ] (90 degree clockwise rotation)

You can use some libraries to help you, EG numpy

x = np.array( ((100,100), (100, 150), (150, 125)) )
y = np.matrix( ((0,-1), (1, 0)) )
print (x * y)

And to get an arbitrary rotation matrix:

def get_matrix(angle) :
     return np.matrix( ((math.cos(angle),-math.sin(angle)), (math.sin(angle), math.cos(angle))) )
beiller
  • 3,105
  • 1
  • 11
  • 19
  • Thanks for the advice, but I'm a bit confused, does the np module come with Python, or is it an outside one? – 1adog1 Feb 26 '15 at 13:12
  • its used a lot. It sometimes comes standard. `import numpy as np` is the usual way to import it. – beiller Feb 26 '15 at 17:58