Calculation to determine needed angle for hinge broken

I have this script, its purpose is to guide a car alongside a path of checkpoints, however the wheels do not turn where I need them to, video showcasing the issue. I am bad at math, the calculation that is currently being done is copied from a similar topic, so any mathematical replies that aren’t the corrected code I would not understand.

local checkpoints = game.Workspace.CarCheckpoints

local Force = 0
local MaxSteer = 15

local MaxSpeedKM = 50

local car = script.Parent.Parent
local seat = script.Parent

local leftMotor = car.FrontLeftWheel.LeftMotor
local rightMotor = car.FrontRightWheel.RightMotor

local leftServo = car.BackleftWheel.LeftServo
local rightServo = car.BackRightWheel.RightServo

local VF = car.Base.VectorForce
local VF2 = car.Base.VectorForce2

local targetCheckpoint = nil :: BasePart

local speedSet

function goToCheckpoint(checkpointName)
	print(checkpointName)
	targetCheckpoint = checkpoints:FindFirstChild(checkpointName)
	
	speedSet = seat.ThrottleFloat * Force
	
	targetCheckpoint.Touched:Connect(function(hit)
		if hit and hit.Parent == car then
			if checkpoints:FindFirstChild(checkpointName + 1) then
				goToCheckpoint(checkpointName + 1)
			else
				targetCheckpoint = nil
			end
			
		end
	end)
end

goToCheckpoint("1")



game["Run Service"].Heartbeat:Connect(function()
	
	if targetCheckpoint ~= nil then
		local checkpointPosition = targetCheckpoint.Position
		local seatPosition = seat.Position

		local deltaX = checkpointPosition.X - seatPosition.X
		local deltaZ = checkpointPosition.Z - seatPosition.Z
		
		local angle = math.deg(math.atan2(-deltaX, -deltaZ))
		
		print(angle)
		
		
		leftMotor.TargetAngle = angle 
		rightMotor.TargetAngle = -(angle)
		
		
		speedSet = 0.8 * Force

		local speed = math.max(math.abs(seat.AssemblyLinearVelocity.X), math.abs(seat.AssemblyLinearVelocity.Z))

		if speed > MaxSpeedKM then
			VF.Force = Vector3.new(speedSet-speedSet/3,0,0)
			VF2.Force = Vector3.new(speedSet-speedSet/3,0,0)
		else
			VF.Force = Vector3.new(speedSet,0,0)
			VF2.Force = Vector3.new(speedSet,0,0)
		end
	end
	
	
end)

to find 2 inner angles of the wheels for your car to aim at a checkpoint i made this drawing


this shows both of your wheels (represented by top 2 corners of the square) pointing at the checkpoint (which is also represented as a circle)

we would then get both side lines of the triangle using
let x2, y2 be the left (and right) side position (in vector2 space)

u = Vector2.new(x1 - x2, y1 - y2)
v = Vector2.new(x3 - x2, y3 - y2)

and then we get the dot product to determine how much they point in the same direction

dot = u.X * v.X + u.Y * v.Y

and then after that we calculate the length (magnitude) of u and v using Pythagorean’s theorem

magU = math.sqrt(u.X^2 + u.Y^2)
magV = math.sqrt(v.X^2 + v.Y^2)

after we got the dot product, the magnitudes all thats left is to determine the angle

angle = math.acos(dot/(magU * magV))

make sure you use only the x and z from the Vector3 and x2 and y2 should be the position of the angle you are measuring, hope this helps!

What do the variable you created represent, I don’t understand nearly enough about math to figure out what they are

I also made this code:

local checkpoints = game.Workspace.CarCheckpoints



local Force = 4000

local MaxSteer = 15



local MaxSpeedKM = 50



local car = script.Parent.Parent

local seat = script.Parent



local leftMotor = car.FrontLeftWheel.LeftMotor

local rightMotor = car.FrontRightWheel.RightMotor



local leftServo = car.BackleftWheel.LeftServo

local rightServo = car.BackRightWheel.RightServo



local VF = car.Base.VectorForce

local VF2 = car.Base.VectorForce2



local targetCheckpoint = nil :: BasePart



local speedSet



function goToCheckpoint(checkpointName)

	print(checkpointName)

	targetCheckpoint = checkpoints:FindFirstChild(checkpointName)



	speedSet = seat.ThrottleFloat * Force



	targetCheckpoint.Touched:Connect(function(hit)

		if hit and hit.Parent == car then

			if checkpoints:FindFirstChild(checkpointName + 1) then

				goToCheckpoint(checkpointName + 1)

			else

				targetCheckpoint = nil

			end



		end

	end)

end



goToCheckpoint("1")







game["Run Service"].Heartbeat:Connect(function()

	print(targetCheckpoint)

	if targetCheckpoint ~= nil then

		local checkpointPosition = targetCheckpoint.Position

		local seatPosition = seat.Position



		local hyp = math.sqrt((seatPosition.X - checkpointPosition.X)^2 + (seatPosition.Z - checkpointPosition.Z)^2)

		local sad = seatPosition.X - checkpointPosition.X


		local angle = math.deg(math.asin(sad/hyp))





		leftMotor.TargetAngle = angle

		rightMotor.TargetAngle = -(angle)





		speedSet = 0.8 * Force



		local speed = math.max(math.abs(seat.AssemblyLinearVelocity.X), math.abs(seat.AssemblyLinearVelocity.Z))



		if speed > MaxSpeedKM then

			VF.Force = Vector3.new(speedSet-speedSet/3,0,0)

			VF2.Force = Vector3.new(speedSet-speedSet/3,0,0)

		else

			VF.Force = Vector3.new(speedSet,0,0)

			VF2.Force = Vector3.new(speedSet,0,0)

		end

	end





end)

which when setting the force to 0 correctly orientates both wheels, but when setting the force to the indented amount the vehicle immediately starts spinning on the spot

x1 and y1 would be the wheel you are NOT getting the angle of, x2 and y2 would be the angle of the wheel you ARE getting the angle of, x3 and y3 would be your checkpoint position.

I made a script that correctly aims the wheels at the target but when i make the car move forwards it just spins in place and I cant figure out why

okay well that wasn’t the original problem and as you’ve given me no other information apart from “just spins in place” i cant help you :person_shrugging: