Applying a physics equation in source engine and lua

So here is what I’m trying to do, and I never took physics so I’m starting to get lost with it.
I’m using ent:SetLocalVelocity() to launch the player from one point to another point higher up.
To figure out the velocity that is needed I asked a teacher of mine that teaches physics and he gave me this equation:

V[SUB]1[/SUB] is the velocity that I’m solving for.
h is how high the end point is compared to the start point.
x is how far away the end point is from the start point.
θ is the angle that the player is launched.
g is gravity but I’m not sure what it is for Garry’s Mod. (helpful link?)
Here is a diagram of the situation:

I have an entity that you place in a map and set the angle and the end point before you compile the map.
In game the entity’s position, the end position and the angle with be put in the formula.

I need help getting this formula into my code properly and figuring out how to calculate g for the formula.


Just noticed the Photoshop tag :downs:

I assume gravity in source is ~-600 units/s^2 or something like that. One issue I see is that the equation you are using requires the angle for launching it, something that can’t be determined as far as me and my 3 months of honors physics are aware.

I’ll try using 600, but first I have to figure out the expression for the root in lua first…
And you set the angle in the entity when you place it in the map, so it will just take some trial and error to get it right.

Well, I figured out an equation to do this in a specified amount of time so that you do not need an angle. The one downside is you need to ensure the time is set high enough that it doesn’t simply travel in a straight line or it may collide with any obstacles in the path. It also will work for any two points you could possibly give it, even points lower than the one before.
[lua]local g = -600 --gravity
function getvel(pos, pos2, time)
local diff = pos - pos2 --subtract the vectors

local velx = diff.x/time -- x velocity
local vely = diff.y/time -- y velocity

local velz = (diff.z - 0.5*g*(time^2))/time --  x = x0 + vt + 0.5at^2 conversion

return Vector(velx, vely, velz)


Yay for physics :smiley:

Thank you, it worked perfectly :smiley:

Use GetConVarNumber(‘sv_gravity’) instead of g so that it stays correct even if gravity is changed ingame.