From 272a1268a05cfc26fd71398bcd4239dfdd1b3214 Mon Sep 17 00:00:00 2001 From: austin Date: Sat, 24 May 2025 22:01:29 -0400 Subject: [PATCH] physics spawning LOD --- docs/src/progress/renderertodo.md | 1 + .../groundmove/ServerGroundMovementTree.java | 4 +++ .../types/common/CommonEntityUtils.java | 4 ++- .../server/MainServerFunctions.java | 1 + .../character/PlayerCharacterCreation.java | 1 + .../server/service/LODEmitterService.java | 31 +++++++++++++++++-- 6 files changed, 39 insertions(+), 3 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index ca9d0584..19419826 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1971,6 +1971,7 @@ Performance improvements - Multiple visual LOD levels - AI does not simulate for low-lod server entities - Nearby entity lookup caching per frame + - Far-away entities do not spawn physics by default Lod emitter service checker function diff --git a/src/main/java/electrosphere/entity/state/movement/groundmove/ServerGroundMovementTree.java b/src/main/java/electrosphere/entity/state/movement/groundmove/ServerGroundMovementTree.java index 0029543f..26dbb4b8 100644 --- a/src/main/java/electrosphere/entity/state/movement/groundmove/ServerGroundMovementTree.java +++ b/src/main/java/electrosphere/entity/state/movement/groundmove/ServerGroundMovementTree.java @@ -181,6 +181,10 @@ public class ServerGroundMovementTree implements BehaviorTree { } } DBody body = PhysicsEntityUtils.getDBody(parent); + //TODO: eventually handle non-rigid-body entities + if(body == null){ + return; + } DVector3C linearVelocity = body.getLinearVel(); // diff --git a/src/main/java/electrosphere/entity/types/common/CommonEntityUtils.java b/src/main/java/electrosphere/entity/types/common/CommonEntityUtils.java index f0efdbac..e3aa19f6 100644 --- a/src/main/java/electrosphere/entity/types/common/CommonEntityUtils.java +++ b/src/main/java/electrosphere/entity/types/common/CommonEntityUtils.java @@ -512,7 +512,9 @@ public class CommonEntityUtils { // if(rawType.getCollidable() != null){ CollidableTemplate physicsTemplate = rawType.getCollidable(); - PhysicsEntityUtils.serverAttachCollidableTemplate(realm, entity, physicsTemplate); + if(Globals.serverState.lodEmitterService.isFullLod(position)){ + PhysicsEntityUtils.serverAttachCollidableTemplate(realm, entity, physicsTemplate); + } ServerLODComponent.attachTree(entity); } // diff --git a/src/main/java/electrosphere/server/MainServerFunctions.java b/src/main/java/electrosphere/server/MainServerFunctions.java index 226c8866..ccb0fc81 100644 --- a/src/main/java/electrosphere/server/MainServerFunctions.java +++ b/src/main/java/electrosphere/server/MainServerFunctions.java @@ -64,6 +64,7 @@ public class MainServerFunctions { private static void simulateServices(){ Globals.profiler.beginCpuSample("MainServerFunctions.simulateServices"); Globals.serverState.structureScanningService.simulate(); + Globals.serverState.lodEmitterService.simulate(); Globals.profiler.endCpuSample(); } diff --git a/src/main/java/electrosphere/server/macro/character/PlayerCharacterCreation.java b/src/main/java/electrosphere/server/macro/character/PlayerCharacterCreation.java index 0894531f..a083312b 100644 --- a/src/main/java/electrosphere/server/macro/character/PlayerCharacterCreation.java +++ b/src/main/java/electrosphere/server/macro/character/PlayerCharacterCreation.java @@ -42,6 +42,7 @@ public class PlayerCharacterCreation { // //spawn entity in world Vector3d spawnPoint = PlayerCharacterCreation.solveSpawnPoint(realm, connectionHandler); + Globals.serverState.lodEmitterService.addTempVec(spawnPoint); Entity newPlayerEntity = CreatureUtils.serverSpawnBasicCreature(realm,new Vector3d(spawnPoint.x,spawnPoint.y,spawnPoint.z),raceName,template); // diff --git a/src/main/java/electrosphere/server/service/LODEmitterService.java b/src/main/java/electrosphere/server/service/LODEmitterService.java index 6d21f9d7..a931bf10 100644 --- a/src/main/java/electrosphere/server/service/LODEmitterService.java +++ b/src/main/java/electrosphere/server/service/LODEmitterService.java @@ -7,7 +7,6 @@ import java.util.concurrent.locks.ReentrantLock; import org.joml.Vector3d; -import electrosphere.engine.Globals; import electrosphere.engine.signal.Signal.SignalType; import electrosphere.engine.signal.SignalServiceImpl; import electrosphere.entity.Entity; @@ -24,6 +23,13 @@ public class LODEmitterService extends SignalServiceImpl { */ private List emitters = new LinkedList(); + /** + * List of temporary vecs for emitter checking - are cleared every frame + *

+ * Intention with this is that it can be used to guarantee a player character has physics generated + */ + private List tempVecs = new LinkedList(); + /** * Lock for thread-safeing the service */ @@ -68,19 +74,40 @@ public class LODEmitterService extends SignalServiceImpl { lock.unlock(); } + /** + * Clears the temp vecs + */ + public void simulate(){ + this.tempVecs.clear(); + } + + /** + * Adds a temporary vector + * @param tempVec The temporary vector + */ + public void addTempVec(Vector3d tempVec){ + this.tempVecs.add(tempVec); + } + /** * Checks if a given position would be full LOD * @param position The position * @return true if it is full lod, false otherwise */ public boolean isFullLod(Vector3d position){ - for(Entity emitter : Globals.serverState.lodEmitterService.getEmitters()){ + for(Entity emitter : this.getEmitters()){ Vector3d emitterLoc = EntityUtils.getPosition(emitter); double dist = position.distance(emitterLoc); if(dist < ServerLODComponent.FULL_RES){ return true; } } + for(Vector3d tempVec : this.tempVecs){ + double dist = position.distance(tempVec); + if(dist < ServerLODComponent.FULL_RES){ + return true; + } + } return false; }