angle
Rotation angles: x — pitch, y — yaw, z — roll, in degrees.
angle(pitch?: number, yaw?: number, roll?: number)#
Creates angles. Components that are not set are 0. angle(a) is a copy. The same as angle.new(...).
| Parameter | Type | Description |
|---|---|---|
pitch, yaw, roll |
number |
angles, default 0 |
Returns: angle.
local view = angle(10, 90, 0)
print(view:pitch(), " ", view:yaw()) --> 10 90
Operators#
| Expression | Result |
|---|---|
a + b, a - b |
component-wise |
a * n, n * a, a / n |
scale by a number — pitch and yaw only, roll does not change |
a * b, a / b |
component-wise |
-a |
angles with the opposite sign |
angle has no length operator #.
local a = angle(10, 20, 30) * 2
print(a) -- (20, 40, 30)
a:pitch(), a:yaw(), a:roll()#
Returns: number — the corresponding component.
local me = entity.local_pawn()
if me then print("Looking at pitch ", me:eye_angles():pitch()) end
a:unpack()#
Returns: number, number, number — x, y, z.
local p, y, r = angle(1, 2, 3):unpack()
a:init(...), src:copy(dst: angle)#
init re-initializes the angles in place (arguments as for the constructor). copy copies src into dst.
| Parameter | Type | Description |
|---|---|---|
dst |
angle |
where to copy |
Returns: nothing.
local a, b = angle(), angle()
a:init(0, 180, 0)
a:copy(b)
print(b) -- (0, 180, 0)
a:forward()#
A unit direction vector by pitch and yaw.
Returns: vector.
local me = entity.local_pawn()
if me then
local eye = me:eye_position()
local r = trace.ray(eye, eye + me:eye_angles():forward() * 8192, trace.filter(me))
print("Looking at ", r.hitpos)
end
a:vectors()#
Direction vectors taking roll into account.
Returns: vector, vector, vector — forward, right, up.
local fwd, right, up = angle(0, 90, 0):vectors()
print(fwd, " ", right, " ", up)
a:normalize()#
Brings each component to the range [-180, 180) in place.
Returns: nothing.
local a = angle(10, 370, 0)
a:normalize()
print(a) -- (10, 10, 0)
angle.normalize(x: number)#
Brings a number to the range [-180, 180).
| Parameter | Type | Description |
|---|---|---|
x |
number |
angle, degrees |
Returns: number.
print(angle.normalize(-190)) --> 170
a:normalized()#
Returns: angle — a normalized copy; the original angles do not change.
-- Yaw difference between the view and the direction to an enemy
local me = entity.local_pawn()
local enemy = entity.pawns(true)[1]
if me and enemy then
local need = (enemy:eye_position() - me:eye_position()):angles()
local diff = (need - me:eye_angles()):normalized()
print("Turn by ", math.floor(diff.y), "°")
end
a:length(), a:lengthsqr()#
The length as for a vector of three components and its square.
Returns: number.
print(angle(3, 4, 0):length()) --> 5
a:negate()#
Flips the sign of all components in place.
Returns: nothing.
local a = angle(10, -20, 0)
a:negate()
print(a) -- (-10, 20, 0)