How would i create a smooth, linear turn with only one point

I would like to turn an NPC, that follows a path (tower defence game). The angles are arbitrary angles, and I would like it to be able to turn smoothly on multiple different angle types (90, 45, 12 etc).

This is the image of the desired curve. As you can see there is one point in the middle and it is a smooth turn. Could i use lerp or any other math function or formula. REFERENCES:

VIDEO1
VIDEO2

EDIT


So far, I have this code that works like this:

CODE:

local RADIUS = 4

local part = Instance.new("Part")
part.Size = Vector3.new(1, 1, 1)
part.Anchored = true
part.Parent = workspace

local cOrigin = Vector3.new(5, 10, 5)

local currentAngle_RAD = 0

game:GetService("RunService").RenderStepped:Connect(function()
	if currentAngle_RAD >= math.rad(360) then
		currentAngle_RAD = 0
	end
	local x = cOrigin.X + RADIUS * math.cos(currentAngle_RAD)
	local z = cOrigin.Z + RADIUS * math.sin(currentAngle_RAD)
	part.Position = Vector3.new(x, cOrigin.Y, z)
	currentAngle_RAD += math.rad(3)
end)

VIDEO:

if anyone could change this code so it can work for 45 degree rotations / arbitrary angles that would be lovely!

EDIT


This is what bezier curves with 3 points would look like

4 Likes

You could use a Slerp, which is like a lerp for directions changing at constant speed. I have the code for it, the arguments work the same as Vector3 Lerp:

local function Slerp(a : Vector3, b : Vector3, alpha : number)
	local omega = math.acos(a:Dot(b))
	local so = math.sin(omega)
	
	local slerp1 = math.sin((1 - alpha) * omega) / so * a
	local slerp2 = math.sin(alpha * omega) / so * b
	
	return slerp1 + slerp2
end

it keeps returning as (nan, nan, nan) not a number
image

The input:

after a bit of debugging, its all due to local omega = math.acos(a:Dot(b)) Im pretty sure its due to the value of (a:Dot(b)) being too large

1 Like