Skip to main content
added 562 characters in body
Source Link
MrGreg
  • 123
  • 4

I'm making a 2d space game in Javascript with canvas. In this game there's a bunch of spaceships, positioned in world space, with turrets, positioned relative to their parent ships. These objects also have an orientation.

How do I efficiently calculate the position of a turret in world coordinates? (i.e. when it fires and I need to know where to place a bullet in the world)? Calculating the turrets orientation is trivial - I just add the turrets relative angle to its parents. For position though, I guess I could do a bunch of trigonometry but this MUST be a common problem with a good/fast general solution?

Should I be relearning how to do matrix math again? :)

Update:

Following the answers below I went away and read up on matrix math - to that end I can highly recommend "3D Math Primer for Graphics and Game Development" by Fletcher Dunn and Ian Parberry. The neat thing about matrices is you dont need to understand the maths involved in each transform, just how to use them.

As my project is in javascript I also needed a matrix library and went with gl-matrix.js as it is fast and has a clean (to my tastes) API. Sylvester.js is also very feature rich and worth a look, although not as fast as gl-matrix

I'm making a 2d space game in Javascript with canvas. In this game there's a bunch of spaceships, positioned in world space, with turrets, positioned relative to their parent ships. These objects also have an orientation.

How do I efficiently calculate the position of a turret in world coordinates? (i.e. when it fires and I need to know where to place a bullet in the world)? Calculating the turrets orientation is trivial - I just add the turrets relative angle to its parents. For position though, I guess I could do a bunch of trigonometry but this MUST be a common problem with a good/fast general solution?

Should I be relearning how to do matrix math again? :)

I'm making a 2d space game in Javascript with canvas. In this game there's a bunch of spaceships, positioned in world space, with turrets, positioned relative to their parent ships. These objects also have an orientation.

How do I efficiently calculate the position of a turret in world coordinates? (i.e. when it fires and I need to know where to place a bullet in the world)? Calculating the turrets orientation is trivial - I just add the turrets relative angle to its parents. For position though, I guess I could do a bunch of trigonometry but this MUST be a common problem with a good/fast general solution?

Should I be relearning how to do matrix math again? :)

Update:

Following the answers below I went away and read up on matrix math - to that end I can highly recommend "3D Math Primer for Graphics and Game Development" by Fletcher Dunn and Ian Parberry. The neat thing about matrices is you dont need to understand the maths involved in each transform, just how to use them.

As my project is in javascript I also needed a matrix library and went with gl-matrix.js as it is fast and has a clean (to my tastes) API. Sylvester.js is also very feature rich and worth a look, although not as fast as gl-matrix

Tweeted twitter.com/#!/StackGameDev/status/216116938030583810
Don't sign your posts. They're already signed by your user information. Also, tidied up a bit.
Source Link
doppelgreener
  • 7.3k
  • 7
  • 44
  • 69

So ImI'm making a 2d space game and I havein Javascript with canvas. In this game there's a bunch of spaceships that have turrets. Objects have a position and orientation, the ships beingpositioned in world coordinates while thespace, with turrets are children and coordinates are, positioned relative to their parentsparent ships. These objects also have an orientation.

How do I efficiently calculate the position of a turret in world coordinates? (i.e. when it fires and I need to know where to place a bullet in the world)? Calculating the turrets orientation is trivial - I just add the turrets relative angle to its parents. For position though, I guess I could do a bunch of trigonometry but this MUST be a common problem with a good/fast general solution?

Should I be relearning how to do matrix math again? :)

btw - Im creating the game in javascript+canvas but its the math/algorithm im interested in here

Cheers, Greg

So Im making a 2d space game and I have a bunch of spaceships that have turrets. Objects have a position and orientation, the ships being in world coordinates while the turrets are children and coordinates are relative to their parents.

How do I efficiently calculate the position of a turret in world coordinates (i.e. when it fires and I need to know where to place a bullet in the world)? Calculating the turrets orientation is trivial - I just add the turrets relative angle to its parents. For position though, I guess I could do a bunch of trigonometry but this MUST be a common problem with a good/fast general solution?

Should I be relearning how to do matrix math again? :)

btw - Im creating the game in javascript+canvas but its the math/algorithm im interested in here

Cheers, Greg

I'm making a 2d space game in Javascript with canvas. In this game there's a bunch of spaceships, positioned in world space, with turrets, positioned relative to their parent ships. These objects also have an orientation.

How do I efficiently calculate the position of a turret in world coordinates? (i.e. when it fires and I need to know where to place a bullet in the world)? Calculating the turrets orientation is trivial - I just add the turrets relative angle to its parents. For position though, I guess I could do a bunch of trigonometry but this MUST be a common problem with a good/fast general solution?

Should I be relearning how to do matrix math again? :)

Source Link
MrGreg
  • 123
  • 4

how to transform child elements position into a world position

So Im making a 2d space game and I have a bunch of spaceships that have turrets. Objects have a position and orientation, the ships being in world coordinates while the turrets are children and coordinates are relative to their parents.

How do I efficiently calculate the position of a turret in world coordinates (i.e. when it fires and I need to know where to place a bullet in the world)? Calculating the turrets orientation is trivial - I just add the turrets relative angle to its parents. For position though, I guess I could do a bunch of trigonometry but this MUST be a common problem with a good/fast general solution?

Should I be relearning how to do matrix math again? :)

btw - Im creating the game in javascript+canvas but its the math/algorithm im interested in here

Cheers, Greg