3D [Slow Follow] Interpolating Camera Targeting!!! Greater than built-in 3D InterpolationCamera

1000h1000h Posts: 77Member
edited November 2016 in Resources

3D [Slow Follow] Interpolating Camera Targeting!!! More practical than built in InterpolationCamera Class!!!

Hello!! I have been able to do a lot of cool things in godot which I am thankful for, and this came together pretty nicely. Before I post the code here is the source I used for reference and knowledge, where it explains how the look_at function works.


The code that follows is what's called linear interpolating between an original orientation and a target orientation over time, so that the camera 'slowly works towards/ follows' its target orientation.

I made this because the build-in InterpolateCamera class seems to only have functions which interpolate its position! I couldn't believe it when I realized that was the case. Anyway, this is for nice smooth camera motion in 3D!

func linear_interpolate_rot():

    # i have this script on my camera which is NOT my parent node in the hierarchy
    # my parent in the hierarchy of ndoes is a spatial. that is why this is the following get_node().get() sequence

    #this interpo variable should actually be called transform something or other.
    #it stores a transform which will hold the target translation of the interpolation
    #its technically just going to be used for the translation value, which is the index '[3]'
    #i.e. the transform's first 3 indices 0,1,2 are for matrix vectors, and the 3 is for the position vector
    var interpo = get_node('/root/Spatial').get('interpolate')

    #because this starts on ready in my case, i have this conditional to make sure it don't run before
    #interpo is set to anything
    if interpo != null:

        #this dir variable stands for direction. it gets normalized below.
        var dir = (get_transform()[3] - interpo[3])

        #the following x y and z variables are vectors to be used as in the basic look_at function with y as up. 
        #these are the target directions
        var z = dir.normalized()
        var x = Vector3(0,1,0).cross(z)
        var y = z.cross(x)

        #the following variable names stand for interpolate + axis. 
        #these lines interpolate between target vectors as above and original rotation vectors
        #this function is run in _process so the following lines update every frame

        #the float .1 represents time, with smaller floats causing slower interpolation
        var intz = get_transform()[2] + .1 * z
        var inty = get_transform()[1] + .1 * y
        var intx = get_transform()[0] + .1 * x

        var trans = Transform(Matrix3(intx.normalized(),inty.normalized(),intz.normalized()),get_translation())

        #the folllowing line sets the camera (of which this script is attached) to the transform we made!
        #for testing you can see that these two values get closer and closer
        print(trans[0].normalized(), 'trans 0',interpo[0],' interpo0')

Tags :
Sign In or Register to comment.