# Move and rotate an object according to travel distance and direction between objects

I need to rotate the target object an equivalent distance to the distance between two objects in the direction of the travel distance between them.

I've tried to subtract them and get the rotation_difference between the subtraction result and a zero rotation axis mathutils.Quaternion() but the result is different from what I need.

import bpy
import mathutils

selected_object = bpy.context.object
car_f1_location = bpy.data.objects['car_f1'].location.copy()
car_f2_location = bpy.data.objects['car_f2'].location.copy()

direction_vec = car_f2_location - car_f1_location
direction = direction_vec.rotation_difference( mathutils.Quaternion() )

selected_object.rotation_quaternion = direction


But the resulting rotation is different from what I want.

• Nice use of grease pencil in the screenshots :) – Moog Nov 5 '19 at 17:48

I figured out that I needed to use to_track_quat() instead of rotation_difference() to get the direction along this vector.

import bpy
import mathutils

selected_object = bpy.context.object
car_f1_location = bpy.data.objects['car_f1'].location.copy()
car_f2_location = bpy.data.objects['car_f2'].location.copy()

direction_vec = car_f2_location - car_f1_location
direction = direction_vec.to_track_quat('Y', 'Z')

selected_object.rotation_quaternion = direction