How to move object at the same speed

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
knuxyl
Citizen
Posts: 52
Joined: Sat Aug 13, 2016 4:40 am

How to move object at the same speed

Post 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.
JoeySalads
Prole
Posts: 1
Joined: Sat Oct 15, 2016 3:45 am

Re: How to move object at the same speed

Post 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
knuxyl
Citizen
Posts: 52
Joined: Sat Aug 13, 2016 4:40 am

Re: How to move object at the same speed

Post by knuxyl »

I'm having several problems with the code you posted.

First, the

Code: Select all

!=
seems to be from another language. It's familiar to me, but doesn't work in lua. This is what lua uses

Code: Select all

~=
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
User avatar
pgimeno
Party member
Posts: 3641
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to move object at the same speed

Post 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?
knuxyl
Citizen
Posts: 52
Joined: Sat Aug 13, 2016 4:40 am

Re: How to move object at the same speed

Post 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.
game.love
(4.93 MiB) Downloaded 103 times
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests