Jump to content
  • Advertisement
Sign in to follow this  
whiterook6

[C++] keyframing using std::mapfloat, T

This topic is 3036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm trying to make a simple linear keyframe class that will let me insert keyframes at positions, then sample between them. I want to sample at a number and, for any float not already inserted, to take the largest keyframe less than this number, and the smallest one larger than this keyframe, then Lerp between the two.

So, if I have [6->20] and [8->24] inserted as keyframes, then sample(7) should return 22, sample(5) and sample(6) should return 20, and sample(10) should return 24.

Of course, my code isn't working. I can handle, I think, when the sample value is already stored exactly, or when it's smaller than the smallest or larger than the largest. How do I handle the case when the sample value is between two keyframes? Or, maybe a simpler question, how do I find the two keyframes on either end of a sample?

Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Have a look at the lower_bound and upper_bound member functions; they should do (half of) what you need. Once you have selected the appropriate boundary, you can simply decrement the iterator to obtain the previous keyframe, assuming it isn't the first keyframe already.


[edit] Addendum: you can also look at some similar functions like adjacent_find or partition, which might give you better results depending on how you like to style your code.

Share this post


Link to post
Share on other sites
I think I've got it. Thanks!
After checking the boundary conditions, I get this:

// the first iterator whose key is larger than the position
std::map<float, T>::iterator rightKeyframe=keyframes.upper_bound(position),
leftKeyframe=rightKeyframe;

// the iterator before the right (hopefully the largest value smaller than position.)
leftKeyframe--;

// find the mix between left and right (between 0 and 1)
float mix=(position-leftKeyframe->first)
/(rightKeyframe->first-leftKeyframe->first);

// return the lerped value.
return Lerp(leftKeyframe->second,
rightKeyframe->second,
mix);

Share this post


Link to post
Share on other sites
Looks about right [smile] Although I would note that there's no "hopefully" about the determination of the previous frame - logically, if upper_bound gives you the first element > X, then upper_bound - 1 is going to be <= X by definition. Again provided that you know upper_bound didn't return the first keyframe, you're guaranteed to get the right result there.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!