Angle between 2 points in a part

What do you want to achieve? I want a part to rotate with a parabole while it’s moving in the parabole

What solutions have you tried so far? I searched multiple posts but all of them are not what I’m looking for. (math.atan and math.atan2)

My script:

local part = Instance.new("Part", workspace)
part.Anchored = true
part.Position = Vector3.new(0, 0, -4)

while true do
	while part.Position.Z <= 4 do
		wait(.01)
		part.Position = Vector3.new(0, -0.25 * (part.Position.Z * part.Position.Z) + 10, part.Position.Z)
		part.Position = Vector3.new(0, part.Position.Y, (part.Position.Z + .1))
		vectorA = Vector3.new(part.Position.X, (part.Position.Z - (part.Size.Z / 2)) * -0.25 + 10, (part.Position.Z - part.Size.Z / 2))
		vectorB = Vector3.new(part.Position.X, (part.Position.Z + (part.Size.Z / 2)) * -0.25 + 10, (part.Position.Z + part.Size.Z / 2))
	end
	while part.Position.Z >= -4 do
		wait(.01)
		part.Position = Vector3.new(0, -0.25 * (part.Position.Z * part.Position.Z) + 10, part.Position.Z)
		part.Position = Vector3.new(0, part.Position.Y, (part.Position.Z - .1))
		vectorA = Vector3.new(part.Position.X, (part.Position.Z - (part.Size.Z / 2)) * -0.25 + 10, (part.Position.Z - part.Size.Z / 2))
		vectorB = Vector3.new(part.Position.X, (part.Position.Z + (part.Size.Z / 2)) * -0.25 + 10, (part.Position.Z + part.Size.Z / 2))
	end
end

The part will follow a path with the formula: -0.25 * (x * x) + 10. VectorA and VectorB are the middle of the left and right side of the part on the positions they should be to rotate with the parabole, that means if I have these 2 points I simply need to turn the difference into an angle. I just can’t seem to figure out how.

Hopefully you guys can help me out,
Mikquint

Can you draw what you mean by this? Probably you can use CFrame.lookAt to make a rotation looking from VectorA to VectorB.

Do you want the part to be rotated such that the direction of its lookvector has the same direction as the parabola has at the center of the part?

You can use the derivative (momentary rate of change of y-coordinate with respect to z-coordinate in this case) of the curve at the position to get a direction vector of the tangent drawn through the point on the parabola.

This vector can be used as the LookVector of the part. The right vector should probably be a normal vector of the plane in which the parabola is. The parabola is in the YZ-plane so a unit vector with the direction of the positive x-axis is one of its normal vectors. The UpVector can be calculated as the cross product of the look vector and the right vector.

The derivative of 0.25z^2 + 10 is 0.5z. You can convert the derivative to an angle using math.atan (the derivative is the tangent of the line direction angle) and calculate the direction vector from the angle using math.cos and math.sin.

local derivative = 0.5 * part.Position.Z
local angle = math.atan(derivative)
local lookVector = Vector3.new(0, math.sin(angle), math.cos(angle))
local rightVector = Vector3.xAxis
local upVector = rightVector:Cross(lookVector)
part.CFrame = CFrame.fromMatrix(part.Position, rightVector, upVector, lookVector)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.