Generating Curves inside a ViewPortFrame

Basically I am trying to Render a Curve inside a viewportframe and currently its very ineffective, Slow and laggy. Basically Im trying to render an orbit Inside the ViewPortFrame and as well known there trajectory is curved Currently Im trying to approximate this curve by using heaps of parts


However I cannot define a inertial reference frame directly in roblox so if I try to render say an orbit around a moon an object already moving I have no choice but to constantly Reposition all the orbit parts 60 times a second for a smooth render and the larger the orbit the more parts I need to approximate the curve nicely obviously this method sucks and unfortunetly beams don’t work inside of ViewPortFrames so I cannot approximate the curve using cubic beziers. This is incredibly limiting because it eats up a huge amount of frame time and visually it also looks bad and this also limits the amount of spacecraft and moons, etc I can have in the system. here is the code

				local function drawEllipticalOrbit(BaryPosition, semiMajorAxis, eccentricity, inclination, longitudeOfAscendingNode, argumentOfPerihelion)
					local numPoints = 100
					local orbitPoints = {}

					-- Calculate the transformation matrix
					local m11, m12, m13, m21, m22, m23, m31, m32, m33 = 
						CalculatePerifocalToECIMatrix(inclination, longitudeOfAscendingNode, argumentOfPerihelion)

					
					for j = 1, numPoints do
						local angle = 2 * math.pi * j / numPoints



						local eccentricity = calculateEccentricity(e_vector)
						local semiMajorAxis = calculateSemiMajorAxis(r_mag, v_mag, mu)

						
						local distance = semiMajorAxis * (1 - eccentricity^2) / (1 + eccentricity * math.cos(angle))

						
						local x = distance * math.cos(angle)
						local y = distance * math.sin(angle)
						local z = 0

						
						local x3 = m11 * x + m12 * y + m13 * z
						local y3 = m21 * x + m22 * y + m23 * z
						local z3 = m31 * x + m32 * y + m33 * z

						
						local finalX, finalY, finalZ = PeriFocalToRoblox(-x3, y3, z3)
						local finalPosition = Vector3.new(finalX, finalY, finalZ) + InertialFrame
						table.insert(orbitPoints, finalPosition)
					end

					-- Create parts for each segment to connect points
					for i = 1, #orbitPoints do
						local pointA = orbitPoints[i]
						local pointB = orbitPoints[i % #orbitPoints + 1]

						
						local midPoint = (pointA + pointB) / 2
						local distance = (pointA - pointB).Magnitude

						
						local segment = Instance.new("Part")
						segment.Size = Vector3.new(10, 10, distance)
						segment.CFrame = CFrame.new(midPoint, pointB)
						segment.Transparency = 0.5
						segment.Color = Color3.fromRGB(92, 144, 255)
						segment.Anchored = true
						segment.Material = Enum.Material.Neon
						segment.Parent = ViewportFrame.Render
						segment.CanCollide = false
					end
				end

				-- Draw the orbit relative to the planet's position
				drawEllipticalOrbit(BaryPosition, semiMajorAxisStuds, eccentricity, inclination, longitudeOfAscendingNode, argumentOfPerihelion)

Basically it calculates the shape of the orbit from certain parameters I’ve calculated further up in the script from there we can calculate points along the orbit and join them together. Im wondering if anyone has any better ideas to draw a curve that isn’t so performance intensive.

This here is what happens if I crank up the part count too much roblox does some kinda script culling and it no longer refreshed at 60 times a second and you get this ugly mess

Maybe you should use Path2D instead. Path2D | Documentation - Roblox Creator Hub

Worked nicely was able to grab the 3D cords on the viewportframe and convert to 2D cords and position the path2D’s However its a little slow when I rotate the camera around other than that works quite nicely.

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