Hitbox Sphere fixes & logic

This commit is contained in:
austin 2021-06-08 20:23:58 -04:00
parent 323a966516
commit 1f90094761
15 changed files with 148 additions and 57 deletions

View File

@ -71,6 +71,7 @@ public class EntityDataStrings {
/* /*
Collision Entity Collision Entity
*/ */
public static final String COLLISION_ENTITY_ID = "collisionEntityId";
public static final String DATA_STRING_COLLISION_ENTITY = "collisionEntity"; public static final String DATA_STRING_COLLISION_ENTITY = "collisionEntity";
public static final String DATA_STRING_COLLISION_ENTITY_TYPE_SPHERE = "collisionSphere"; public static final String DATA_STRING_COLLISION_ENTITY_TYPE_SPHERE = "collisionSphere";
@ -81,7 +82,9 @@ public class EntityDataStrings {
public static final String COLLISION_ENTITY_DATA_PARENT = "collisionDataParent"; public static final String COLLISION_ENTITY_DATA_PARENT = "collisionDataParent";
public static final String COLLISION_ENTITY_DATA_BONE = "collisionDataBone"; public static final String COLLISION_ENTITY_DATA_BONE = "collisionDataBone";
public static final String COLLISION_ENTITY_DATA_SIZE = "collisionSphereSize"; public static final String COLLISION_ENTITY_DATA_RADIUS = "collisionSphereRadius";
public static final String COLLISION_ENTITY_BEHAVIOR_TREE = "collisionEntityBehaviorTree";
/* /*

View File

@ -1,6 +1,8 @@
package electrosphere.entity.collision; package electrosphere.entity.collision;
import electrosphere.entity.Entity; import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.game.world.World; import electrosphere.game.world.World;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -82,9 +84,27 @@ public class CollisionEngine {
} }
public void registerCollidableEntity(Entity collidable){
collisionEntities.add(collidable);
}
public List<Entity> getCollisionEntities(){ public List<Entity> getCollisionEntities(){
return collisionEntities; return collisionEntities;
} }
public boolean collisionSphereCheck(Entity hitbox1, Entity hitbox2){
Vector3f position1 = EntityUtils.getEntityPosition(hitbox1);
Vector3f position2 = EntityUtils.getEntityPosition(hitbox2);
float radius1 = (float)hitbox1.getData(EntityDataStrings.COLLISION_ENTITY_DATA_RADIUS);
float radius2 = (float)hitbox2.getData(EntityDataStrings.COLLISION_ENTITY_DATA_RADIUS);
float distance = position1.distance(position2);
if(distance < radius1 + radius2){
return true;
} else {
return false;
}
}
} }

View File

@ -57,21 +57,6 @@ public class MovementTree {
state = MovementTreeState.SLOWDOWN; state = MovementTreeState.SLOWDOWN;
} }
public void transitionState(){
switch(state){
case STARTUP:
//transition if velocity >= acceleration
state = MovementTreeState.MOVE;
break;
case MOVE:
state = MovementTreeState.SLOWDOWN;
break;
case SLOWDOWN:
state = MovementTreeState.IDLE;
break;
}
}
public void simulate(){ public void simulate(){
float velocity = CreatureUtils.getVelocity(parent); float velocity = CreatureUtils.getVelocity(parent);
float acceleration = CreatureUtils.getAcceleration(parent); float acceleration = CreatureUtils.getAcceleration(parent);

View File

@ -44,7 +44,11 @@ public class CreatureUtils {
CreatureType rawType = Globals.entityTypeMap.get(creatureId); CreatureType rawType = Globals.entityTypeMap.get(creatureId);
Entity rVal = EntityUtils.spawnDrawableEntity(rawType.getModelPath()); Entity rVal = EntityUtils.spawnDrawableEntity(rawType.getModelPath());
for(HitboxData hitboxdata : rawType.getHitboxes()){ for(HitboxData hitboxdata : rawType.getHitboxes()){
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHitbox(rVal, hitboxdata.getBone(), hitboxdata.getSize())); if(hitboxdata.getType().equals("hit")){
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHitbox(rVal, hitboxdata.getBone(), hitboxdata.getRadius()));
} else if(hitboxdata.getType().equals("hurt")){
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHurtbox(rVal, hitboxdata.getBone(), hitboxdata.getRadius()));
}
} }
rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_IS_CREATURE, true); rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_IS_CREATURE, true);
rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_TYPE, creatureId); rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_TYPE, creatureId);

View File

@ -7,7 +7,7 @@ package electrosphere.entity.types.creature.creaturemap;
public class HitboxData { public class HitboxData {
String type; String type;
String bone; String bone;
float size; float radius;
public String getType() { public String getType() {
return type; return type;
@ -17,8 +17,8 @@ public class HitboxData {
return bone; return bone;
} }
public float getSize() { public float getRadius() {
return size; return radius;
} }

View File

@ -1,8 +1,10 @@
package electrosphere.entity.types.hitbox; package electrosphere.entity.types.hitbox;
import electrosphere.entity.Entity; import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/** /**
* *
@ -10,7 +12,8 @@ import java.util.List;
*/ */
public class HitboxManager { public class HitboxManager {
List<Entity> hitboxes = new ArrayList(); CopyOnWriteArrayList<Entity> hitboxes = new CopyOnWriteArrayList();
long idIncrementer = 0;
public HitboxManager(){ public HitboxManager(){
@ -18,9 +21,11 @@ public class HitboxManager {
public void registerHitbox(Entity hitbox){ public void registerHitbox(Entity hitbox){
hitboxes.add(hitbox); hitboxes.add(hitbox);
idIncrementer++;
hitbox.putData(EntityDataStrings.COLLISION_ENTITY_ID, idIncrementer);
} }
public List<Entity> getAllHitboxes(){ public CopyOnWriteArrayList<Entity> getAllHitboxes(){
return hitboxes; return hitboxes;
} }
} }

View File

@ -3,6 +3,7 @@ package electrosphere.entity.types.hitbox;
import electrosphere.entity.Entity; import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils; import electrosphere.entity.EntityUtils;
import electrosphere.main.Globals;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import org.joml.Quaternionf; import org.joml.Quaternionf;
import org.joml.Vector3f; import org.joml.Vector3f;
@ -19,8 +20,10 @@ public class HitboxUtils {
Entity rVal = new Entity(); Entity rVal = new Entity();
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT, parent); rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT, parent);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_BONE, bone); rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_BONE, bone);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_SIZE,size); rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_RADIUS,size);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE,EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HIT);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3f(0,0,0)); rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3f(0,0,0));
Globals.hitboxManager.registerHitbox(rVal);
return rVal; return rVal;
} }
@ -28,8 +31,10 @@ public class HitboxUtils {
Entity rVal = new Entity(); Entity rVal = new Entity();
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT, parent); rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT, parent);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_BONE, bone); rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_BONE, bone);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_SIZE,size); rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_RADIUS,size);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE,EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3f(0,0,0)); rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3f(0,0,0));
Globals.hitboxManager.registerHitbox(rVal);
return rVal; return rVal;
} }
@ -67,4 +72,30 @@ public class HitboxUtils {
((Vector3f)hitbox.getData(EntityDataStrings.DATA_STRING_POSITION)).set(worldPosition); ((Vector3f)hitbox.getData(EntityDataStrings.DATA_STRING_POSITION)).set(worldPosition);
} }
public static void collideEntities(Entity generatorHitbox){
// long generatorId = (Long)generatorHitbox.getData(EntityDataStrings.COLLISION_ENTITY_ID);
Entity generatorParent = (Entity)generatorHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT);
for(Entity receiverHitbox : Globals.hitboxManager.getAllHitboxes()){
Entity receiverParent = (Entity)receiverHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT);
// long targetId = (Long)receiverHitbox.getData(EntityDataStrings.COLLISION_ENTITY_ID);
if(receiverParent != generatorParent && Globals.collisionEngine.collisionSphereCheck(generatorHitbox, receiverHitbox)){
String generatorType = (String)generatorHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE);
String receiverType = (String)receiverHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE);
if(
(generatorType.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HIT) || generatorType.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT)) &&
(receiverType.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HIT) || receiverType.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT)) &&
generatorType != receiverType
){
if(generatorType.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT)){
EntityUtils.getEntityPosition(generatorParent).set(Globals.spawnPoint);
} else if(receiverParent.equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT)){
EntityUtils.getEntityPosition(receiverParent).set(Globals.spawnPoint);
}
}
}
}
}
} }

