domingo, 3 de mayo de 2015

Understanding CaboTrafalgar Physics 1 - Tidal forces in shipModelZ

Index:

(To come)

This is the first entry of a series to explain where we are now with physics in the game, and where we want to be.

Most of the entries will refer this "dinghy", ShipModelZ, in regards of movement and commands.

This post is actually an easy win, and that's the best way to start a path, with a victory in an apparently difficult topic as "Tidal movement".

During the hundredths of seconds a frame lasts, several calculations are made to try to simulate movement realistically, considering real or apparent wind, boat position, sail position, rudder position and weight location. In this calculation, tide is not considered, as we're calculating movement OVER the tide (same way, we don't consider the movement of the earth around the Sun for highway speed calculation).

Once we know what's the tri-dimensional distance we want to move over the sea, we need to add the absolute movement of the water, giving as the final distance we have moved.

Code explanation:

private void updatePosition(float tpf) {
Vector3f shipOrientation3f = this.getGlobalDirection();
this.move(shipOrientation3f.x * localSpeed * tpf, 0, shipOrientation3f.z * localSpeed * tpf);
this.move(getContext().getWater().getMovement(this.getGlobalDirection()).mult(-30 * tpf));
shipDirection.setValue(shipOrientation3f);
}

For this model, all movement is calculated in the direction of the keel, no lateral movement has been modelled yet. Direction of the keel is "this.getGlobalDirection()"

Local speed is the speed "ahead", direction defined by the keel, it acts as the modulus of a vector, while the keel is the direction of the vector.

As we're just interested in horizontal movement, we take first and third elements in the vector, times the speed, times tpf (time per frame) to get the final distance we've moved over the sea.
On top of this, we apply the movement of the tide for that particular location (this is a bug, we should be passing position, not orientation, but it doesn't matter, the implementation of water context is not as complex yet for it to matter), times some handy constant times time per framework.

So magic is done, on top of the ship movement, we have tide speed complicating everything.

That's it for today.. :)