Need help with CFrames and orientation

Hello all,

I am trying to create a road plugin which is able to create and position road segments. One of the key features of the plugin is it is able to create sloped curves without the prominent tilt that you would get from plugins such as Archimedes 2.

I have managed to create the functions for creating curves and placing straight segments, but when the plugin creates slopes on multiple axes (so if I slope the road segment and also curve it), it still creates the tilting effect, and the orientation of the segment of the X Axis is a decimal number, which it shouldn’t be.

Here is the angle I expected:
Expected outcome

Here is the actual outcome:
Actual outcome

This can be problematic because if the orientation of one segment is a little bit off, it can create some weird angles in future segments:
wierdAngle

I need to be able to round the X value on the segment to 0 in order to ensure that the road segment is level. I have searched the Developer Forum to find articles which could help me accomplish this, but I couldn’t find anything

Here is the code for the sloping tool:

local function slopeSegment(direction)
	if direction == "Up" then
		local currentPivot = newRoadSegment:GetPivot()
		if currentPivot ~= nil and newRoadSegment.PrimaryPart then
			local targetAngle = CFrame.Angles(0, 0, -math.rad(slopeIncrement))
			local pivot = currentPivot * targetAngle
			newRoadSegment:PivotTo(pivot)
			print(newRoadSegment:GetPivot())
		end
	elseif direction == "Down" then
		local currentPivot = newRoadSegment:GetPivot()
		if currentPivot ~= nil and newRoadSegment.PrimaryPart then
			local targetAngle = CFrame.Angles(0, 0, math.rad(slopeIncrement))
			local pivot = currentPivot * targetAngle
			newRoadSegment:PivotTo(pivot)
			print(newRoadSegment:GetPivot())
		end
	end
	GUI.Controls.Info.Slope:FindFirstChild("Number").Text = newRoadSegment.Road.Orientation.Z
end

Any help on this would be much appreciated.

What is the pivot orientation for the road segment? GetPivot() returns a CFrame value.

I assume you are talking about the World Pivot Orientation property. The World Pivot Orientation property is the same as the Origin Orientation property.
pivotOrientation

I was talking about the original road model that’s being cloned, and how it might be tilted a little

The original model’s World Pivot Orientation is 0, 90, 0.
OriginalModel

I’m not sure why it rotates slightly on the X axis but if you wanted to round the orientation of the X axis to 0 you could use the ToOrientation() method and multiply the pivot by the opposite of the X orientation. (Don’t use math.rad, ToOrientation returns in radians)

local x,y,z = currentPivot:ToOrientation()
newRoadSegment:PivotTo(newRoadSegment:GetPivot() * CFrame.Angles(-x,0,0))

Now that I think of it, are you talking about the newRoadSegment model? Because that model’s Pivot Offset Orientation is 0, 0, 0.

newRoadSegmentCFrame

The Origin Orientation of it’s PrimaryPart is 0.02, 91, -1.

RearEndpointProperties

newRoadSegment is a guide model, it is a visual indicator of where the road is going to go, and when the new segment is actually placed, newRoadSegmenr