Monday, August 29, 2016

physics - 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);

add(entity);
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)
v3.add(platformVelocity);

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.



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...