GravityModels now take position into account

- Also documented GravityModel
This commit is contained in:
OLEGSHA 2021-01-25 22:06:34 +03:00
parent 8c5493f78e
commit 553837f207
Signed by: OLEGSHA
GPG Key ID: E57A4B08D64AFF7A
3 changed files with 47 additions and 5 deletions

View File

@ -204,7 +204,7 @@ public class LayerWorld extends Layer {
}
Vec3 gravitationalAcceleration = Vectors.grab3();
ClientState.getInstance().getWorld().getData().getGravityModel().getGravity(gravitationalAcceleration);
ClientState.getInstance().getWorld().getData().getGravityModel().getGravity(entity.getPosition(), gravitationalAcceleration);
gravitationalAcceleration.mul(tickLength);
entity.getVelocity().add(gravitationalAcceleration);

View File

@ -17,25 +17,67 @@
*/
package ru.windcorp.progressia.common.world;
import java.util.Objects;
import glm.vec._3.Vec3;
import ru.windcorp.progressia.common.util.crash.CrashReports;
import ru.windcorp.progressia.common.util.namespaces.Namespaced;
/**
* Gravity model specifies the gravitational acceleration field. A gravity model may be queried for the vector of gravitational acceleration that should affect an object. This vector is, generally speaking, a function of space: gravity in two different locations may vary. Gravity may also be a zero vector.
*
* @author javapony
*/
public abstract class GravityModel extends Namespaced {
public GravityModel(String id) {
super(id);
}
public Vec3 getGravity(Vec3 output) {
/**
* Computes the vector of gravitational acceleration at the provided location.
*
* @param pos the position to compute gravity at
* @param output a {@link Vec3} where the result is stored. May be {@code null}.
*
* @return the vector of gravitational acceleration. The returned object will match {@code output} parameter is it is non-null.
*/
public Vec3 getGravity(Vec3 pos, Vec3 output) {
Objects.requireNonNull(pos, "pos");
if (output == null) {
output = new Vec3();
}
doGetGravity(output);
try {
doGetGravity(pos, output);
} catch (Exception e) {
throw CrashReports.report(e, "%s failed to compute gravity at (%d; %d; %d)", this, pos.x, pos.y, pos.z);
}
return output;
}
protected abstract void doGetGravity(Vec3 output);
/**
* Computes the up direction at the provided location. Up vector is defined as the normalized gravitational acceleration vector or {@code (0; 0; 0)} if there is no gravity.
*
* @param pos the position to compute up vector at
* @param output a {@link Vec3} where the result is stored. May be {@code null}.
*
* @return the up vector. The returned object will match {@code output} parameter is it is non-null.
*/
public Vec3 getUp(Vec3 pos, Vec3 output) {
output = getGravity(pos, output);
if (output.any()) output.normalize();
return output;
}
/**
* Computes the gravitational acceleration vector at the provided location. Actual computation of gravity is delegated to this method by the other methods in this class.
*
* @param pos the position to compute gravity at
* @param output a {@link Vec3} where the result must be stored. Never {@code null}.
*/
protected abstract void doGetGravity(Vec3 pos, Vec3 output);
}

View File

@ -27,7 +27,7 @@ public class TestGravityModel extends GravityModel {
}
@Override
protected void doGetGravity(Vec3 output) {
protected void doGetGravity(Vec3 pos, Vec3 output) {
output.set(0, 0, -9.8);
}