Fix desynced hitboxes with animations

This commit is contained in:
austin 2021-06-28 22:40:41 -04:00
parent 3f899ed746
commit 5eae8dee7a
13 changed files with 139 additions and 91 deletions

View File

@ -12,11 +12,11 @@ import electrosphere.game.client.player.ClientPlayerData;
import electrosphere.game.client.terrain.manager.ClientTerrainManager;
import electrosphere.game.client.world.ClientWorldData;
import electrosphere.game.collision.CommonWorldData;
import electrosphere.game.state.SimulationState.SimulationStateMachine;
import electrosphere.game.state.MacroSimulation;
import electrosphere.game.server.terrain.manager.ServerTerrainManager;
import electrosphere.game.server.world.ServerWorldData;
import electrosphere.game.state.AttachUtils;
import electrosphere.game.state.SimulationState;
import electrosphere.game.state.MicroSimulation;
import electrosphere.logger.LoggerInterface;
import electrosphere.main.Globals;
import static electrosphere.main.Globals.loadingBox;
@ -73,7 +73,6 @@ public class LoadingThread extends Thread {
MenuUtils.makeMenuDrawable(Globals.currentMenu);
SimulationState.simulationState = SimulationStateMachine.TITLE_MENU;
break;
@ -131,6 +130,12 @@ public class LoadingThread extends Thread {
}
}
//initialize the "virtual" objects simulation
initMacroSimulation();
//initialize the "real" objects simulation
initMicroSimulation();
loadingBox.setDraw(false);
Globals.RENDER_FLAG_RENDER_SHADOW_MAP = true;
@ -141,7 +146,6 @@ public class LoadingThread extends Thread {
LoggerInterface.loggerEngine.INFO("Finished loading");
SimulationState.simulationState = SimulationStateMachine.MAIN_SIMULATION;
Globals.controlHandler.setHandlerState(ControlHandler.ControlsState.MAIN_GAME);
break;
@ -195,6 +199,12 @@ public class LoadingThread extends Thread {
}
}
//initialize the "virtual" objects simulation
initMacroSimulation();
//initialize the "real" objects simulation
initMicroSimulation();
loadingBox.setDraw(false);
Globals.RENDER_FLAG_RENDER_SHADOW_MAP = true;
@ -205,7 +215,6 @@ public class LoadingThread extends Thread {
LoggerInterface.loggerEngine.INFO("Finished loading");
SimulationState.simulationState = SimulationStateMachine.MAIN_SIMULATION;
Globals.controlHandler.setHandlerState(ControlHandler.ControlsState.MAIN_GAME);
break;
@ -406,6 +415,14 @@ public class LoadingThread extends Thread {
}
static void initMacroSimulation(){
Globals.macroSimulation = new MacroSimulation();
}
static void initMicroSimulation(){
Globals.microSimulation = new MicroSimulation();
}
static void creatingRandomEntities(){
// String unitCubeModelPath = Globals.assetManager.registerModel(ModelUtils.createUnitCube());

View File

@ -47,29 +47,14 @@ public class HitboxUtils {
Vector3f bonePosition = EntityUtils.getEntityActor(parent).getBonePosition(boneName);
worldPosition.set(bonePosition.x,bonePosition.y,bonePosition.z);
Quaternionf rotation = new Quaternionf(parentRotation);
// rotation.w = -rotation.w;
// rotation.x = -rotation.x;
// rotation.y = -rotation.y;
// rotation.z = -rotation.z;
// System.out.println(bonePosition);
worldPosition = worldPosition.mul(positionScale);
worldPosition = worldPosition.rotate(rotation);
// Matrix4f rotationMatrix = new Matrix4f().rotate(parentRotation);
// Vector4f rawRotatedOffset = rotationMatrix.transform(new Vector4f(worldPosition.x,worldPosition.y,worldPosition.z,1));
// worldPosition = new Vector3f(rawRotatedOffset.x,rawRotatedOffset.y,rawRotatedOffset.z);
worldPosition.add(EntityUtils.getEntityPosition(parent));
// System.out.println(worldPosition);
//
// System.out.println("parent rotation: " + new Vector3f(0,0,1).rotate(rotation));
((Vector3f)hitbox.getData(EntityDataStrings.DATA_STRING_POSITION)).set(worldPosition);
}
@ -88,6 +73,7 @@ public class HitboxUtils {
generatorType != receiverType
){
if(generatorType.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT)){
// Globals.microSimulation.freeze();
EntityUtils.getEntityPosition(generatorParent).set(Globals.spawnPoint);
} else if(receiverParent.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT)){
EntityUtils.getEntityPosition(receiverParent).set(Globals.spawnPoint);

View File

@ -257,7 +257,7 @@ public class TerrainGen {
float[][] rVal = new float[elevation.length][elevation[0].length];
for(int x = 0; x < DIMENSION-1; x++){
for(int y = 0; y < DIMENSION-1; y++){
rVal[x][y] = elevation[x][y] + Utilities.random_Integer(0, (int)(elevation[x][y] / 4));
rVal[x][y] = elevation[x][y] + Utilities.random_Integer(0, (int)(elevation[x][y] / verticalInterpolationRatio));
}
}
return rVal;

View File

@ -34,7 +34,7 @@ public class AttachUtils {
position = position.rotate(((Quaternionf)EntityUtils.getEntityRotation(parent)));
position.add(new Vector3f(EntityUtils.getEntityPosition(parent)));
EntityUtils.getEntityPosition(currentEntity).set(position);
EntityUtils.getEntityRotation(currentEntity).add(EntityUtils.getEntityRotation(parent)).normalize();
EntityUtils.getEntityRotation(currentEntity).set(EntityUtils.getEntityRotation(parent)).normalize();
}
}
}

View File

@ -1,4 +1,4 @@
package electrosphere.game.server.simulation;
package electrosphere.game.state;
import electrosphere.game.server.civilization.Civilization;
import electrosphere.game.server.civilization.model.CivilizationMap;
@ -11,7 +11,7 @@ import electrosphere.util.Utilities;
import java.util.LinkedList;
import java.util.List;
public class Simulation {
public class MacroSimulation {
List<Civilization> civilizationList = new LinkedList();
List<Culture> cultureList = new LinkedList();
@ -19,13 +19,13 @@ public class Simulation {
List<CreatureType> creatureList = new LinkedList();
List<Character> characterList = new LinkedList();
public Simulation(){
public MacroSimulation(){
init();
}
void init(){
CreatureTypeMap creatureTypeMap = FileLoadingUtils.loadObjectFromAssetPath("Data/creatures.json", CreatureTypeMap.class);
CivilizationMap civilizationMap = FileLoadingUtils.loadObjectFromAssetPath("Data/civilization.json", CivilizationMap.class);
// CreatureTypeMap creatureTypeMap = FileLoadingUtils.loadObjectFromAssetPath("Data/creatures.json", CreatureTypeMap.class);
// CivilizationMap civilizationMap = FileLoadingUtils.loadObjectFromAssetPath("Data/civilization.json", CivilizationMap.class);
}
public void simulate(){

View File

@ -0,0 +1,58 @@
package electrosphere.game.state;
import electrosphere.entity.Entity;
import electrosphere.entity.state.MovementTree;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.types.hitbox.HitboxUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.main.Globals;
/**
*
* @author amaterasu
*/
public class MicroSimulation {
boolean isReady = false;
public MicroSimulation(){
isReady = true;
}
public void simulate(){
//make items play idle animation
for(Entity item : Globals.entityManager.getItemEntities()){
ItemUtils.updateItemActorAnimation(item);
}
//simulate creature behavior trees
for(Entity currentMoveable : Globals.entityManager.getMoveable()){
MovementTree behaviorTree = CreatureUtils.getEntityMovementTree(currentMoveable);
behaviorTree.simulate();
}
//update attached entity positions
AttachUtils.updateAttachedEntityPositions();
//update hitbox positions
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
HitboxUtils.updatePosition(currentHitbox);
}
//collide hitboxes
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
if(isReady){
HitboxUtils.collideEntities(currentHitbox);
}
}
}
public boolean isReady(){
return isReady;
}
public void freeze(){
isReady = false;
}
public void unfreeze(){
isReady = true;
}
}

View File

@ -1,17 +0,0 @@
package electrosphere.game.state;
/**
*
* @author amaterasu
*/
public class SimulationState {
public enum SimulationStateMachine {
LOADING,
TITLE_MENU,
MAIN_SIMULATION,
ARENA_SIMULATION,
}
public static SimulationStateMachine simulationState;
}

View File

@ -17,8 +17,8 @@ public class LoggerInterface {
public static Logger loggerEngine;
public static void initLoggers(){
loggerNetworking = new Logger(LogLevel.DEBUG);
loggerFileIO = new Logger(LogLevel.ERROR);
loggerNetworking = new Logger(LogLevel.WARNING);
loggerFileIO = new Logger(LogLevel.WARNING);
loggerGameLogic = new Logger(LogLevel.DEBUG);
loggerRenderer = new Logger(LogLevel.WARNING);
loggerEngine = new Logger(LogLevel.WARNING);

View File

@ -21,8 +21,10 @@ import electrosphere.game.client.world.ClientWorldData;
import electrosphere.game.collision.CommonWorldData;
import electrosphere.game.state.AliveManager;
import electrosphere.engine.LoadingThread;
import electrosphere.game.state.MacroSimulation;
import electrosphere.game.server.terrain.manager.ServerTerrainManager;
import electrosphere.game.server.world.ServerWorldData;
import electrosphere.game.state.MicroSimulation;
import electrosphere.menu.Menu;
import electrosphere.net.client.ClientNetworking;
import electrosphere.net.server.Server;
@ -84,6 +86,7 @@ public class Globals {
//Controls Handler
//
public static ControlHandler controlHandler;
public static boolean updateCamera = true;
//
@ -170,6 +173,12 @@ public class Globals {
//manages all models loaded into memory
public static AssetManager assetManager;
//macro simulation
public static MacroSimulation macroSimulation;
//micro simulation
public static MicroSimulation microSimulation;
//manages hitboxes
public static HitboxManager hitboxManager;

View File

@ -14,8 +14,7 @@ import electrosphere.entity.types.hitbox.HitboxUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.game.state.AttachUtils;
import electrosphere.engine.LoadingThread;
import electrosphere.game.state.SimulationState;
import electrosphere.game.state.SimulationState.SimulationStateMachine;
import electrosphere.game.state.MicroSimulation;
import electrosphere.logger.LoggerInterface;
import electrosphere.renderer.RenderingEngine;
import electrosphere.util.FileLoadingUtils;
@ -103,9 +102,6 @@ public class Main {
//initialize logging interfaces
LoggerInterface.initLoggers();
//set simulation status to loading title menu
SimulationState.simulationState = SimulationStateMachine.LOADING;
//controls
initControlHandler();
@ -170,7 +166,7 @@ public class Main {
/// I N P U T C O N T R O L S
///
cameraSpeed = 2.5f * deltaTime;
if (glfwGetKey(Globals.window, GLFW_KEY_ESCAPE) == GLFW_PRESS && SimulationState.simulationState == SimulationStateMachine.MAIN_SIMULATION) {
if (glfwGetKey(Globals.window, GLFW_KEY_ESCAPE) == GLFW_PRESS && Globals.RENDER_FLAG_RENDER_SCREEN_FRAMEBUFFER_CONTENT) {
break;
}
@ -190,26 +186,8 @@ public class Main {
///
/// C L I E N T S I M U L A T I O N S T U F F
///
if(SimulationState.simulationState == SimulationStateMachine.MAIN_SIMULATION || SimulationState.simulationState == SimulationStateMachine.ARENA_SIMULATION){
//make items play idle animation
for(Entity item : Globals.entityManager.getItemEntities()){
ItemUtils.updateItemActorAnimation(item);
}
//simulate creature behavior trees
for(Entity currentMoveable : Globals.entityManager.getMoveable()){
MovementTree behaviorTree = CreatureUtils.getEntityMovementTree(currentMoveable);
behaviorTree.simulate();
}
//update attached entity positions
AttachUtils.updateAttachedEntityPositions();
//update hitbox positions
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
HitboxUtils.updatePosition(currentHitbox);
}
//collide hitboxes
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
HitboxUtils.collideEntities(currentHitbox);
}
if(Globals.microSimulation != null && Globals.microSimulation.isReady()){
Globals.microSimulation.simulate();
}
@ -231,7 +209,7 @@ public class Main {
/// C A M E R A S T U F F
///
//poll mouse variables and update camera variables
if(SimulationState.simulationState == SimulationStateMachine.MAIN_SIMULATION || SimulationState.simulationState == SimulationStateMachine.ARENA_SIMULATION){
if(Globals.RENDER_FLAG_RENDER_SCREEN_FRAMEBUFFER_CONTENT){
updateMouseVariables();
@ -295,22 +273,22 @@ public class Main {
public static void updateMouseVariables(){
glfwGetCursorPos(Globals.window, mouse_X_Buffer, mouse_Y_Buffer);
xpos = mouse_X_Buffer[0];
ypos = mouse_Y_Buffer[0];
float xoffset = (float) (xpos - mouse_lastX) * mouseSensitivity;
float yoffset = (float) (mouse_lastY - ypos) * mouseSensitivity;
mouse_lastX = (float) xpos;
mouse_lastY = (float) ypos;
xpos = mouse_X_Buffer[0];
ypos = mouse_Y_Buffer[0];
float xoffset = (float) (xpos - mouse_lastX) * mouseSensitivity;
float yoffset = (float) (mouse_lastY - ypos) * mouseSensitivity;
mouse_lastX = (float) xpos;
mouse_lastY = (float) ypos;
yaw = yaw + xoffset;
pitch = pitch - yoffset;
yaw = yaw + xoffset;
pitch = pitch - yoffset;
if (pitch > 100.0f) {
pitch = 100.0f;
}
if (pitch < -99.0f) {
pitch = -99.0f;
}
if (pitch > 100.0f) {
pitch = 100.0f;
}
if (pitch < -99.0f) {
pitch = -99.0f;
}
}

View File

@ -109,7 +109,6 @@ public class Actor {
if(model != null){
if(animation != null){
model.playAnimation(animation);
model.incrementTime(0.001);
model.incrementTime(animationTime);
model.update_node_transform(model.root_anim_node);
Bone currentBone = model.boneMap.get(boneName);
@ -125,4 +124,22 @@ public class Actor {
return rVal;
}
public Matrix4f getBoneTransform(String boneName){
Matrix4f rVal = new Matrix4f();
Model model = Globals.assetManager.fetchModel(modelPath);
if(model != null){
if(animation != null){
model.playAnimation(animation);
model.incrementTime(animationTime);
model.update_node_transform(model.root_anim_node);
Bone currentBone = model.boneMap.get(boneName);
if(currentBone != null){
rVal = currentBone.final_transform;
// currentBone.inverseBindPoseMatrix
}
}
}
return rVal;
}
}

View File

@ -94,7 +94,7 @@ public class RenderingEngine {
// glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE); Allows you to make the background transparent
// glfwWindowHint(GLFW_OPACITY, 23);
//Creates the window reference object
Globals.window = glfwCreateWindow(screenWidth, screenHeight, "LearnOpenGL", NULL, NULL);
Globals.window = glfwCreateWindow(screenWidth, screenHeight, "ORPG", NULL, NULL);
//Errors for failure to create window (IE: No GUI mode on linux ?)
if (Globals.window == NULL) {
LoggerInterface.loggerEngine.ERROR("Failed to make window.", new Exception("Renderer Creation Failure"));

View File

@ -1,7 +1,7 @@
package electrosphere.util.worldviewer;
import com.google.gson.Gson;
import electrosphere.game.server.simulation.Simulation;
import electrosphere.game.state.MacroSimulation;
import electrosphere.game.server.terrain.manager.ServerTerrainManager;
import electrosphere.game.server.terrain.models.TerrainModel;
import electrosphere.main.Globals;
@ -41,7 +41,7 @@ public class TerrainViewer {
// Utilities.saveObjectToBakedJsonFile("/Config/testingTerrain.json", terrainModel);
terrainModel = FileLoadingUtils.loadObjectFromAssetPath("/Config/testingTerrain.json", TerrainModel.class);
Simulation simulation = new Simulation();
MacroSimulation simulation = new MacroSimulation();
JFrame frame = new JFrame();
TerrainViewerJComponent jComponent = new TerrainViewerJComponent(terrainModel);