OK, it means you have to reimplement what the lerp is doing for you automatically. A lerp is fundamentally a smooth adjustment of a value over time, and it comes down to the basic lerp equation: A + (B - A) * t, where t is a value 0 to 1. Each frame, you have to compute A, B, and t appropriately.
Here’s a sample program that demonstrates this in practice. See if you can understand how it works:
from direct.directbase.DirectStart import *
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from direct.showbase.PythonUtil import fitSrcAngle2Dest
import random
camera = loader.loadModel('camera.egg')
smiley = loader.loadModel('smiley.egg')
axis = loader.loadModel('zup-axis.egg')
axis2 = loader.loadModel('zup-axis.egg')
# Smiley can wander at random around a [-4 .. 4] cube.
smileyRange = 4
smileyTime = (0.5, 3)
def chooseSmileyPlace():
target = (random.uniform(-smileyRange, smileyRange),
random.uniform(-smileyRange, smileyRange),
random.uniform(-smileyRange, smileyRange))
moveTime = random.uniform(*smileyTime)
waitTime = random.uniform(*smileyTime)
ival = Sequence(smiley.posInterval(moveTime, target),
Wait(waitTime),
Func(chooseSmileyPlace))
ival.start()
smiley.reparentTo(render)
chooseSmileyPlace()
# Now a task to keep the "camera" model looking at smiley.
cameraSpeed = 10 # degrees per second
def rotateCamera(task):
# What direction are we facing now?
origHpr = camera.getHpr()
# What direction do we want to be facing?
axis.lookAt(smiley)
targetHpr = axis.getHpr()
# Make the rotation go the shortest way around.
origHpr = VBase3(fitSrcAngle2Dest(origHpr[0], targetHpr[0]),
fitSrcAngle2Dest(origHpr[1], targetHpr[1]),
fitSrcAngle2Dest(origHpr[2], targetHpr[2]))
# How far do we have to go from here?
delta = max(abs(targetHpr[0] - origHpr[0]),
abs(targetHpr[1] - origHpr[1]),
abs(targetHpr[2] - origHpr[2]))
if delta == 0:
# We're already looking at the target.
return task.cont
# Figure out how far we should rotate in this frame, based on the
# distance to go, and the speed we should move each frame.
t = globalClock.getDt() * cameraSpeed / delta
# If we reach the target, stop there.
t = min(t, 1.0)
# Now use simple componentwise lerp to rotate just a little bit
# from the current direction towards the target direction. This
# is the basic lerp equation, to compute a value on the line
# somewhere between origHpr and targetHpr. If t == 0, this
# computes origHpr; if t == 1, this computes targetHpr. If t is
# anywhere in between, it computes a point proportionately
# between.
newHpr = origHpr + (targetHpr - origHpr) * t
camera.setHpr(newHpr)
return task.cont
# The large, colored axis shows the direction the camera is
# trying to look.
axis.reparentTo(render)
# The small, gray axis shows the direction the camera is actually
# looking.
axis2.reparentTo(camera)
axis2.setScale(0.5)
axis2.setColor(0.8, 0.8, 0.8, 1)
camera.reparentTo(render)
taskMgr.add(rotateCamera, 'rotateCamera')
# Also, start the viewpoint a little bit back so we can see what's
# going on.
base.trackball.node().setPos(0, 20, 0)
run()