Pygame trigonometry: Position jittering, when moving target
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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!
python pygame python-3.6 trigonometry
add a comment |
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!
python pygame python-3.6 trigonometry
1
Why(180 / math.pi) * math.atan2(relY, relX), you convert it to degree, butmath.cos/math.sinuse radian. You are using wrong unit, see if removing(180 / math.pi)fixes the issue. (Or times( math.pi/ 180)before usingsinandcos)
– MatrixTai
Nov 22 '18 at 8:08
That's it, thanks a lot! Appearently the image rotation still needs the angle in degrees, but I just gave the classself.radiansas well and use this in the calculation ofvelX, velY
– Thilo G
Nov 22 '18 at 8:54
add a comment |
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!
python pygame python-3.6 trigonometry
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!
python pygame python-3.6 trigonometry
python pygame python-3.6 trigonometry
asked Nov 22 '18 at 7:44
Thilo GThilo G
405
405
1
Why(180 / math.pi) * math.atan2(relY, relX), you convert it to degree, butmath.cos/math.sinuse radian. You are using wrong unit, see if removing(180 / math.pi)fixes the issue. (Or times( math.pi/ 180)before usingsinandcos)
– MatrixTai
Nov 22 '18 at 8:08
That's it, thanks a lot! Appearently the image rotation still needs the angle in degrees, but I just gave the classself.radiansas well and use this in the calculation ofvelX, velY
– Thilo G
Nov 22 '18 at 8:54
add a comment |
1
Why(180 / math.pi) * math.atan2(relY, relX), you convert it to degree, butmath.cos/math.sinuse radian. You are using wrong unit, see if removing(180 / math.pi)fixes the issue. (Or times( math.pi/ 180)before usingsinandcos)
– MatrixTai
Nov 22 '18 at 8:08
That's it, thanks a lot! Appearently the image rotation still needs the angle in degrees, but I just gave the classself.radiansas well and use this in the calculation ofvelX, velY
– Thilo G
Nov 22 '18 at 8:54
1
1
Why
(180 / math.pi) * math.atan2(relY, relX), you convert it to degree, but math.cos/ math.sin use radian. You are using wrong unit, see if removing (180 / math.pi) fixes the issue. (Or times ( math.pi/ 180) before using sin and cos)– MatrixTai
Nov 22 '18 at 8:08
Why
(180 / math.pi) * math.atan2(relY, relX), you convert it to degree, but math.cos/ math.sin use radian. You are using wrong unit, see if removing (180 / math.pi) fixes the issue. (Or times ( math.pi/ 180) before using sin and cos)– MatrixTai
Nov 22 '18 at 8:08
That's it, thanks a lot! Appearently the image rotation still needs the angle in degrees, but I just gave the class
self.radians as well and use this in the calculation of velX, velY– Thilo G
Nov 22 '18 at 8:54
That's it, thanks a lot! Appearently the image rotation still needs the angle in degrees, but I just gave the class
self.radians as well and use this in the calculation of velX, velY– Thilo G
Nov 22 '18 at 8:54
add a comment |
1 Answer
1
active
oldest
votes
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))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426079%2fpygame-trigonometry-position-jittering-when-moving-target%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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))
add a comment |
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))
add a comment |
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))
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))
answered Nov 22 '18 at 8:58
MatrixTaiMatrixTai
2,0511623
2,0511623
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426079%2fpygame-trigonometry-position-jittering-when-moving-target%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Why
(180 / math.pi) * math.atan2(relY, relX), you convert it to degree, butmath.cos/math.sinuse radian. You are using wrong unit, see if removing(180 / math.pi)fixes the issue. (Or times( math.pi/ 180)before usingsinandcos)– MatrixTai
Nov 22 '18 at 8:08
That's it, thanks a lot! Appearently the image rotation still needs the angle in degrees, but I just gave the class
self.radiansas well and use this in the calculation ofvelX, velY– Thilo G
Nov 22 '18 at 8:54