Added GravityModels, removed gravity switch

- Added GravityModel
  - can specify gravity varying by location and time
  - Added GravityModelRegistry
  - Stored in WorldData
- Removed Minecraft gravity mode
This commit is contained in:
OLEGSHA 2021-01-25 21:35:46 +03:00
parent fc85eb5658
commit 8c5493f78e
Signed by: OLEGSHA
GPG Key ID: E57A4B08D64AFF7A
10 changed files with 133 additions and 35 deletions

View File

@ -41,6 +41,7 @@ import ru.windcorp.progressia.common.Units;
import ru.windcorp.progressia.common.collision.Collideable; import ru.windcorp.progressia.common.collision.Collideable;
import ru.windcorp.progressia.common.collision.colliders.Collider; import ru.windcorp.progressia.common.collision.colliders.Collider;
import ru.windcorp.progressia.common.util.FloatMathUtil; import ru.windcorp.progressia.common.util.FloatMathUtil;
import ru.windcorp.progressia.common.util.Vectors;
import ru.windcorp.progressia.common.world.entity.EntityData; import ru.windcorp.progressia.common.world.entity.EntityData;
import ru.windcorp.progressia.test.CollisionModelRenderer; import ru.windcorp.progressia.test.CollisionModelRenderer;
import ru.windcorp.progressia.test.TestPlayerControls; import ru.windcorp.progressia.test.TestPlayerControls;
@ -197,16 +198,18 @@ public class LayerWorld extends Layer {
entity.getVelocity().mul((float) Math.exp(-FRICTION_COEFF / entity.getCollisionMass() * tickLength)); entity.getVelocity().mul((float) Math.exp(-FRICTION_COEFF / entity.getCollisionMass() * tickLength));
} }
private static final float MC_g = Units.get("32 m/s^2");
private static final float IRL_g = Units.get("9.8 m/s^2");
private void tmp_applyGravity(EntityData entity, float tickLength) { private void tmp_applyGravity(EntityData entity, float tickLength) {
if (ClientState.getInstance().getLocalPlayer().getEntity() == entity && tmp_testControls.isFlying()) { if (ClientState.getInstance().getLocalPlayer().getEntity() == entity && tmp_testControls.isFlying()) {
return; return;
} }
final float gravitationalAcceleration = tmp_testControls.useMinecraftGravity() ? MC_g : IRL_g; Vec3 gravitationalAcceleration = Vectors.grab3();
entity.getVelocity().add(0, 0, -gravitationalAcceleration * tickLength); ClientState.getInstance().getWorld().getData().getGravityModel().getGravity(gravitationalAcceleration);
gravitationalAcceleration.mul(tickLength);
entity.getVelocity().add(gravitationalAcceleration);
Vectors.release(gravitationalAcceleration);
} }
@Override @Override

View File

