Physics of shooting

I am working on an asteroids-ish game and am just not seeing what I need to do with the shots.
The ship is always in the center of the screen.
The behavior I am trying to achieve is that no matter how fast the ship is moving, firing in any direction results in shots moving away from the ship at a constant rate.
In other words, if the ship is moving very fast from upper right to lower left, then a shot fired at the lower left corner should take the same time to reach that corner as a shot fired towards the upper right corner.

Currently, the new shot object starts out with the same movement vector as the ship and I add a fixed amount of thrust in the direction of fire. This results in variable speed and apparent direction when moving fast. I know I need to adjust based on the current movement vector, I am just not sure of the operation(s) I want to perform.

So you’re wanting your bullets to have a fixed speed relative to a stationary point, regardless of how fast the ship is moving?
Just don’t add the ship’s velocity on to the bullet’s initial velocity.

It is a perceived stationary point, I want a fixed speed relative to the ship, which is moving.
I am achieving the movement effect by keeping the viewport centered on the ship.
Not setting the bullet’s initial velocity to match the ship results in greater perceived inaccuracy.
To get the effect I am gong for, shots fired in the opposite direction of the vector of the ship’s movement will need to have greater speed then shots fired in same direction as the vector of the ship’s movement.

Does that make more sense?

If I’m understanding your question correctly…

Store the bullet’s position relative to the ship (so initial coordinates are (0,0)). Bullet’s velocity is independent of ship’s velocity. To find the bullet’s “true” position just add the ship’s current position.

Is that the sort of thing?
Simon

Sort of, I don’t want to have to do special bullet calculations every movement update separate from my existing object system. I should be able to do a single calculation at time of fire to get the correct magnitude for the shot vector.

Let’s say the center of the screen is 50 pixels away from any given corner.
I want my bullets to move at 10 pixels per second, so a shot at any corner should take 5 seconds to arrive.
Let’s say the ship is moving towards the lower left corner at 20 pixels per second.
A shot towards the lower left corner would need to have 10 pixels per second added to it.
While a shot towards the upper right corner would need to have 30 pixels per second added to it.
And forcing the magnitude of the shot vector to always be the sum of the desired speed and the ship speed, doesn’t really work either, though it is close.

I guess my problem stems from the fact that the desired vector is always changing based on the vector of the ship and the direction of fire.

Just dont move the ship?

Ok I guess it was a stupid comment.

If you want to make it look like the ship is not moving from the point of view of the shot.
Here is the solution :

Let suppose that :

  • A shot have an initial speed and an angle of fire
  • Each game step you move the ship and the shots

When you move a shot, take the velocity of the ship at this current game step and add it to the shot velocity. Then move the shot, then reset the shot speed to it’s original one.

The ship has to be able to move for the game to work, the playing field is much larger than the viewport.

Yes, if the velocity of the ship is changing then the behaviour of the bullets may look strange, particularly if the ship’s speed can be faster than the bullet’s speed (which looks to be the case for the numbers you’re quoting).

For example, if the ship is moving right at 20 pixels per second (velocity (+20,0) relative to the map) and it fires a bullet to the left with a relative speed of 10 pixels per second, then the bullet’s velocity relative to the map is (+10,0). To the player this will look fine – on the screen the bullet moves to the left at a speed of 10 pixels per second. But relative to the map the bullet is actually moving to the right, only slower than the ship is. So if the ship slows to a stop then the bullet catches it up and hits it. Not what the player expects!

So using relative velocities may not give quite the effect you’re expecting. But then I still don’t fully understand what it is you’re doing.

Simon

I am trying to replicate a very old game. I suspect that game had special handling for shots in that they moved only in the viewport and not in the world. The shots always moved at a constant speed and always straight in the direction of fire.

