weird behavior with hump camera and player rotation
Posted: Wed Oct 30, 2019 6:55 pm
Hey there new to the love2d scene. What I am trying to do is make the camera follow the player sprite based off the x and y values of player.body. What ends up happening with the code I've written below is the camera does indeed follow the players sprite. However. There is some weird behavior when the sprite rotates. the sprite no longer rotates according to the location of the mouse. Once the camera is implemented it tends to through off the player rotation for some reason. I don't need the camera to rotate to the players sprite, I just need it to follow the player based off movement.
I've attached a screen shot to get a better reference to whats going on. the white dot represents the mouse...
thank you guys!
I've attached a screen shot to get a better reference to whats going on. the white dot represents the mouse...
thank you guys!
Code: Select all
function love.load()
world = love.physics.newWorld(0, 0, false)
sprites = {}
sprites.player1 = love.graphics.newImage("sprites/Soldier1/soldier1_hold.png")
sprites.player2 = love.graphics.newImage("sprites/Soldier1/soldier1_gun.png")
sprites.weapon1 = love.graphics.newImage("sprites/weapon_gun.png")
require("player")
cameraFile = require('library/hump-master/camera')
cam = cameraFile()
bullets = {}
end
function love.update(dt)
for i, b in ipairs(bullets) do
b.x = b.x + math.cos(b.direction) * b.speed * dt
b.y = b.y + math.sin(b.direction) * b.speed * dt
end
cam:lookAt(player.body:getX(), player.body:getY())
world:update(dt)
playerUpdate(dt)
end
function love.draw()
cam:attach()
if player.hasWeapon == false then
love.graphics.draw(sprites.player1, player.body:getX(), player.body:getY(), playerRotation(), nil, nil, sprites.player1:getWidth()/2, sprites.player1:getHeight()/2)
elseif player.hasWeapon == true then
love.graphics.draw(sprites.player2, player.body:getX(), player.body:getY(), playerRotation(), nil, nil, sprites.player2:getWidth()/2, sprites.player2:getHeight()/2)
end
for i, b in ipairs(bullets) do
love.graphics.draw(sprites.weapon1, b.x, b.y, nil, 0.25, 0.5, sprites.weapon1:getWidth()/2, sprites.weapon1:getHeight()/2)
end
cam:detach()
end
function spawnBullet()
bullet = {}
bullet.x = player.body:getX()
bullet.y = player.body:getY()
bullet.direction = playerRotation()
bullet.speed = 500
bullet.dead = false
table.insert(bullets, bullet)
end
function playerRotation()
return math.atan2(player.body:getY() - love.mouse.getY(), player.body:getX() - love.mouse.getX()) + math.pi
end