quaternion.lookdir

Edit

Returns a quaternion that looks in a direction. More accurately, it returns a quaternion that rotates from the forward vector (0, 0, -1) to a given direction.

The up vector is used to control the "roll" of the orientation, since there are an infinite number of orientations that look in a given direction.

q = quaternion.lookdir(direction, up)

Arguments

NameTypeDefaultDescription
directionvector The direction to look towards. It does not need to be normalized.
upnumbervector.up The up vector. It does not need to be normalized.

Returns

NameTypeDescription
qquaternion A quaternion that rotates from the forward vector to direction.

Example

This is very useful for rotating an object to face a target.

local origin = vector(x, y, z) -- the position of the object
local target = vector(tx, ty, tz) -- what it should look at

local rotation = quaternion.lookdir(target - origin)

pass:draw(model, origin, 1, rotation)

See also