I am making a simple TDS (WASD or arrow keys to move, mouse to rotate, LMB to shoot) mostly just to learn more about LOVE and Lua and have fun, but I ran into a problem. How do I find the point on my players hand to attach a gun and shoot from? I get it to start there, but it strays off when I move my mouse to rotate the direction that the player is facing.
I wrote it first without using the physics module and couldn't figure it out so I rewrote it using the physics module (hoping joints would help me) but soon figured out that I have no idea how to use joints!
here is my code for it with the physics module:
Code: Select all
function love.load()
player = love.graphics.newImage("images/derp.png")
bg = love.graphics.newImage("images/bg.png")
reticule = love.graphics.newImage("images/reticule.png")
pistol = love.graphics.newImage("images/pistol.png")
love.physics.setMeter(64)
world = love.physics.newWorld(0, 0, true)
love.mouse.setVisible(false)
bullets = {}
bulletSpeed = 200
actors = {}
actors.player = {}
actors.player.body = love.physics.newBody(world, 100, 100, "dynamic")
actors.player.shape = love.physics.newCircleShape(16)
actors.player.fixture = love.physics.newFixture(actors.player.body, actors.player.shape)
weapons = {}
weapons.pistol = {}
weapons.pistol.body = love.physics.newBody(world, 200, 200, "dynamic")
weapons.pistol.shape = love.physics.newCircleShape(.5)
weapons.pistol.fixture = love.physics.newFixture(weapons.pistol.body, weapons.pistol.shape)
joint = love.physics.newWeldJoint( --joint attempt?
actors.player.body, weapons.pistol.body,
actors.player.body:getX(), actors.player.body:getY(),
0, 0, false
)
end
function love.mousepressed(x, y, button)
if button == "l" then
startX = actors.player.body:getX() -- where bullets start from, I have it at the center of the player right now
startY = actors.player.body:getY()
mouseX = x
mouseY = y
angle = math.atan2((mouseY - startY), (mouseX - startX))
bulletDx = bulletSpeed * math.cos(angle)
bulletDy = bulletSpeed * math.sin(angle)
table.insert(bullets, {x = startX, y = startY, dX = bulletDx, dY = bulletDy})
end
print(pistolX)
end
function love.update(dt)
if love.keyboard.isDown("left","a") == true then
print(actors.player.body:getX())
actors.player.body:setX(actors.player.body:getX() - 200*dt)
end
if love.keyboard.isDown("right","d") == true then
actors.player.body:setX(actors.player.body:getX() + 200*dt)
end
if love.keyboard.isDown("up","w") == true then
actors.player.body:setY(actors.player.body:getY() - 200*dt)
end
if love.keyboard.isDown("down","s") == true then
actors.player.body:setY(actors.player.body:getY() + 200*dt)
end
mX,mY = love.mouse.getPosition()
pRot = math.atan2((mY - actors.player.body:getY()), (mX - actors.player.body:getX()))
print(math.deg(pRot))
weapons.pistol.body:setX(actors.player.body:getX() + math.sin(pRot + math.rad(33.69)) * 28.84) -- attempt to find the point
weapons.pistol.body:setY(actors.player.body:getY() + math.sin(pRot + math.rad(56.31)) * 28.84)
for i,v in ipairs(bullets) do
v["x"] = v["x"] + (v["dX"] * dt)
v["y"] = v["y"] + (v["dY"] * dt)
end
end
function love.draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(bg, 0, 0)
love.graphics.draw(player, actors.player.body:getX(), actors.player.body:getY(), pRot, 2, 2, 16, 16)
love.graphics.draw(pistol, weapons.pistol.body:getX(), weapons.pistol.body:getY(), pRot, 2, 2)
love.graphics.draw(reticule, mX, mY)
love.graphics.setColor(64, 64, 64)
for i,v in ipairs(bullets) do
love.graphics.circle("fill", v["x"], v["y"], 2)
end
end
My pre-physics version is in the .love file if you want to see that too.
Thank you all so much in advance!