Hello! I wonder if its possible to calculate how much velocity you need for object to get closely to the certain point? (Xgreen = Start from | Xred = goal) (Check image below)
In the air, any velocity – assuming it doesn’t fall and hit the ground. Roblox physics is in a vacuum, so there’s no air resistance.
On the ground, it highly depends on a bunch of factors. Is the object sliding or rolling? Friction acts differently if sliding or rolling. Is the object starting from a resting position? More force is needed to overcome initial friction. The friction coefficients for both the moving object and the ground need to be considered too (i.e. the Friction and FrictionWeight properties under a part’s CurrentPhysicalProperties), as well as any slope to the ground (gravity starts affecting the force of friction on different slope angles).
In other words, it’s quite complex. I’m sure there’s math that helps approximate how much force is needed, but I’m not sure what it would be, especially because I’m not entirely sure how the friction coefficients affect the actual movement of the object. My suggestion would be to run some experiments and see how objects are affected.
I need for it to be in the air, something like catapult
Then you’re gonna want to solve the ballistics trajectory. This depends on your release angle too. Here’s an interesting website that might help: Trajectories
You’ll probably be interested in the Launch Velocity part of it.
Site isnt loading. Also I hope formula will be shown in there?
Do you have a fixed launch angle, and need to calculate the needed velocity?
Or do you have a fixed launch velocity, and need to calculate the desired launch angle?
I need to calculate velocity to reach the point from pos1 to pos2
Are your start and target positions always on the same Y position? Or could they be at different heights?
different, Start pos is projectile pos, Endpos is mouse pos
The link @sleitnick provided unfortunately won’t give you that equation directly. This wikipedia article, however, does, at the end of the “Kinematic quantities > Displacement” section.
Here’s that equation translated into lua:
-- Compute 2D launch speed.
-- a: launch angle
-- g: gravity (positive) e.g. 196.2
-- d: horizontal distance
-- h: vertical distance
local function LaunchSpeed(angle: number, g: number, d: number, h: number): number
local cosAngle = math.cos(angle)
return math.sqrt(d * d * g / (d * math.sin(2 * angle) - 2 * h * cosAngle * cosAngle))
end
To actually use it, you just need to figure out the horizontal and vertical distance between the points, then calculate the final velocity from the angle and direction:
-- Compute 3D launch direction
-- start: start position
-- target: target position
-- angle: launch angle
-- g: gravity (positive) e.g. 196.2
local function LaunchDirectionFromAngle(start: Vector3, target: Vector3, angle: number, g: number)
local horizontal = Vector3.new(target.X - start.X, 0, target.Z - start.Z)
local h = target.Y - start.Y
local d = horizontal.Magnitude
local v = LaunchSpeed(angle, g, d, h)
-- launch direction if we were launching at a flat angle:
local vec = horizontal.Unit * v
-- rotate around the axis perpendicular to that direction...
local rotAxis = Vector3.new(-horizontal.Z, 0, horizontal.X)
-- ...by the angle amount
return CFrame.fromAxisAngle(rotAxis, angle) * vec
end
For example:
local character = script.Parent
local root = character:WaitForChild("HumanoidRootPart")
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:Connect(function()
local dir = LaunchDirectionFromAngle(root.Position, mouse.Hit.Position, math.rad(45), workspace.Gravity)
local p = Instance.new("Part")
p.Size = Vector3.new(1, 1, 1)
p.CanCollide = false
p.Position = root.Position
p.Velocity = dir
p.Parent = workspace
end)
For the record, if you want the inverse (finding a launch angle when given a launch speed), you can see this post:
That may end up giving you a slightly nicer result with some tweaks. You could for instance have three fixed launch speeds, calculate the launch angle for each one, and choose the slowest launch speed that still reaches the target.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.