Jump to content
• Advertisement

# [Python + PyGame]Spawn shot at end of barrel independent of angle

This topic is 2597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hello there. I've been trying to make my shots come out from the end of my player's gun independent of the angle of the player (360 degrees turn). I stumbled across a tutorial that does just this, but of course nothing is that simple. The link to the tutorial is here: link.

The code to calculate the actual position is the calculate_origin function, that I have copied here straight from the tutorial:
def calculate_origin(self): # - spawn bullet at end of turret barrel instead tank center - # cannon is around Tank.side long, calculatet from Tank center # later subtracted 20 pixel from this distance # so that bullet spawns closer to tank muzzle self.pos[0] += math.cos(degrees_to_radians(self.boss.turretAngle)) * (Tank.side-20) self.pos[1] += math.sin(degrees_to_radians(-self.boss.turretAngle)) * (Tank.side-20) 

Now my code doesn't look like that of course, my calculate_origin function looks like this:
 def calculate_origin(self): # - spawn bullet at end of turret barrel instead tank center - # cannon is around Tank.side long, calculatet from Tank center # later subtracted 20 pixel from this distance # so that bullet spawns closer to tank muzzle self.pos[0] += math.cos(degrees_to_radians(self.boss.angle_to_pointer +1))# * (104) self.pos[1] += math.sin(degrees_to_radians(-self.boss.angle_to_pointer -1))# *(104)
Obviously that did not work, so here's where you come in Here is a link to all my relevant files: link. In the file game.py there is a class called Player with a method called update, that's where all the code is for the Player. In the class Projectile there is also an update method and __init__ method with the necessary code. The calculate_origin function can be found in the Projectile class.

If you have any questions, just ask.
Thanks!

#### Share this post

##### Share on other sites
Advertisement

• Advertisement

### Announcements

• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5
• Advertisement

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999980
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!