View File

@ -22,7 +22,7 @@ public class ItemUtils {
CreatureType rawType = Globals.entityTypeMap.get(itemId); CreatureType rawType = Globals.entityTypeMap.get(itemId);
Entity rVal = EntityUtils.spawnDrawableEntity(rawType.getModelPath()); Entity rVal = EntityUtils.spawnDrawableEntity(rawType.getModelPath());
for(HitboxData hitboxdata : rawType.getHitboxes()){ for(HitboxData hitboxdata : rawType.getHitboxes()){
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHitbox(rVal, hitboxdata.getBone(), hitboxdata.getSize())); Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHitbox(rVal, hitboxdata.getBone(), hitboxdata.getRadius()));
} }
rVal.putData(EntityDataStrings.ITEM_IS_ITEM, true); rVal.putData(EntityDataStrings.ITEM_IS_ITEM, true);
rVal.putData(EntityDataStrings.ITEM_TYPE, itemId); rVal.putData(EntityDataStrings.ITEM_TYPE, itemId);

View File

@ -254,8 +254,10 @@ public class Globals {
defaultMeshShader = ShaderProgram.smart_assemble_shader(false,true); defaultMeshShader = ShaderProgram.smart_assemble_shader(false,true);
//init skybox //init skybox
assetManager.registerModelToSpecificString(RenderUtils.createSkyboxModel(null), AssetDataStrings.ASSET_STRING_SKYBOX_BASIC); assetManager.registerModelToSpecificString(RenderUtils.createSkyboxModel(null), AssetDataStrings.ASSET_STRING_SKYBOX_BASIC);
//init hitbox //init hurtbox
assetManager.addModelPathToQueue("Models/unitsphere.fbx"); assetManager.addModelPathToQueue("Models/unitsphere.fbx");
//init hitbox
assetManager.addModelPathToQueue("Models/unitsphere_1.fbx");
//init smallcube //init smallcube
assetManager.addModelPathToQueue("Models/SmallCube.fbx"); assetManager.addModelPathToQueue("Models/SmallCube.fbx");
} }

