Player diagonal speed

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
victornecromancer
Prole
Posts: 1
Joined: Mon Jul 23, 2018 7:46 pm

Player diagonal speed

Post 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
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Player diagonal speed

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Duck Duckinson
Prole
Posts: 6
Joined: Sun Jul 15, 2018 1:23 pm

Re: Player diagonal speed

Post 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
User avatar
NotARaptor
Citizen
Posts: 59
Joined: Thu Feb 22, 2018 3:15 pm

Re: Player diagonal speed

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 83 guests