• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[Solved]Projectile speed and direction from a moving object.

Z

Zandooma

Guest
I am working on top down spaceship game where the player can shoot in any direction. What I would like is for the projectiles to realistically inherit momentum from the player's speed and direction. Imagine just shooting a cannon from a moving train, except no gravity. What would the formula be to calculate the speed and direction of the projectile so that it was realistic?
 
P

Pyxus

Guest
I am working on top down spaceship game where the player can shoot in any direction. What I would like is for the projectiles to realistically inherit momentum from the player's speed and direction. Imagine just shooting a cannon from a moving train, except no gravity. What would the formula be to calculate the speed and direction of the projectile so that it was realistic?
Ignoring forces like gravity and drag, the reason a cannonball fires just as well on a train as it would on ground is because when it fires it starts at the same speed as the cannon and thus the cannonball's speed is always relative to the cannon's. So when a projectile is fired from your ship its speed would equal the ship's speed + its firing speed.

If the ship moves at 0m/s and the projectile fires at 0m/s + 10m/s then its speed relative to the ship is 10m/s (10-0=10). If the ship moves at 10m/s and the projectile fires at 10m/s + 10m/s, then the projectile's speed is 20m/s but relative to the ship it is still 10m/s (20-10=10).
 

NightFrost

Member
Do you mean Asteroids-style movement (free rotation regardless of current movement direction)? In that case I would think the expectation with most players would be static bullet speed. But if you want variable bullet speed, you'll have to do some maths that take ship's speed and current facing into consideration. If GML supported vectors it would be a matter of adding ship's current velocity vector to bullet's initial velocity vector, but you'll have to do it the hard way.
 
Z

Zandooma

Guest
Thank you for the replies. I was surprised at how hard it was to find a problem like this solved in gamemaker or otherwise.

Ignoring forces like gravity and drag, the reason a cannonball fires just as well on a train as it would on ground is because when it fires it starts at the same speed as the cannon and thus the cannonball's speed is always relative to the cannon's. So when a projectile is fired from your ship its speed would equal the ship's speed + its firing speed.

If the ship moves at 0m/s and the projectile fires at 0m/s + 10m/s then its speed relative to the ship is 10m/s (10-0=10). If the ship moves at 10m/s and the projectile fires at 10m/s + 10m/s, then the projectile's speed is 20m/s but relative to the ship it is still 10m/s (20-10=10).
I understand how the physics should theoretically work. But the issue I was having was translating the ships momentum into the bullet codewise. Many of my attempts to program it ended up with the bullets flying off in the wrong direction or with little speed at all.

Do you mean Asteroids-style movement (free rotation regardless of current movement direction)? In that case I would think the expectation with most players would be static bullet speed. But if you want variable bullet speed, you'll have to do some maths that take ship's speed and current facing into consideration. If GML supported vectors it would be a matter of adding ship's current velocity vector to bullet's initial velocity vector, but you'll have to do it the hard way.
My ship flies more like a boat or plane. I still wanted to take the ships momentum into account though because the player will by flying at extremely high speeds with enemies that can keep pace with him.



What I ended up doing was this: I had a speed and direction variable from both the player and the projectile. I broke them both down using the "lengthdir_x" and "lengthdir_y" functions. Then I just added them together and passed those onto the projectile. It seems to work like what I was imagining.
 
Top