View File

@ -216,16 +216,21 @@ public class Main {
for(Entity item : Globals.entityManager.getItemEntities()){ for(Entity item : Globals.entityManager.getItemEntities()){
ItemUtils.updateItemActorAnimation(item); ItemUtils.updateItemActorAnimation(item);
} }
//update attached entity positions
AttachUtils.updateAttachedEntityPositions();
//simulate creature behavior trees //simulate creature behavior trees
for(Entity currentMoveable : Globals.entityManager.getMoveable()){ for(Entity currentMoveable : Globals.entityManager.getMoveable()){
MovementTree behaviorTree = CreatureUtils.getEntityMovementTree(currentMoveable); MovementTree behaviorTree = CreatureUtils.getEntityMovementTree(currentMoveable);
behaviorTree.simulate(); behaviorTree.simulate();
} }
//update attached entity positions
AttachUtils.updateAttachedEntityPositions();
//update hitbox positions
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){ for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
HitboxUtils.updatePosition(currentHitbox); HitboxUtils.updatePosition(currentHitbox);
} }
//collide hitboxes
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
HitboxUtils.collideEntities(currentHitbox);
}
} }

View File

@ -672,18 +672,32 @@ public class RenderUtils {
// currentModel.draw(); // currentModel.draw();
// } // }
} }
boolean renderHitboxes = false;
if(renderHitboxes){
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){ for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
Model hitboxModel; Model hitboxModel;
if(currentHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE).equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HURT)){
if((hitboxModel = Globals.assetManager.fetchModel("Models/unitsphere.fbx")) != null){ if((hitboxModel = Globals.assetManager.fetchModel("Models/unitsphere.fbx")) != null){
Vector3f position = EntityUtils.getEntityPosition(currentHitbox); Vector3f position = EntityUtils.getEntityPosition(currentHitbox);
modelTransformMatrix.identity(); modelTransformMatrix.identity();
modelTransformMatrix.translate(new Vector3f(position).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)).sub(new Vector3f(0,1,0))); modelTransformMatrix.translate(new Vector3f(position).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)).sub(new Vector3f(0,1,0)));
// modelTransformMatrix.translate(-0.25f, 0.0f, 0.5f); //center sphere // modelTransformMatrix.translate(-0.25f, 0.0f, 0.5f); //center sphere
modelTransformMatrix.scale((Float)currentHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_SIZE)); modelTransformMatrix.scale((Float)currentHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_RADIUS) * 2);
hitboxModel.modelMatrix = modelTransformMatrix; hitboxModel.modelMatrix = modelTransformMatrix;
hitboxModel.draw(); hitboxModel.draw();
} }
} else if(currentHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE).equals(EntityDataStrings.COLLISION_ENTITY_DATA_TYPE_HIT)){
if((hitboxModel = Globals.assetManager.fetchModel("Models/unitsphere_1.fbx")) != null){
Vector3f position = EntityUtils.getEntityPosition(currentHitbox);
modelTransformMatrix.identity();
modelTransformMatrix.translate(new Vector3f(position).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)).sub(new Vector3f(0,1,0)));
// modelTransformMatrix.translate(-0.25f, 0.0f, 0.5f); //center sphere
modelTransformMatrix.scale((Float)currentHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_RADIUS) * 2);
hitboxModel.modelMatrix = modelTransformMatrix;
hitboxModel.draw();
}
}
}
} }

