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
*/
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_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_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;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.game.world.World;
import java.util.ArrayList;
import java.util.List;
@ -82,9 +84,27 @@ public class CollisionEngine {
}
public void registerCollidableEntity(Entity collidable){
collisionEntities.add(collidable);
}
public List<Entity> getCollisionEntities(){
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;
}
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(){
float velocity = CreatureUtils.getVelocity(parent);
float acceleration = CreatureUtils.getAcceleration(parent);

View File

@ -44,7 +44,11 @@ public class CreatureUtils {
CreatureType rawType = Globals.entityTypeMap.get(creatureId);
Entity rVal = EntityUtils.spawnDrawableEntity(rawType.getModelPath());
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_TYPE, creatureId);

View File

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

View File

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

View File

@ -3,6 +3,7 @@ package electrosphere.entity.types.hitbox;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.main.Globals;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;
@ -19,8 +20,10 @@ public class HitboxUtils {
Entity rVal = new Entity();
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT, parent);
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));
Globals.hitboxManager.registerHitbox(rVal);
return rVal;
}
@ -28,8 +31,10 @@ public class HitboxUtils {
Entity rVal = new Entity();
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT, parent);
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));
Globals.hitboxManager.registerHitbox(rVal);
return rVal;
}
@ -67,4 +72,30 @@ public class HitboxUtils {
((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);
Entity rVal = EntityUtils.spawnDrawableEntity(rawType.getModelPath());
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_TYPE, itemId);

View File

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

View File

@ -216,16 +216,21 @@ public class Main {
for(Entity item : Globals.entityManager.getItemEntities()){
ItemUtils.updateItemActorAnimation(item);
}
//update attached entity positions
AttachUtils.updateAttachedEntityPositions();
//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);
}
}

View File

@ -672,17 +672,31 @@ public class RenderUtils {
// currentModel.draw();
// }
}
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
Model hitboxModel;
if((hitboxModel = Globals.assetManager.fetchModel("Models/unitsphere.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_SIZE));
hitboxModel.modelMatrix = modelTransformMatrix;
hitboxModel.draw();
boolean renderHitboxes = false;
if(renderHitboxes){
for(Entity currentHitbox : Globals.hitboxManager.getAllHitboxes()){
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){
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();
}
} 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",
"hitboxes" : [
{
"type": "hurt",
"bone": "Bone.031",
"size": 0.08
},
{
"bone": "Bone.017",
"size": 0.08
"radius": 0.04
},
{
"type": "hurt",
"bone": "Bone.012",
"size": 0.08
"radius": 0.04
},
{
"type": "hurt",
"bone": "Bone.003",
"size": 0.08
"radius": 0.04
},
{
"type": "hurt",
"bone": "Bone.010",
"size": 0.11
"radius": 0.06
},
{
"type": "hurt",
"bone": "Bone.001",
"size": 0.11
},
{
"bone": "Bone",
"size": 0.15
"radius": 0.06
},
{
"type": "hurt",
"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",
"hitboxes" : [
{
"type": "hit",
"bone": "Blade1",
"size": 0.08
"radius": 0.04
},
{
"type": "hit",
"bone": "Blade2",
"size": 0.08
"radius": 0.04
},
{
"type": "hit",
"bone": "Blade3",
"size": 0.08
"radius": 0.04
}
]
}

Binary file not shown.

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 233 B