Hello, I like projectile physics. What this code is doing is basically creating a class that has a p (position) component, and v (velocity) component. Every heartbeat, the position is changed by the velocity*dt
which is the change in time since the last heartbeat. This is from the equation distance = rate*time
. The second thing it does is change the velocity with the acceleration, which is defined as gravity in this case. The change in velocity is measured by acceleration, so it basically multiplies the acceleration by the change in time to get the current velocity, which will be used the next time heartbeat is called to calculate the position. Finally it sets the CFrame of the part to equal the calculated position, but looking in the direction of the velocity vector.
While this works, I prefer to think of the projectile as the output of a continuous function.
function position(a, vi, xi, t)
return .5*a*t^2 + vi*t + xi
end
This is basically what that code was doing, but as a single operation. With this you can plug in the variables for acceleration, initial velocity, initial position, and time elapsed to get the position that a projectile will be along a parabola.
An example of a simulation would be:
local part = workspace.Part1
local heartbeat = game:GetService("RunService").Heartbeat
local acceleration = Vector3.new(0,-workspace.Gravity,0)
local initialVelocity = Vector3.new(100,100,100)
local initialPosition = part.Position
function position(a, vi, xi, t)
return .5*a*t^2 + vi*t + xi
end
local startTime = tick()
local elapsedTime = 0
while elapsedTime < 10 do
heartbeat:wait()
elapsedTime = tick()-startTime
part.CFrame = CFrame.new(position(acceleration, initialVelocity, initialPosition, elapsedTime))
end
This does pretty much what the previous code does, except the velocity is defined as 100,100,100, which means launch in the direction of (1,1,1) at a speed of the square root of 30,000 or something. Also the break case for the while loop is if the total elapsed time exceeds 10 seconds, rather than if it goes below 0 Y.