Diagonal Movement
Posted: Sun Mar 16, 2014 6:44 pm
Hey... I saw another thread on here sort of like this one but I can't use the solution posted in it because I can't figure it out...
I need to make it so my player has 8-direction movement, so that if I press both Up and Right the player would go up and to the right. I saw something on the other thread about using "dx" and "dy", but I don't know what to define those as and such. I think I would do this sort of like:
But I am bad at math so I dunno what to do to make it work. I am trying to use the Pythagorean Theorem here, because that makes sense to me. Any help would be appreciated!
I need to make it so my player has 8-direction movement, so that if I press both Up and Right the player would go up and to the right. I saw something on the other thread about using "dx" and "dy", but I don't know what to define those as and such. I think I would do this sort of like:
Code: Select all
function love.load()
playerimg = love.graphics.newImage("player.png")
enemyimg = love.graphics.newImage("enemy.png")
song1 = love.audio.newSource("song.mp3")
song1:setLooping(true)
playerx = 300
playery = 400
playerspeed = 250
end
function love.update(dt)
love.audio.play(song1)
if love.keyboard.isDown("right") then
playerx = playerx + (playerspeed * dt)
elseif love.keyboard.isDown("left") then
playerx = playerx - (playerspeed * dt)
elseif love.keyboard.isDown("down") then
playery = playery + (playerspeed * dt)
elseif love.keyboard.isDown("up") then
playery = playery - (playerspeed * dt)
end
if love.keyboard.isDown("up") and love.keyboard.isDown("right") then -- This is just one direction, I'd want it for all
playerx = playerx + (sqrt(playerx + playery) * dt)
playery = playery - (sqrt(playerx + playery) * dt)
end
end
function love.draw()
love.graphics.draw(enemyimg, 50, 50)
love.graphics.draw(playerimg, playerx, playery)
end