-1

I'm using some stuff I've put together from various methods around the internet to make this work yet my bullets fly off in seemingly random directions. I've tried throwing negative signs in front of stuff and switching up the trig but to no avail. I've tried using the rotation of the player's arm because that accurately points to the user's mouse but that failed to give me any more accuracy.

I've tried to determine if the bullets follow a pattern like how I needed to invert the Y variable for my arm, but I cannot find a pattern here.

local x, y = objects.PlayerArm:GetPos()
local bullet = createBullet( x + objects.Player:GetWide(), y )

local mX, mY = gui.MouseX(), gui.MouseY()
local shootAngle = math.atan2((mY - y), (mX - x))
shootAngle = math.deg( math.Clamp(shootAngle, -90, 90) )
--shootAngle = objects.PlayerArm.Rotation
    
bullet.VelocityX = math.cos(shootAngle) * 5 
bullet.VelocityY = math.sin(shootAngle) * 5
--bullet.Rotation = shootAngle
print("Angle", shootAngle, "Velocity X and Y", bullet.VelocityX, bullet.VelocityY)

Here is some of what was printed in console each time I shot a bullet.

Angle 47.920721521 Velocity X and Y -3.4948799788437 -3.5757256513158

Angle 24.928474135461 Velocity X and Y 4.8960495864893 -1.0142477244922

Angle 16.837472625676 Velocity X and Y -2.1355174970471 -4.5210137159497

Angle 10.684912400003 Velocity X and Y -1.5284445365972 -4.7606572338855

Angle -1.029154804306 Velocity X and Y 2.5777162320797 -4.2843178018061

Angle -11.63363399894 Velocity X and Y 2.978190104641 4.0162648942293

Angle -22.671343621981 Velocity X and Y -3.8872502993046 3.1447233758403

http://i.gyazo.com/e8ed605098a91bd450b10fda7d484975.png

Community
  • 1
  • 1
Exho
  • 113
  • 2
  • 9

1 Answers1

1

As @iamnotmaynard suspected, Lua uses C's math library and so all the trig functions use radians instead of degrees. It is best to store all angles in radians and just print them in degrees for a more friendly format. Otherwise you have a lot of conversions to and from radians every time an angle is used. Below is the code updated to only use radians and print in degrees.

local mX, mY = gui.MouseX(), gui.MouseY()
local shootAngle = math.atan2((mY - y), (mX - x))
shootAngle = math.max(-math.pi/2, math.min(shootAngle, math.pi/2))
bullet.VelocityX = math.cos(shootAngle) * 5
bullet.VelocityY = math.sin(shootAngle) * 5
print("Angle (radians)", shootAngle, "(degrees)", math.deg(shootAngle),
        "Velocity X and Y", bullet.VelocityX, bullet.VelocityY)

However to compute velocity in the x and y directions angles are not necessary at all. The function below computes VelocityX and VelocityY using only the displacements and makes sure that the velocity is only in the lower right and upper right quadrants as well.

function shoot(x, y, dirx, diry, vel)
   local dx = math.max(dirx - x, 0)
   local dy = diry - y
   local sdist = dx * dx + dy * dy
   if sdist > 0 then
     local m = vel / math.sqrt(sdist)
     return dx * m, dy * m
   end
end

bullet.VelocityX, bullet.VeclocityY = shoot(x, y, gui.MouseX(), gui.MouseY(), 5)
ryanpattison
  • 6,151
  • 1
  • 21
  • 28