Page 1 of 1

help with movement

Posted: Fri Mar 17, 2017 3:06 am
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.

Re: help with movement

Posted: Fri Mar 17, 2017 3:38 am
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:

Re: help with movement

Posted: Fri Mar 17, 2017 3:47 am
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

Re: help with movement

Posted: Fri Mar 17, 2017 3:55 am
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!

Re: help with movement

Posted: Fri Mar 17, 2017 6:51 am
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

Re: help with movement

Posted: Fri Mar 17, 2017 7:29 pm
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.

Re: help with movement

Posted: Tue Mar 21, 2017 1:04 pm
by NickRock
If you multiply by dt you need to change 1 with 100 since it makes your speed really slow

Re: help with movement

Posted: Tue Mar 21, 2017 1:12 pm
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".