Animation

Alright here’s the deal:

I have an MD2 loader that loads keyframes into memory. My draw function takes two parameters, a mode (not important right now), and a floating point value named frame. Here’s the idea:

If it gets passed 3.5 for the frame variable, then it means draw the coordinates halfway inbetween keyframe 3 and keyframe 4.

So, what should be my algorithm for doing this? I was thinking something that looks like this:

(frames[(int)frame].x - frames[(int)frame-1)].x)*((int)frame-((int)frame-1)) + frames[(int)frame].x

However I get some very strange results. Does anybody see the error in my way? Thanks a lot

Originally posted by 31337:
(frames[(int)frame].x - frames[(int)frame-1)].x)*b[/b] + frames[(int)frame].x

So basically you are trying to do linear interpolation between the two keyframes. However, in your code snippet, the “scaling factor” (bolded) will be an integer. You want to use:

(frame-(int)frame)

You are also “offsetting” with the wrong frame (last part should be +frames[(int)frame-1].x)

Also, I suggest that you calculate indeces and “interplation fraction” before doing the interpolation (e.g. to handle the start/end of the animation without access violations etc).

Here’s an example which should work:

frame1 = (int) frame;
frame2 = frame1 + 1;
frac   = frame - frame1;
x = frames[frame1].x + (frames[frame2].x - frames[frame1].x)*frac;
y = frames[frame1].y + (frames[frame2].y - frames[frame1].y)*frac;
z = frames[frame1].z + (frames[frame2].z - frames[frame1].z)*frac;

I also assume that you are looping over all the x/y/z coordinates in the model, not just one vertex as this example implies.

[This message has been edited by marcus256 (edited 12-14-2002).]

thanks a lot for your advice, when I get home I’ll try out the changes you’ve suggested. From what I can tell you’ve nailed it right on the nose.

Ok thanks a lot i did what you suggested and it works great except for what looks like a little bit of morphing.