# Calculating Drop Speeds

I am trying to create a script that draws a small box on the client’s screen where to throw a knife to hit a predetermined target, but I have no Idea how to calculate the drop amount. If it would be possible could someone explain how to calculate this ?

Assuming you made a swep with the code or something similar below, just draw the box from the hit position and instead of subtracting it, add it. and draw the box there.
**
local Drop = tr.HitPos - Vector( tr.HitPos.x, tr.HitPos.y, 15);
**

I don’t exactly know what you mean so

You are going to have to calculate trajectory.

Relevant code ( entirely clientside ) from the weapon:

``````
sv_gravity = GetConVar( "sv_gravity" )

SWEP.BoxColor = Color( 000, 000, 000, 180 )
SWEP.TextColor = Color( 255, 128, 000, 255 )

SWEP.Numbers = { }

SWEP.TargetPos = nil
SWEP.BigPitch = nil
SWEP.LowPitch = nil

SWEP.State = "ASST OFF"

function SWEP:GetGravAccel( )
--I was going to do the fancy form of getting the real gravitational acceleration here
--So here's the ultimate guess!

return ( sv_gravity:GetInt( ) / 600 ) * ( sv_gravity:GetInt( ) / -16 )
end

function SWEP:CalculateTrajectory( )
local theta, g, d, v, tr, a, b, x, y, k, v, v2, v4, x2, p, q, n

tr = util.TraceLine{ start = self.Owner:EyePos( ), endpos = self.Owner:EyePos( ) + self.Owner:GetAimVector( ) * 8192, filter = self.Owner, mask = MASK_SHOT }

a = tr.HitPos
b = self.Owner:EyePos( )

a = a - b

y = math.abs( a.z )

a.z = 0

d = a:Length( )

g = self:GetGravAccel( )

v = 500  --velocity of entity

v2 = v * v
v4 = v2 * v2
x = d
x2 = x * x

k = math.atan( ( ( v2 + ( v4 - g * ( g * x2 + 2 * y * v2 ) ) ^ .5 ) / ( g * x ) ) )
v = math.atan( ( ( v2 - ( v4 - g * ( g * x2 + 2 * y * v2 ) ) ^ .5 ) / ( g * x ) ) )

if math.min( k, v ) == math.huge then
--It isn't possible to hit it from here, according to this math
self.State = "ASST ERR"
self.TargetPos = nil
self:EmitSound( self.Sounds.Error, 30 )
return
end

k = 90 + math.deg( k )
v = 90 + math.deg( v )

p = tr.HitPos:ToScreen( )
q = self.Owner:EyeAngles( )

n = q.p

q.p = n - math.min( k, v )

self.LowPitch = q:Forward( ) * v * .5

q.p = n - math.max( k, v )

self.BigPitch = q:Forward( ) * v * .5

self.TargetPos = tr.HitPos

self.YCoord = ( self.Owner:GetShootPos( ) + q:Forward( ) * 6 ):ToScreen( ).y
self.State = "ASST ON"

self:EmitSound( self.Sounds.LockedOn, 30 )
end

function SWEP:DrawHUD( )
self:OwnerChanged( )
local x, y, w, h, r, n, i, s, a, b, p, q, z, pos

r, n = surface.GetTextSize( " " )

w = ( 4 + 8 + 2 ) * r
h = n * 6

x = surface.ScreenWidth( ) - ( w + r )
y = surface.ScreenHeight( ) * .5 - ( h + n * 2 ) * .5

draw.RoundedBox( 1, x, y, w + r, h + n, self.BoxColor )

y = y
x = x

self.LastUpdate = self.LastUpdate or CurTime( )
self.LastPos = self.LastPos or LocalPlayer( ):GetPos( )

pos = LocalPlayer( ):GetPos( )

if pos:Distance( self.LastPos ) > 2 and self.TargetPos then
--We've moved, probably not accurate anymore
self.State = "ASST ERR"
self.TargetPos = nil
self:EmitSound( self.Sounds.Error, 30 )
end

self.TargetPos = nil
self.State = "LOCKED"
end

self.LastPos = pos

for i = 1, 6 do
s = "&x%02x %04x %04x"

a = math.random( 65535 )
b = math.random( 65535 )

a = tostring( a ):sub( 1, 4 )
b = tostring( b ):sub( 1, 4 )

a = 65535
b = 65535
end

if not self.Numbers[ i ] then
self.Numbers[ i ] = { a, b }
end

if CurTime( ) > self.LastUpdate + .15 then
self.Numbers[ i ][ 1 ] = a
self.Numbers[ i ][ 2 ] = b
end

s = s:format( i, self.Numbers[ i ][ 1 ], self.Numbers[ i ][ 2 ] ):upper( )

draw.DrawText( s, "Grenade_Computer_Screen", x, y, self.TextColor, TEXT_ALIGN_LEFT )

y = y + n
end

draw.DrawText( "&X07 " .. self.State, "Grenade_Computer_Screen", x, y, self.TextColor, TEXT_ALIGN_LEFT )

if CurTime( ) > self.LastUpdate + .15 then
self.LastUpdate = CurTime( )
end

surface.SetDrawColor( 189, 000, 189, 120 )

if self.TargetPos then
p = self.TargetPos:ToScreen( )
q = self.Owner:EyePos( )

z = ( q + self.LowPitch ):ToScreen( )

surface.DrawRect( p.x - ScrW( ) * .0125, z.y - ScrH( ) * .0125, ScrW( ) * .025, ScrH( ) * .025 )

z = ( q + self.BigPitch ):ToScreen( )

surface.SetDrawColor( 000, 189, 189, 120 )

surface.DrawRect( p.x - ScrW( ) * .0125, z.y - ScrH( ) * .0125, ScrW( ) * .025, ScrH( ) * .025 )
end

surface.SetDrawColor( 255, 000, 000, 150 )

x = ScrW( ) * .5 - 1
y = ScrH( ) * .5 - 1
w = ScrW( ) * .0125 * .5
h = ScrH( ) * .0125 * .5

surface.DrawRect( x - w * .5, y - h * .5, w, h )
end

``````

This code is from nearly two years ago, so it might be in need of some fine tuning or adjustment, but the video shows it works.