I'm trying to make GMod bots that fire at the player with a projectile weapon, which works perfectly fine, until you go a certain distance from the bot and it starts outputting nan instead of a valid angle
local pos = aimtarget:LocalToWorld(aimtarget:OBBMaxs())
local lPos = LocalPlayer():GetPos()
local vel = GetProjSpeed()
local time = 1.0
local dist = 1.0
local v = vel
local v2 = v*v
local v4 = v2*v2
local g = -800.0
local x = lPos:Distance(pos)
local x2 = x*x
local y = pos.z - lPos.z
local angle = 0.0
angle = math.atan2(v2 - math.sqrt(v4 - g * ( g * x2 + ( 2 * y ) * v2 )), g * x)
print(angle)
--LocalPlayer():SetEyeAngles((pos - LocalPlayer():GetShootPos()):Angle() - Angle(angle, 0, 0))
https://files.facepunch.com/forum/upload/436659/8f270ef4-e411-450a-b6f3-52ff9949e918/image.png
Besides just printing the thing that needs to work (angle), you should print out all the variables you used to calculate and investigate whether the math should work out on paper.
There's a big chance something is being divided by zero (impossible) or a value is otherwise causing an arithmetic error.
Seems a little over complicated to me. It should be simple as:
Direction = (Target - Source):GetNormalized()
The 'nan' occurs because there is no launch angle for which the projectile will hit the target. Specifically, this expression:
g * ( g * x2 + ( 2 * y ) * v2 )
Is evaluating to a negative number when the distance (x2) is sufficiently high, resulting in a negative value inside the square root function which is the cause of the 'nan' output.
You're running into this problem because the range of the projectile is limited by the launch velocity. Have a look at this gif, which illustrates the range of a projectile with a fixed launch velocity, but varying launch angle:
https://i.imgur.com/pBHnjCx.gif
Your options are either to have the bot move itself in range before it attempts to fire, or increase the launch velocity with distance.
Get this man a degree.
Will this also worth to be used for stuff like grenades? Like in MGSV?
Can you share with us how you made this gif?
I did not create the gif, it was retrieved from the wikipedia page for projectile motion.
If you are interesting in the parametric equation for a projectile's motion given its initial launch angle and velocity:
x(t) = V * cos(alpha) * t
y(t) = V * sin(alpha) * t - 0.5 * g * t^2
Where x(t) represents the displacement along the x-axis with respect to time, t, and y(t) the displacement along the y-axis. V is the initial launch velocity, and alpha is the launch angle measured from the x-axis. A similar animation could be reproduced using these two equations.
Or if your bot has multiple weapons, this would be a cool time to swap to a weapon that can fire at this distance (making your bot seem super smart, with a small feature)
I managed to figure this out, mostly the arc calculation, however at the moment it isn't taking into account the elevation of the bot and the enemy.
For anyone interested:
local height = newAim.z - Bot:GetShootPos().z
local target = math.sqrt(math.pow(AimSpot.y - Bot:GetShootPos().y, 2) + math.pow(AimSpot.x - Bot:GetShootPos().x, 2))
AimSpot.z = newAim.z + ss.GetDrop(height, target, ss.GetGravity(), ss.GetProjSpeed())
Functions used: GetDrop(height, target, projectilegravity, projectilespeed)
ss.GetDrop = function(height, target, grav, vel, dist)
local pitch = (math.atan2(height, target))
local velXY = vel * math.cos(pitch)
local Time = target / velXY
local TotalVertDrop = (0.5 * grav * Time * Time)
return TotalVertDrop
end
If you want to take into account elevation, you need to use the formula you originally used in the OP. It will be very difficult to accomplish that otherwise.
I noticed you had posted another thread where you had used a ballistics formula to calculate the launch angle to hit the target. Why did you opt for this instead?
Sorry, you need to Log In to post a reply to this thread.