How to offset a bullet from center of a rotating object?

I have a man with gun sprite and I’d like the bullet to actually come from his gun rather than the center of the sprite (his head).

The problem is that I can’t just use a simple offset as the guy can rotate 360 degrees.

I imagine there’s some kind of formula that would take into account his current rotation, but my math-fu isn’t that strong.

Anyone know such a formula or some other way to handle that? (If it’s stupid easy and I’m just overlooking it, let’s chalk it up to the fact that it’s coming up on finals and I shouldn’t even be coding at all right now!)

 Jay

Here’s a look at the guy.

manRed_gun.png

Hey,

just take the offset (x,y) when the guy is at 0 degrees and use that and the guys current angle with the following function:

local function rotateVector(x, y, angle) local radAngle = math.rad(angle) local sinAngle = math.sin(radAngle) local cosAngle = math.cos(radAngle) local rotatedX = cosAngle\*x - sinAngle\*y local rotatedY = sinAngle\*x + cosAngle\*y return rotatedX, rotatedY end local rotatedOffsetX, rotatedOffsetY = rotateVector(offsetX, offsetY, angle)

Could be smaller, but like that it’s more readable.

Hope that helps.

Don’t forget the math2d library exists to simplify these calculations (although I must say torbenratzlaff’s code is quite compact too):

build.settings

 plugins = { ['plugin.math2d'] = {publisherId = 'com.roaminggamer'} }

Code (run more than once to see how it works; may contain typos):

local math2d = require "plugin.math2d" local function getBulletOffset( player, offsetAngle, offsetDist ) local offset = math2d.angle2Vector( player.rotation + offsetAngle, true ) offset = math2d.scale( offset, offsetDist ) return player.x + offset.x, player.y + offset.y end local player = display.newRect( 10, 10, 20, 40 ) player.rotation = math.random( 0, 360 ) -- Try running this sample more than once. local bulletStartX, bulletStartY = getBulletOffset( player, 90, 15 ) local bullet = display.newCircle( bulletStartX, bulletStartY, 5 ) bullet:setFillColor(1,0,0)

Thanks, guys! I have an ENG212 assignment to finish today, but I’ll give it a shot first thing tomorrow!

Didn’t know about the math2d plugin – that looks like something fun to explore.

 Jay

I have the same problem but with a earth with a cannon shooting missiles. So, after I offset the missile, how can I shoot it? I want to shoot it forward from the cannon’s position, going always forward until it goes outside of the screen. Can you help me with that?

@ruimika90,

** If my answer seems a little daunting or over the top, just grab the answer packs below and try them out.  You’ll see where I made things that ‘shoot’ in a number of answers.  That is a starting place for you. **

That requires 2D math.  You’ll need to buff up on the topic.  You really must be versed in 2D math to make games (if you intend to handle the programming).

This book has a great section (Appendix C) on 2D math:

http://portal.aauj.edu/portal_resources/downloads/programming/windows_game_programming_guru.pdf

Of course, this is the situation where a formal education would help a lot.  You’re asking for a one off solution, but knowing the fundamentals would allow you to solve your current question and any other related one.

Finally, you should dig through my answer packs:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015%20Answer%20Packs.zip

I answered similar and related questions in several different ways.

  • April 2015 - #2, #9, #10
  • August 2015 - #7 (tangentially related), #13 (also somewhat related)

Here is a randomly ordered list of math related sites and articles (mostly 2D, some 3D):

This is the kind of stuff that a game programmer must understand to handle making action and other games.

I will try all of this and hope that I can find the solution to my problems. Thx for everything man, you saved me in a great way.

Hey,

just take the offset (x,y) when the guy is at 0 degrees and use that and the guys current angle with the following function:

local function rotateVector(x, y, angle) local radAngle = math.rad(angle) local sinAngle = math.sin(radAngle) local cosAngle = math.cos(radAngle) local rotatedX = cosAngle\*x - sinAngle\*y local rotatedY = sinAngle\*x + cosAngle\*y return rotatedX, rotatedY end local rotatedOffsetX, rotatedOffsetY = rotateVector(offsetX, offsetY, angle)

Could be smaller, but like that it’s more readable.

Hope that helps.

Don’t forget the math2d library exists to simplify these calculations (although I must say torbenratzlaff’s code is quite compact too):

build.settings

 plugins = { ['plugin.math2d'] = {publisherId = 'com.roaminggamer'} }

Code (run more than once to see how it works; may contain typos):

local math2d = require "plugin.math2d" local function getBulletOffset( player, offsetAngle, offsetDist ) local offset = math2d.angle2Vector( player.rotation + offsetAngle, true ) offset = math2d.scale( offset, offsetDist ) return player.x + offset.x, player.y + offset.y end local player = display.newRect( 10, 10, 20, 40 ) player.rotation = math.random( 0, 360 ) -- Try running this sample more than once. local bulletStartX, bulletStartY = getBulletOffset( player, 90, 15 ) local bullet = display.newCircle( bulletStartX, bulletStartY, 5 ) bullet:setFillColor(1,0,0)

Thanks, guys! I have an ENG212 assignment to finish today, but I’ll give it a shot first thing tomorrow!

Didn’t know about the math2d plugin – that looks like something fun to explore.

 Jay

I have the same problem but with a earth with a cannon shooting missiles. So, after I offset the missile, how can I shoot it? I want to shoot it forward from the cannon’s position, going always forward until it goes outside of the screen. Can you help me with that?

@ruimika90,

** If my answer seems a little daunting or over the top, just grab the answer packs below and try them out.  You’ll see where I made things that ‘shoot’ in a number of answers.  That is a starting place for you. **

That requires 2D math.  You’ll need to buff up on the topic.  You really must be versed in 2D math to make games (if you intend to handle the programming).

This book has a great section (Appendix C) on 2D math:

http://portal.aauj.edu/portal_resources/downloads/programming/windows_game_programming_guru.pdf

Of course, this is the situation where a formal education would help a lot.  You’re asking for a one off solution, but knowing the fundamentals would allow you to solve your current question and any other related one.

Finally, you should dig through my answer packs:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015%20Answer%20Packs.zip

I answered similar and related questions in several different ways.

  • April 2015 - #2, #9, #10
  • August 2015 - #7 (tangentially related), #13 (also somewhat related)

Here is a randomly ordered list of math related sites and articles (mostly 2D, some 3D):

This is the kind of stuff that a game programmer must understand to handle making action and other games.

I will try all of this and hope that I can find the solution to my problems. Thx for everything man, you saved me in a great way.