How can I interpolate between two positions within a specified time?
So I've been stuck on this one for a bit. I'm making a replay system and everything is working, but it's just moving objects to the next frames positions and rotations at the moment. I want to create a sort of lerp function that will move and rotate the object to the next frame position over the course of one frames time like 0.02 seconds.
I've tried a couple of things with unity standard Lerp functions but none of them really did it for me.
Thanks
I've tried a couple of things with unity standard Lerp functions but none of them really did it for me.
Thanks
Comments
So here is an idea : you know where you are, you know where you want to be and you know how long you want it to take to get there. So as they teach us in school : speed = distance/time ;)
For instance you are at (0, 0, 0) you want to be at (0, 1, 0) in 0.02 seconds.
You get the amount of frames 0.02 seconds equates to by doing this :
Then you get the delta distance you need to travel :
Now you need to apply the deltaMovement every frame and it will get you to your destination in time.
I did not test any of this but it should work. Its also not the only way but its probably the route I would go. Check out easing functions as well.
*Edit*
Instead of adding the deltaMovement each frame you can also do this :
Is the issue from trying to lerp two things (position and rotation) and then combining those results being difficult?
This linear interpolation function (from @garethf) is probably what he wants :
Math.linearTween = function (t, b, c, d) {
return c*t/d + b;
};
I'm waiting for @notsimon207 to explain the actual problem :)
P.S. A lerp not actually reaching its destination generally happens when you shift either the end-point of the lerp and/or change the time in which you want the lerp to happen in. You really shouldn't be trying to lerp towards a non-static position, there are different functions for that... Replays are usually static though.
I think further explanation from @notsimon207 might help, assuming the information others have helped him with hasn't already solved the problem.
I don't think that it's a good idea to confuse lerping with other kinds of interpolation, given how often the way that lerping works is misunderstood... The interpolation you're talking about changes the factor of the lerp based on the distance between the most recent lerp output and state B. That's totally a valid usage of lerping, but the problems in that come from the self-referential nature of taking the fraction of a distance (Zeno's paradox) and not from anything inherent in linear interpolation itself.
It's also pretty easy to fix the problem you're bringing up: just have a minimum value for your factor and the interpolation will definitely get to its goal point ;)
In any case, I was just trying to clear up some confusion of what I thought to be a problem of semantics, rather than of logic. Seems I made more confusion instead :(
@Dislekcia the problem wasn't really that it's hard to combine rotations and positions, it 2as just that unity lerp function didn't seem to take the value from one position to another in a specified time.
I found a pretty cool solution for the lerping in the end. I use physics for everything. So I just save the rigid body velocity and angular velocity each frame as well as the positions and rotations. Then I move the rigid body to the position and rotation of the current frame and set it's velocity and angular velocity to what it was at that time and let the physix take care of it until the next frame. It works pretty well at about 20fps. But I think I can get it to work at lower frame rates by doing tome clever interpolation of velocities. Also keeping everything in physics state the whole time means the replay system can double as a sort of layer able time traveling system.
So if you have a list of saved states that include time, then lerping between state 0 and 1, you need to do something like this:
I would be REALLY suspicious of a physics-based system like you've described doing well. It probably works okay at specific framerates, but if that changes drastically or differs from the rate it was recorded at, shit is going to go crazy alarmingly fast. Think about it this way: You've already done all that calculation once AND saved it, why risk doing it again when it might have different results?