Page 1 of 2

[Help] How to move object to mouse clicked loaction

Posted: Fri Sep 07, 2012 6:11 am
by pat.clarke
About Me
Hi the name is Pat I have almost no coding experience. I am in Afghanistan board out of my minde lol So i figure why not put my time to good use. I have watched all the sock monkey tutorials and some of the love tutorials on the wiki.

The question
I was working on Love Tutorial:Hamster Ball. When I wondered how I could get it so that when I left click the mouse on a location he will move at a set speed to that location. I did a few searches on the forums and did not see what I was looking fore. Im sure its out there I may have just missed out. So if there is a good tutorial on this could you please point me in the right direction.

Edit 9/7/2012 This issues has been solved by Roland_Yonaba

Updated code of working mouse moments

Code: Select all

function love.load() 

    medium = love.graphics.newFont(45)  
    hamster = love.graphics.newImage("Images/hamster.png")               --- Define variable for .png and file location of .png
	x = 0 
	y = 0  
	destX = 0                                                                                     --- Must Declare a start location for destX and destY 
	destY = 0 
	speed = 100                                                                                 --- The movement Speed of the .png

end  

function love.mousepressed(x,y,button)                                                --- This is the code to for the mouse buttons 

	if button == 'l' then                                                                       --- This is for the left mouse button 
    destX, destY = x,y
   end
   
   
end

function love.update(dt)
	  
	  if x ~= destX then                                                                        --- The math for the movement
     x = x + (dt*speed)*((destX-x)/math.abs(destX-x))
   end
   
      if y ~= destY then
     y = y + (dt*speed)*((destY-y)/math.abs(destY-y))
   end
   
end


function love.draw() 

 love.graphics.draw(hamster, x, y)                                                          --- The variable defined for the .png start location 

end 
Also take a look at https://love2d.org/wiki/love.mousepressed

Re: [Help] How to move object to mouse clicked loaction

Posted: Fri Sep 07, 2012 10:15 am
by Roland_Yonaba
Welcome around, Clarke.
Considering your source, you have a hamster ball that you can move around with arrow keys.
So you want to be able to move that picture to a location by clicking. There are many ways to proceed.
As an example you can use love.mousepressed to register where we clicked, and register this very location as a further destination for your hamster ball.

Code: Select all

function love.mousepressed(x,y,button)
  if button == 'l' then
    destX, destY = x,y
  end
end
Now, in the update callback:

Code: Select all

function love.update(dt)
   if x ~= destX then 
     x = x + (dt*speed)*((destX-x)/math.abs(destX-x))
   end
   --- same stuff on y -axis
end
The ratio (destX-x)/math.abs(destX-x) just gives either -1 or 1, which correspond to a left/right moves on X-axis, or up/down move on Y-axis.
Hope this helps.

Re: [Help] How to move object to mouse clicked loaction

Posted: Fri Sep 07, 2012 10:34 am
by pat.clarke
Thank you for the quick reply :) I will test this out and give my feed back. I greatly appreciate your help.

Ok looks like I may have miss understood. I get error
"main.lua23: attempt to perform Arithmetic on global 'destX' (a nil value)

Traceback
main.lua:23: in function 'update'
[C]: in function 'xpcall' "

Sounds like its a issue with

Code: Select all

x = x + (dt*speed)*((destX-x)/math.abs(destX-x))


but hear is what my whole source looks like at this time

Code: Select all

function love.load() 

    medium = love.graphics.newFont(45) 
    hamster = love.graphics.newImage("Images/hamster.png")
	x = 0 
	y = 0 
	speed = 100

end  

function love.mousepressed(x,y,button)

	if button == 'l' then
    destX, destY = x,y
   end
   
   
end

function love.update(dt)
	  
	  if x ~= destX then
     x = x + (dt*speed)*((destX-x)/math.abs(destX-x))
   end
   
      if y ~= destY then
     y = y + (dt*speed)*((destY-y)/math.abs(destY-y))
   end
   
end


function love.draw() 

 love.graphics.draw(hamster, x, y)

end 


I have not read thought that walk though that you linked to I will do so now

Re: [Help] How to move object to mouse clicked loaction

Posted: Fri Sep 07, 2012 10:56 am
by Roland_Yonaba
You need to declare destX and destY and give them initial values (in love.load) , as you declared x, y, speed.

Re: [Help] How to move object to mouse clicked loaction

Posted: Fri Sep 07, 2012 11:57 am
by pat.clarke
Ty so much for all the quick replys! I updated post 1 with the working code so if any one ever does a search they should find a quick solution.

New issue is its that its moving my the top left corner of my .png file to the location selected. How do I move the center of the .png to selected location?

Re: [Help] How to move object to mouse clicked loaction

Posted: Fri Sep 07, 2012 12:12 pm
by Roland_Yonaba
pat.clarke wrote:Ty so much for all the quick replys! I updated post 1 with the working code so if any one ever does a search they should find a quick solution.

New issue is its that its moving my the top left corner of my .png file to the location selected. How do I move the center of the .png to selected location?
Center the picture on the destination point.
See Image:getWidth() and Image:getHeight()

Code: Select all

function love.mousepressed(x,y,button)
   if button =='l' then
      destX = x - hamster:getWidth()/2
      destY = y - hamster:getHeight()/2
   end
end

Re: [Help] How to move object to mouse clicked loaction

Posted: Fri Sep 07, 2012 12:20 pm
by Petunien
Or draw the picture by its center:

Code: Select all

 love.graphics.draw(hamster, x, y, 0, 1, 1, 64, 64)
It depends on what you are wanting. The code above is perfect for rotating.

Re: [Help] How to move object to mouse clicked loaction

Posted: Fri Sep 07, 2012 12:55 pm
by pat.clarke
Ty both I will look into both options
can you please explain what all the numbers after x and y
love.graphics.draw(hamster, x, y, 0, 1, 1, 64, 64)
Also sorry i just cant seem to figure this out my self
I now what to have to left click the image before i can tell it to go some wear with the right click. Think RTS.
I was playing around thinking something like this but does not work lol

Code: Select all

function love.mousepressed(x,y,button)
		if button == 'l'  
		and x == destX and y == destY then	
	if button == 'r' then
    destX, destY = x,y
   end 
   end
   
end


or something like this

Code: Select all

		if button == 'l'  
		and 'l' == hamster then	
	if button == 'r' then
    destX, destY = x,y
   end 

Re: [Help] How to move object to mouse clicked loaction

Posted: Fri Sep 07, 2012 1:32 pm
by Petunien
Sorry, I forgot to explain. It's my fault.

The fourth argument is the orientation of the image (radians), if its set to 0 (default), it won't rotate the image.
The fifth and sixth arguments are the scale factors (for x and y), so you can enlarge or reduce the scale of the image.

And cause I'm bad at explaining in english, please check http://nova-fusion.com/2011/05/06/draw- ... in-love2d/ for the last ones. :D

See also: https://love2d.org/wiki/love.graphics.draw

I don't know the solution for your RTS like system, someone will come and help you out. :)

Re: [Help] How to move object to mouse clicked loaction

Posted: Fri Sep 07, 2012 1:42 pm
by Roland_Yonaba
pat.clarke wrote: I now what to have to left click the image before i can tell it to go some wear with the right click. Think RTS.
Actually, I got nothing. What are you exactly trying to do ?
Left-click on a picture to select it, then right-click to make the picture move to that location ?
Or something different ?