Page 1 of 1
How to move object at the same speed
Posted: Sat Oct 15, 2016 3:29 am
by knuxyl
I am trying to make the movement speed from WASD the same as the move to mouse functions.
Here is the code I have
Code: Select all
if love.mouse.isDown(mouse) then
touchx = love.mouse.getX()
touchy = love.mouse.getY()
direction = math.atan2(touchy-(height/2), touchx-(width/2))
angle = direction*180/math.pi
playerx = playerx + math.cos(direction) * dt * speed
playery = playery + math.sin(direction) * dt * speed
end
if love.keyboard.isDown("w") then
playery = playery - dt * speed
end
if love.keyboard.isDown("a") then
playerx = playerx - (speed*dt)
end
if love.keyboard.isDown("s") then
playery = playery + (speed*dt)
end
if love.keyboard.isDown("d") then
playerx = playerx + (speed*dt)
end
The problem is that the mouse click event moves the player faster than the wasd movement. How can I equalize these values? I don't even know where to start with this one.
Re: How to move object at the same speed
Posted: Sat Oct 15, 2016 4:01 am
by JoeySalads
It's probably best that you make those variables local, but I'll leave them as global in case you actually use them elsewhere. What this code does is convert the WASD movement to an angle and uses the same formula as the mouse method to calculate the horizontal and vertical speed.
Code: Select all
direction = 0
angle = 0
if love.mouse.isDown(mouse) then
touchx = love.mouse.getX()
touchy = love.mouse.getY()
else
touchx = 0
touchy = 0
local wIsDown, sIsDown = love.keyboard.isDown("w"),love.keyboard.isDown("s")
if wIsDown != sIsDown then
touchy = wIsDown and 0 or height
end
local aIsDown, dIsDown = love.keyboard.isDown("a"),love.keyboard.isDown("d")
if aIsDown != dIsDown then
touchx = aIsDown and 0 or width
end
end
direction = math.atan2(touchy-(height/2), touchx-(width/2))
angle = direction*180/math.pi
playerx = playerx + math.cos(direction) * dt * speed
playery = playery + math.sin(direction) * dt * speed
Re: How to move object at the same speed
Posted: Sun Oct 16, 2016 3:30 am
by knuxyl
I'm having several problems with the code you posted.
First, the
seems to be from another language. It's familiar to me, but doesn't work in lua. This is what lua uses
Also, the playerx and playery values need to be set in every if.isDown because if it is not, it will constantly move playerx and playery values.
Also, the keyboard controls do not work. It does not move the player in the correct direction. Also, I do not understand why you would use the touchx and touchy values for when using keyboard controls, unless u reset the mouseposition variables each time, but that's inefficient.
Thanks for trying though! This code was able to update the playerx and playery values only once regardless of what input was put. There are too many problems with the code though for me to try to make it work. I'm a beginner :p
Re: How to move object at the same speed
Posted: Sun Oct 16, 2016 11:31 am
by pgimeno
Edit: When taking a second look at your code and your question, I really, really don't see how it can be faster with touch to go movement than with keyboard (other than going diagonally makes you go faster than vertically then horizontally, of course). Maybe you can post a complete example?
Re: How to move object at the same speed
Posted: Mon Oct 17, 2016 2:35 am
by knuxyl
pgimeno wrote:Edit: When taking a second look at your code and your question, I really, really don't see how it can be faster with touch to go movement than with keyboard (other than going diagonally makes you go faster than vertically then horizontally, of course). Maybe you can post a complete example?
yeah looking again, it doesn't seem to be going faster. i thought it was yesterday.
here is the code anyways. sorry file so big. i dont want to optimize pngs until im done with layers.