Sample Code: Create Animation with Matrix in HPS sandbox
This is a sample code to create an animation from matrices. Use HPS::MatrixKit::Decompose
to get a translation and rotation from that matrix, then make a keyframe for each of those values that occur at tick X and append them to their respective channel's keyframe arrays.
https://docs.techsoft3d.com/hps/latest/prog_guide/0306_animation.html?
Language: C++ / mfc_sandbox
void CHPSView::OnUserCode1()
{
HPS::SegmentKey mySegmentKey = GetCanvas().GetFrontView().GetAttachedModel().GetSegmentKey().Subsegment("shell");
HPS::SegmentKey mySegmentKeySub = mySegmentKey.Subsegment("sub");
Point points[8] = { Point(0, 0, 0), Point(2, 0, 0), Point(2, 3, 0), Point(0, 3, 0),
Point(0, 0, 2), Point(2, 0, 2), Point(2, 3, 2), Point(0, 3, 2) };
int faceList[30] = { 4, 0, 1, 2, 3,
4, 1, 5, 6, 2,
4, 5, 4, 7, 6,
4, 4, 0, 3, 7,
4, 3, 2, 6, 7,
4, 0, 4, 5, 1 };
mySegmentKeySub.InsertShell(8, points, 30, faceList);
mySegmentKey.GetModellingMatrixControl().Translate(100, 100, 100);
mySegmentKey.GetVisibilityControl().SetHardEdges(true);
GetCanvas().GetFrontView().FitWorld().Update();
}
void CHPSView::OnUserCode2()
{
AnimationArray animations;
VectorKeyframeArray vectorKeyframes;
QuaternionKeyframeArray quaternionKeyframes;
HPS::MatrixKit matrix1;
matrix1.Translate(5, 0, 0);
HPS::Vector vec1, scale1;
HPS::Quaternion quat1;
matrix1.Decompose(vec1, scale1, quat1);
HPS::MatrixKitArray aMatrix;
int num = 4;
float deg = 45.0f;
for (int i = 0; i <= num; ++i)
{
HPS::MatrixKit matrix;
matrix.Rotate(0, 0, deg*i);
matrix.Translate(5, 0, 0);
aMatrix.push_back(matrix);
}
vectorKeyframes.push_back(VectorKeyframe(0, Vector(0, 0, 0)));
Quaternion original_rotation(1, 0, 0, 0);
quaternionKeyframes.push_back(QuaternionKeyframe(0, original_rotation));
int time = 1;
for (int i = 0; i <= num; ++i)
{
HPS::Vector vec, scale;
HPS::Quaternion quat;
aMatrix[i].Decompose(vec, scale, quat);
vectorKeyframes.push_back(VectorKeyframe(time, vec));
quaternionKeyframes.push_back(QuaternionKeyframe(time, quat));
time++;
}
VectorSampler vectorSampler;
vectorSampler.SetInterpolation(HPS::Sampler::InterpolationType::Linear);
vectorSampler.AddKeyframes(vectorKeyframes);
QuaternionSampler rotationSampler;
rotationSampler.SetKeyframes(quaternionKeyframes);
rotationSampler.SetInterpolation(HPS::Sampler::InterpolationType::Linear);
HPS::Model model = GetCanvas().GetFrontView().GetAttachedModel();
KeyPath keyPath;
keyPath.Append(model.GetSegmentKey().Subsegment("shell").Subsegment("sub"));
keyPath.Append(model.GetSegmentKey());
Animation translationAnimation = model.CreateAnimation("translation");
translationAnimation.AddTranslationChannel("translationChannel", keyPath, vectorSampler);
animations.push_back(translationAnimation);
Animation rotationAnimation = model.CreateAnimation("rotation");
rotationAnimation.AddRotationChannel("rotationChannel", keyPath, rotationSampler);
animations.push_back(rotationAnimation);
AnimationControl animationControl = GetCanvas().GetFrontView().GetAnimationControl();
animationControl.SetAnimations(animations);
animationControl.SetMillisecondsPerTick(500);
animationControl.Play();
}