Visualizing Bezier Curves

I recently made a post about how to create curves and I got some useful things so I decided to code a simple path generation system with Bezier Curves but the problem is that I can’t make an accurate connection between these points in 3D

Example

ppp

The objects don’t fit correctly with their size and I don’t know why, here’s my script

local function QuadraticBezier(t,p0,p1,p2)
    return (1-t)^2*p0+2*(1-t)*t*p1+t^2*p2
end

local origin = workspace.Part1
local curve = workspace.Part3
local goal = workspace.Part2

local steps = 10

for i = 0, steps do
    local pos = QuadraticBezier(i/steps , origin.Position, curve.Position, goal.Position);
    local pointfront = QuadraticBezier((i+1)/steps, origin.Position, curve.Position, goal.Position) 

   -- pos is the current node and pointfront is the next node
    
    local mag = (pointfront - pos).Magnitude
    local cf = CFrame.lookAt(
        (pos + pointfront) / 2,
        pointfront
    )
    
    local line = Instance.new("Part", workspace)
    line.Anchored = true
    line.Size = Vector3.new(4, 1, mag)
    line.BrickColor = BrickColor.new("Really red")
    line.CFrame = cf
    line.TopSurface = Enum.SurfaceType.Smooth
    line.BottomSurface = Enum.SurfaceType.Smooth
end
6 Likes

If your talking about the gaps between parts, you can probably use cylinder parts between the parts to give it a fluid look.

I don’t know how get the position of the cylinders, I tried adding offset with “VectorToObjectSpace” into the current cframe of the part but didn’t get anything

You can offset the position using rightvector (or lookvector, depending on orientation), and I think your looking for toworldspace instead of vectortoobject space.

And that doesn’t seem to work, I did it manually on Studio but the gaps are still there

What code did you try? What was the result?

I didn’t write any code, I created the cylinder between the parts but the main problem is that the offset between the parts depends on the angle of the part, so I can’t get the cylinder position and it still looks weird

Judging from the image, the point where the two parts meet is where the cylinder needs to be. This is the middle side of the parts.

I want to get something like this
example1
There are no gaps and there are no cylinders but I don’t know how to do this with code

Cylinders would produce the same exact result with a lot less work. The image you sent was easy, because the angles between the parts remains an exact constant. Even then, it wouldn’t be perfect.

I already tried some minutes ago with cylinders but as I said I don’t know how to get the offset because the distance between bounds is not a constant

(Using different colors to check the real size of each part)

Yes, your totally right, but the exact position can be fetched by going to the right of a part by half it’s size to the right. Thats how you can get that position.

So either rightvector, or ToWorldSpace(part.Size.X/2)

This is the best I got because I don’t know too much about CFrames
loool

So I see your probably just adding the position. This works fine for the first few, as you can see, but as you turn the vector changes. Try doing ToWorldSpace(part.Size.X/2) (this might not be correct for your specific code)

Doesn’t seems to work properly, I already figured out how to set the cylinder position but there’s a very small offset on the part that makes it irregular

Can’t really help further without seeing the code your trying to do. Can I also have another picture of this “irregularity”?

This is the code that calculates the position from the path

    local pos = QuadraticBezier(i/steps , origin.Position, curve.Position, goal.Position);
    local pointfront = QuadraticBezier((i+1)/steps, origin.Position, curve.Position, goal.Position)
    
    local cf = CFrame.lookAt(
        (pos + pointfront) / 2,
        pointfront
    )
    
    local mag = (
        pos - pointfront
    ).Magnitude

and this is the picture of the irregularity

Lets do this my way. It looks like your using CFrame:LookAt(), which will produce an irregularity if they parts are angled enough, since their positions are not aligned. Try doing something along the lines of what i said earlier.

ToWorldSpace(part.Size.X/2)

I don’t get what exactly you mean with that CFrame function because I already implemented that

        cyl.CFrame = (
            cf + (
                cf:VectorToWorldSpace(
                    Vector3.new(0, 0, 
                        (mag/2)
                    )
                )
            )
        ) * CFrame.Angles(0, 0, math.rad(90))

Your not really listening to me, your using VectorToWorldSpace in that code. You said it didn’t work, right?