Page 1 of 1

Player diagonal speed

Posted: Mon Jul 23, 2018 7:54 pm
by victornecromancer
Hello friends, how do I get my player to move diagonally?

Player movement code:

Code: Select all

function playerMovement() -- Contains the player movement logic

  if love.keyboard.isDown("w") then
  	Player.y = Player.y - Player.speed * deltaT
  end
  if love.keyboard.isDown("a") then
  	Player.x = Player.x - Player.speed * deltaT
  end
  if love.keyboard.isDown("s") then
	Player.y = Player.y + Player.speed * deltaT
  end
  if love.keyboard.isDown("d") then
	Player.x = Player.x + Player.speed * deltaT
  end
end
And the variables setup:

Code: Select all

function playerSetup() -- Contains the player and his fields

  Player         = {} -- Create a table to store the player fields
  Player.texture = love.graphics.newImage("assets/player.png") -- Import the texture of player and places it in a variable
  Player.x       = love.graphics.getWidth()/2-- Player X location is equal to the width of the windows divided by 2
  Player.y       = love.graphics.getHeight()/2 -- The same from above, but is the Player Y location
  Player.size    = 0.45 -- Player size (Scale factor)
  Player.speed   = 250 -- Player speed
  Player.r       = 0 -- Player radians
end

Re: Player diagonal speed

Posted: Mon Jul 23, 2018 11:03 pm
by zorg
Hi and welcome to the forums.

What you have shown should work already; but if the issue is that your player is faster along the diagonals than if you only moved on one axis, then you need a method to check for diagonal movement explicitly, and then divide Player.speed by math.sqrt(2).

This is also a known error, it even has a tvtropes page: https://tvtropes.org/pmwiki/pmwiki.php/ ... SpeedBoost

Re: Player diagonal speed

Posted: Thu Jul 26, 2018 2:53 am
by Duck Duckinson
The code is fine. Just make the speed adjustment in case both X and Y speeds are bigger than 0.

Rewriting:

function playerMovement() -- Contains the player movement logic

if love.keyboard.isDown("w") then
Player.y = Player.y - Player.speed * deltaT
end
if love.keyboard.isDown("a") then
Player.x = Player.x - Player.speed * deltaT
end
if love.keyboard.isDown("s") then
Player.y = Player.y + Player.speed * deltaT
end
if love.keyboard.isDown("d") then
Player.x = Player.x + Player.speed * deltaT
end
Player.x = (Player.y ~= 0) and (Player.x * 0.7071) or (Player.x)
Player.y = (Player.x ~= 0) and (Player.y * 0.7071) or (Player.y)

end

Re: Player diagonal speed

Posted: Fri Jul 27, 2018 9:01 am
by NotARaptor
Duck Duckinson wrote: Thu Jul 26, 2018 2:53 am [...]
Player.x = (Player.y ~= 0) and (Player.x * 0.7071) or (Player.x)
Player.y = (Player.x ~= 0) and (Player.y * 0.7071) or (Player.y)
[...]
Unless I'm misunderstanding this, in OP's original code Player.x and Player.y represented the position of the player, the code you've written there assumes they're the velocity.

I'd do it a bit like this (code written for ease of understanding; there are several micro-optimisations that could be done if required)

Code: Select all

-- (Assuming `deltaT` is a global variable)
function playerMovement() 
	local dx, dy = 0, 0
	if love.keyboard.isDown("w") then dy = -1 end
	if love.keyboard.isDown("a") then dx = -1 end
	if love.keyboard.isDown("s") then dx = 1 end
	if love.keyboard.isDown("d") then dy = 1 end
	if dx ~= 0 or dy ~= 0 then
		if dx ~= 0 and dy ~= 0 then
			dx = dx * 0.7071
			dy = dy * 0.7071
		end
		Player.x = Player.x + dx * Player.speed * deltaT
		Player.y = Player.y + dy * Player.speed * deltaT
	end
end