Currently, I am simply adding a fixed amount of thrust to the shot in the direction of fire. Since the shot started out with the same velocity as the ship, this works OK in a line with the direction of the ships movement. Shooting backwards results in slower shots than shooting forward and shooting at an angle to the line of movement results in the shot leaving the ship noticeably off from the desired shot direction.

I don’t want it to matter how fast the ship is moving; I would like the shot travel to always look the same. At this point, though, I would settle for the shot going in the correct direction.

Did you read my post?

sounds like you really want to completely disconnect the ship and the shots:

consider the shots to be on their own screen layer which doesn’t give a damn about
where the ship is flying.

Hitting enemies will have to take into account this screen-relative collision of course,
but you wont have shots wobbling about depending on which way you turn your ship.

There seems to be an inconsistency here. By “adding a fixed amount of thrust to the shot” I’m assuming you mean something like the following. If the ship’s velocity (moving right relative to the map) is (+20,0) and the magnitude of the thrust is 10, then a bullet fired to the right will have velocity (+30,0), a bullet fired to the left will have velocity (+10,0), and a bullet fired up will have velocity (+20,+10). Unless the ship’s velocity changes, the player will see the bullets move on the screen with speeds (+10,0), (-10,0) and (0,+10).

But from what you’re saying, this isn’t the case – if you fire directly upwards the path of the bullet on screen appears to be at an angle.

If that’s true then either the ship’s velocity is changing (you’re accelerating? turning?), or something’s gone wrong with your calculation of the bullet’s velocity (are you imposing a maximum speed on the bullet?), or your rule for moving the bullet is something other than “new_position = old_position + time_step * velocity” (the velocity of a bullet is constant, right?).

Simon

@Gudradain - yes, I read your post. I am really trying to avoid special handling of the shots after they have been fired. With the short time to live on the shot and the inability to quickly reduce speed, it should be very, very hard to shoot yourself.

Maybe I need to subtract the movement vector from the shot vector and use the magnitude of that result as the magnitude of the shot vector.

@Karmington - yes, a completely separate layer would get me to where I want to be in terms of appearance, but I want to get there without special handling the shots/collision

@dishmoth - that is the case when the ship isn’t moving, when the ship is moving, say (5,5) then you have (15,5) (-5,5) and (5,15)

have you tried looking in the source of another astroids clone and finding the answer…

Unfortunately, it isn’t an asteroids clone, I just said Asteroids-ish.
In all of the asteroids games I have seen, the ship moves around the screen while the background stays stationary, and the field of play is the screen size.
In mine, the ship stays centered and viewport moves around the much larger playing field.
I said Asteroids-ish in that the controls are similar, you ship can turn, apply thrust, and fire.

It seems like it ought to “just work” if you are following the approach dishmoth described.

Perhaps one point of confusion could be the relationship between the world coordinates and the viewport. How do you handle this? Do the ship, bullets and other world objects all use the same coordinate space? Then at render time the view is translated so the ship is at the centre?

Maybe it’s just time to go bug hunting…

Yes, everything uses the same world coordinate space, and the view remains centered on the ship at render time.

I have been bug hunting as I thought that method should “just work” as well.

If you match speed/direction with an asteroid and attempt to shoot it from the side, you will always miss as the shot appears to leave from a different angle than the one you are aiming. That part is hard to describe. There are 32 angles you could turn the ship to and fire from. The sideways shots, when moving fast enough, appear to come from one angle past the current ship angle and always leading the target. The closer the angle of the ship matches line of travel, the less this error is. So, it has to be something I am doing incorrectly with initial velocity of the shot that is getting magnified by the increased initial velocity imparted by the ship.

time to do a screencap youtube video or applet test or something… lets see it in action?

So you want to add the ships velocity to the bullet, but not in a realistic way, because you want the bullet to hit where the mouse was cllicked (rather than at the angle of the mouse relative to the ship…?

Lol! Beaten to it!
Don’t forget the MVC thing:- Model, View, Controller.
Think in meters and seconds for the model and in pixels and fps for the view.