# How to account for speed of the vehicle when shooting shells from it?

I'm developing a simple 3D ship game using libgdx and bullet.

When a user taps the mouse I create a new shell object and send it in the direction of the mouse click. However, if the user has tapped the mouse in the direction where the ship is currently moving, the ship catches up to the shells very quickly and can sometimes even get hit by them - simply because the speed of shells and the ship are quite comparable.

I think I need to account for ship speed when generating the initial impulse for the shells, and I tried doing that (see "new line added"), but I cannot figure out if what I'm doing is the proper way and if yes, how to calculate the correct coefficient.

public void createShell(Vector3 origin, Vector3 direction, Vector3 platformVelocity, float velocity) {
long shellId = System.currentTimeMillis(); // hack
ShellState state = getState().createShellState(shellId, origin.x, origin.y, origin.z);
ShellEntity entity = EntityFactory.getInstance().createShellEntity(shellId, state);

entity.getBody().applyCentralImpulse(platformVelocity.mul(velocity * 0.02f)); // new line added, to compensate for the moving platform, no idea how to calculate proper coefficient
entity.getBody().applyCentralImpulse(direction.nor().mul(velocity));
}

private final Vector3 v3 = new Vector3();
public void shootGun(Vector3 direction) {
Vector3 shipVelocity = world.getShipEntities().get(id).getBody().getLinearVelocity();
world.getState().getShipStates().get(id).transform.getTranslation(v3); // current location of our ship
v3.add(direction.nor().mul(10.0f)); // hack; this is to avoid shell immediately impacting the ship that it got shot out from
world.createShell(v3, direction, shipVelocity, 500);
}


Edit - I switched to:

    v3.set(direction);
v3.nor().mul(velocity);

if (platformVelocity != null)

entity.getBody().setLinearVelocity(v3);


And it seems to work (so adding the velocities and setting them on the body).

However, I'm not sure what the drawbacks are and why the tutorial application used impulses instead of setting velocities.

I also think that while this is physically correct it leads to strange results (gun range is two times further when shooting forward than when shooting sideways).

The root cause is because really the shells are indeed too slow, but I like them slow as otherwise the gameplay is too fast-paced, and they travel too far.

I think I need some compromise between physics-realistic and ignore-platform-velocity.

• What I feel from reading this is that either the ship is too fast or your bullet is too slow. You could account for the velocity of the ship when firing bullets, but does that mean the bullets will have slower velocity when shot in a direction opposite to the ship's movement? – Vite Falcon Nov 28 '12 at 15:24

The shell's speed was the same as the ship's speed before it got fired, if you think about it. It was sitting on the ship. So, if you add the velocity from being fired to the velocity of the ship, you should get the correct result.

• I still feel his bullet is too slow or his ship is too fast. If he feels that his ship catches up with the bullet, if he bullet is fired against the direction of motion, adding the ship's velocity will either make the bullet stand-still or move in the direction of ship (and may even hit it :O). – Vite Falcon Nov 28 '12 at 15:28
• That doesn't make conservation of momentum the wrong thing to do, it just means his ship's gun is embarassingly ineffective. Maybe the sailors should consider using a little more gun powder. If the bullet catches up with the ship when fired AGAINST the direction of motion, that means the ship is either slowing down incredibly fast, or the bullet was fired with negative velocity. In the first case, the boys in the engine room are really working hard! Maybe too hard. In the second case, I'd say that gun needs serious repairs. – Eric B Nov 28 '12 at 15:29
• LOL, or programmatically, the Bullet-to-Ship velocity ratio should be higher than what it is. – Vite Falcon Nov 28 '12 at 15:34
• From a gameplay perspective, conservation of momentum may very well be wrong. So few games use it that it can (and often does) feel wrong in games that use it. Whether that holds true for this particular genre of game, I don't know. But in general, do what's fun, not what's most realistic. – Sean Middleditch Nov 28 '12 at 17:31
• Tribes conserves momentum, making it incredibly hard to hit targets, which is exactly why some players LIKE it. – Eric B Nov 28 '12 at 19:36

entity.getBody().setLinearVelocity(platformVelocity);