From 90031765a9fe2da555688c7d35b668433029ec84 Mon Sep 17 00:00:00 2001 From: austin Date: Thu, 22 May 2025 17:25:54 -0400 Subject: [PATCH] floating world origin work --- docs/src/progress/renderertodo.md | 2 + .../client/sim/ClientSimulation.java | 4 ++ .../menu/debug/entity/ImGuiEntityMacros.java | 7 +++ .../entity/tabs/ImGuiEntityPhysicsTab.java | 13 ++--- .../collision/CollisionEngine.java | 55 +++++++++++-------- .../physicssync/ClientPhysicsSyncTree.java | 8 ++- .../entity/types/terrain/TerrainChunk.java | 4 +- .../gridded/GriddedDataCellManager.java | 4 +- .../character/PlayerCharacterCreation.java | 3 + 9 files changed, 63 insertions(+), 37 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 28f7bdce..37855622 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1934,6 +1934,8 @@ Geom-only collidables on server Block colliders leveraging spaces Scene view debug window Debug view of entity colliders +Fix lookup bug in debug physics ui +Floating point starting to play nice with engine diff --git a/src/main/java/electrosphere/client/sim/ClientSimulation.java b/src/main/java/electrosphere/client/sim/ClientSimulation.java index ef0b6220..3ae5050e 100644 --- a/src/main/java/electrosphere/client/sim/ClientSimulation.java +++ b/src/main/java/electrosphere/client/sim/ClientSimulation.java @@ -122,6 +122,10 @@ public class ClientSimulation { Globals.clientState.clientSceneWrapper.getChemistryEngine().clearCollidableImpulseLists(); Globals.profiler.endCpuSample(); + // + //Rebase world origin + Globals.clientState.clientSceneWrapper.getCollisionEngine().rebaseWorldOrigin(); + // //wrap up functions this.runClientFunctions(); diff --git a/src/main/java/electrosphere/client/ui/menu/debug/entity/ImGuiEntityMacros.java b/src/main/java/electrosphere/client/ui/menu/debug/entity/ImGuiEntityMacros.java index 7214a909..f08e907a 100644 --- a/src/main/java/electrosphere/client/ui/menu/debug/entity/ImGuiEntityMacros.java +++ b/src/main/java/electrosphere/client/ui/menu/debug/entity/ImGuiEntityMacros.java @@ -64,6 +64,7 @@ public class ImGuiEntityMacros { private static boolean filterToCreatures = false; //filters the entity list to just creatures private static boolean filterToFoliage = false; //filters the entity list to just foliage private static boolean filterToTerrain = false; //filters the entity list to just terrain + private static boolean filterHasCollidable = false; //filters the entity list to just entities that have collidables //window for viewing details about an entity protected static ImGuiWindow clientEntityDetailWindow; @@ -118,6 +119,9 @@ public class ImGuiEntityMacros { if(ImGui.checkbox("Filter to Terrain", filterToTerrain)){ filterToTerrain = !filterToTerrain; } + if(ImGui.checkbox("Filter has Collidable", filterHasCollidable)){ + filterHasCollidable = !filterHasCollidable; + } for(Entity entity : Globals.clientState.clientSceneWrapper.getScene().getEntityList()){ //filters if(filterToCreatures && !CreatureUtils.isCreature(entity)){ @@ -129,6 +133,9 @@ public class ImGuiEntityMacros { if(filterToTerrain && !TerrainChunk.isTerrainEntity(entity)){ continue; } + if(filterHasCollidable && PhysicsEntityUtils.getCollidable(entity) == null){ + continue; + } if(filterBasic && ( AmbientFoliage.getAmbientFoliageTree(entity) != null || diff --git a/src/main/java/electrosphere/client/ui/menu/debug/entity/tabs/ImGuiEntityPhysicsTab.java b/src/main/java/electrosphere/client/ui/menu/debug/entity/tabs/ImGuiEntityPhysicsTab.java index 3878cfc2..2a224738 100644 --- a/src/main/java/electrosphere/client/ui/menu/debug/entity/tabs/ImGuiEntityPhysicsTab.java +++ b/src/main/java/electrosphere/client/ui/menu/debug/entity/tabs/ImGuiEntityPhysicsTab.java @@ -10,7 +10,6 @@ import electrosphere.data.entity.collidable.CollidableTemplate; import electrosphere.engine.Globals; import electrosphere.entity.Entity; import electrosphere.entity.EntityDataStrings; -import electrosphere.entity.EntityUtils; import electrosphere.entity.types.creature.CreatureUtils; import electrosphere.server.datacell.utils.EntityLookupUtils; import imgui.ImGui; @@ -29,8 +28,8 @@ public class ImGuiEntityPhysicsTab { if(PhysicsEntityUtils.getDBody(detailViewEntity) != null){ DBody physicsBody = PhysicsEntityUtils.getDBody(detailViewEntity); if(physicsBody != null){ - ImGui.text("Position: " + EntityUtils.getPosition(detailViewEntity)); - ImGui.text("Rotation: " + EntityUtils.getRotation(detailViewEntity)); + ImGui.text("Position: " + physicsBody.getPosition()); + ImGui.text("Rotation: " + physicsBody.getQuaternion()); ImGui.text("Velocity: " + physicsBody.getLinearVel()); ImGui.text("Force: " + physicsBody.getForce()); ImGui.text("Angular Velocity: " + physicsBody.getAngularVel()); @@ -53,8 +52,8 @@ public class ImGuiEntityPhysicsTab { if(serverPhysicsBody != null){ ImGui.newLine(); ImGui.text("Linked server entity:"); - ImGui.text("Position (Server): " + EntityUtils.getPosition(serverEntity)); - ImGui.text("Rotation (Server): " + EntityUtils.getRotation(serverEntity)); + ImGui.text("Position (Server): " + physicsBody.getPosition()); + ImGui.text("Rotation (Server): " + physicsBody.getQuaternion()); ImGui.text("Velocity (Server): " + serverPhysicsBody.getLinearVel()); ImGui.text("Force (Server): " + serverPhysicsBody.getForce()); ImGui.text("Angular Velocity: " + serverPhysicsBody.getAngularVel()); @@ -73,8 +72,8 @@ public class ImGuiEntityPhysicsTab { if(clientPhysicsBody != null){ ImGui.newLine(); ImGui.text("Linked client entity:"); - ImGui.text("Position (Client): " + EntityUtils.getPosition(clientEntity)); - ImGui.text("Rotation (Client): " + EntityUtils.getRotation(clientEntity)); + ImGui.text("Position (Client): " + physicsBody.getPosition()); + ImGui.text("Rotation (Client): " + physicsBody.getQuaternion()); ImGui.text("Velocity (Client): " + clientPhysicsBody.getLinearVel()); ImGui.text("Force (Client): " + clientPhysicsBody.getForce()); ImGui.text("Angular Velocity: " + clientPhysicsBody.getAngularVel()); diff --git a/src/main/java/electrosphere/collision/CollisionEngine.java b/src/main/java/electrosphere/collision/CollisionEngine.java index 31100424..568fd8e5 100644 --- a/src/main/java/electrosphere/collision/CollisionEngine.java +++ b/src/main/java/electrosphere/collision/CollisionEngine.java @@ -94,6 +94,11 @@ public class CollisionEngine { * Default distance to interact with collidables at (ie picking where to place things) */ public static final double DEFAULT_INTERACT_DISTANCE = 5.0; + + /** + * Distance from current floating point origin to trigger a rebase + */ + private static final double REBASE_TRIGGER_DISTANCE = 500; /** * world data that the collision engine leverages for position correction and the like @@ -698,7 +703,7 @@ public class CollisionEngine { if(geom != null){ if(geom instanceof DSpace space){ for(DGeom child : space.getGeoms()){ - Vector3d currentBodyOffset = PhysicsUtils.odeVecToJomlVec(child.getPosition()); + Vector3d currentBodyOffset = PhysicsUtils.odeVecToJomlVec(child.getPosition()).add(this.floatingOrigin); if(collected == 0){ newOrigin.set(currentBodyOffset); } else { @@ -709,7 +714,7 @@ public class CollisionEngine { collected++; } } else { - Vector3d currentBodyOffset = PhysicsUtils.odeVecToJomlVec(geom.getPosition()); + Vector3d currentBodyOffset = PhysicsUtils.odeVecToJomlVec(geom.getPosition()).add(this.floatingOrigin); if(collected == 0){ newOrigin.set(currentBodyOffset); } else { @@ -721,27 +726,32 @@ public class CollisionEngine { } } } - Vector3d delta = new Vector3d(this.floatingOrigin).sub(newOrigin); - this.floatingOrigin = this.floatingOrigin.set(newOrigin); - //apply new origin to all geoms - //calculate new origin - for(Collidable collidable : collidableList){ - Entity physicsEntity = collidable.getParent(); - DBody rigidBody = PhysicsEntityUtils.getDBody(physicsEntity); - if(rigidBody != null){ - Vector3d existingPosition = PhysicsUtils.odeVecToJomlVec(rigidBody.getPosition()); - rigidBody.setPosition(PhysicsUtils.jomlVecToOdeVec(existingPosition.add(delta))); - } - DGeom geom = PhysicsEntityUtils.getDGeom(physicsEntity); - if(geom != null){ - if(geom instanceof DSpace space){ - for(DGeom child : space.getGeoms()){ - Vector3d existingPosition = PhysicsUtils.odeVecToJomlVec(child.getPosition()); - child.setPosition(PhysicsUtils.jomlVecToOdeVec(existingPosition.add(delta))); + Vector3d delta = new Vector3d(this.floatingOrigin); + delta = delta.sub(newOrigin); + + //only perform rebase if sufficiently far away + if(delta.length() > REBASE_TRIGGER_DISTANCE){ + this.floatingOrigin = newOrigin; + //apply new origin to all geoms + //calculate new origin + for(Collidable collidable : collidableList){ + Entity physicsEntity = collidable.getParent(); + DBody rigidBody = PhysicsEntityUtils.getDBody(physicsEntity); + if(rigidBody != null){ + Vector3d existingPosition = PhysicsUtils.odeVecToJomlVec(rigidBody.getPosition()); + rigidBody.setPosition(PhysicsUtils.jomlVecToOdeVec(existingPosition.add(delta))); + } + DGeom geom = PhysicsEntityUtils.getDGeom(physicsEntity); + if(geom != null){ + if(geom instanceof DSpace space){ + for(DGeom child : space.getGeoms()){ + Vector3d existingPosition = PhysicsUtils.odeVecToJomlVec(child.getPosition()); + child.setPosition(PhysicsUtils.jomlVecToOdeVec(existingPosition.add(delta))); + } + } else { + Vector3d existingPosition = PhysicsUtils.odeVecToJomlVec(geom.getPosition()); + geom.setPosition(PhysicsUtils.jomlVecToOdeVec(existingPosition.add(delta))); } - } else { - Vector3d existingPosition = PhysicsUtils.odeVecToJomlVec(geom.getPosition()); - geom.setPosition(PhysicsUtils.jomlVecToOdeVec(existingPosition.add(delta))); } } } @@ -1483,6 +1493,7 @@ public class CollisionEngine { "Geom Ptrs: " + this.geomPointerMap.size() + "\n" + "Collidables: " + this.collidableList.size() + "\n" + "Space geom count: " + this.space.getNumGeoms() + "\n" + + "Floating origin: " + this.floatingOrigin.x + "," + this.floatingOrigin.y + "," + this.floatingOrigin.z + "\n" + "" ; return message; diff --git a/src/main/java/electrosphere/entity/state/physicssync/ClientPhysicsSyncTree.java b/src/main/java/electrosphere/entity/state/physicssync/ClientPhysicsSyncTree.java index dd35ac0c..be3ffaa3 100644 --- a/src/main/java/electrosphere/entity/state/physicssync/ClientPhysicsSyncTree.java +++ b/src/main/java/electrosphere/entity/state/physicssync/ClientPhysicsSyncTree.java @@ -63,8 +63,12 @@ public class ClientPhysicsSyncTree implements BehaviorTree { //bust distance caches if this is the player's entity and we've traveled a long distance suddenly if(parent == Globals.clientState.playerEntity){ if(position.distance(EntityUtils.getPosition(parent)) > FoliageCellManager.TELEPORT_DISTANCE){ - Globals.clientState.clientDrawCellManager.bustDistanceCache(); - Globals.clientState.foliageCellManager.bustDistanceCache(); + if(Globals.clientState.clientDrawCellManager != null){ + Globals.clientState.clientDrawCellManager.bustDistanceCache(); + } + if(Globals.clientState.foliageCellManager != null){ + Globals.clientState.foliageCellManager.bustDistanceCache(); + } } } diff --git a/src/main/java/electrosphere/entity/types/terrain/TerrainChunk.java b/src/main/java/electrosphere/entity/types/terrain/TerrainChunk.java index 543a24f6..37284189 100644 --- a/src/main/java/electrosphere/entity/types/terrain/TerrainChunk.java +++ b/src/main/java/electrosphere/entity/types/terrain/TerrainChunk.java @@ -90,9 +90,7 @@ public class TerrainChunk { } LoggerInterface.loggerEngine.DEBUG("Finished generating terrain polygons; however, entity has already been deleted."); } - } catch (Error e){ - LoggerInterface.loggerEngine.ERROR(e); - } catch(Exception e){ + } catch (Throwable e){ LoggerInterface.loggerEngine.ERROR(e); } }); diff --git a/src/main/java/electrosphere/server/datacell/gridded/GriddedDataCellManager.java b/src/main/java/electrosphere/server/datacell/gridded/GriddedDataCellManager.java index c3be30c8..c997f09a 100644 --- a/src/main/java/electrosphere/server/datacell/gridded/GriddedDataCellManager.java +++ b/src/main/java/electrosphere/server/datacell/gridded/GriddedDataCellManager.java @@ -843,9 +843,7 @@ public class GriddedDataCellManager implements DataCellManager, VoxelCellManager //set ready dataCell.setReady(true); - } catch(Exception ex){ - LoggerInterface.loggerEngine.ERROR(ex); - } catch (Error ex){ + } catch(Throwable ex){ LoggerInterface.loggerEngine.ERROR(ex); } }); diff --git a/src/main/java/electrosphere/server/macro/character/PlayerCharacterCreation.java b/src/main/java/electrosphere/server/macro/character/PlayerCharacterCreation.java index 93b9645f..16cb2feb 100644 --- a/src/main/java/electrosphere/server/macro/character/PlayerCharacterCreation.java +++ b/src/main/java/electrosphere/server/macro/character/PlayerCharacterCreation.java @@ -3,6 +3,7 @@ package electrosphere.server.macro.character; import org.joml.Vector3d; import org.joml.Vector3i; +import electrosphere.collision.PhysicsEntityUtils; import electrosphere.engine.Globals; import electrosphere.entity.Entity; import electrosphere.entity.state.server.ServerCharacterData; @@ -59,6 +60,8 @@ public class PlayerCharacterCreation { //must come after the player is assigned, otherwise the player will not get the item attachment messages CreatureUtils.serverApplyTemplate(realm, newPlayerEntity, template); + PhysicsEntityUtils.disableBody(realm.getCollisionEngine(), newPlayerEntity); + //if macro data hasn't been generated in this area, generate it //but only if it's a player's entity realm.updateMacroData(spawnPoint);