@ -0,0 +1,41 @@
/*
* Progressia
* Copyright (C) 2020-2021 Wind Corporation and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package ru.windcorp.progressia.common.world;
import glm.vec._3.Vec3;
import ru.windcorp.progressia.common.util.namespaces.Namespaced;
public abstract class GravityModel extends Namespaced {
public GravityModel(String id) {
super(id);
}
public Vec3 getGravity(Vec3 output) {
if (output == null) {
output = new Vec3();
}
doGetGravity(output);
return output;
}
protected abstract void doGetGravity(Vec3 output);
}

View File

@ -0,0 +1,30 @@
/*
* Progressia
* Copyright (C) 2020-2021 Wind Corporation and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package ru.windcorp.progressia.common.world;
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
public class GravityModelRegistry extends NamespacedInstanceRegistry<GravityModel> {
public static final GravityModelRegistry INSTANCE = new GravityModelRegistry();
public static GravityModelRegistry getInstance() {
return INSTANCE;
}
}

View File

@ -51,6 +51,8 @@ public class WorldData
private final TLongObjectMap<EntityData> entitiesById = TCollections.synchronizedMap(new TLongObjectHashMap<>()); private final TLongObjectMap<EntityData> entitiesById = TCollections.synchronizedMap(new TLongObjectHashMap<>());
private final Collection<EntityData> entities = Collections.unmodifiableCollection(entitiesById.valueCollection()); private final Collection<EntityData> entities = Collections.unmodifiableCollection(entitiesById.valueCollection());
private GravityModel gravityModel = GravityModelRegistry.getInstance().get("Test:TheGravityModel");
private float time = 0; private float time = 0;
@ -200,6 +202,20 @@ public class WorldData
return null; return null;
return block.getCollisionModel(); return block.getCollisionModel();
} }
/**
* @return the gravity model
*/
public GravityModel getGravityModel() {
return gravityModel;
}
/**
* @param gravityModel the gravity model to set
*/
public void setGravityModel(GravityModel gravityModel) {
this.gravityModel = gravityModel;
}
public Collection<WorldDataListener> getListeners() { public Collection<WorldDataListener> getListeners() {
return listeners; return listeners;

View File

@ -91,17 +91,6 @@ public class LayerTestGUI extends GUILayer {
) )
); );
panel.addChild(
new Label(
"GravityModeDisplay",
font,
tmp_dynFormat(
"LayerTestGUI.GravityModeDisplay",
() -> tpc.useMinecraftGravity() ? "Minecraft" : "Realistic"
)
)
);
panel.addChild( panel.addChild(
new Label( new Label(
"LanguageDisplay", "LanguageDisplay",

View File

@ -47,6 +47,7 @@ import ru.windcorp.progressia.common.collision.AABB;
import ru.windcorp.progressia.common.collision.CollisionModel; import ru.windcorp.progressia.common.collision.CollisionModel;
import ru.windcorp.progressia.common.comms.controls.*; import ru.windcorp.progressia.common.comms.controls.*;
import ru.windcorp.progressia.common.state.StatefulObjectRegistry.Factory; import ru.windcorp.progressia.common.state.StatefulObjectRegistry.Factory;
import ru.windcorp.progressia.common.world.GravityModelRegistry;
import ru.windcorp.progressia.common.world.block.*; import ru.windcorp.progressia.common.world.block.*;
import ru.windcorp.progressia.common.world.entity.*; import ru.windcorp.progressia.common.world.entity.*;
import ru.windcorp.progressia.common.world.io.ChunkIO; import ru.windcorp.progressia.common.world.io.ChunkIO;
@ -56,6 +57,7 @@ import ru.windcorp.progressia.server.comms.controls.*;
import ru.windcorp.progressia.server.world.block.*; import ru.windcorp.progressia.server.world.block.*;
import ru.windcorp.progressia.server.world.entity.*; import ru.windcorp.progressia.server.world.entity.*;
import ru.windcorp.progressia.server.world.tile.*; import ru.windcorp.progressia.server.world.tile.*;
import ru.windcorp.progressia.test.gen.TestGravityModel;
public class TestContent { public class TestContent {
@ -423,6 +425,7 @@ public class TestContent {
private static void registerMisc() { private static void registerMisc() {
ChunkIO.registerCodec(new TestChunkCodec()); ChunkIO.registerCodec(new TestChunkCodec());
ChunkRenderOptimizerRegistry.getInstance().register("Core:SurfaceOptimizer", ChunkRenderOptimizerSurface::new); ChunkRenderOptimizerRegistry.getInstance().register("Core:SurfaceOptimizer", ChunkRenderOptimizerSurface::new);
GravityModelRegistry.getInstance().register(new TestGravityModel());
} }
} }

View File

@ -83,7 +83,6 @@ public class TestPlayerControls {
private double lastSprintPress = Double.NEGATIVE_INFINITY; private double lastSprintPress = Double.NEGATIVE_INFINITY;
private boolean captureMouse = true; private boolean captureMouse = true;
private boolean useMinecraftGravity = false;
private int selectedBlock = 0; private int selectedBlock = 0;
private int selectedTile = 0; private int selectedTile = 0;
@ -199,12 +198,6 @@ public class TestPlayerControls {
handleCameraMode(); handleCameraMode();
break; break;
case GLFW.GLFW_KEY_G:
if (!event.isPress())
return false;
handleGravitySwitch();
break;
case GLFW.GLFW_KEY_L: case GLFW.GLFW_KEY_L:
if (!event.isPress()) if (!event.isPress())
return false; return false;
@ -269,7 +262,7 @@ public class TestPlayerControls {
return; return;
} }
getEntity().getVelocity().add(0, 0, JUMP_VELOCITY * (useMinecraftGravity ? 2 : 1)); getEntity().getVelocity().add(0, 0, JUMP_VELOCITY);
} }
private void handleShift(int multiplier) { private void handleShift(int multiplier) {
@ -313,11 +306,6 @@ public class TestPlayerControls {
} }
} }
private void handleGravitySwitch() {
useMinecraftGravity = !useMinecraftGravity;
updateGUI();
}
private void handleLanguageSwitch() { private void handleLanguageSwitch() {
Localizer localizer = Localizer.getInstance(); Localizer localizer = Localizer.getInstance();
if (localizer.getLanguage().equals("ru-RU")) { if (localizer.getLanguage().equals("ru-RU")) {
@ -427,10 +415,6 @@ public class TestPlayerControls {
return captureMouse; return captureMouse;
} }
public boolean useMinecraftGravity() {
return useMinecraftGravity;
}
public BlockData getSelectedBlock() { public BlockData getSelectedBlock() {
return TestContent.PLACEABLE_BLOCKS.get(selectedBlock); return TestContent.PLACEABLE_BLOCKS.get(selectedBlock);
} }

View File

@ -0,0 +1,34 @@
/*
* Progressia
* Copyright (C) 2020-2021 Wind Corporation and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package ru.windcorp.progressia.test.gen;
import glm.vec._3.Vec3;
import ru.windcorp.progressia.common.world.GravityModel;
public class TestGravityModel extends GravityModel {
public TestGravityModel() {
super("Test:TheGravityModel");
}
@Override
protected void doGetGravity(Vec3 output) {
output.set(0, 0, -9.8);
}
}

View File

@ -8,7 +8,6 @@ LayerTestGUI.IsFlyingDisplay = Flying: %5s (Space bar x2)
LayerTestGUI.IsSprintingDisplay = Sprinting: %5s (W x2) LayerTestGUI.IsSprintingDisplay = Sprinting: %5s (W x2)
LayerTestGUI.IsMouseCapturedDisplay = Mouse captured: %5s (Esc) LayerTestGUI.IsMouseCapturedDisplay = Mouse captured: %5s (Esc)
LayerTestGUI.CameraModeDisplay = Camera mode: %5d (F5) LayerTestGUI.CameraModeDisplay = Camera mode: %5d (F5)
LayerTestGUI.GravityModeDisplay = Gravity: %9s (G)
LayerTestGUI.LanguageDisplay = Language: %5s (L) LayerTestGUI.LanguageDisplay = Language: %5s (L)
LayerTestGUI.FPSDisplay = FPS: LayerTestGUI.FPSDisplay = FPS:
LayerTestGUI.TPSDisplay = TPS: LayerTestGUI.TPSDisplay = TPS:

View File

@ -8,7 +8,6 @@ LayerTestGUI.IsFlyingDisplay = Полёт: %5s (Пробел x2)
LayerTestGUI.IsSprintingDisplay = Бег: %5s (W x2) LayerTestGUI.IsSprintingDisplay = Бег: %5s (W x2)
LayerTestGUI.IsMouseCapturedDisplay = Захват мыши: %5s (Esc) LayerTestGUI.IsMouseCapturedDisplay = Захват мыши: %5s (Esc)
LayerTestGUI.CameraModeDisplay = Камера: %5d (F5) LayerTestGUI.CameraModeDisplay = Камера: %5d (F5)
LayerTestGUI.GravityModeDisplay = Гравитация: %9s (G)
LayerTestGUI.LanguageDisplay = Язык: %5s (L) LayerTestGUI.LanguageDisplay = Язык: %5s (L)
LayerTestGUI.FPSDisplay = FPS: LayerTestGUI.FPSDisplay = FPS:
LayerTestGUI.TPSDisplay = TPS: LayerTestGUI.TPSDisplay = TPS: