I’m making a car system in my game, where npc cars will be animated, driving around my city.
I have multiple points which I tween the cars between.
I haven’t run into any problems until I approach turning around corners realistically.
I read this post prompting the idea of using bezier curves, however I’m struggling on finding the midpoint of the curve.
I managed to calculate it using this:
local midPoint = Vector3.new(carPos.X,carHeight,turnToPos.Z)
But this only works at certain rotations:
I’ve also tried using CFrame:ToObjectSpace, but this didn’t make any difference.
That isn’t the midpoint, that’s the second (out of four) control point. The midpoint if obtained by taking the curve position when t = 0.5. Keep in mind that this is not the midpoint in terms or arc length, the the midpoint in terms of time.
I thought there would be a way to generate one since all curves are at 90 degrees. It would’ve been nice to not have to specify for every single corner in the city
You can generate the control points however you want but it kinda sounds like you want a Hermite spline instead, which lets you specify two endpoints and two velocities. The Continuity of Splines - YouTube
I have solved my own problem, through use of the following code: local function calculateBezierCurvePoint(carPos,carHeight,turnToPos,dist)
local midPoint = Vector3.new(carPos.X,carHeight,turnToPos.Z)
local bezierVector = midPoint - turnToPos
local bezierCurvePoint = turnToPos + (bezierVector.Unit*dist)
return bezierCurvePoint
end
local function calculateBezierCurve(carPos,carHeight,turnToPos,dist)
local bezierCurvePoint = calculateBezierCurvePoint(carPos,carHeight,turnToPos,dist)
local bezierCurve = TweenService:GetBezierWaypoints(carPos,bezierCurvePoint,turnToPos)
return bezierCurve