Help with probably basic movement controls

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
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Help with probably basic movement controls

Post 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
Attachments
game.love
(12.94 KiB) Downloaded 124 times
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

Re: Help with probably basic movement controls

Post 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 :)
User avatar
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Re: Help with probably basic movement controls

Post by CanadianGamer »

Thank you for the help. I probably should comment my code better but you pretty much got the general idea.
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google Adsense [Bot] and 3 guests