Hello developers!!!
In this publication I wanted to teach you a method of calculating trajectories, but not like the others which are from one point to another, but depending on two undefined numbers, the trajectory will be different and it does not depend on two points, but on these numbers and starting position, this method is most effective for bullet calculations! But first I would like to thank Ince_FS for helping me learn this!
Formula
The path formula is:
Origin + Initial * Time + g * Time * Time / 2
āOriginā, āInitialā and āgā are vectors, āOriginā is the vector where the trajectory begins, āInitialā would be like the direction and āgā would be gravity. And āTimeā is a number that keeps changing:
-For the vector āOriginā we put the initial position of the trajectory, for example, suppose that we want the trajectory to start in a part that is in Workspace and is called āStartā, because we will put:
For the vector āOriginā we put the initial position of the trajectory, for example, suppose that we want the trajectory to start in a part that exists in Workspace and is called āStartā, since the value would be:
workspace.Start.Position
-For the vector āInitialā we put the direction we want to go using the LookVector, UpVector and RightVector, to put them together we would use the sum, the 2 numbers that I mentioned at the beginning of the whole, they are used here multiplying these values, the larger the number that we multiply by, the bigger the path will be on the corresponding axis, for example, suppose we want it to go up and to the right, it would be:
Origin.CFrame.RightVector * 45 + Origin.CFrame.UpVector * 67
In case of not using the UpVector, the trajectory would fall without raising anything.
-For the vector āgā it would be simpler than the previous ones, this is simply gravity, this could be done with the X and Z axes at 0 and the Y axis, the gravity would be set to negative, that is:
Vector3.new (0, -Gravity, 0)
For those who do not know, there is a property in Workspace which is gravity, so the result would be:
Vector3.new (0, -workspace.Gravity, 0)
-And finally we have the number āTimeā, this number is undefined, it always starts at 0 and then it is added with the Heartbeat: Wait () and multiplied by the speed of the projectile, this would be done as follows:
local Velocity = .25
local Total = 0
while true do
Total += game:GetService("RunService").Heartbeat:Wait()*Velocity
end
Applying the formula in code
Once we know the formula and what each thing is, we can easily apply it to our code, first of all I recommend putting said formula with a return inside a function like this:
local function Position(Origin, Initial, g, Time)
return Origin + Initial * Time + g * Time * Time / 2
end
Once this is done, we would have to put a loop which positions the object in question in its corresponding position and is adding Time as explained above, it would look like this:
local Velocity = .25
local Total = 0
while true do
Total += game:GetService("RunService").Heartbeat:Wait()*Velocity
Part.Position = Position(Origin, Initial, g, Time)
end
And we would already have the code done! It would look like this:
local function Position(Origin, Initial, g, Time)
return Origin + Initial * Time + g * Time * Time / 2
end
local OriginPart = workspace:WaitForChild("Start")
local Gravity = Vector3.new(0, -workspace.Gravity, 0)
local GoTo = OriginPart.CFrame.LookVector * 46 + OriginPart.CFrame.UpVector * 65
local StartPosition = OriginPart.Position
local Velocity = .25
local Total = 0
while true do
Total += game:GetService("RunService").Heartbeat:Wait()*Velocity
Part.Position = Position(StartPosition, GoTo, Gravity, Total)
end
Not long ago I did a test of this in a game which I published, you can see for yourself the possible results, since you can modify how much it advances in the X axis and in the Y axis using the boxes below in the corners, Here you got the game!
https://www.roblox.com/games/4338276582/Physics?refPageId=8cd05f03-98ea-41c3-933c-bbc46bae94b1
I hope this helps you for a future project or it will serve you now, Good luck with your projects!