Vector
Mutable 3D vector in world units; field writes change it, operators and methods return new Vectors.
Methods
Vector
Vector([x [, y [, z]]]) -> Vector| Name | Type | Description |
|---|---|---|
| xoptional | number | Default 0. |
| yoptional | number | Default 0. |
| zoptional | number | Default 0. |
New Vector; missing components are 0.
Non-number, non-nil arguments raise 'number expected'.
vec.x
vec.x -> numberRead and write.
Writing fields other than x, y, z, or non-numbers, raises.
vec.y
vec.y -> numberRead and write.
vec.z
vec.z -> numberUp axis. Read and write.
vec:length
vec:length() -> number3D length in world units.
vec:length2d
vec:length2d() -> numberLength ignoring Z, like horizontal speed.
vec:dot
vec:dot(other) -> number| Name | Type |
|---|---|
| other | Vector |
Dot product.
vec:distance
vec:distance(other) -> number| Name | Type |
|---|---|
| other | Vector |
3D distance to other, in world units.
vec:normalized
vec:normalized() -> VectorUnit-length copy; vec is unchanged.
vec:cross
vec:cross(other) -> Vector| Name | Type |
|---|---|
| other | Vector |
Cross product, perpendicular to both.
vec:unpack
vec:unpack() -> number, number, numberComponents as three values.
Returns x, y, z
vec:angle_between
vec:angle_between(other) -> number| Name | Type |
|---|---|
| other | Vector |
Angle between two directions, 0 to 180 degrees.
Returns 90 if either vector is zero.
vec:lerp
vec:lerp(to, t) -> Vector| Name | Type | Description |
|---|---|---|
| to | Vector | |
| t | number | 0 is vec, 1 is to. |
Linear blend: vec + (to - vec) * t.
t is not clamped.
vec:to_angles
vec:to_angles() -> AngleDirection to view Angle; (target - eye):to_angles() aims at a point.
Returns Roll is always 0
vec:to_screen
vec:to_screen() -> number, number | nilProjects this world position to the screen.
Returns x, y in pixels, or nil
Single nil when not projectable, like behind the camera.
vec:rotate_yaw
vec:rotate_yaw(degrees) -> Vector| Name | Type | Description |
|---|---|---|
| degrees | number | Positive is counter-clockwise from above. |
Rotated copy around Z; Z is unchanged.
a + b
Vector + Vector -> VectorPer-component sum of two Vectors.
Adding a number or Angle raises 'Vector expected'.
a - b
Vector - Vector -> VectorPer-component difference of two Vectors.
a * b
Vector * number | number * Vector | Vector * Vector -> VectorScales by a number; Vector * Vector is per-component, not dot.
a / b
Vector / number -> VectorDivides each component by a number.
Dividing by 0 gives inf or NaN, no error.
-a
-Vector -> VectorNegates each component.
a == b
Vector == Vector -> booleanExact match on all components, no tolerance.
tostring(vec)
tostring(Vector) -> stringReadable form, also used by print().
Returns Like "Vector(1.0, 2.5, 0.0)"