View File

@ -5,36 +5,49 @@
"modelPath" : "Models/person1walkanim.fbx", "modelPath" : "Models/person1walkanim.fbx",
"hitboxes" : [ "hitboxes" : [
{ {
"type": "hurt",
"bone": "Bone.031", "bone": "Bone.031",
"size": 0.08 "radius": 0.04
},
{
"bone": "Bone.017",
"size": 0.08
}, },
{ {
"type": "hurt",
"bone": "Bone.012", "bone": "Bone.012",
"size": 0.08 "radius": 0.04
}, },
{ {
"type": "hurt",
"bone": "Bone.003", "bone": "Bone.003",
"size": 0.08 "radius": 0.04
}, },
{ {
"type": "hurt",
"bone": "Bone.010", "bone": "Bone.010",
"size": 0.11 "radius": 0.06
}, },
{ {
"type": "hurt",
"bone": "Bone.001", "bone": "Bone.001",
"size": 0.11 "radius": 0.06
},
{
"bone": "Bone",
"size": 0.15
}, },
{ {
"type": "hurt",
"bone": "Bone.014", "bone": "Bone.014",
"size": 0.12 "radius": 0.06
},
{
"type": "hurt",
"bone": "Bone",
"radius": 0.08
},
{
"type": "hurt",
"bone": "Bone.014",
"radius": 0.06
},
{
"type": "hurt",
"bone": "Bone.019",
"radius": 0.04
} }
] ]
}, },
@ -48,16 +61,19 @@
"modelPath" : "Models/katana1alt.fbx", "modelPath" : "Models/katana1alt.fbx",
"hitboxes" : [ "hitboxes" : [
{ {
"type": "hit",
"bone": "Blade1", "bone": "Blade1",
"size": 0.08 "radius": 0.04
}, },
{ {
"type": "hit",
"bone": "Blade2", "bone": "Blade2",
"size": 0.08 "radius": 0.04
}, },
{ {
"type": "hit",
"bone": "Blade3", "bone": "Blade3",
"size": 0.08 "radius": 0.04
} }
] ]
} }

Binary file not shown.

View File

@ -24,7 +24,13 @@
"Textures/transparent_blue.png" "Textures/transparent_blue.png"
] ]
}, },
"Models/katana1.fbx": { "Models/unitsphere_1.fbx": {
"Sphere": [
"Textures/transparent_red.png",
"Textures/transparent_red.png"
]
},
"Models/katana1alt.fbx": {
"SwordMesh": [ "SwordMesh": [
"Textures/katana1.png", "Textures/katana1.png",
"Textures/katana1.png" "Textures/katana1.png"

Binary file not shown.

After

Width:  |  Height:  |  Size: 233 B