Unifies gravity and movement trees under common "collidabletree" that
parses impulses from collidable objects
This commit is contained in:
austin 2021-10-27 21:56:43 -04:00
parent 766093d843
commit 64e6d60755
21 changed files with 302 additions and 213 deletions

View File

@ -88,11 +88,11 @@
"movementSystems" : [
{
"type" : "GROUND",
"acceleration" : 0.15,
"maxVelocity" : 1.5
"acceleration" : 0.015,
"maxVelocity" : 0.15
}
],
"physicsObject" : {
"collidable" : {
"type" : "CYLINDER",
"dimension1" : 0.1,
"dimension2" : 0.45,
@ -216,7 +216,7 @@
"maxVelocity" : 0.025
}
],
"physicsObject" : {
"collidable" : {
"type" : "CYLINDER",
"dimension1" : 0.1,
"dimension2" : 0.2,
@ -271,7 +271,7 @@
"maxVelocity" : 1
}
],
"physicsObject" : {
"collidable" : {
"type" : "CYLINDER",
"dimension1" : 0.1,
"dimension2" : 0.45,
@ -330,7 +330,7 @@
"maxVelocity" : 0.025
}
],
"physicsObject" : {
"collidable" : {
"type" : "CYLINDER",
"dimension1" : 0.1,
"dimension2" : 0.2,

View File

@ -5,8 +5,8 @@ import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.AttackTree;
import electrosphere.entity.state.movement.MovementTree;
import electrosphere.entity.state.movement.MovementTree.MovementTreeState;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.state.movement.GroundMovementTree.MovementTreeState;
import electrosphere.main.Globals;
import electrosphere.menu.MenuTransition;
import electrosphere.net.parser.net.message.EntityMessage;
@ -213,7 +213,7 @@ public class ControlHandler {
public void pollMainGameControls(){
if(Globals.playerCharacter != null){
MovementTree movementTree = CreatureUtils.getEntityMovementTree(Globals.playerCharacter);
GroundMovementTree movementTree = CreatureUtils.getEntityMovementTree(Globals.playerCharacter);
AttackTree attackTree = CreatureUtils.getAttackTree(Globals.playerCharacter);
Vector3f cameraEyeVector = CameraEntityUtils.getCameraEye(Globals.playerCamera);
/*

View File

@ -114,7 +114,7 @@ public class EntityDataStrings {
public static final String COLLISION_ENTITY_DATA_PARENT = "collisionDataParent";
public static final String COLLISION_ENTITY_BEHAVIOR_TREE = "collisionEntityBehaviorTree";
public static final String COLLIDABLE_TREE = "collidableTree";
public static final String HITBOX_DATA = "hitboxData";

View File

@ -30,6 +30,7 @@ public class EntityManager {
static CopyOnWriteArrayList<Entity> lifeStateList = new CopyOnWriteArrayList();
static CopyOnWriteArrayList<Entity> particleList = new CopyOnWriteArrayList();
static CopyOnWriteArrayList<Entity> gravityList = new CopyOnWriteArrayList();
static CopyOnWriteArrayList<Entity> collidableList = new CopyOnWriteArrayList();
public EntityManager(){
@ -128,6 +129,14 @@ public class EntityManager {
return gravityList;
}
public void registerCollidableEntity(Entity e){
collidableList.add(e);
}
public CopyOnWriteArrayList<Entity> getCollidables(){
return collidableList;
}
public void deregisterEntity(Entity e){
if(lightList.contains(e)){
lightList.remove(e);
@ -163,6 +172,9 @@ public class EntityManager {
if(gravityList.contains(e)){
gravityList.remove(e);
}
if(collidableList.contains(e)){
collidableList.remove(e);
}
}
public void recursiveDeregister(Entity target){

View File

@ -5,7 +5,8 @@
*/
package electrosphere.entity;
import electrosphere.entity.state.movement.MovementTree;
import electrosphere.entity.state.collidable.CollidableTree;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.renderer.Model;
@ -90,4 +91,8 @@ public class EntityUtils {
return (Actor)e.getData(EntityDataStrings.DATA_STRING_ACTOR);
}
public static CollidableTree getCollidableTree(Entity e){
return (CollidableTree)e.getData(EntityDataStrings.COLLIDABLE_TREE);
}
}

View File

@ -3,7 +3,7 @@ package electrosphere.entity.state;
import electrosphere.collision.dispatch.CollisionObject;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.movement.Impulse;
import electrosphere.entity.state.collidable.Impulse;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.game.collision.PhysicsUtils;
import electrosphere.game.collision.collidable.Collidable;
@ -39,16 +39,18 @@ public class GravityTree {
int maxFrame = 60;
public GravityTree(Entity e){
public GravityTree(Entity e, Collidable collidable, CollisionObject body){
state = GravityTreeState.ACTIVE;
parent = e;
}
public void setCollisionObject(CollisionObject body, Collidable collidable){
this.body = body;
this.collidable = collidable;
}
// public void setCollisionObject(CollisionObject body, Collidable collidable){
// this.body = body;
// this.collidable = collidable;
// }
public GravityTreeState getState(){
return state;
}
@ -115,14 +117,19 @@ public class GravityTree {
float gravityDif = gravityConstant * (float)Math.pow(1.0f - linearDamping,deltaTime * 2);
Vector3d newGravityPos = new Vector3d(position.x,position.y - gravityDif,position.z);
float hitFraction = Globals.collisionEngine.sweepTest(body, new Vector3f((float)position.x,(float)position.y,(float)position.z), new Vector3f((float)newGravityPos.x,(float)newGravityPos.y,(float)newGravityPos.z));
if(hitFraction >= 0){
position.set(new Vector3d(position.x,position.y - gravityDif * hitFraction,position.z));
} else {
position.set(new Vector3d(position.x,position.y - gravityDif,position.z));
// if(hitFraction >= 0){
// collidable.addImpulse(new Impulse(new Vector3d(0,-1,0),gravityDif * hitFraction,"gravity"));
// position.set(new Vector3d(position.x,position.y - gravityDif * hitFraction,position.z));
// } else {
// position.set(new Vector3d(position.x,position.y - gravityDif,position.z));
// }
if(hitFraction < 0){
hitFraction = 1;
}
collidable.addImpulse(new Impulse(new Vector3d(0,-1,0),gravityDif * hitFraction,"gravity"));
// System.out.println(hitFraction);
bodyTransformMatrix = new javax.vecmath.Matrix4f(PhysicsUtils.jomlToVecmathQuaternionf(rotation),PhysicsUtils.jomlToVecmathVector3f(new Vector3f((float)position.x,(float)position.y,(float)position.z)),1.0f);
body.setWorldTransform(new electrosphere.linearmath.Transform(bodyTransformMatrix));
// bodyTransformMatrix = new javax.vecmath.Matrix4f(PhysicsUtils.jomlToVecmathQuaternionf(rotation),PhysicsUtils.jomlToVecmathVector3f(new Vector3f((float)position.x,(float)position.y,(float)position.z)),1.0f);
// body.setWorldTransform(new electrosphere.linearmath.Transform(bodyTransformMatrix));
}
break;
case NOT_ACTIVE:
@ -158,7 +165,7 @@ public class GravityTree {
break;
} else if(
impulse.getType().equals(Collidable.TYPE_STRUCTURE) &&
new Vector3f(impulse.getDirection()).normalize().y > 0.7
new Vector3d(impulse.getDirection()).normalize().y > 0.7
){
rVal = true;
}

View File

@ -1,11 +1,11 @@
package electrosphere.entity.state;
import electrosphere.entity.state.movement.MovementTree;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.AttackTree.AttackTreeState;
import electrosphere.entity.state.movement.MovementTree.MovementTreeState;
import electrosphere.entity.state.movement.GroundMovementTree.MovementTreeState;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.renderer.Actor;
@ -57,7 +57,7 @@ public class IdleTree {
Actor entityActor = EntityUtils.getActor(parent);
boolean hasMovementTree = parent.getDataKeys().contains(EntityDataStrings.DATA_STRING_MOVEMENT_BT);
MovementTree movementTree = null;
GroundMovementTree movementTree = null;
if(hasMovementTree){
movementTree = CreatureUtils.getEntityMovementTree(parent);
}

View File

@ -0,0 +1,72 @@
package electrosphere.entity.state.collidable;
import electrosphere.collision.dispatch.CollisionObject;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.game.collision.PhysicsUtils;
import electrosphere.game.collision.collidable.Collidable;
import electrosphere.main.Globals;
import org.joml.Quaternionf;
import org.joml.Vector3d;
import org.joml.Vector3f;
/**
*
* @author amaterasu
*/
public class CollidableTree {
Entity parent;
CollisionObject body;
Collidable collidable;
public CollidableTree(Entity e, Collidable collidable, CollisionObject body){
parent = e;
this.collidable = collidable;
this.body = body;
}
public void simulate(){
Vector3d position = EntityUtils.getPosition(parent);
Quaternionf rotation = EntityUtils.getRotation(parent);
Vector3d offsetVector = new Vector3d();
Vector3d newPosition = new Vector3d(position);
javax.vecmath.Matrix4f bodyTransformMatrix;
//handle impulses
for(Impulse impulse : collidable.getImpulses()){
// collidable.getImpulses().remove(impulse);
Vector3d impulseForce = new Vector3d(impulse.getDirection()).mul(impulse.getForce());
// if(impulse.type.matches("movement")){
// System.out.println("Impulse force: " + impulseForce);
// }
offsetVector.add(impulseForce);
}
//make sure we're in a valid (World bounds) position
newPosition.add(offsetVector);
if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){
newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition);
}
position.set(newPosition);
//update collision engine of this thing's position
bodyTransformMatrix = new javax.vecmath.Matrix4f(PhysicsUtils.jomlToVecmathQuaternionf(rotation),PhysicsUtils.jomlToVecmathVector3f(new Vector3f((float)position.x,(float)position.y,(float)position.z)),1.0f);
body.setWorldTransform(new electrosphere.linearmath.Transform(bodyTransformMatrix));
// bodyTransformMatrix = new javax.vecmath.Matrix4f(PhysicsUtils.jomlToVecmathQuaternionf(rotation),PhysicsUtils.jomlToVecmathVector3f(new Vector3f((float)newPosition.x,(float)newPosition.y,(float)newPosition.z)),1.0f);
// body.setWorldTransform(new electrosphere.linearmath.Transform(bodyTransformMatrix));
}
public void setCollisionObject(CollisionObject body, Collidable collidable){
this.body = body;
this.collidable = collidable;
}
}

View File

@ -1,5 +1,6 @@
package electrosphere.entity.state.movement;
package electrosphere.entity.state.collidable;
import org.joml.Vector3d;
import org.joml.Vector3f;
/**
@ -8,21 +9,21 @@ import org.joml.Vector3f;
*/
public class Impulse {
Vector3f direction;
float force;
Vector3d direction;
double force;
String type;
public Impulse(Vector3f dir, float force, String type){
public Impulse(Vector3d dir, double force, String type){
this.force = force;
this.direction = dir;
this.type = type;
}
public Vector3f getDirection() {
public Vector3d getDirection() {
return direction;
}
public float getForce() {
public double getForce() {
return force;
}

View File

@ -1,5 +1,6 @@
package electrosphere.entity.state.movement;
import electrosphere.entity.state.collidable.Impulse;
import electrosphere.collision.dispatch.CollisionObject;
import electrosphere.dynamics.RigidBody;
import electrosphere.entity.CameraEntityUtils;
@ -8,6 +9,7 @@ import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.GravityTree;
import electrosphere.entity.state.GravityTree;
import electrosphere.game.collision.CollisionEngine;
import electrosphere.game.collision.PhysicsUtils;
import electrosphere.game.collision.collidable.Collidable;
@ -28,7 +30,7 @@ import org.joml.Vector3f;
/*
Behavior tree for movement in an entity
*/
public class MovementTree {
public class GroundMovementTree {
public static enum MovementTreeState {
STARTUP,
@ -43,7 +45,6 @@ public class MovementTree {
Entity parent;
CollisionObject body;
Collidable collidable;
CopyOnWriteArrayList<EntityMessage> networkMessageQueue = new CopyOnWriteArrayList();
@ -51,20 +52,16 @@ public class MovementTree {
long lastUpdateTime = 0;
public MovementTree(Entity e){
public GroundMovementTree(Entity e, Collidable collidable){
state = MovementTreeState.IDLE;
parent = e;
this.collidable = collidable;
}
public MovementTreeState getState(){
return state;
}
public void setCollisionObject(CollisionObject body, Collidable collidable){
this.body = body;
this.collidable = collidable;
}
public void start(){
//TODO: check if can start moving
state = MovementTreeState.STARTUP;
@ -90,7 +87,6 @@ public class MovementTree {
Quaternionf movementQuaternion = new Quaternionf().rotationTo(new Vector3f(0,0,1), movementVector).normalize();
Quaternionf rotation = EntityUtils.getRotation(parent);
Vector3d newPosition;
javax.vecmath.Matrix4f bodyTransformMatrix;
//parse attached network messages
for(EntityMessage message : networkMessageQueue){
@ -148,19 +144,7 @@ public class MovementTree {
}
}
}
//handle impulses
if(collidable != null){
for(Impulse impulse : collidable.getImpulses()){
// collidable.getImpulses().remove(impulse);
Vector3f impulseForce = new Vector3f(impulse.direction).mul(impulse.force).mul(Main.deltaTime);
// System.out.println("Impulse force: " + impulseForce);
position.add(impulseForce);
}
}
if(body != null){
bodyTransformMatrix = new javax.vecmath.Matrix4f(PhysicsUtils.jomlToVecmathQuaternionf(rotation),PhysicsUtils.jomlToVecmathVector3f(new Vector3f((float)position.x,(float)position.y,(float)position.z)),1.0f);
body.setWorldTransform(new electrosphere.linearmath.Transform(bodyTransformMatrix));
}
//state machine
switch(state){
@ -181,19 +165,17 @@ public class MovementTree {
}
// body.applyCentralForce(PhysicsUtils.jomlToVecmathVector3f(new Vector3f(movementVector.x,0,movementVector.z).normalize().mul(velocity)));
EntityUtils.getRotation(parent).set(movementQuaternion);
//move the entity
newPosition = new Vector3d(position).add(new Vector3d(movementVector).mul(velocity).mul(Main.deltaTime));
//check/update if collision
if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){
newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition);
}
// //move the entity
// newPosition = new Vector3d(position).add(new Vector3d(movementVector).mul(velocity).mul(Main.deltaTime));
// //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);
collidable.addImpulse(new Impulse(new Vector3d(movementVector), velocity, "movement"));
// position.set(newPosition);
rotation.set(movementQuaternion);
bodyTransformMatrix = new javax.vecmath.Matrix4f(PhysicsUtils.jomlToVecmathQuaternionf(rotation),PhysicsUtils.jomlToVecmathVector3f(new Vector3f((float)newPosition.x,(float)newPosition.y,(float)newPosition.z)),1.0f);
body.setWorldTransform(new electrosphere.linearmath.Transform(bodyTransformMatrix));
activateGravityTree();
if(Globals.RUN_SERVER){
@ -211,37 +193,37 @@ public class MovementTree {
// 0
// )
// );
Globals.dataCellManager.sendNetworkMessageToChunk(
EntityMessage.constructmoveUpdateMessage(
parent.getId(),
System.currentTimeMillis(),
(float)newPosition.x,
(float)newPosition.y,
(float)newPosition.z,
movementVector.x,
movementVector.y,
movementVector.z,
velocity,
0
),
Globals.serverWorldData.convertRealToChunkSpace(newPosition.x),
Globals.serverWorldData.convertRealToChunkSpace(newPosition.z)
);
// Globals.dataCellManager.sendNetworkMessageToChunk(
// EntityMessage.constructmoveUpdateMessage(
// parent.getId(),
// System.currentTimeMillis(),
// (float)newPosition.x,
// (float)newPosition.y,
// (float)newPosition.z,
// movementVector.x,
// movementVector.y,
// movementVector.z,
// velocity,
// 0
// ),
// Globals.serverWorldData.convertRealToChunkSpace(newPosition.x),
// Globals.serverWorldData.convertRealToChunkSpace(newPosition.z)
// );
} else if(Globals.RUN_CLIENT && parent.getId() == Globals.clientCharacterID){
Globals.clientConnection.queueOutgoingMessage(
EntityMessage.constructmoveUpdateMessage(
parent.getId(),
System.currentTimeMillis(),
(float)newPosition.x,
(float)newPosition.y,
(float)newPosition.z,
movementVector.x,
movementVector.y,
movementVector.z,
velocity,
0
)
);
// Globals.clientConnection.queueOutgoingMessage(
// EntityMessage.constructmoveUpdateMessage(
// parent.getId(),
// System.currentTimeMillis(),
// (float)newPosition.x,
// (float)newPosition.y,
// (float)newPosition.z,
// movementVector.x,
// movementVector.y,
// movementVector.z,
// velocity,
// 0
// )
// );
}
break;
case MOVE:
@ -257,16 +239,14 @@ public class MovementTree {
EntityUtils.getRotation(parent).set(movementQuaternion);
//check if can move forward (collision engine)
//if can, move forward by entity movement stats
newPosition = new Vector3d(position).add(new Vector3d(movementVector).mul(velocity).mul(Main.deltaTime));
if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){
newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition);
}
position.set(newPosition);
// newPosition = new Vector3d(position).add(new Vector3d(movementVector).mul(velocity).mul(Main.deltaTime));
// if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){
// newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition);
// }
collidable.addImpulse(new Impulse(new Vector3d(movementVector), velocity, "movement"));
// position.set(newPosition);
rotation.set(movementQuaternion);
bodyTransformMatrix = new javax.vecmath.Matrix4f(PhysicsUtils.jomlToVecmathQuaternionf(rotation),PhysicsUtils.jomlToVecmathVector3f(new Vector3f((float)newPosition.x,(float)newPosition.y,(float)newPosition.z)),1.0f);
body.setWorldTransform(new electrosphere.linearmath.Transform(bodyTransformMatrix));
activateGravityTree();
if(Globals.RUN_SERVER){
@ -284,37 +264,37 @@ public class MovementTree {
// 1
// )
// );
Globals.dataCellManager.sendNetworkMessageToChunk(
EntityMessage.constructmoveUpdateMessage(
parent.getId(),
System.currentTimeMillis(),
(float)newPosition.x,
(float)newPosition.y,
(float)newPosition.z,
movementVector.x,
movementVector.y,
movementVector.z,
velocity,
1
),
Globals.serverWorldData.convertRealToChunkSpace(newPosition.x),
Globals.serverWorldData.convertRealToChunkSpace(newPosition.z)
);
// Globals.dataCellManager.sendNetworkMessageToChunk(
// EntityMessage.constructmoveUpdateMessage(
// parent.getId(),
// System.currentTimeMillis(),
// (float)newPosition.x,
// (float)newPosition.y,
// (float)newPosition.z,
// movementVector.x,
// movementVector.y,
// movementVector.z,
// velocity,
// 1
// ),
// Globals.serverWorldData.convertRealToChunkSpace(newPosition.x),
// Globals.serverWorldData.convertRealToChunkSpace(newPosition.z)
// );
} else if(Globals.RUN_CLIENT && parent.getId() == Globals.clientCharacterID){
Globals.clientConnection.queueOutgoingMessage(
EntityMessage.constructmoveUpdateMessage(
parent.getId(),
System.currentTimeMillis(),
(float)newPosition.x,
(float)newPosition.y,
(float)newPosition.z,
movementVector.x,
movementVector.y,
movementVector.z,
velocity,
1
)
);
// Globals.clientConnection.queueOutgoingMessage(
// EntityMessage.constructmoveUpdateMessage(
// parent.getId(),
// System.currentTimeMillis(),
// (float)newPosition.x,
// (float)newPosition.y,
// (float)newPosition.z,
// movementVector.x,
// movementVector.y,
// movementVector.z,
// velocity,
// 1
// )
// );
}
break;
case SLOWDOWN:
@ -335,16 +315,14 @@ public class MovementTree {
// body.applyCentralForce(PhysicsUtils.jomlToVecmathVector3f(new Vector3f(movementVector).mul(-1.0f).normalize().mul(velocity)));
EntityUtils.getRotation(parent).rotationTo(new Vector3f(0,0,1), movementVector);
//move the entity
newPosition = new Vector3d(position).add(new Vector3d(movementVector).mul(velocity).mul(Main.deltaTime));
if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){
newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition);
}
position.set(newPosition);
// newPosition = new Vector3d(position).add(new Vector3d(movementVector).mul(velocity).mul(Main.deltaTime));
// if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){
// newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition);
// }
collidable.addImpulse(new Impulse(new Vector3d(movementVector), velocity, "movement"));
// position.set(newPosition);
rotation.rotationTo(new Vector3f(0,0,1), movementVector);
bodyTransformMatrix = new javax.vecmath.Matrix4f(PhysicsUtils.jomlToVecmathQuaternionf(rotation),PhysicsUtils.jomlToVecmathVector3f(new Vector3f((float)newPosition.x,(float)newPosition.y,(float)newPosition.z)),1.0f);
body.setWorldTransform(new electrosphere.linearmath.Transform(bodyTransformMatrix));
activateGravityTree();
if(Globals.RUN_SERVER){
@ -362,37 +340,37 @@ public class MovementTree {
// 2
// )
// );
Globals.dataCellManager.sendNetworkMessageToChunk(
EntityMessage.constructmoveUpdateMessage(
parent.getId(),
System.currentTimeMillis(),
(float)newPosition.x,
(float)newPosition.y,
(float)newPosition.z,
movementVector.x,
movementVector.y,
movementVector.z,
velocity,
2
),
Globals.serverWorldData.convertRealToChunkSpace(newPosition.x),
Globals.serverWorldData.convertRealToChunkSpace(newPosition.z)
);
// Globals.dataCellManager.sendNetworkMessageToChunk(
// EntityMessage.constructmoveUpdateMessage(
// parent.getId(),
// System.currentTimeMillis(),
// (float)newPosition.x,
// (float)newPosition.y,
// (float)newPosition.z,
// movementVector.x,
// movementVector.y,
// movementVector.z,
// velocity,
// 2
// ),
// Globals.serverWorldData.convertRealToChunkSpace(newPosition.x),
// Globals.serverWorldData.convertRealToChunkSpace(newPosition.z)
// );
} else if(Globals.RUN_CLIENT && parent.getId() == Globals.clientCharacterID){
Globals.clientConnection.queueOutgoingMessage(
EntityMessage.constructmoveUpdateMessage(
parent.getId(),
System.currentTimeMillis(),
(float)newPosition.x,
(float)newPosition.y,
(float)newPosition.z,
movementVector.x,
movementVector.y,
movementVector.z,
velocity,
2
)
);
// Globals.clientConnection.queueOutgoingMessage(
// EntityMessage.constructmoveUpdateMessage(
// parent.getId(),
// System.currentTimeMillis(),
// (float)newPosition.x,
// (float)newPosition.y,
// (float)newPosition.z,
// movementVector.x,
// movementVector.y,
// movementVector.z,
// velocity,
// 2
// )
// );
}
break;
case IDLE:

View File

@ -131,4 +131,8 @@ public class CollisionObjUtils {
PhysicsUtils.setRigidBodyTransform(position, rotation, body);
}
public static Collidable getCollidable(Entity e){
return (Collidable)e.getData(EntityDataStrings.PHYSICS_COLLIDABLE);
}
}

View File

@ -5,7 +5,7 @@ import electrosphere.dynamics.RigidBody;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.movement.MovementTree;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.types.hitbox.HitboxData;
import electrosphere.entity.types.hitbox.HitboxUtils;
import electrosphere.game.config.creature.type.CreatureType;
@ -13,11 +13,13 @@ import electrosphere.game.config.creature.type.MovementSystem;
import electrosphere.entity.state.AttackTree;
import electrosphere.entity.state.GravityTree;
import electrosphere.entity.state.IdleTree;
import electrosphere.entity.state.collidable.CollidableTree;
import electrosphere.entity.types.collision.CollisionObjUtils;
import electrosphere.entity.types.life.LifeState;
import electrosphere.game.collision.PhysicsUtils;
import electrosphere.game.collision.collidable.Collidable;
import electrosphere.game.config.creature.type.AttackMove;
import electrosphere.game.config.creature.type.PhysicsObject;
import electrosphere.game.config.creature.type.CollidableTemplate;
import electrosphere.logger.LoggerInterface;
import electrosphere.main.Globals;
import electrosphere.main.Main;
@ -68,20 +70,8 @@ public class CreatureUtils {
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHurtbox(rVal, hitboxdata.getBone(), hitboxdata.getRadius()));
}
}
for(MovementSystem movementSystem : rawType.getMovementSystems()){
switch(movementSystem.getType()){
case "GROUND":
rVal.putData(EntityDataStrings.DATA_STRING_MOVEMENT_BT, new MovementTree(rVal));
rVal.putData(EntityDataStrings.DATA_STRING_MOVEMENT_VECTOR, new Vector3f(0,0,0));
rVal.putData(EntityDataStrings.DATA_STRING_MAX_NATURAL_VELOCITY, movementSystem.getMaxVelocity());
rVal.putData(EntityDataStrings.DATA_STRING_ACCELERATION, movementSystem.getAcceleration());
rVal.putData(EntityDataStrings.DATA_STRING_VELOCITY, 0f);
Globals.entityManager.registerMoveableEntity(rVal);
break;
}
}
if(rawType.getPhysicsObject() != null){
PhysicsObject physicsTemplate = rawType.getPhysicsObject();
if(rawType.getCollidable() != null){
CollidableTemplate physicsTemplate = rawType.getCollidable();
switch(physicsTemplate.getType()){
case "CYLINDER":
CollisionObject rigidBody = PhysicsUtils.getCylinderObject(new Vector3f(physicsTemplate.getDimension1(),physicsTemplate.getDimension2(),physicsTemplate.getDimension3()));
@ -90,12 +80,26 @@ public class CreatureUtils {
rVal.putData(EntityDataStrings.PHYSICS_COLLISION_BODY_OFFSET, new Vector3f(physicsTemplate.getOffsetX(),physicsTemplate.getOffsetY(),physicsTemplate.getOffsetZ()));
rVal.putData(EntityDataStrings.PHYSICS_MODEL_TEMPLATE, physicsTemplate);
rVal.putData(EntityDataStrings.PHYSICS_COLLIDABLE,collidable);
rVal.putData(EntityDataStrings.COLLIDABLE_TREE, new CollidableTree(rVal,collidable,rigidBody));
Globals.collisionEngine.registerPhysicsEntity(rVal);
Globals.collisionEngine.registerDynamicPhysicsEntity(rVal);
Globals.collisionEngine.registerCollisionObject(rigidBody, collidable);
if(rVal.getDataKeys().contains(EntityDataStrings.DATA_STRING_MOVEMENT_BT)){
CreatureUtils.getEntityMovementTree(rVal).setCollisionObject(rigidBody, collidable);
}
Globals.entityManager.registerCollidableEntity(rVal);
// if(rVal.getDataKeys().contains(EntityDataStrings.DATA_STRING_MOVEMENT_BT)){
// CreatureUtils.getEntityMovementTree(rVal).setCollisionObject(rigidBody, collidable);
// }
break;
}
}
for(MovementSystem movementSystem : rawType.getMovementSystems()){
switch(movementSystem.getType()){
case "GROUND":
rVal.putData(EntityDataStrings.DATA_STRING_MOVEMENT_BT, new GroundMovementTree(rVal,CollisionObjUtils.getCollidable(rVal)));
rVal.putData(EntityDataStrings.DATA_STRING_MOVEMENT_VECTOR, new Vector3f(0,0,0));
rVal.putData(EntityDataStrings.DATA_STRING_MAX_NATURAL_VELOCITY, movementSystem.getMaxVelocity());
rVal.putData(EntityDataStrings.DATA_STRING_ACCELERATION, movementSystem.getAcceleration());
rVal.putData(EntityDataStrings.DATA_STRING_VELOCITY, 0f);
Globals.entityManager.registerMoveableEntity(rVal);
break;
}
}
@ -111,10 +115,10 @@ public class CreatureUtils {
Globals.entityManager.registerAttackerEntity(rVal);
break;
case "GRAVITY":
GravityTree gravityTree = new GravityTree(rVal);
Collidable collidable = (Collidable)rVal.getData(EntityDataStrings.PHYSICS_COLLIDABLE);
CollisionObject collisionObject = (CollisionObject)rVal.getData(EntityDataStrings.PHYSICS_COLLISION_BODY);
gravityTree.setCollisionObject(collisionObject, collidable);
GravityTree gravityTree = new GravityTree(rVal,collidable,collisionObject);
// gravityTree.setCollisionObject(collisionObject, collidable);
rVal.putData(EntityDataStrings.GRAVITY_ENTITY, true);
rVal.putData(EntityDataStrings.GRAVITY_TREE, gravityTree);
Globals.entityManager.registerGravityEntity(rVal);
@ -187,8 +191,8 @@ public class CreatureUtils {
e.putData(EntityDataStrings.DATA_STRING_MAX_NATURAL_VELOCITY, scalar);
}
public static MovementTree getEntityMovementTree(Entity e){
return (MovementTree)e.getData(EntityDataStrings.DATA_STRING_MOVEMENT_BT);
public static GroundMovementTree getEntityMovementTree(Entity e){
return (GroundMovementTree)e.getData(EntityDataStrings.DATA_STRING_MOVEMENT_BT);
}
public static void attachEntityMessageToMovementTree(Entity e, EntityMessage em){

View File

@ -3,7 +3,7 @@ package electrosphere.entity.types.item;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.movement.MovementTree;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.types.attach.AttachUtils;
import electrosphere.entity.types.hitbox.HitboxData;
import electrosphere.entity.types.hitbox.HitboxUtils;

View File

@ -25,7 +25,7 @@ import electrosphere.util.ObjectArrayList;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.movement.Impulse;
import electrosphere.entity.state.collidable.Impulse;
import electrosphere.entity.types.hitbox.HitboxData;
import electrosphere.game.collision.collidable.Collidable;
import static electrosphere.main.Main.deltaTime;
@ -84,7 +84,7 @@ public class CollisionEngine {
Collidable physicsObject1 = (Collidable)object1.getUserPointer();
Collidable physicsObject2 = (Collidable)object2.getUserPointer();
boolean hit = false;
Vector3f normal = null;
Vector3d normal = null;
float magnitude = 0.0f;
for (int j = 0; j < manifold.getNumContacts(); j++) {
ManifoldPoint contactPoint = manifold.getContactPoint(j);
@ -94,13 +94,13 @@ public class CollisionEngine {
magnitude = magnitude;// * (float)Math.pow(1.0f - linearDamping,deltaTime * 2);
hit = true;
// System.out.println(contactPoint.positionWorldOnA + " " + contactPoint.positionWorldOnB);
normal = new Vector3f(contactPoint.normalWorldOnB.x,contactPoint.normalWorldOnB.y,contactPoint.normalWorldOnB.z);
normal = new Vector3d(contactPoint.normalWorldOnB.x,contactPoint.normalWorldOnB.y,contactPoint.normalWorldOnB.z);
break;
}
}
if (hit) {
resolveCollision(physicsObject1,physicsObject2, normal, magnitude);
resolveCollision(physicsObject2,physicsObject1, new Vector3f(normal).mul(-1.0f), magnitude);
resolveCollision(physicsObject2,physicsObject1, new Vector3d(normal).mul(-1.0f), magnitude);
// System.out.println("HIT + " + normal);
// Collision happened between physicsObject1 and physicsObject2. Collision normal is in variable 'normal'.
}
@ -115,7 +115,7 @@ public class CollisionEngine {
}
public static void resolveCollision(Collidable impactor, Collidable receiver, Vector3f normal, float magnitude){
public static void resolveCollision(Collidable impactor, Collidable receiver, Vector3d normal, float magnitude){
switch(receiver.getType()){
case Collidable.TYPE_CREATURE:
switch(impactor.getType()){
@ -123,7 +123,7 @@ public class CollisionEngine {
// System.out.println(EntityUtils.getPosition(impactor.getParent()) + " " + EntityUtils.getPosition(receiver.getParent()));
// System.out.println();
// System.out.println("Terrain-creature collision: " + normal + " mag:" + magnitude);
receiver.addImpulse(new Impulse(new Vector3f(0,normal.y,0), -magnitude*2, Collidable.TYPE_TERRAIN));
receiver.addImpulse(new Impulse(new Vector3d(0,normal.y,0), -magnitude*2, Collidable.TYPE_TERRAIN));
break;
case Collidable.TYPE_CREATURE:
receiver.addImpulse(new Impulse(normal, magnitude, Collidable.TYPE_CREATURE));

View File

@ -3,7 +3,7 @@ package electrosphere.game.collision.collidable;
import electrosphere.collision.shapes.CollisionShape;
import electrosphere.dynamics.RigidBody;
import electrosphere.entity.Entity;
import electrosphere.entity.state.movement.Impulse;
import electrosphere.entity.state.collidable.Impulse;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

View File

@ -4,7 +4,7 @@ package electrosphere.game.config.creature.type;
*
* @author amaterasu
*/
public class PhysicsObject {
public class CollidableTemplate {
String type;

View File

@ -9,7 +9,7 @@ public class CreatureType {
List<HitboxData> hitboxes;
List<String> tokens;
List<MovementSystem> movementSystems;
PhysicsObject physicsObject;
CollidableTemplate collidable;
List<AttackMove> attackMoves;
HealthSystem healthSystem;
String modelPath;
@ -46,8 +46,8 @@ public class CreatureType {
return healthSystem;
}
public PhysicsObject getPhysicsObject() {
return physicsObject;
public CollidableTemplate getCollidable() {
return collidable;
}

View File

@ -4,7 +4,7 @@ import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.AttackTree;
import electrosphere.entity.state.movement.MovementTree;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.game.server.ai.AI;
import electrosphere.main.Globals;
@ -70,8 +70,8 @@ public class MindlessAttacker extends AI{
Vector3d characterPosition = EntityUtils.getPosition(character);
Vector3d moveVector = new Vector3d(targetPosition).sub(characterPosition).normalize();
CreatureUtils.setMovementVector(character, new Vector3f((float)moveVector.x,(float)moveVector.y,(float)moveVector.z));
MovementTree characterMoveTree = CreatureUtils.getEntityMovementTree(character);
if(characterMoveTree.getState()==MovementTree.MovementTreeState.IDLE || characterMoveTree.getState()==MovementTree.MovementTreeState.SLOWDOWN){
GroundMovementTree characterMoveTree = CreatureUtils.getEntityMovementTree(character);
if(characterMoveTree.getState()==GroundMovementTree.MovementTreeState.IDLE || characterMoveTree.getState()==GroundMovementTree.MovementTreeState.SLOWDOWN){
characterMoveTree.start();
}
}

View File

@ -7,8 +7,9 @@ import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.AttackTree;
import electrosphere.entity.state.GravityTree;
import electrosphere.entity.state.IdleTree;
import electrosphere.entity.state.movement.MovementTree;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.state.ParticleTree;
import electrosphere.entity.state.collidable.CollidableTree;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.types.hitbox.HitboxUtils;
import electrosphere.entity.types.item.ItemUtils;
@ -56,7 +57,7 @@ public class MicroSimulation {
}
//simulate creature behavior trees
for(Entity currentMoveable : Globals.entityManager.getMoveable()){
MovementTree behaviorTree = CreatureUtils.getEntityMovementTree(currentMoveable);
GroundMovementTree behaviorTree = CreatureUtils.getEntityMovementTree(currentMoveable);
behaviorTree.simulate();
}
//simulate creature gravity trees
@ -97,6 +98,11 @@ public class MicroSimulation {
HitboxUtils.collideEntities(currentHitbox);
}
}
//tally collidables and offset position accordingly
for(Entity currentCollidable : Globals.entityManager.getCollidables()){
CollidableTree tree = EntityUtils.getCollidableTree(currentCollidable);
tree.simulate();
}
//clear collidable impulse lists
Globals.collisionEngine.clearCollidableImpulseLists();
//delete all client side entities that aren't in visible chunks

View File

@ -12,7 +12,7 @@ import electrosphere.renderer.Model;
import electrosphere.renderer.RenderUtils;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.movement.MovementTree;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.types.hitbox.HitboxUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.entity.types.attach.AttachUtils;

View File

@ -6,7 +6,7 @@ import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.types.hitbox.HitboxData;
import electrosphere.entity.types.hitbox.HitboxUtils;
import electrosphere.game.config.creature.type.PhysicsObject;
import electrosphere.game.config.creature.type.CollidableTemplate;
import electrosphere.logger.LoggerInterface;
import electrosphere.main.Globals;
import static electrosphere.main.Main.deltaTime;
@ -474,7 +474,7 @@ public class RenderingEngine {
Model physicsGraphicsModel;
for(Entity physicsEntity : Globals.collisionEngine.getDynamicPhysicsEntities()){
if((boolean)physicsEntity.getData(EntityDataStrings.DATA_STRING_DRAW)){
PhysicsObject template = (PhysicsObject)physicsEntity.getData(EntityDataStrings.PHYSICS_MODEL_TEMPLATE);
CollidableTemplate template = (CollidableTemplate)physicsEntity.getData(EntityDataStrings.PHYSICS_MODEL_TEMPLATE);
switch(template.getType()){
case "CYLINDER":
if((physicsGraphicsModel = Globals.assetManager.fetchModel("Models/unitcylinder.fbx")) != null){