Fix animation bug

This commit is contained in:
James Lambert 2022-12-06 08:33:29 -07:00
parent 89004dcb4f
commit 3275716ac8
3 changed files with 29 additions and 12 deletions

View file

@ -239,16 +239,19 @@ void quatLook(struct Vector3* lookDir, struct Vector3* up, struct Quaternion* ou
}
void quatLerp(struct Quaternion* a, struct Quaternion* b, float t, struct Quaternion* out) {
if (a->x * b->x + a->y * b->y + a->z * b->z + a->w * b->w < 0) {
quatNegate(a, a);
}
float tInv = 1.0f - t;
if (quatDot(a, b) < 0) {
out->x = tInv * a->x - t * b->x;
out->y = tInv * a->y - t * b->y;
out->z = tInv * a->z - t * b->z;
out->w = tInv * a->w - t * b->w;
} else {
out->x = tInv * a->x + t * b->x;
out->y = tInv * a->y + t * b->y;
out->z = tInv * a->z + t * b->z;
out->w = tInv * a->w + t * b->w;
}
quatNormalize(out, out);
}
@ -284,3 +287,7 @@ void quatDecompose(struct Quaternion* input, struct Vector3* axis, float* angle)
axis->z = input->z * magInv;
*angle = sinf(axisMag) * 2.0f;
}
float quatDot(struct Quaternion* a, struct Quaternion* b) {
return a->x * b->x + a->y * b->y + a->z * b->z + a->w * b->w;
}

View file

@ -30,4 +30,6 @@ void quatLerp(struct Quaternion* a, struct Quaternion* b, float t, struct Quater
void quatApplyAngularVelocity(struct Quaternion* input, struct Vector3* w, float timeStep, struct Quaternion* output);
void quatDecompose(struct Quaternion* input, struct Vector3* axis, float* angle);
float quatDot(struct Quaternion* a, struct Quaternion* b);
#endif

View file

@ -118,11 +118,19 @@ void skAnimatorBlendTransform(struct SKAnimationBoneFrame* frame, struct Transfo
vector3AddScaled(&transforms[i].position, &boneTransform.position, weight, &transforms[i].position);
if (quatDot(&transforms[i].rotation, &boneTransform.rotation) < 0) {
transforms[i].rotation.x -= boneTransform.rotation.x * weight;
transforms[i].rotation.y -= boneTransform.rotation.y * weight;
transforms[i].rotation.z -= boneTransform.rotation.z * weight;
transforms[i].rotation.w -= boneTransform.rotation.w * weight;
} else {
transforms[i].rotation.x += boneTransform.rotation.x * weight;
transforms[i].rotation.y += boneTransform.rotation.y * weight;
transforms[i].rotation.z += boneTransform.rotation.z * weight;
transforms[i].rotation.w += boneTransform.rotation.w * weight;
}
}
}
void skAnimatorReadTransformWithWeight(struct SKAnimator* animator, struct Transform* transforms, float weight) {