-
Notifications
You must be signed in to change notification settings - Fork 18
Vector 3
The vector3
(vec3) class is essential for representing and manipulating 3D coordinates, widely used in spatial calculations and graphical positioning.
vec3(x, y, z)
-
x
: Number - The x-coordinate of the vector. -
y
: Number - The y-coordinate of the vector. -
z
: Number - The z-coordinate of the vector.
vec3(other)
-
other
:vec3
- Another vec3 object to copy.
Note
These constructors allow for the creation of vec3 objects either from specific coordinates or as a copy of another vec3, facilitating 3D operations.
get_rotation()
-> vec3
Calculates and returns the rotation vector corresponding to the direction this vec3 is pointing.
normalize_rotation()
-> vec3
Normalizes the rotation component of this vec3, useful in rotational calculations.
normalize()
-> vec3
Normalizes this vec3, scaling it to a unit vector while maintaining direction.
-
x()
: Returns the x-coordinate (Float). -
y()
: Returns the y-coordinate (Float). -
z()
: Returns the z-coordinate (Float).
equals_ignore_z(other)
-> Boolean
-
other
:vec3
- The vector to compare with, ignoring Z coordinates.
Checks if this vec3 is equal to another vec3, disregarding the Z coordinate.
equals_ignore_z_threshold(other, threshold)
-> Boolean
-
other
:vec3
- The vector to compare with. -
threshold
: Number - The maximum allowed difference.
Evaluates if this vec3 is approximately equal to another vec3 within a specified threshold, ignoring Z coordinates.
intersects(segment_end, point, margin, radius, denominator)
-> Boolean
-
segment_end
:vec3
- The end point of the line segment. -
point
:vec3
- The point to check for intersection. -
margin
,radius
,denominator
: Numbers - Parameters for intersection calculation.
Determines if a line segment from this vec3 to
segment_end
intersects a point within given margins.
is_facing(other)
-> Boolean
-
other
:vec3
- The vector to check orientation against.
Returns true if this vec3 is oriented towards the specified vector.
to_string()
-> String
Converts this vec3 to a string representation, displaying its coordinates.
randomize_xz(margin)
-
margin
: Number - The range for randomization.
Randomizes the X and Z coordinates within the specified margin, altering the vector's position.
-
length_2d()
: Calculates the 2D length (ignoring Z coordinate). -
length_3d()
: Computes the full 3D length of the vector. -
length_3d_ignore_z()
: Determines the 3D length, excluding Z coordinate. -
length_3d_squared()
: Calculates the squared 3D length for performance efficiency.
-
dist_to(other)
: Measures the distance to another vec3. -
dist_to_ignore_z(other)
: Measures distance, ignoring Z coordinates. -
squared_dist_to(other)
: Calculates the squared distance for efficiency. -
squared_dist_to_ignore_z(other)
: Squared distance calculation, excluding Z.
Tip
These distance methods are useful for determining proximity between points in both 2D and 3D contexts, with options to consider or ignore the Z-axis.
rotate_around(origin, degree)
-> vec3
-
origin
:vec3
- The pivot point for rotation. -
degree
: Number - The angle in degrees for rotation.
Rotates this vec3 around a specified origin point by a given degree, altering its orientation.
project_2d()
-> vec2
Projects the vec3 onto a 2D plane, typically by ignoring the Y coordinate, and returns a vec2.
cross(other)
-> vec3
-
other
:vec3
- The vector to cross with.
Calculates the cross product with another vec3, yielding a vector perpendicular to both.
-
get_unit_vector()
: Returns the normalized unit vector. -
get_extended(target, units)
: Extends towards the target by a specified distance. -
get_perp_left(origin, factor)
: Calculates the left perpendicular vector. -
get_perp_right(origin, factor)
: Calculates the right perpendicular vector. -
lerp(target, coefficient)
: Linear interpolation towards another vec3. -
get_relative_angle(other)
: Angle relative to another vector. -
is_nan()
: Checks for NaN values. -
is_zero()
: Determines if the vector represents zero. -
get_angle(target, origin)
: Angle to a target from an origin. -
get_angle_side(target, origin)
: Angle including the side (left/right) relative to an origin.
Note
The vec3
class offers a comprehensive set of methods for 3D vector manipulation, pivotal for spatial calculations, movement, and graphical positioning in the Diablo Lua API.
Tip
Use get_player_position()
to get local player vec3 position from global_functions
Use actors_manager.get_enemy_npcs()
to get a list of enemies game_object from Actors Manager
Use vec3
:squared_dist_to_ignore_z
(vec3
) for performance
-- Example: Using vec3 in enemy proximity checks
local player_position = get_player_position();
local enemies = actors_manager.get_enemy_npcs();
for _, obj in ipairs(enemies) do
local enemy_position = obj:get_position();
-- note squared_dist_to_ignore_z for performance
local distance_sqr = enemy_position:squared_dist_to_ignore_z(player_position);
if distance_sqr > (2.0 * 2.0) then
-- Skip if the enemy is not in melee range
goto continue;
end;
-- Logic for close-range enemy interaction
::continue::
end;