Calculation of trajectories in Roblox Studio

Hello developers!!! :smiley:
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!

ezgif-1-bf83e634f03b

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!

22 Likes

Pretty well tutorial,

You explained the math and all good, hope you make better stuff one day.

( but make sure to use GIFs, Medias and more so people can understand it better )

2 Likes

You should explain this properly. Right now, origin is an initial vector, the initial vector is another vector that apparently isnā€™t initialā€¦ And then you got a 3rd vector. There are many other examples of similar issues.

This tutorial was clearly meant for beginners, so it should be explained in a way that doesnā€™t require much additional thought. I somewhat understand what most of what you are saying, but not fully even after working with this kind of stuff on over 7 projects.

1 Like

thanks
very well explained very well all

1 Like