From 9958a6c5d4016819235e79f1b7d4fe60c6ad7151 Mon Sep 17 00:00:00 2001 From: austin Date: Thu, 1 Aug 2024 21:46:19 -0400 Subject: [PATCH] math class refactoring start --- docs/src/progress/currenttarget.md | 1 - docs/src/progress/renderertodo.md | 1 + .../electrosphere/audio/AudioListener.java | 2 +- .../electrosphere/controls/CameraHandler.java | 2 +- .../electrosphere/entity/EntityUtils.java | 2 +- .../entity/state/attack/ServerAttackTree.java | 2 +- .../state/hitbox/HitboxCollectionState.java | 2 +- .../groundmove/ClientGroundMovementTree.java | 2 +- .../groundmove/ServerGroundMovementTree.java | 2 +- .../entity/types/attach/AttachUtils.java | 2 +- .../types/camera/CameraEntityUtils.java | 2 +- .../entity/types/creature/CreatureUtils.java | 2 +- .../types/projectile/ProjectileUtils.java | 2 +- .../renderer/actor/ActorUtils.java | 45 -------------- .../pipelines/debug/DebugBonesPipeline.java | 6 +- .../electrosphere/script/ScriptEngine.java | 2 +- .../ai/creature/OpportunisticAttacker.java | 2 +- .../electrosphere/util/math/MathBones.java | 58 +++++++++++++++++++ .../util/{ => math}/MathUtils.java | 2 +- 19 files changed, 76 insertions(+), 63 deletions(-) create mode 100644 src/main/java/electrosphere/util/math/MathBones.java rename src/main/java/electrosphere/util/{ => math}/MathUtils.java (97%) diff --git a/docs/src/progress/currenttarget.md b/docs/src/progress/currenttarget.md index 71e4db5e..f3712a41 100644 --- a/docs/src/progress/currenttarget.md +++ b/docs/src/progress/currenttarget.md @@ -16,7 +16,6 @@ Things that feel bad: Can't instantly start/stop blocking Attack animation feels slow Hands don't align with blade - Need to be able to visualize bones No audio Short movement bursts feel jittery Part of this may be cylinder collidable instead of capsule diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index b66f0e65..b8c14115 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -495,6 +495,7 @@ Fix jump bugginess (08/01/2024) Fix server entity camera rotation Fix world bounds application +Bone debug rendering # TODO diff --git a/src/main/java/electrosphere/audio/AudioListener.java b/src/main/java/electrosphere/audio/AudioListener.java index 8ea0440d..3075c67a 100644 --- a/src/main/java/electrosphere/audio/AudioListener.java +++ b/src/main/java/electrosphere/audio/AudioListener.java @@ -3,7 +3,7 @@ package electrosphere.audio; import org.joml.Vector3d; import org.joml.Vector3f; -import electrosphere.util.MathUtils; +import electrosphere.util.math.MathUtils; import static org.lwjgl.openal.AL10.*; diff --git a/src/main/java/electrosphere/controls/CameraHandler.java b/src/main/java/electrosphere/controls/CameraHandler.java index 62393c28..5fc13d1d 100644 --- a/src/main/java/electrosphere/controls/CameraHandler.java +++ b/src/main/java/electrosphere/controls/CameraHandler.java @@ -12,7 +12,7 @@ import electrosphere.entity.EntityUtils; import electrosphere.entity.types.camera.CameraEntityUtils; import electrosphere.net.parser.net.message.EntityMessage; import electrosphere.renderer.ui.events.MouseEvent; -import electrosphere.util.MathUtils; +import electrosphere.util.math.MathUtils; /** * Handler for camera-related events and controls diff --git a/src/main/java/electrosphere/entity/EntityUtils.java b/src/main/java/electrosphere/entity/EntityUtils.java index 6352f4eb..7f2bf5ca 100644 --- a/src/main/java/electrosphere/entity/EntityUtils.java +++ b/src/main/java/electrosphere/entity/EntityUtils.java @@ -8,7 +8,7 @@ import electrosphere.server.datacell.ServerDataCell; import electrosphere.server.datacell.utils.EntityLookupUtils; import electrosphere.server.poseactor.PoseActor; import electrosphere.server.poseactor.PoseActorUtils; -import electrosphere.util.MathUtils; +import electrosphere.util.math.MathUtils; import org.joml.Quaterniond; import org.joml.Vector3d; diff --git a/src/main/java/electrosphere/entity/state/attack/ServerAttackTree.java b/src/main/java/electrosphere/entity/state/attack/ServerAttackTree.java index d36bf463..8fbeb784 100644 --- a/src/main/java/electrosphere/entity/state/attack/ServerAttackTree.java +++ b/src/main/java/electrosphere/entity/state/attack/ServerAttackTree.java @@ -33,7 +33,7 @@ import electrosphere.net.synchronization.annotation.SynchronizedBehaviorTree; import electrosphere.renderer.actor.Actor; import electrosphere.server.datacell.Realm; import electrosphere.server.poseactor.PoseActor; -import electrosphere.util.MathUtils; +import electrosphere.util.math.MathUtils; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; diff --git a/src/main/java/electrosphere/entity/state/hitbox/HitboxCollectionState.java b/src/main/java/electrosphere/entity/state/hitbox/HitboxCollectionState.java index 977d4d61..206c7937 100644 --- a/src/main/java/electrosphere/entity/state/hitbox/HitboxCollectionState.java +++ b/src/main/java/electrosphere/entity/state/hitbox/HitboxCollectionState.java @@ -24,7 +24,7 @@ import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityUtils; import electrosphere.entity.state.hitbox.HitboxCollectionState.HitboxState.HitboxShapeType; import electrosphere.game.data.collidable.HitboxData; -import electrosphere.util.MathUtils; +import electrosphere.util.math.MathUtils; /** * The state of the collection of all hitboxes on this entity diff --git a/src/main/java/electrosphere/entity/state/movement/groundmove/ClientGroundMovementTree.java b/src/main/java/electrosphere/entity/state/movement/groundmove/ClientGroundMovementTree.java index 40ac88f9..86b5d3b9 100644 --- a/src/main/java/electrosphere/entity/state/movement/groundmove/ClientGroundMovementTree.java +++ b/src/main/java/electrosphere/entity/state/movement/groundmove/ClientGroundMovementTree.java @@ -27,7 +27,7 @@ import electrosphere.net.synchronization.annotation.SyncedField; import electrosphere.net.synchronization.annotation.SynchronizableEnum; import electrosphere.net.synchronization.annotation.SynchronizedBehaviorTree; import electrosphere.renderer.anim.Animation; -import electrosphere.util.MathUtils; +import electrosphere.util.math.MathUtils; import electrosphere.renderer.actor.Actor; import java.util.concurrent.CopyOnWriteArrayList; 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 1ec83556..67174257 100644 --- a/src/main/java/electrosphere/entity/state/movement/groundmove/ServerGroundMovementTree.java +++ b/src/main/java/electrosphere/entity/state/movement/groundmove/ServerGroundMovementTree.java @@ -36,7 +36,7 @@ import electrosphere.script.utils.AccessTransforms; import electrosphere.server.datacell.utils.DataCellSearchUtils; import electrosphere.server.poseactor.PoseActor; import electrosphere.server.utils.ServerScriptUtils; -import electrosphere.util.MathUtils; +import electrosphere.util.math.MathUtils; import java.util.concurrent.CopyOnWriteArrayList; diff --git a/src/main/java/electrosphere/entity/types/attach/AttachUtils.java b/src/main/java/electrosphere/entity/types/attach/AttachUtils.java index c527841e..3bb56ac4 100644 --- a/src/main/java/electrosphere/entity/types/attach/AttachUtils.java +++ b/src/main/java/electrosphere/entity/types/attach/AttachUtils.java @@ -11,7 +11,7 @@ import electrosphere.renderer.actor.Actor; import electrosphere.server.datacell.ServerDataCell; import electrosphere.server.datacell.utils.ServerEntityTagUtils; import electrosphere.server.poseactor.PoseActor; -import electrosphere.util.MathUtils; +import electrosphere.util.math.MathUtils; import java.util.LinkedList; import java.util.List; diff --git a/src/main/java/electrosphere/entity/types/camera/CameraEntityUtils.java b/src/main/java/electrosphere/entity/types/camera/CameraEntityUtils.java index 77094ea7..7b9143b1 100644 --- a/src/main/java/electrosphere/entity/types/camera/CameraEntityUtils.java +++ b/src/main/java/electrosphere/entity/types/camera/CameraEntityUtils.java @@ -6,7 +6,7 @@ import electrosphere.entity.EntityCreationUtils; import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityUtils; import electrosphere.entity.btree.BehaviorTree; -import electrosphere.util.MathUtils; +import electrosphere.util.math.MathUtils; import org.joml.Matrix4d; import org.joml.Matrix4f; diff --git a/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java b/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java index da819088..ebe3bcea 100644 --- a/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java +++ b/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java @@ -73,8 +73,8 @@ import electrosphere.server.datacell.utils.ServerBehaviorTreeUtils; import electrosphere.server.datacell.utils.ServerEntityTagUtils; import electrosphere.server.poseactor.PoseActor; import electrosphere.server.poseactor.PoseActorUtils; -import electrosphere.util.MathUtils; import electrosphere.util.Utilities; +import electrosphere.util.math.MathUtils; /** * Utilities for creating creatures on the client and server diff --git a/src/main/java/electrosphere/entity/types/projectile/ProjectileUtils.java b/src/main/java/electrosphere/entity/types/projectile/ProjectileUtils.java index cbc4ea53..f5a16b4c 100644 --- a/src/main/java/electrosphere/entity/types/projectile/ProjectileUtils.java +++ b/src/main/java/electrosphere/entity/types/projectile/ProjectileUtils.java @@ -19,7 +19,7 @@ import electrosphere.game.data.collidable.HitboxData; import electrosphere.game.data.projectile.ProjectileType; import electrosphere.server.datacell.Realm; import electrosphere.server.datacell.utils.ServerBehaviorTreeUtils; -import electrosphere.util.MathUtils; +import electrosphere.util.math.MathUtils; public class ProjectileUtils { diff --git a/src/main/java/electrosphere/renderer/actor/ActorUtils.java b/src/main/java/electrosphere/renderer/actor/ActorUtils.java index 3568ac79..63cff187 100644 --- a/src/main/java/electrosphere/renderer/actor/ActorUtils.java +++ b/src/main/java/electrosphere/renderer/actor/ActorUtils.java @@ -1,13 +1,8 @@ package electrosphere.renderer.actor; -import org.joml.Quaterniond; -import org.joml.Vector3d; - import electrosphere.engine.Globals; import electrosphere.entity.Entity; import electrosphere.entity.EntityUtils; -import electrosphere.entity.types.creature.CreatureUtils; -import electrosphere.util.MathUtils; /** * Utils for dealing with actors @@ -44,46 +39,6 @@ public class ActorUtils { Actor entityActor = EntityUtils.getActor(actorEntity); return entityActor.getStaticMorph(); } - - - /** - * Gets the world position of a bone - * @param actorEntity The entity that has a bone - * @param boneName The name of the bone - */ - public static Vector3d getBoneWorldPosition(Entity actorEntity, String boneName){ - Actor actor = EntityUtils.getActor(actorEntity); - Vector3d localPos = new Vector3d(actor.getBonePosition(boneName)); - - //transform bone space - Vector3d position = new Vector3d(localPos); - position = position.mul(EntityUtils.getScale(actorEntity)); - position = position.rotate(new Quaterniond(EntityUtils.getRotation(actorEntity))); - //transform worldspace - position.add(new Vector3d(EntityUtils.getPosition(actorEntity))); - return position; - } - - /** - * Gets the global rotation of the bone - * @param actorEntity The entity with the bone - * @param boneName The name of the bone - * @return The global rotation of the bone - */ - public static Quaterniond getBoneWorldRotation(Entity actorEntity, String boneName){ - Actor actor = EntityUtils.getActor(actorEntity); - Quaterniond localRot = actor.getBoneRotation(boneName); - - Vector3d facingAngle = CreatureUtils.getFacingVector(actorEntity); - if(facingAngle == null){ - facingAngle = MathUtils.getOriginVector(); - } - //calculate rotation of model - return new Quaterniond() - .rotationTo(MathUtils.getOriginVector(), new Vector3d(facingAngle.x,facingAngle.y,facingAngle.z)) - .mul(localRot) - .normalize(); - } } diff --git a/src/main/java/electrosphere/renderer/pipelines/debug/DebugBonesPipeline.java b/src/main/java/electrosphere/renderer/pipelines/debug/DebugBonesPipeline.java index 94ba8d0e..c699d208 100644 --- a/src/main/java/electrosphere/renderer/pipelines/debug/DebugBonesPipeline.java +++ b/src/main/java/electrosphere/renderer/pipelines/debug/DebugBonesPipeline.java @@ -14,10 +14,10 @@ import electrosphere.renderer.OpenGLState; import electrosphere.renderer.RenderPipelineState; import electrosphere.renderer.RenderingEngine; import electrosphere.renderer.actor.Actor; -import electrosphere.renderer.actor.ActorUtils; import electrosphere.renderer.model.Bone; import electrosphere.renderer.model.Model; import electrosphere.renderer.pipelines.RenderPipeline; +import electrosphere.util.math.MathBones; /** * Renders the bones for a given mesh @@ -71,8 +71,8 @@ public class DebugBonesPipeline implements RenderPipeline { Model boneModel = Globals.assetManager.fetchModel("Models/basic/geometry/unitcylinder.fbx"); boneModel.getMaterials().get(0).set_diffuse(Globals.textureDiffuseDefault); for(Bone bone : targetActor.getBoneValues()){ - Vector3d bonePos = ActorUtils.getBoneWorldPosition(targetEntity, bone.boneID); - Quaterniond boneRot = ActorUtils.getBoneWorldRotation(targetEntity, bone.boneID); + Vector3d bonePos = MathBones.getBoneWorldPosition(targetEntity, bone.boneID); + Quaterniond boneRot = MathBones.getBoneWorldRotation(targetEntity, bone.boneID); //put pos + rot into model Vector3f cameraModifiedPosition = new Vector3f((float)bonePos.x,(float)bonePos.y,(float)bonePos.z).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)); diff --git a/src/main/java/electrosphere/script/ScriptEngine.java b/src/main/java/electrosphere/script/ScriptEngine.java index d20ee742..94e13eca 100644 --- a/src/main/java/electrosphere/script/ScriptEngine.java +++ b/src/main/java/electrosphere/script/ScriptEngine.java @@ -18,7 +18,7 @@ import electrosphere.engine.Main; import electrosphere.logger.LoggerInterface; import electrosphere.menu.tutorial.TutorialMenus; import electrosphere.util.FileUtils; -import electrosphere.util.MathUtils; +import electrosphere.util.math.MathUtils; /** * Interface for executing scripts in the game engine diff --git a/src/main/java/electrosphere/server/ai/creature/OpportunisticAttacker.java b/src/main/java/electrosphere/server/ai/creature/OpportunisticAttacker.java index 35313c97..7e64f5d5 100644 --- a/src/main/java/electrosphere/server/ai/creature/OpportunisticAttacker.java +++ b/src/main/java/electrosphere/server/ai/creature/OpportunisticAttacker.java @@ -13,7 +13,7 @@ import electrosphere.entity.types.item.ItemUtils; import electrosphere.server.ai.AITree; import electrosphere.server.datacell.Realm; import electrosphere.server.datacell.utils.DataCellSearchUtils; -import electrosphere.util.MathUtils; +import electrosphere.util.math.MathUtils; import org.joml.Quaterniond; import org.joml.Vector3d; diff --git a/src/main/java/electrosphere/util/math/MathBones.java b/src/main/java/electrosphere/util/math/MathBones.java new file mode 100644 index 00000000..8fbd34a7 --- /dev/null +++ b/src/main/java/electrosphere/util/math/MathBones.java @@ -0,0 +1,58 @@ +package electrosphere.util.math; + +import org.joml.Quaterniond; +import org.joml.Vector3d; + +import electrosphere.entity.Entity; +import electrosphere.entity.EntityUtils; +import electrosphere.entity.types.creature.CreatureUtils; +import electrosphere.renderer.actor.Actor; + +/** + * Math functions related to bones + */ +public class MathBones { + + + + /** + * Gets the world position of a bone + * @param actorEntity The entity that has a bone + * @param boneName The name of the bone + */ + public static Vector3d getBoneWorldPosition(Entity actorEntity, String boneName){ + Actor actor = EntityUtils.getActor(actorEntity); + Vector3d localPos = new Vector3d(actor.getBonePosition(boneName)); + + //transform bone space + Vector3d position = new Vector3d(localPos); + position = position.mul(EntityUtils.getScale(actorEntity)); + position = position.rotate(new Quaterniond(EntityUtils.getRotation(actorEntity))); + //transform worldspace + position.add(new Vector3d(EntityUtils.getPosition(actorEntity))); + return position; + } + + /** + * Gets the global rotation of the bone + * @param actorEntity The entity with the bone + * @param boneName The name of the bone + * @return The global rotation of the bone + */ + public static Quaterniond getBoneWorldRotation(Entity actorEntity, String boneName){ + Actor actor = EntityUtils.getActor(actorEntity); + Quaterniond localRot = actor.getBoneRotation(boneName); + + Vector3d facingAngle = CreatureUtils.getFacingVector(actorEntity); + if(facingAngle == null){ + facingAngle = MathUtils.getOriginVector(); + } + //calculate rotation of model + return new Quaterniond() + .rotationTo(MathUtils.getOriginVector(), new Vector3d(facingAngle.x,facingAngle.y,facingAngle.z)) + .mul(localRot) + .normalize(); + } + + +} diff --git a/src/main/java/electrosphere/util/MathUtils.java b/src/main/java/electrosphere/util/math/MathUtils.java similarity index 97% rename from src/main/java/electrosphere/util/MathUtils.java rename to src/main/java/electrosphere/util/math/MathUtils.java index ff07dc0c..ae42b5bb 100644 --- a/src/main/java/electrosphere/util/MathUtils.java +++ b/src/main/java/electrosphere/util/math/MathUtils.java @@ -1,4 +1,4 @@ -package electrosphere.util; +package electrosphere.util.math; import org.joml.Quaterniond; import org.joml.Vector3d;