Lerp also known as linear interpolation is used to smoothen the objects' transformations. In this tutorial we'll be lerping the location and rotation.
Fire up the engine, we create a cube, leave it as it is, create a plane scale it to 5-10 and move the cube away from the plane to like 2-3 units. Now were going to write the code. We import our game engine module, define the controller and owner outside of the function which were wrapping it under the sensor which runs every frame.
try:
import bge
except:
import Range as bge
cont = bge.logic.getCurrentController()
own = cont.owner
Were going to copy the position and rotation of our plane and set it to our
cube. For this we're going to use localPosition and
localOrientation, which helps us to copy the
location and rotation.
Were going to access our cube whose name is Cube and
our plane whose name is Plane by default and put them under a function.
So the code below will be like this (not full code).
def main(cont):
cube = own.scene.objects["Cube"]
plane = own.scene.objects["Plane"]
Here we're accessing the name of "Cube" and "Plane" and define them as cube and plane repectively. then we set our cube's position equal to plane's position.
try:
import bge
except:
import Range as bge
cont = bge.logic.getCurrentController()
own = cont.owner
def main(cont):
cube = own.scene.objects["Cube"]
plane = own.scene.objects["Plane"]
cube.localPosition = plane.localPosition
Accessing everything in the scene with owner
You can access every objects in the scene using "owner" attribute like in the previous example code. With this, you can connect the sensor to any object and access other objects. There is no need to use bge.logic.getCurrentScene() list.
Before running the game set the cube's collision to No Collision, or the cube may spawn under the plane. Your cube must look something like this.
After setting the cube's position we set its rotation as:
cube.localOrientation = plane.localOrientation
To test if this is working, rotate the plane.
Now here comes the lerp method, unlike setting the cube's transformation equal to plane's transformation, we set the cube's transformation equal to itself, use the lerp function and then put the plane's transformation inside the function. Here is an example.
cube.localPosition = cube.localPosition.lerp(plane.localPosition, 0.1)
Now we comment (or delete) the line that copies the plane's position and write the above code.
#cube.localPosition = plane.localPosition
cube.localPosition = cube.localPosition.lerp(plane.localPosition, 0.1)
Now try running the game and your cube smoothly goes it assigned position. The above step applies to our cube's rotation too.
#cube.localOrientation = plane.localOrientation
cube.localOrientation = cube.localOrientation.lerp(plane.localOrientation, 0.1)
the value 0.1 after our plane's position is the
interpolation factor. It accepts between 0-1 as float. The greater the number, the faster
it goes to it's assigned position.
Now your cube's rotation must be smooth rn.
If you're still wondering how the code looks, here it is.
try:
import bge
except:
import Range as bge
cont = bge.logic.getCurrentController()
own = cont.owner
def main(cont):
cube = own.scene.objects["Cube"]
plane = own.scene.objects["Plane"]
#cube.localPosition = plane.localPosition
cube.localPosition = cube.localPosition.lerp(plane.localPosition, 0.1)
#cube.localOrientation = plane.localOrientation
cube.localOrientation = cube.localOrientation.lerp(plane.localOrientation, 0.1)