Max acceleration/deceleration (can assume them to be equivalent
Max speed
Distance to travel
You want to get to the end as fast as possible and end with 0 velocity
Also I want it to look natural, not just an abrupt stop by losing max acceleration every time (this is the part I’m having trouble with)
How do you do this?
I feel like you’re supposed to use a spring for this but the issue is that it has to be based on speed and position (?) because you want it to maintain the max speed but you also don’t want to go past the target so maybe you have to use 2? lol
If you have a function for acceleration rather than just a maximum magnitude then that would clarify what you want the graph of velocity to look like.
Assuming a constant acceleration, the max acceleration, one case is that you accelerate up to top speed, continue at top speed for some time, then decelerate to 0 and stop at the precise distance. d = v*t + a*(v/a)^2 simplifies to t = d/v - v/a, where t is how much time is spent at top speed.
The other case is where the distance is so short that you couldn’t get to top speed and back. d = a*t^2 → t = sqrt(d/a), where t indicates how long you accelerate for before decelerating.
I don’t really know much about physics but I want to have a realistic acceleration
Would a constant acceleration be realistic? / what kind of function would be realistic?
Edit: btw if possible I would prefer an iterative formula that is based on previous values and delta time but I can probably work with a closed formula too
I would assume that with a top speed, acceleration would slow down before reaching that top speed, like an asymptote. Initial acceleration might also start slow. If you define a function for acceleration then
d = total distance, v = max velocity, a = acceleration function t = d/v + 2/v*(∫∫a)((∫a)^-1(v)) time spent at max velocity t = (∫∫a)^-1(d/2) time spent accelerating before decelerating
This requires calculating the inverse of the integral and double integral and inverse of the double integral of the acceleration function.
An iterative formula would turn everything into an approximation.
*Is there any parent acceleration function like there is for springs?
Also how would I plug in an initial velocity into the equations if a change is made in the middle?
In my first example the acceleration function is a = max acceleration. If you want something else that better suits whatever your case is then you will need to define some different function for acceleration and calculate the necessary relationships with distance and velocity.
If initial velocity is not 0 then time calculations need to be adjusted; the time needed to accelerate up to the initial velocity needs to be subtracted from the time needed to accelerate to max velocity.
local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace
local acceleration = 5
local maxvelocity = 32
local distance = 100
local part2 = part:Clone()
part2.CFrame = CFrame.new(distance, -.1, 0)
part2.Parent = workspace
wait(3)
local t, connection = 0
connection = game:GetService("RunService").Heartbeat:Connect(function(dt)
t = t + dt
local starttime = maxvelocity/acceleration -- time spent accelerating
if distance/maxvelocity < starttime then -- will not reach top speed
local halftime = math.sqrt(distance/acceleration) -- time spent accelerating
if t >= halftime*2 then
part.CFrame = CFrame.new(distance, 0, 0)
connection:Disconnect()
elseif t < halftime then
part.CFrame = CFrame.new(acceleration/2*t^2, 0, 0)
else
local position = distance/2 + acceleration*halftime*(t-halftime) - acceleration/2*(t-halftime)^2
part.CFrame = CFrame.new(position, 0, 0)
end
else
if t >= distance/maxvelocity + maxvelocity/acceleration then
part.CFrame = CFrame.new(distance, 0, 0)
connection:Disconnect()
elseif t < starttime then
part.CFrame = CFrame.new(acceleration/2*t^2, 0, 0)
else
part.CFrame = CFrame.new(maxvelocity*(t-starttime/2) - acceleration/2*math.max(0, t-distance/maxvelocity)^2, 0, 0)
end
end
end)
In the case where the max speed is not reached, speed is accelerated up to half the distance and decelerated for the other half. The second integral of acceleration with respect to time is the distance traveled; d = (1/2)*a*t^2, but since you also want to decelerate back down to 0, d = (1/2)*a*t^2 + (1/2)*a*t^2 or d = a*t^2, which means t = sqrt(d/a)
If you accelerate for a time of sqrt(d/a) then the distance traveled is (1/2)*a*(sqrt(d/a))^2 = d/2
The second equation of motion says that s = u*t + 1/2*a*t^2 so wouldn’t the entire distance traveled be (1/2)*a*t^2 and a*t^2 would be going double the distance?
if these are what the variables represent d = total distance to travel, t = how long to accelerate before decelarating (aka 1/2 * (time spent accelerating and decelerating since it is symetrical?)), and a = the max acceleration
then shouldn’t this be true?
d = 1/2*a*(2*t)^2
d = 2*a*t^2
t = sqrt(d/2/a)
If you accelerate for twice as long then you travel four times the distance. In your case, you accelerate and then decelerate at the same rate for the same amount of time over the same distance.
One more quick question, what’s the best way of handling direction? Do you just multiply what you “feel” should be multiplied or is there a systematic way? Should you take abs and sign, or should you use if statements?
If you mean like physically turning something or having some source of propulsion then there are more equations to involve. How something is supposed to happen depends on what it is supposed to be.