simplify physics cases

This commit is contained in:
austin 2024-11-29 12:08:52 -05:00
parent 36d2271b35
commit 524f328fae
10 changed files with 46 additions and 127 deletions

View File

@ -1168,6 +1168,7 @@ Fix particles not spawning in correct positions
Fix block not firing Fix block not firing
Fix reentrant locking bug Fix reentrant locking bug
Convert server physics cell generation to executor service Convert server physics cell generation to executor service
Simplifying physics cases
# TODO # TODO

View File

@ -133,6 +133,11 @@ public class CollisionEngine {
//buffer for collisions //buffer for collisions
DContactBuffer contacts = null; DContactBuffer contacts = null;
/**
* The body that contains all static shapes
*/
DBody staticBody;
// Callback for any near collisions in the broadphase of the collision check // Callback for any near collisions in the broadphase of the collision check
private DNearCallback nearCallback; private DNearCallback nearCallback;
@ -150,6 +155,10 @@ public class CollisionEngine {
//base plane //base plane
OdeHelper.createPlane(space, 0, 1, 0, 0); OdeHelper.createPlane(space, 0, 1, 0, 0);
//static body
staticBody = this.createDBody();
staticBody.setKinematic();
contactgroup = OdeHelper.createJointGroup(); contactgroup = OdeHelper.createJointGroup();
this.nearCallback = new DNearCallback() { this.nearCallback = new DNearCallback() {
@ -184,7 +193,7 @@ public class CollisionEngine {
switch(receiver.getType()){ switch(receiver.getType()){
case Collidable.TYPE_CREATURE: case Collidable.TYPE_CREATURE:
switch(impactor.getType()){ switch(impactor.getType()){
case Collidable.TYPE_TERRAIN: case Collidable.TYPE_STATIC:
// System.out.println(EntityUtils.getPosition(impactor.getParent()) + " " + EntityUtils.getPosition(receiver.getParent())); // System.out.println(EntityUtils.getPosition(impactor.getParent()) + " " + EntityUtils.getPosition(receiver.getParent()));
// System.out.println(); // System.out.println();
// System.out.println("Terrain-creature collision: " + normal + " mag:" + magnitude); // System.out.println("Terrain-creature collision: " + normal + " mag:" + magnitude);
@ -192,52 +201,11 @@ public class CollisionEngine {
// normal.x = 0; // normal.x = 0;
// normal.z = 0; // normal.z = 0;
// } // }
receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude * 2, Collidable.TYPE_TERRAIN)); receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude * 2, Collidable.TYPE_STATIC));
break; break;
case Collidable.TYPE_CREATURE: case Collidable.TYPE_CREATURE:
receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_CREATURE)); receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_CREATURE));
break; break;
case Collidable.TYPE_STRUCTURE:
// float realMag = 1f/(float)Math.pow(0.1, magnitude);
// System.out.println(normal + " - " + realMag);
receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_STRUCTURE));
// System.out.println("Structure-creature collision");
break;
case Collidable.TYPE_ITEM:
receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_ITEM));
break;
case Collidable.TYPE_OBJECT:
receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_OBJECT));
break;
case Collidable.TYPE_FOLIAGE_STATIC:
receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_OBJECT));
break;
}
break;
case Collidable.TYPE_ITEM:
switch(impactor.getType()){
case Collidable.TYPE_TERRAIN:
// System.out.println(EntityUtils.getPosition(impactor.getParent()) + " " + EntityUtils.getPosition(receiver.getParent()));
// System.out.println();
// System.out.println("Terrain-item collision: " + normal + " mag:" + magnitude);
// if(normal.y > normal.x + normal.z){
// normal.x = 0;
// normal.z = 0;
// }
receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude * 2, Collidable.TYPE_TERRAIN));
break;
case Collidable.TYPE_CREATURE:
receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_CREATURE));
break;
case Collidable.TYPE_STRUCTURE:
// float realMag = 1f/(float)Math.pow(0.1, magnitude);
// System.out.println(normal + " - " + realMag);
receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_STRUCTURE));
// System.out.println("Structure-creature collision");
break;
case Collidable.TYPE_ITEM:
receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_ITEM));
break;
case Collidable.TYPE_OBJECT: case Collidable.TYPE_OBJECT:
receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_OBJECT)); receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_OBJECT));
break; break;
@ -363,20 +331,19 @@ public class CollisionEngine {
if( if(
!( !(
bodyPointerMap.get(b1).getType() == Collidable.TYPE_TERRAIN && bodyPointerMap.get(b1).getType() == Collidable.TYPE_STATIC &&
( (
bodyPointerMap.get(b2).getType() == Collidable.TYPE_TERRAIN || bodyPointerMap.get(b2).getType() == Collidable.TYPE_STATIC ||
!b2.isEnabled() !b2.isEnabled()
) )
) && ) &&
!( !(
bodyPointerMap.get(b2).getType() == Collidable.TYPE_TERRAIN && bodyPointerMap.get(b2).getType() == Collidable.TYPE_STATIC &&
( (
bodyPointerMap.get(b1).getType() == Collidable.TYPE_TERRAIN || bodyPointerMap.get(b1).getType() == Collidable.TYPE_STATIC ||
!b1.isEnabled() !b1.isEnabled()
) )
) && )
!(bodyPointerMap.get(b1).getType() == Collidable.TYPE_FOLIAGE_STATIC && bodyPointerMap.get(b2).getType() == Collidable.TYPE_FOLIAGE_STATIC)
){ ){
try { try {
//creates a buffer to store potential collisions //creates a buffer to store potential collisions

View File

@ -489,7 +489,7 @@ public class PhysicsEntityUtils {
*/ */
public static void clientAttachTriGeomRigidBody(Entity terrain, TriGeomData data){ public static void clientAttachTriGeomRigidBody(Entity terrain, TriGeomData data){
DBody terrainBody = CollisionBodyCreation.generateBodyFromTerrainData(Globals.clientSceneWrapper.getCollisionEngine(), data, Collidable.TYPE_STATIC_BIT); DBody terrainBody = CollisionBodyCreation.generateBodyFromTerrainData(Globals.clientSceneWrapper.getCollisionEngine(), data, Collidable.TYPE_STATIC_BIT);
Collidable collidable = new Collidable(terrain,Collidable.TYPE_TERRAIN, false); Collidable collidable = new Collidable(terrain,Collidable.TYPE_STATIC, false);
Globals.clientSceneWrapper.getCollisionEngine().registerCollisionObject(terrainBody, collidable); Globals.clientSceneWrapper.getCollisionEngine().registerCollisionObject(terrainBody, collidable);
PhysicsEntityUtils.setDBody(terrain,terrainBody); PhysicsEntityUtils.setDBody(terrain,terrainBody);
terrain.putData(EntityDataStrings.PHYSICS_COLLIDABLE, collidable); terrain.putData(EntityDataStrings.PHYSICS_COLLIDABLE, collidable);
@ -506,7 +506,7 @@ public class PhysicsEntityUtils {
Realm terrainRealm = Globals.realmManager.getEntityRealm(terrain); Realm terrainRealm = Globals.realmManager.getEntityRealm(terrain);
DBody terrainBody = CollisionBodyCreation.generateBodyFromTerrainData(terrainRealm.getCollisionEngine(),data,Collidable.TYPE_STATIC_BIT); DBody terrainBody = CollisionBodyCreation.generateBodyFromTerrainData(terrainRealm.getCollisionEngine(),data,Collidable.TYPE_STATIC_BIT);
terrainRealm.getCollisionEngine().registerCollisionObject(terrainBody, new Collidable(terrain,Collidable.TYPE_TERRAIN, false)); terrainRealm.getCollisionEngine().registerCollisionObject(terrainBody, new Collidable(terrain,Collidable.TYPE_STATIC, false));
PhysicsEntityUtils.setDBody(terrain,terrainBody); PhysicsEntityUtils.setDBody(terrain,terrainBody);
return terrainBody; return terrainBody;

View File

@ -33,27 +33,12 @@ public class Collidable {
public static final String TYPE_STATIC = "static"; public static final String TYPE_STATIC = "static";
public static final long TYPE_STATIC_BIT = 0x1; public static final long TYPE_STATIC_BIT = 0x1;
public static final String TYPE_TERRAIN = "terrain";
public static final long TYPE_TERRAIN_BIT = 0x2;
public static final String TYPE_CREATURE = "creature"; public static final String TYPE_CREATURE = "creature";
public static final long TYPE_CREATURE_BIT = 0x4; public static final long TYPE_CREATURE_BIT = 0x4;
public static final String TYPE_STRUCTURE = "structure";
public static final long TYPE_STRUCTURE_BIT = 0x8;
public static final String TYPE_ITEM = "item";
public static final long TYPE_ITEM_BIT = 0x10;
public static final String TYPE_FORCE = "force";
public static final long TYPE_FORCE_BIT = 0x20;
public static final String TYPE_OBJECT = "object"; public static final String TYPE_OBJECT = "object";
public static final long TYPE_OBJECT_BIT = 0x40; public static final long TYPE_OBJECT_BIT = 0x40;
public static final String TYPE_FOLIAGE_STATIC = "foliageStatic";
public static final long TYPE_FOLIAGE_BIT = 0x80;
public static final String TYPE_WORLD_BOUND = "worldBound"; public static final String TYPE_WORLD_BOUND = "worldBound";
public static final long TYPE_WORLD_BOUND_BIT = 0x100; public static final long TYPE_WORLD_BOUND_BIT = 0x100;
@ -63,11 +48,7 @@ public class Collidable {
public static final List<String> MASK_NO_TERRAIN = Arrays.asList(new String[]{ public static final List<String> MASK_NO_TERRAIN = Arrays.asList(new String[]{
TYPE_STATIC, TYPE_STATIC,
TYPE_CREATURE, TYPE_CREATURE,
TYPE_STRUCTURE,
TYPE_ITEM,
TYPE_FORCE,
TYPE_OBJECT, TYPE_OBJECT,
TYPE_FOLIAGE_STATIC,
TYPE_WORLD_BOUND, TYPE_WORLD_BOUND,
}); });

View File

@ -48,11 +48,6 @@ public class ServerCollidableTree implements BehaviorTree {
//have we hit a terrain impulse? //have we hit a terrain impulse?
//handle impulses //handle impulses
for(Impulse impulse : collidable.getImpulses()){ for(Impulse impulse : collidable.getImpulses()){
if(impulse.type.matches(Collidable.TYPE_ITEM)){
if(ServerGravityTree.getServerGravityTree(parent)!=null){
ServerGravityTree.getServerGravityTree(parent).start();
}
}
if(impulse.type.matches(Collidable.TYPE_CREATURE)){ if(impulse.type.matches(Collidable.TYPE_CREATURE)){
if(ServerGravityTree.getServerGravityTree(parent)!=null){ if(ServerGravityTree.getServerGravityTree(parent)!=null){
ServerGravityTree.getServerGravityTree(parent).start(); ServerGravityTree.getServerGravityTree(parent).start();

View File

@ -1,7 +1,6 @@
package electrosphere.entity.state.gravity; package electrosphere.entity.state.gravity;
import org.joml.Vector3d;
import org.ode4j.ode.DBody; import org.ode4j.ode.DBody;
import electrosphere.collision.collidable.Collidable; import electrosphere.collision.collidable.Collidable;
@ -95,8 +94,6 @@ public class ClientGravityTree implements BehaviorTree {
switch(state){ switch(state){
case ACTIVE: case ACTIVE:
if(hadGroundCollision()){ if(hadGroundCollision()){
if(!hadStructureCollision()){
}
ClientJumpTree jumpTree; ClientJumpTree jumpTree;
if((jumpTree = ClientJumpTree.getClientJumpTree(parent))!=null){ if((jumpTree = ClientJumpTree.getClientJumpTree(parent))!=null){
jumpTree.land(); jumpTree.land();
@ -124,29 +121,13 @@ public class ClientGravityTree implements BehaviorTree {
break; break;
} }
} }
public boolean hadStructureCollision(){
boolean rVal = false;
for(Impulse impulse : collidable.getImpulses()){
if(impulse.getType().equals(Collidable.TYPE_STRUCTURE)){
rVal = true;
break;
}
}
return rVal;
}
public boolean hadGroundCollision(){ public boolean hadGroundCollision(){
boolean rVal = false; boolean rVal = false;
for(Impulse impulse : collidable.getImpulses()){ for(Impulse impulse : collidable.getImpulses()){
if(impulse.getType().equals(Collidable.TYPE_TERRAIN)){ if(impulse.getType().equals(Collidable.TYPE_STATIC)){
rVal = true; rVal = true;
break; break;
} else if(
impulse.getType().equals(Collidable.TYPE_STRUCTURE) &&
new Vector3d(impulse.getDirection()).normalize().y > 0.7
){
rVal = true;
} }
} }
return rVal; return rVal;

View File

@ -6,7 +6,6 @@ import electrosphere.net.parser.net.message.SynchronizationMessage;
import electrosphere.server.datacell.utils.DataCellSearchUtils; import electrosphere.server.datacell.utils.DataCellSearchUtils;
import electrosphere.server.datacell.utils.ServerBehaviorTreeUtils; import electrosphere.server.datacell.utils.ServerBehaviorTreeUtils;
import org.joml.Vector3d;
import org.ode4j.ode.DBody; import org.ode4j.ode.DBody;
import electrosphere.collision.collidable.Collidable; import electrosphere.collision.collidable.Collidable;
@ -129,21 +128,6 @@ public class ServerGravityTree implements BehaviorTree {
break; break;
} }
} }
/**
* Checks if the gravity tree had a collision with a structure
* @return true if collided on the most recent frame, false otherwise
*/
public boolean hadStructureCollision(){
boolean rVal = false;
for(Impulse impulse : collidable.getImpulses()){
if(impulse.getType().equals(Collidable.TYPE_STRUCTURE)){
rVal = true;
break;
}
}
return rVal;
}
/** /**
* Checks if the gravity tree had a collision with terrain * Checks if the gravity tree had a collision with terrain
@ -152,14 +136,9 @@ public class ServerGravityTree implements BehaviorTree {
public boolean hadGroundCollision(){ public boolean hadGroundCollision(){
boolean rVal = false; boolean rVal = false;
for(Impulse impulse : collidable.getImpulses()){ for(Impulse impulse : collidable.getImpulses()){
if(impulse.getType().equals(Collidable.TYPE_TERRAIN)){ if(impulse.getType().equals(Collidable.TYPE_STATIC)){
rVal = true; rVal = true;
break; break;
} else if(
impulse.getType().equals(Collidable.TYPE_STRUCTURE) &&
new Vector3d(impulse.getDirection()).normalize().y > 0.7
){
rVal = true;
} }
} }
return rVal; return rVal;

View File

@ -133,7 +133,7 @@ public class CommonEntityUtils {
DBody collisionObject = Globals.assetManager.fetchCollisionObject(Globals.clientSceneWrapper.getCollisionEngine(),rawType.getGraphicsTemplate().getModel().getPath()); DBody collisionObject = Globals.assetManager.fetchCollisionObject(Globals.clientSceneWrapper.getCollisionEngine(),rawType.getGraphicsTemplate().getModel().getPath());
if(collisionObject != null){ if(collisionObject != null){
Globals.clientSceneWrapper.getScene().deregisterBehaviorTree(this); Globals.clientSceneWrapper.getScene().deregisterBehaviorTree(this);
CollisionObjUtils.clientAttachCollisionObjectToEntity(entity, collisionObject, 0, Collidable.TYPE_TERRAIN); CollisionObjUtils.clientAttachCollisionObjectToEntity(entity, collisionObject, 0, Collidable.TYPE_STATIC);
} }
}}); }});
} break; } break;
@ -352,9 +352,6 @@ public class CommonEntityUtils {
ClientAlwaysUprightTree.attachTree(entity); ClientAlwaysUprightTree.attachTree(entity);
CreatureUtils.setFacingVector(entity, SpatialMathUtils.getOriginVector()); CreatureUtils.setFacingVector(entity, SpatialMathUtils.getOriginVector());
} break; } break;
case "TERRAIN_COLLISION": {
CollisionObjUtils.getCollidable(entity).overrideType(Collidable.TYPE_TERRAIN);
} break;
case "BLENDER_ROTATION": { case "BLENDER_ROTATION": {
ActorUtils.applyBlenderRotation(entity); ActorUtils.applyBlenderRotation(entity);
} break; } break;
@ -441,7 +438,7 @@ public class CommonEntityUtils {
DBody collisionObject = Globals.assetManager.fetchCollisionObject(realm.getCollisionEngine(),rawType.getGraphicsTemplate().getModel().getPath()); DBody collisionObject = Globals.assetManager.fetchCollisionObject(realm.getCollisionEngine(),rawType.getGraphicsTemplate().getModel().getPath());
if(collisionObject != null){ if(collisionObject != null){
ServerBehaviorTreeUtils.detatchBTreeFromEntity(entity, this); ServerBehaviorTreeUtils.detatchBTreeFromEntity(entity, this);
CollisionObjUtils.serverAttachCollisionObjectToEntity(entity, collisionObject, 0, Collidable.TYPE_TERRAIN); CollisionObjUtils.serverAttachCollisionObjectToEntity(entity, collisionObject, 0, Collidable.TYPE_STATIC);
} }
}}); }});
} break; } break;
@ -656,9 +653,6 @@ public class CommonEntityUtils {
ServerAlwaysUprightTree.attachTree(entity); ServerAlwaysUprightTree.attachTree(entity);
CreatureUtils.setFacingVector(entity, SpatialMathUtils.getOriginVector()); CreatureUtils.setFacingVector(entity, SpatialMathUtils.getOriginVector());
} break; } break;
case "TERRAIN_COLLISION": {
CollisionObjUtils.getCollidable(entity).overrideType(Collidable.TYPE_TERRAIN);
} break;
case "BLENDER_ROTATION": { case "BLENDER_ROTATION": {
ActorUtils.applyBlenderRotation(entity); ActorUtils.applyBlenderRotation(entity);
} break; } break;

View File

@ -134,7 +134,7 @@ public class ProceduralTree {
); );
CollisionBodyCreation.setOffsetPosition(Globals.clientSceneWrapper.getCollisionEngine(), rigidBody, new Vector3d(0,treeModel.getPhysicsBody().getOffsetY(),0)); CollisionBodyCreation.setOffsetPosition(Globals.clientSceneWrapper.getCollisionEngine(), rigidBody, new Vector3d(0,treeModel.getPhysicsBody().getOffsetY(),0));
CollisionBodyCreation.setKinematic(Globals.clientSceneWrapper.getCollisionEngine(), rigidBody); CollisionBodyCreation.setKinematic(Globals.clientSceneWrapper.getCollisionEngine(), rigidBody);
Collidable collidable = new Collidable(trunkChild, Collidable.TYPE_FOLIAGE_STATIC, true); Collidable collidable = new Collidable(trunkChild, Collidable.TYPE_STATIC, true);
PhysicsEntityUtils.setDBody(trunkChild, rigidBody); PhysicsEntityUtils.setDBody(trunkChild, rigidBody);
Matrix4d offsetTransform = new Matrix4d().translationRotateScale( Matrix4d offsetTransform = new Matrix4d().translationRotateScale(
0,treeModel.getPhysicsBody().getOffsetY(),0, //translate 0,treeModel.getPhysicsBody().getOffsetY(),0, //translate
@ -539,7 +539,7 @@ public class ProceduralTree {
); );
CollisionBodyCreation.setOffsetPosition(realm.getCollisionEngine(), rigidBody, new Vector3d(0,treeModel.getPhysicsBody().getOffsetY(),0)); CollisionBodyCreation.setOffsetPosition(realm.getCollisionEngine(), rigidBody, new Vector3d(0,treeModel.getPhysicsBody().getOffsetY(),0));
CollisionBodyCreation.setKinematic(realm.getCollisionEngine(), rigidBody); CollisionBodyCreation.setKinematic(realm.getCollisionEngine(), rigidBody);
Collidable collidable = new Collidable(trunkChild, Collidable.TYPE_FOLIAGE_STATIC, true); Collidable collidable = new Collidable(trunkChild, Collidable.TYPE_STATIC, true);
PhysicsEntityUtils.setDBody(trunkChild, rigidBody); PhysicsEntityUtils.setDBody(trunkChild, rigidBody);
Matrix4d offsetTransform = new Matrix4d().translationRotateScale( Matrix4d offsetTransform = new Matrix4d().translationRotateScale(
0,treeModel.getPhysicsBody().getOffsetY(),0, //translate 0,treeModel.getPhysicsBody().getOffsetY(),0, //translate

View File

@ -85,6 +85,11 @@ public class CollidableTemplate {
*/ */
boolean kinematic; boolean kinematic;
/**
* The type of body (ie creature, static, foliage, etc)
*/
String collisionType;
/** /**
* The primitive shape type * The primitive shape type
* @return The primitive shape * @return The primitive shape
@ -269,6 +274,22 @@ public class CollidableTemplate {
this.kinematic = kinematic; this.kinematic = kinematic;
} }
/**
* Gets the type of body (ie creature, static, foliage, etc)
* @return The type of body (ie creature, static, foliage, etc)
*/
public String getCollisionType() {
return collisionType;
}
/**
* Sets the type of body (ie creature, static, foliage, etc)
* @param collisionType The type of body (ie creature, static, foliage, etc)
*/
public void setCollisionType(String collisionType) {
this.collisionType = collisionType;
}