Page 1 of 1

Help with probably basic movement controls

Posted: Sun Apr 09, 2017 11:18 pm
by CanadianGamer
Okay so I'm working on a game where the player is able to pilot a ship. Pretty straightforward right? But now I'm working on the movement controls and for some reason something is going wrong with the code and I have no idea why. I've now spent an hour trying to figure it out but I can't find the problem. I've supplied the code for the forward and backward movement, along with the .love file, which is where the real problem is. Any help would be much appreciated.

Code: Select all

      if love.keyboard.isDown('w') then
        local yMovement = math.sin(math.deg(player.r)) * (player.speed * dt)
        local xMovement = math.sqrt(math.pow(player.speed * dt, 2)-math.pow(yMovement, 2))
        player.x = player.x - xMovement
        player.y = player.y - yMovement
      end
            if love.keyboard.isDown('s') then
        local yMovement = math.sin(math.deg(player.r)) * (player.speed * dt)
        local xMovement = math.sqrt(math.pow(player.speed * dt, 2)-math.pow(yMovement, 2))
        player.x = player.x + xMovement
        player.y = player.y + yMovement
      end

Re: Help with probably basic movement controls

Posted: Sun Apr 09, 2017 11:58 pm
by drunken_munki
I didn't understand the code directly dude, but it looks like one problem is you are converting the radian into degree during the move calculation. Then I got stuck with the rest.

So here is a common calculation used, If I understood what you are trying to do. Drop this snippit into your code and try it:

Code: Select all

      if love.keyboard.isDown('w') then
        player.x = player.x + (math.sin(player.r) * player.speed * dt)
        player.y = player.y - (math.cos(player.r) * player.speed * dt)
      end
      
      if love.keyboard.isDown('s') then
        player.x = player.x - (math.sin(player.r) * player.speed * dt)
        player.y = player.y + (math.cos(player.r) * player.speed * dt)
      end
Some notes: this doesn't have 'velovity' but you can easily write that into the calculation (if you want to model it), also the turning speed seems sluggish compared to the momentum :)

Re: Help with probably basic movement controls

Posted: Mon Apr 10, 2017 1:57 am
by CanadianGamer
Thank you for the help. I probably should comment my code better but you pretty much got the general idea.