Reputation: 93
I'm having a problem with a trigonometry algorithm in Python. I work on a small topdown shooter, and the way I calculate the angle and rotate the sprite works for my player, my bullets, but for my enemies (always facing the player), it does something weird..
It works fine when the player is static, but once the player moves (and the angle changes), the enemies are jittering in place, appearently moving back and forth one or two pixels each frame. I left my debugging prints in the code and will attach some of the console printout during the Problem:
class Enemy:
def __init__(self, x, y,):
self.x = x
self.y = y
self.angle = 0
self.speed = 2
self.velX = 0
self.velY = 0
self.scoreVal = 10
self.texture = pygame.image.load('char_terrorist.png')
self.image = pygame.transform.rotate(self.texture, int(self.angle))
def rotate(self):
targetX, targetY = player1.x, player1.y
relX, relY = targetX - self.x, targetY - self.y
self.angle = (180 / math.pi) * math.atan2(relY, relX)
self.image = pygame.transform.rotate(self.texture, int(self.angle)*-1-90)
self.rect = self.image.get_rect(center=(self.x, self.y))
self.velX = self.speed * math.cos(self.angle)
self.velY = self.speed * math.sin(self.angle)
print()
print('## ROTATE: velX, velY: ', self.velX, self.velY)
print('## ROTATE ANGLE: ', self.angle)
def move(self):
print('## MOVE: velX: ', self.velX, ' velY: ', self.velY)
self.x += self.velX
self.y += self.velY
def render(self):
imageRect = self.image.get_rect()
imageRect.center = self.x, self.y
playSurface.blit(self.image, imageRect)
And a portion of the console printout:
## ROTATE: velX, velY: 1.5033760962079608 1.31903764667674
## ANGLE: -93.52760115485472
## MOVE: x, y: 158.34842803029636 374.5728764810448
## MOVE: velX, velY: 1.5033760962079608 1.31903764667674
## ROTATE: velX, velY: -0.15199863628529925 -1.9942157392236701
## ANGLE: -95.89464860414104
## MOVE: x, y: 158.19642939401106 372.5786607418211
## MOVE: velX, velY: -0.15199863628529925 -1.9942157392236701
## ROTATE: velX, velY: -1.6646141877222582 1.1086296072330768
## ANGLE: -97.97691175203515
## MOVE: x, y: 156.5318152062888 373.6872903490542
## MOVE: velX, velY: -1.6646141877222582 1.1086296072330768
## ROTATE: velX, velY: 1.2713600983706417 1.5439052756794998
## ANGLE: -99.64905773569804
## MOVE: x, y: 157.80317530465945 375.2311956247337
## MOVE: velX, velY: 1.2713600983706417 1.5439052756794998
## ROTATE: velX, velY: 0.47746450045160277 -1.9421708603540784
## ANGLE: -101.8607010982417
## MOVE: x, y: 158.28063980511104 373.28902476437963
## MOVE: velX, velY: 0.47746450045160277 -1.9421708603540784
## ROTATE: velX, velY: 1.743360387120541 0.9801502745084132
## ANGLE: -100.01878896583855
## MOVE: x, y: 160.02400019223157 374.269175038888
## MOVE: velX, velY: 1.743360387120541 0.9801502745084132
## ROTATE: velX, velY: -1.2224714556282295 1.5828971982331
## ANGLE: -98.30254758732487
## MOVE: x, y: 158.80152873660333 375.85207223712115
## MOVE: velX, velY: -1.2224714556282295 1.5828971982331
I noticed, the velX and velY get calculated wrong, but the angle seems to be right.. I really wouldn't know how to debug this any better, I'm staring at the angle algorithm for a while now but won't get the right idea..
Thanks guys for your help!
Upvotes: 0
Views: 174
Reputation: 2502
The reason of error as I stated in comment, is due to using degree
for math.sin()
and math.cos()
.
So simply change them back to radian solved the problem.
self.velX = self.speed * math.cos(self.angle * (math.pi /180))
self.velY = self.speed * math.sin(self.angle * (math.pi /180))
Upvotes: 1