Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.zigythebird.playeranimcore.animation;

import com.zigythebird.playeranimcore.PlayerAnimLib;
import com.zigythebird.playeranimcore.animation.keyframe.*;
import com.zigythebird.playeranimcore.animation.keyframe.event.CustomKeyFrameEvents;
import com.zigythebird.playeranimcore.animation.keyframe.event.data.CustomInstructionKeyframeData;
Expand Down Expand Up @@ -545,6 +546,9 @@ private void processCurrentAnimation(float adjustedTick, AnimationData animation
for (AdvancedPlayerAnimBone bone : this.bones.values()) {
bone.setToInitialPose();
}
for (PlayerAnimBone bone : this.pivotBones.values()) {
bone.setToInitialPose();
}

return;
}
Expand All @@ -562,6 +566,9 @@ private void processCurrentAnimation(float adjustedTick, AnimationData animation
for (PlayerAnimBone bone : this.bones.values()) {
bone.setToInitialPose();
}
for (PlayerAnimBone bone : this.pivotBones.values()) {
bone.setToInitialPose();
}

for (Map.Entry<String, BoneAnimation> entry : animation.boneAnimations().entrySet()) {
PlayerAnimBone bone = this.bones.getOrDefault(entry.getKey(), null);
Expand Down Expand Up @@ -633,27 +640,38 @@ protected void applyCustomPivotPoints() {
Map<String, String> parentsMap = this.currentAnimation.animation().parents();
if (parentsMap.isEmpty()) return;

List<PlayerAnimBone> bones1 = new ArrayList<>(this.bones.values());
for (PlayerAnimBone pivotBone : this.pivotBones.values()) {
if (!parentsMap.containsValue(pivotBone.getName()))
bones1.add(pivotBone);
Set<String> processedBones = new HashSet<>();
for (PlayerAnimBone bone : this.bones.values()) {
processBoneHierarchy(bone, parentsMap, processedBones);
}
for (PlayerAnimBone bone : this.pivotBones.values()) {
processBoneHierarchy(bone, parentsMap, processedBones);
}
}

for (PlayerAnimBone bone : bones1) {
if (parentsMap.containsKey(bone.getName())) {
this.activeBones.put(bone.getName(), bone);
private void processBoneHierarchy(PlayerAnimBone bone, Map<String, String> parentsMap, Set<String> processedBones) {
String boneName = bone.getName();
if (processedBones.contains(boneName)) return;

List<PivotBone> parents = new ArrayList<>();
PivotBone currentParent = this.pivotBones.get(parentsMap.get(bone.getName()));
parents.add(currentParent);
while (parentsMap.containsKey(currentParent.getName())) {
currentParent = this.pivotBones.get(parentsMap.get(currentParent.getName()));
parents.addFirst(currentParent);
}
String parentName = parentsMap.get(boneName);
if (parentName == null) {
processedBones.add(boneName);
return;
}

MatrixUtil.applyParentsToChild(bone, parents, this::getBonePosition);
}
PlayerAnimBone parent = this.pivotBones.get(parentName);
if (parent == null) parent = this.bones.get(parentName);
if (parent == null) {
PlayerAnimLib.LOGGER.error("Parent {} not found for {}", parentName, boneName);
return;
}

processBoneHierarchy(parent, parentsMap, processedBones);

this.activeBones.put(boneName, bone);
MatrixUtil.applyParentsToChild(bone, Collections.singletonList(parent), this::getBonePosition);

processedBones.add(boneName);
}

protected <T extends KeyFrameData> void handleCustomKeyframe(T[] keyframes, @Nullable CustomKeyFrameEvents.CustomKeyFrameHandler<T> main, CustomKeyFrameEvents.CustomKeyFrameHandler<T> event, float animationTick, AnimationData animationData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
* Used for applying custom pivot bones to player bones
*/
public class MatrixUtil {
public static void translateMatrixForBone(ModMatrix4f matrix, PlayerAnimBone bone) {
matrix.translate(-bone.getPosX(), bone.getPosY(), -bone.getPosZ());
}

public static void rotateMatrixAroundBone(ModMatrix4f matrix, PlayerAnimBone bone) {
if (bone.getRotZ() != 0 || bone.getRotY() != 0 || bone.getRotX() != 0)
matrix.rotateZ(bone.getRotZ()).rotateY(bone.getRotY()).rotateX(bone.getRotX());
Expand All @@ -31,6 +35,7 @@ public static void translateAwayFromPivotPoint(ModMatrix4f matrix, Vec3f pivot)

public static void prepMatrixForBone(ModMatrix4f matrix, PlayerAnimBone bone, Vec3f pivot) {
translateToPivotPoint(matrix, pivot);
translateMatrixForBone(matrix, bone);
rotateMatrixAroundBone(matrix, bone);
scaleMatrixForBone(matrix, bone);
translateAwayFromPivotPoint(matrix, pivot);
Expand All @@ -42,7 +47,6 @@ public static void applyParentsToChild(PlayerAnimBone child, Iterable<? extends
for (PlayerAnimBone parent : parents) {
Vec3f pivot = parent instanceof PivotBone pivotBone ? pivotBone.getPivot() : positions.apply(parent.getName());
MatrixUtil.prepMatrixForBone(matrix, parent, pivot);
child.addPos(parent.getPosX(), parent.getPosY(), parent.getPosZ());
}

Vec3f defaultPos = positions.apply(child.getName());
Expand Down