help with movement

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
eqnox
Prole
Posts: 44
Joined: Fri Jul 31, 2015 2:36 pm

help with movement

Post by eqnox »

So I was working on a bit of code for player movement and I wanted to add the option to be able to hold shift to sprint

Code: Select all

function love.update()
  if love.keyboard.isDown("w") then
    py = py - 1
  end
  if love.keyboard.isDown("s") then
    py = py + 1
  end
  if love.keyboard.isDown("a") then
    px = px - 1
  end
  if love.keyboard.isDown("d") then
    px = px + 1
  end
  if love.keyboard.isDown("lshift" and "w") then
    py = py - 10
    elseif love.keyboard.isDown("lshift" and "s") then
      py = py + 10
  end
  if love.keyboard.isDown("lshift" and "a") then
    px = px - 10
    elseif love.keyboard.isDown("lshift" and "d") then
      px = px + 10
  end
This results in the player always moving at sprint speeds.

Code: Select all

ws = false
ss = false
as = false
ds = false

function love.update()
  while(ws == true) do
    py = py - 10
  end
  while(ss == true) do 
    py = py + 10
  end
  while(as == true) do 
    px = px - 10
  end
  while(ds == true) do
    px = px + 10
  end
 end
 
 function love.keypressed(key)
  if key == "right" then
    if key == down then
    px = px + 3
    end
  end
  if key == "lshift" and key == "s" then
    ss = true
  end
  if key == "lshift" and key == "a" then
    as = true
  end
  if key == "lshift" and key == "d" then
    ds = true
  end
 end
 
 function love.keyreleased(key)
  
    if key == "lshift" and "w" then
      ws = false
    end
    if key == "lshift" and "s" then
      ss = false
    end
    if key == "lshift" and "a" then
      as = false
    end
    if key == "lshift" and "d" then
      ds = false
    end
  end
 
I have also tried something like this and it doesn't seem to work either. I think I might be doing something wrong. Over all im out of ways I know to fix it or at least ones that I have thought of.
User avatar
Sulunia
Party member
Posts: 203
Joined: Tue Mar 22, 2016 1:10 pm
Location: SRS, Brazil

Re: help with movement

Post by Sulunia »

Did you try checking if shift was pressed first before checking what direction the player wants to move? Like this:

Code: Select all

if love.keyboard.isDown("w") then
	if love.keyboard.isDown("lshift") then
		py = py - 5
	else
		py = py - 1
	end
end
For some reason, I just like to check if the run key was pressed before the actual movement key. :roll:
Don't check my github! It contains thousands of lines of spaghetti code in many different languages cool software! :neko:
https://github.com/Sulunia
User avatar
eqnox
Prole
Posts: 44
Joined: Fri Jul 31, 2015 2:36 pm

Re: help with movement

Post by eqnox »

So I did this

Code: Select all

  if love.keyboard.isDown("w") then
    if love.keyboard.isDown("lshift") then
      py = py - 3
    end
    else
      py = py - 1
    end
  end
but my player just started going up without any key press
User avatar
eqnox
Prole
Posts: 44
Joined: Fri Jul 31, 2015 2:36 pm

Re: help with movement

Post by eqnox »

Ah I saw my mistake in my last bit of code i posted

Code: Select all

function love.update()
  
  if love.keyboard.isDown("w") then
    if love.keyboard.isDown("lshift") then
      py = py - 3
    else
      py = py - 1
    end
  end
  
  if love.keyboard.isDown("s") then
    if love.keyboard.isDown("lshift") then
      py = py + 3
    else
      py = py +1
    end
  end
  
  if love.keyboard.isDown("a") then
    if love.keyboard.isDown("lshift") then
      px = px - 3
    else
      px = px -1
    end
  end
  
  if love.keyboard.isDown("d") then
    if love.keyboard.isDown("lshift") then
      px = px + 3
    else 
      px = px +1
    end
  end
  
end
This is the revised version and it works great!
Thanks for your help!
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: help with movement

Post by ivan »

Hello eqnox, the code needs work, I recommend the following changes:

1.Don't allow holding W/S or A/D at the same time.

Code: Select all

  local vx, vy = 0, 0
  if love.keyboard.isDown("w") then
    vy = -1
  elseif love.keyboard.isDown("s") then
    vy = 1
  end
  if love.keyboard.isDown("a") then
    vx = -1
  elseif love.keyboard.isDown("d") then
    vx = 1
  end
2.There is another subtle thing happening,
the player will move faster diagonally -
sqrt(2) = 1.41421356237 times faster to be precise.

Code: Select all

  local d = math.sqrt(vx^2 + vy^2)
  if d > 0 then
    -- normalize vx/vy so that its length equals 1
    vx, vy = vx/d, vy/d
  end
3.Use multiplication instead of several if-else statements
(this must be done after normalizing vx,vy as described in step 2):

Code: Select all

  if love.keyboard.isDown("lshift") then
    -- move x5 as fast when holding lshift
    vx = vx*5
    vy = vy*5
  end
PS. It should be clarified that vx and vy is the velocity which is later added to the position:

Code: Select all

player.x = player.x + vx*dt
player.y = player.y + vy*dt
Last edited by ivan on Sun Mar 19, 2017 7:27 am, edited 2 times in total.
User avatar
Le_juiceBOX
Citizen
Posts: 71
Joined: Sat Mar 26, 2016 3:07 pm

Re: help with movement

Post by Le_juiceBOX »

Use the suggestions above also use this:

Code: Select all

if love.keyboard.isDown('w') then
	py = py - 1*dt
end
the dt makes the game run at the same speed for 'all' computers. Use dt with movement etc.
SteamLibrary-like Program For Love2D Games:
take me to the forum thread!
User avatar
NickRock
Citizen
Posts: 76
Joined: Thu Dec 25, 2014 9:33 pm
Location: Earth
Contact:

Re: help with movement

Post by NickRock »

If you multiply by dt you need to change 1 with 100 since it makes your speed really slow
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: help with movement

Post by zorg »

NickRock wrote: Tue Mar 21, 2017 1:04 pm If you multiply by dt you need to change 1 with 100 since it makes your speed really slow
More correctly, with dt there, the number multiplied by it will basically have "pixels per second" as its unit, instead of pixels per "loop".
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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests