I've tried doing a few different methods of trig and whatnot to find where I need to place the new object, but it just wont work.
What I'm currently trying is the only thing that gives me a close result
Code: Select all
function update(dt)
world:update(dt)
mPosX, mPosY = love.mouse.getPosition()
lbPosX, lbPosY = LBarrelBody:getPosition()
mbPosX, mbPosY = MBarrelBody:getPosition()
rbPosX, rbPosY = RBarrelBody:getPosition()
LBarrelBody:setAngle(math.deg(math.atan2(mPosY-lbPosY,mPosX-lbPosX)))
MBarrelBody:setAngle(math.deg(math.atan2(mPosY-mbPosY,mPosX-mbPosX)))
RBarrelBody:setAngle(math.deg(math.atan2(mPosY-rbPosY,mPosX-rbPosX)))
for k, v in ipairs(bullets) do
if v.b:getX() > 1024 or v.b:getX() < 0 or v.b:getY() < 0 then
table.remove(bullets, k)
end
end
end
lastclick = 0
function mousepressed(x1, y1, button)
if button == love.mouse_left or button == love.mouse_wheeldown or button == love.mouse_wheelup then --and lastclick + 0.1 <= love.timer.getTime() then
lastclick = love.timer.getTime()
--local spawner
if x1 < 384 then spawner = LBarrelBody elseif x1 < 640 then spawner = MBarrelBody elseif x1 > 768 then spawner = RBarrelBody end
if x1 < 384 then spawnerS = LBarrelShape elseif x1 < 640 then spawnerS = MBarrelShape elseif x1 > 768 then spawnerS = RBarrelShape end
local x2, y2 = spawner:getPosition()
local sPosX, sPosY = 512, 768
local sAngle = spawner:getAngle()
sPosX = x2 + math.cos(sAngle + 180) * 30 --The positions
sPosY = y2 + math.sin(sAngle + 180) * 30 --^ ^
local t = {}
t.b = phys.newBody(world, sPosX, sPosY, 1)
t.s = phys.newCircleShape(t.b, 5)
t.b:setBullet(true)
t.s:setData(t)
table.insert(bullets, t)
t.b:setVelocity(0,0)
end
end
Basic trig w/ the unit square says that the position x, y based on rotation theta around the unit square is cos(x),sin(y) - and that's what im doing, only having the center be at the correct guns base then multiplying the result to scale for distance. For some reason - I get some weird non related, non scalar angle doing it this way.