PointClickEngine::Utils::VectorMath
Mathematical utilities for 2D vector operations
Instance methods
Add two vectors
- a : First vector
- b : Second vector
total = VectorMath.add(position, offset)
Calculate the angle between two points in radians
Returns the angle from 'from' to 'to' in radians. Uses atan2 for proper quadrant handling.
- from : Starting position
- to : Target position
angle = VectorMath.angle_between(character_pos, target_pos)
character.rotation = angle
Convert angle to direction vector
Creates a unit vector pointing in the direction of the given angle.
- angle : Angle in radians
direction = VectorMath.angle_to_vector(Math::PI / 2) # Points up
Clamp a vector to maximum magnitude
If the vector's length exceeds max_length, it's scaled down to that length. Otherwise, the vector is returned unchanged.
- vector : Vector to clamp
- max_length : Maximum allowed length
velocity = VectorMath.clamp_magnitude(velocity, max_speed)
Calculate direction vector and distance between two points
Returns a tuple of {direction_vector, distance}. The direction vector is not normalized to preserve the original delta values.
- from : Starting position
- to : Target position
direction, distance = VectorMath.direction_and_distance(start, target)
Calculate distance between two points
Simple distance calculation using Raylib's optimized function.
- from : First point
- to : Second point
dist = VectorMath.distance(player_pos, target_pos)
Calculate squared distance between two points
More efficient than distance() when you only need to compare distances or when the exact distance value isn't needed.
- from : First point
- to : Second point
if VectorMath.distance_squared(pos1, pos2) < threshold_squared
# Points are close enough
end
Calculate dot product of two vectors
Useful for determining the angle between vectors or projections.
- a : First vector
- b : Second vector
dot = VectorMath.dot_product(forward, direction)
Check if two vectors are equal
- a : First vector
- b : Second vector
if VectorMath.equals?(pos1, pos2)
# Positions are the same
end
Get the length/magnitude of a vector
- vector : Vector to measure
speed = VectorMath.length(velocity)
Get the squared length of a vector
More efficient than length() when comparing magnitudes.
- vector : Vector to measure
if VectorMath.length_squared(velocity) > max_speed_squared
# Velocity is too high
end
Interpolate between two points
Linear interpolation between 'from' and 'to' by factor 't'. When t=0, returns 'from'. When t=1, returns 'to'.
- from : Starting position
- to : Target position
- t : Interpolation factor (0.0 to 1.0)
interpolated = VectorMath.lerp(start_pos, end_pos, 0.5) # Halfway point
Move a point towards a target by a specified distance
Moves 'from' towards 'to' by 'step' units. If the step is larger than the distance to target, returns the target position.
- from : Starting position
- to : Target position
- step : Maximum distance to move
new_pos = VectorMath.move_towards(current_pos, target_pos, speed * dt)
Negate a vector (reverse direction)
- vector : Vector to negate
opposite = VectorMath.negate(direction)
Normalize a vector
Returns a unit vector in the same direction. If the vector is zero, returns a zero vector.
- vector : The vector to normalize
unit_vector = VectorMath.normalize(velocity)
Normalize a vector given its length
More efficient than recalculating the length when it's already known. Returns a zero vector if the distance is zero or very small.
- vector : The vector to normalize
- length : The known length of the vector
direction, distance = VectorMath.direction_and_distance(start, target)
normalized = VectorMath.normalize_vector(direction, distance)
Calculate normalized direction vector between two points
Returns a unit vector pointing from 'from' to 'to'. If the points are the same (distance is zero), returns a zero vector.
- from : Starting position
- to : Target position
normalized = VectorMath.normalized_direction(start, target)
Check if a point is within a circle
- point : Point to test
- center : Center of the circle
- radius : Radius of the circle
if VectorMath.point_in_circle?(click_pos, character_pos, interaction_radius)
# Handle character interaction
end
Check if a point is within a rectangular area
- point : Point to test
- rect_pos : Top-left corner of rectangle
- rect_size : Width and height of rectangle
if VectorMath.point_in_rect?(mouse_pos, button_pos, button_size)
# Handle button click
end
Reflect a vector off a normal
- vector : Incoming vector
- normal : Surface normal (should be normalized)
bounce_dir = VectorMath.reflect(velocity, wall_normal)
Rotate a vector by an angle
- vector : Vector to rotate
- angle : Angle in radians
rotated = VectorMath.rotate(direction, Math::PI / 4)
Scale a vector by a scalar
- vector : Vector to scale
- scale : Scaling factor
scaled = VectorMath.scale(direction, speed)
Subtract two vectors
- a : First vector
- b : Second vector to subtract
difference = VectorMath.subtract(target, position)