Fix server part of entity instancing

This commit is contained in:
austin 2021-08-01 22:51:42 -04:00
parent 3388325838
commit e18e342492
3 changed files with 66 additions and 27 deletions

View File

@ -198,7 +198,7 @@ public class ControlHandler {
if(controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_FORWARD).isIsKey() && glfwGetKey(Globals.window, controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_FORWARD).getKeyValue()) == GLFW_PRESS){
Vector3f newFacingVector = new Vector3f(-cameraEyeVector.x,0,-cameraEyeVector.z).normalize();
CreatureUtils.setMovementVector(Globals.playerCharacter, newFacingVector);
System.out.println("Movement vector: " + newFacingVector);
// System.out.println("Movement vector: " + newFacingVector);
if(movementTree.getState()==MovementTreeState.IDLE || movementTree.getState()==MovementTreeState.SLOWDOWN){
movementTree.start();
}

View File

@ -83,6 +83,7 @@ public class MovementTree {
// Model entityModel = Globals.assetManager.fetchModel(EntityUtils.getEntityModelPath(parent));
Vector3f position = EntityUtils.getPosition(parent);
Vector3f movementVector = CreatureUtils.getMovementVector(parent);
Quaternionf movementQuaternion = new Quaternionf().rotationTo(new Vector3f(0,0,1), movementVector).normalize();
Quaternionf rotation = EntityUtils.getRotation(parent);
Vector3f newPosition;
javax.vecmath.Matrix4f bodyTransformMatrix;
@ -120,7 +121,7 @@ public class MovementTree {
break;
case 1:
state = MovementTreeState.MOVE;
System.out.println("Set state MOVE");
// System.out.println("Set state MOVE");
activateGravityTree();
break;
case 2:
@ -171,16 +172,16 @@ public class MovementTree {
state = MovementTreeState.MOVE;
}
// body.applyCentralForce(PhysicsUtils.jomlToVecmathVector3f(new Vector3f(movementVector.x,0,movementVector.z).normalize().mul(velocity)));
EntityUtils.getRotation(parent).rotationTo(new Vector3f(0,0,1), new Vector3f(movementVector.x,0,movementVector.z));
EntityUtils.getRotation(parent).set(movementQuaternion);
//move the entity
newPosition = new Vector3f(position).add(new Vector3f(movementVector).mul(velocity));
newPosition = new Vector3f(position).add(new Vector3f(0,0,1).rotate(movementQuaternion).mul(velocity));
//check/update if collision
if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){
newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition);
}
// //actually update
position.set(newPosition);
rotation.rotationTo(new Vector3f(0,0,1), movementVector);
rotation.set(movementQuaternion);
bodyTransformMatrix = new javax.vecmath.Matrix4f(PhysicsUtils.jomlToVecmathQuaternionf(rotation),PhysicsUtils.jomlToVecmathVector3f(newPosition),1.0f);
body.setWorldTransform(new electrosphere.linearmath.Transform(bodyTransformMatrix));
@ -244,17 +245,16 @@ public class MovementTree {
entityActor.incrementAnimationTime(0.01);
}
}
Vector3f force = new Vector3f(movementVector).normalize().mul(velocity);
// body.applyCentralForce(PhysicsUtils.jomlToVecmathVector3f(force));
EntityUtils.getRotation(parent).rotationTo(new Vector3f(0,0,1), new Vector3f(movementVector.x,0,movementVector.z));
EntityUtils.getRotation(parent).set(movementQuaternion);
//check if can move forward (collision engine)
//if can, move forward by entity movement stats
newPosition = new Vector3f(position).add(new Vector3f(movementVector).mul(velocity));
newPosition = new Vector3f(position).add(new Vector3f(0,0,1).rotate(movementQuaternion).mul(velocity));
if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){
newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition);
}
position.set(newPosition);
rotation.rotationTo(new Vector3f(0,0,1), movementVector);
rotation.set(movementQuaternion);
bodyTransformMatrix = new javax.vecmath.Matrix4f(PhysicsUtils.jomlToVecmathQuaternionf(rotation),PhysicsUtils.jomlToVecmathVector3f(newPosition),1.0f);
body.setWorldTransform(new electrosphere.linearmath.Transform(bodyTransformMatrix));
@ -325,7 +325,7 @@ public class MovementTree {
state = MovementTreeState.IDLE;
}
// body.applyCentralForce(PhysicsUtils.jomlToVecmathVector3f(new Vector3f(movementVector).mul(-1.0f).normalize().mul(velocity)));
EntityUtils.getRotation(parent).rotationTo(new Vector3f(0,0,1), new Vector3f(movementVector.x,0,movementVector.z));
EntityUtils.getRotation(parent).rotationTo(new Vector3f(0,0,1), movementVector);
//move the entity
newPosition = new Vector3f(position).add(new Vector3f(movementVector).mul(velocity));
if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){

View File

@ -31,28 +31,16 @@ public class DataCellManager {
public void addPlayer(Player player){
playerList.add(player);
}
public void movePlayer(Player player, int newX, int newY){
int playerSimulationRadius = player.getSimulationRadius();
int oldX = player.getWorldX();
int oldY = player.getWorldY();
player.setWorldX(newX);
player.setWorldY(newY);
for(int x = oldX - playerSimulationRadius; x < oldX + playerSimulationRadius + 1; x++){
for(int y = oldY - playerSimulationRadius; y < oldY + playerSimulationRadius + 1; y++){
if(x >= 0 && x < discreteWorldSize && y >= 0 && y < discreteWorldSize){
if(dataCells[x][y] != null){
if(dataCells[x][y].containsPlayer(player)){
dataCells[x][y].removePlayer(player);
}
}
}
}
}
for(int x = newX - playerSimulationRadius; x < newX + playerSimulationRadius + 1; x++){
for(int y = newY - playerSimulationRadius; y < newY + playerSimulationRadius + 1; y++){
if(x >= 0 && x < discreteWorldSize && y >= 0 && y < discreteWorldSize){
if(
x >= 0 && x < discreteWorldSize &&
y >= 0 && y < discreteWorldSize
){
System.out.println("Add player to " + x + " " + y);
if(dataCells[x][y] != null){
dataCells[x][y].addPlayer(player);
} else {
@ -66,6 +54,57 @@ public class DataCellManager {
}
}
public void movePlayer(Player player, int newX, int newY){
int playerSimulationRadius = player.getSimulationRadius();
int oldX = player.getWorldX();
int oldY = player.getWorldY();
player.setWorldX(newX);
player.setWorldY(newY);
// System.out.println("=======" + "SET" + newX + " " + newY + " FROM " + oldX + " " + oldY + "========");
int removals = 0;
int additions = 0;
for(int x = oldX - playerSimulationRadius; x < oldX + playerSimulationRadius + 1; x++){
for(int y = oldY - playerSimulationRadius; y < oldY + playerSimulationRadius + 1; y++){
if(
x >= 0 && x < discreteWorldSize &&
y >= 0 && y < discreteWorldSize &&
(x < newX - playerSimulationRadius || x > newX + playerSimulationRadius || y < newY - playerSimulationRadius || y > newY + playerSimulationRadius)
){
if(dataCells[x][y] != null){
if(dataCells[x][y].containsPlayer(player)){
removals++;
dataCells[x][y].removePlayer(player);
}
}
}
}
}
for(int x = newX - playerSimulationRadius; x < newX + playerSimulationRadius + 1; x++){
for(int y = newY - playerSimulationRadius; y < newY + playerSimulationRadius + 1; y++){
if(
x >= 0 && x < discreteWorldSize &&
y >= 0 && y < discreteWorldSize &&
(x < oldX - playerSimulationRadius || x > oldX + playerSimulationRadius || y < oldY - playerSimulationRadius || y > oldY + playerSimulationRadius)
){
// System.out.println("Add player to " + x + " " + y);
if(dataCells[x][y] != null){
dataCells[x][y].addPlayer(player);
} else {
//create data cell
dataCells[x][y] = new ServerDataCell();
//add player
dataCells[x][y].addPlayer(player);
}
additions++;
} else {
// System.out.println(x + "\t" + (oldX - playerSimulationRadius) + "\t" + (oldX + playerSimulationRadius));
// System.out.println(y + "\t" + (oldY - playerSimulationRadius) + "\t" + (oldY + playerSimulationRadius));
}
}
}
// System.out.println("removals: " + removals + "\tadditions: " + additions);
}
public void removePlayer(Player player){
throw new UnsupportedOperationException("Removing players from the data cell manager isn't supported -- DataCellManager -- removePlayer(Player player)");
}