سؤال

I want to create an animation system in c++ in wich I store keyframes, that have a time and a value. Those values shall be interpolated during playback, so i need them sortet by their time variable. Because when interpolating, i allways want to interpolate only between last and next keyframe (how it's usually done).

How would I store the keyframes, so I can easily (and fast) access the keyframe before and after a specific time?

At first std::map came to my mind, but there I have problems with the correct order of the keyframes... Any ideas how to do this better?

هل كانت مفيدة؟

المحلول

You can use std::vector and keep the correct order of the keyframes. Assuming that the keyframes are sorted by time in the vector you can then extract the relevant keyframe with std::lower_bound or std::binary_search in logarithmic time.

std::map internally keeps the elements sorted by the key following a strict weak ordering criterion. So, if you use time as the key, you will keep the correct order of the keyframes.

Personally, I would use std::vector.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top