New to love

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
RunningGamesStudios
Prole
Posts: 46
Joined: Fri May 31, 2013 9:42 pm

New to love

Post by RunningGamesStudios »

trying to make it so test.png will glide to where ever i click the screen. this is what i got so far

Code: Select all

--int
function love.load()
   home = love.filesystem.getWorkingDirectory()   
   img = love.graphics.newImage("test.png")
   key = ""
   imgx = 100
   imgy = 100
end
--int END

--main
function love.update(dt)
   if love.keyboard.isDown("w") then
         imgy = imgy - 1
         key = "w"
   end
   if love.keyboard.isDown("a") then
         imgx = imgx - 1
         key = "a"
   end
   if love.keyboard.isDown("s") then
         imgy = imgy + 1
         key = "s"
   end
   if love.keyboard.isDown("d") then
         imgx = imgx + 1
         key = "d"
   end
end

function love.mousepressed(x, y, button)
   imgx = x
   imgy = y
end

function love.draw()
   love.graphics.setColor(255,255,255)
   love.graphics.draw(img, imgx, imgy, 0, 1, 1, 128, 128)
   love.graphics.setColor(255,255,0)
   love.graphics.print(key, 300, 300)
   love.graphics.print(home, 100, 100)
end


--main end

User avatar
zoboomafoo
Prole
Posts: 7
Joined: Sun May 19, 2013 9:05 pm

Re: New to love

Post by zoboomafoo »

You want it to move to the mouse slowly then use something like this.

Code: Select all

function love.update (dt)

   angle = math.atan2((newY - imgY), (newX - imgX))

	if imgX ~= newX or imgY ~= newY then
    	imgX = imgX + (50 * math.cos(angle)) * dt
    	imgY = imgY + (50 * math.sin(angle)) * dt
  end
end

function love.mousepressed(x, y, b)
   if b == "l" then
      newX = x
      newY = y
   end
end
I dint test it but that should work. Hope this helps!
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: New to love

Post by Sheepolution »

atan2, sin and cos are advanced math functions. You probably don't understand how they work so let me try to explain.

math.atan2 calculates the angle between 2 distances. So the first distance is newY -imgY and the other is newX - imgX.

sin and cos return -1 to 1, depending on the angle.
5fpYND4.gif
5fpYND4.gif (73.49 KiB) Viewed 305 times
Fdoqt06.png
Fdoqt06.png (10.27 KiB) Viewed 305 times
This is sinus and cosinus! The red line is the angle (so math.atan2). The brown line is cosinus, and the blue line is sinus.

Now for cosinus, if the red line would be to the far left, it would result in -1, and to the far right it returns 1. As for sin the same, but then up and down. Next you take those values and multiply them with your speed value.
Last edited by Sheepolution on Fri Sep 13, 2013 3:28 pm, edited 1 time in total.
RunningGamesStudios
Prole
Posts: 46
Joined: Fri May 31, 2013 9:42 pm

Re: New to love

Post by RunningGamesStudios »

Thank you very much!
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests