### Author Topic: [SOLVED]Bullet Origin from a Rotating Gun Barrel  (Read 1297 times)

#### VictorGrunn

• Member
• Posts: 79
• Karma: +0/-0
##### [SOLVED]Bullet Origin from a Rotating Gun Barrel
« on: Tue, Nov 22, 2011 »
Let's say I have a turret that rotates 360 degrees from a point, with a barrel that extends outward. Is there a simple way (okay, or any way at all) to get the bullet origin be set to whatever the tip of the barrel is at any given time? As it stands I'm making the origin the center of the turret, but of course if the barrel swerves too fast then you see the bullet coming out of the base of the turret rather than the barrel, which looks silly.

Just changing the offset in relation to the turret just doesn't work, since the angle of the turret may change but the x/y is still treated as immobile, unless the whole gun is moving.
« Last Edit: Wed, Nov 23, 2011 by VictorGrunn »

#### JakeWilund

• Member
• Posts: 7
• Karma: +0/-0
##### Re: Bullet Origin from a Rotating Gun Barrel
« Reply #1 on: Tue, Nov 22, 2011 »
I'm not going to know the math, but you'll definitely be needing to use some type of trigonometric function to make this happen.

Edit: Check out this tutorial. http://www.emanueleferonato.com/2007/04/28/create-a-flash-artillery-game-step-1/

Should have what you need in there.

#### Kytin

• Member
• Posts: 22
• Karma: +0/-0
##### Re: Bullet Origin from a Rotating Gun Barrel
« Reply #2 on: Tue, Nov 22, 2011 »
Get the angle of the turret as a normalised vector (or point), mutiply this vector by the length of the turret, and add it to the origin of the turret. This is the position of the tip of the barrel.

Actually, the Powertools have a function that would do most of this for you (more or less). Call FlxVelocity.velocityFromAngle, but for the speed parameter put in the length of the barrel. That will get you the position of the tip of the barrel relative to the origin.
I have seen the future. You will not.

#### VictorGrunn

• Member
• Posts: 79
• Karma: +0/-0
##### Re: Bullet Origin from a Rotating Gun Barrel
« Reply #3 on: Wed, Nov 23, 2011 »
Thank you both, that tutorial in particular helped. This is solved for me - that tutorial pretty much gives exactly what's necessary to solve it, or at least solve it on my own.