My current code attempts to get the end of the barrel (which doesn't work quite right either) then throws the bullet outward with the angle of the barrel, but it always has a bias to the right.
Code: Select all
lastclick = 0
function mousepressed(x, y, button)
if button == love.mouse_left then -- and lastclick + 0.1 <= love.timer.getTime() then
lastclick = love.timer.getTime()
local sPosX, sPosY
local spawner
if x < 256 then spawner = LBarrelBody elseif x < 768 then spawner = MBarrelBody elseif x > 768 then spawner = RBarrelBody end
sPosX = spawner:getX() * math.cos(spawner:getAngle()) - spawner:getY() * math.sin(spawner:getAngle())
sPosY = spawner:getX() * math.sin(spawner:getAngle()) + spawner:getY() * math.cos(spawner:getAngle())
local x1, y1 = love.mouse.getPosition()
local x2, y2 = spawner:getPosition()
local dirx = math.atan2(y2 - y1, x2 - x1) * 180 / math.pi
local diry = math.atan2(y1 - y2, x1 - x2) * 180 / math.pi
if x1 <= x2 then x1 = x2 + 5 end
local t = {}
t.b = phys.newBody(world, spawner:getX() + (dirx / 11), spawner:getY() + (diry / 3), 1)
t.s = phys.newCircleShape(t.b, 5)
t.b:setBullet(true)
t.dirx = dirx
t.diry = diry
t.s:setData(t)
table.insert(bullets, t)
t.b:setVelocity(t.dirx / 3,t.diry * 2)
end
end
Can anyone suggest how to apply velocity to the bullets more accurately then this? Sort of like an object:ApplyVelocityForward(force) using the angle the object is at.