add lerp function to util and lerp method to vector3
This commit is contained in:
		@@ -216,6 +216,25 @@ end
 | 
				
			|||||||
function Vector3:copy()
 | 
					function Vector3:copy()
 | 
				
			||||||
  return Vector3(self)
 | 
					  return Vector3(self)
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					---Returns a new vector as a linear interpolation between self and b.
 | 
				
			||||||
 | 
					---@param b vectorlike
 | 
				
			||||||
 | 
					---@param t number
 | 
				
			||||||
 | 
					---@return Vector3
 | 
				
			||||||
 | 
					function Vector3:lerp(b, t)
 | 
				
			||||||
 | 
					  -- return self:copy()
 | 
				
			||||||
 | 
					  if not is_weak_vector3(b) then
 | 
				
			||||||
 | 
					    error("Vector3: b must be a Vector3-like table. Returning copy of self.")
 | 
				
			||||||
 | 
					    return self:copy()
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					  local w = Vector3(b)
 | 
				
			||||||
 | 
					  return Vector3{
 | 
				
			||||||
 | 
					    util.lerp(self.x, w.x, t),
 | 
				
			||||||
 | 
					    util.lerp(self.y, w.y, t),
 | 
				
			||||||
 | 
					    util.lerp(self.z, w.z, t),
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
---- CONSTANTS
 | 
					---- CONSTANTS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Vector3.UP = Vector3(0, 1, 0)
 | 
					Vector3.UP = Vector3(0, 1, 0)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -97,4 +97,24 @@ function util.list_insert_once(t, value)
 | 
				
			|||||||
  if util.list_find(t, value) ~= -1 then return end
 | 
					  if util.list_find(t, value) ~= -1 then return end
 | 
				
			||||||
  table.insert(t, value)
 | 
					  table.insert(t, value)
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					---linear interpolation
 | 
				
			||||||
 | 
					---@param a number
 | 
				
			||||||
 | 
					---@param b number
 | 
				
			||||||
 | 
					---@param t number in [0, 1]
 | 
				
			||||||
 | 
					---@return number
 | 
				
			||||||
 | 
					function util.lerp(a, b, t)
 | 
				
			||||||
 | 
					  return a + t * (b - a)
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					---constrains v between min and max
 | 
				
			||||||
 | 
					---@param v number
 | 
				
			||||||
 | 
					---@param min number
 | 
				
			||||||
 | 
					---@param max number
 | 
				
			||||||
 | 
					---@return number
 | 
				
			||||||
 | 
					function util.clamp(v, min, max)
 | 
				
			||||||
 | 
					  return math.max(math.min(v, max), min)
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
return util
 | 
					return util
 | 
				
			||||||
		Reference in New Issue
	
	Block a user