From 3f899ed746ddd686e6c18556a1bf7e789b867410 Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 28 Jun 2021 01:59:58 -0400 Subject: [PATCH] add gravity to movement tree --- .../java/electrosphere/entity/state/MovementTree.java | 9 ++++++--- .../electrosphere/game/collision/CollisionEngine.java | 9 +++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/electrosphere/entity/state/MovementTree.java b/src/main/java/electrosphere/entity/state/MovementTree.java index d82d9cd5..7b61038a 100644 --- a/src/main/java/electrosphere/entity/state/MovementTree.java +++ b/src/main/java/electrosphere/entity/state/MovementTree.java @@ -140,7 +140,7 @@ public class MovementTree { state = MovementTreeState.MOVE; } //move the entity - newPosition = new Vector3f(position).add(new Vector3f(movementVector).mul(velocity)); + newPosition = new Vector3f(position).add(new Vector3f(movementVector).mul(velocity)).add(0,-9.8f,0); //check/update if collision if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){ newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition); @@ -191,7 +191,7 @@ public class MovementTree { } //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(movementVector).mul(velocity)).add(0,-9.8f,0); if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){ newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition); } @@ -245,7 +245,7 @@ public class MovementTree { state = MovementTreeState.IDLE; } //move the entity - newPosition = new Vector3f(position).add(new Vector3f(movementVector).mul(velocity)); + newPosition = new Vector3f(position).add(new Vector3f(movementVector).mul(velocity)).add(0,-9.8f,0); if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){ newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition); } @@ -290,6 +290,9 @@ public class MovementTree { entityActor.incrementAnimationTime(0.01); } } + if(Globals.collisionEngine.gravityCheck(Globals.commonWorldData, parent)){ + EntityUtils.getEntityPosition(parent).set(Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData,parent,new Vector3f(position.x,position.y - 9.8f,position.z))); + } break; } } diff --git a/src/main/java/electrosphere/game/collision/CollisionEngine.java b/src/main/java/electrosphere/game/collision/CollisionEngine.java index e6874488..751b1d15 100644 --- a/src/main/java/electrosphere/game/collision/CollisionEngine.java +++ b/src/main/java/electrosphere/game/collision/CollisionEngine.java @@ -107,4 +107,13 @@ public class CollisionEngine { } } + /* + Check if the entity is being accelerated by gravity + */ + public boolean gravityCheck(CommonWorldData w, Entity e){ + float worldHeight = w.getElevationAtPoint(EntityUtils.getEntityPosition(e)); + float entityHeight = EntityUtils.getEntityPosition(e).y; + return entityHeight > worldHeight + 0.1f; + } + }