Click to Move Question
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Click to Move Question
Hello I want to do this: Player moves towards there and stops when I click somewhere. How can i achieve this? Please note that i am a bit newbie. Thanks in advance!
Re: Click to Move Question
Get the coordinates of player and the coordinates of click, then just add (or subtract step (e.g. 1) depending on which is higher) from player x,y until you get there (loop). If x,y equals click x,y, stop looping. You need to have two pairs of x,y variables  current player position, final player position.
Re: Click to Move Question
Note that you don't need to actually write a loop, just use LÖVE's builtin game loop. Each frame, ie. inside love.update, you want to move the player closer to the destination by some "step" variable, like dusoft said, multiplied by dt (delta time, the argument to love.update). To update the position, you can use the love.mousepressed callback.
I updated this tutorial to work with mouse clicks, maybe that'll make it clear:
Of course you'll probably want to use a more naturallooking algorithm for the movement, this is just what the tutorial already had.
I updated this tutorial to work with mouse clicks, maybe that'll make it clear:
Code: Select all
function love.load()
circle = {}
mouse = {}
circle.speed = 300
 just some initial values here
mouse.x, mouse.y = love.mouse.getPosition()
circle.x, circle.y = mouse.x, mouse.y
end
function love.mousepressed(x, y, button)
if button == 1 then
mouse.x, mouse.y = x, y
end
end
function love.update(dt)
if circle.x < mouse.x then
circle.x = circle.x + (circle.speed * 2.5 * dt)
end
if circle.x > mouse.x then
circle.x = circle.x  (circle.speed * 2.5 * dt)
end
if circle.y < mouse.y then
circle.y = circle.y + (circle.speed * 2.5 * dt)
end
if circle.y > mouse.y then
circle.y = circle.y  (circle.speed * 2.5 * dt)
end
end
function love.draw()
love.graphics.setColor(1, 0, 0)
love.graphics.circle("fill", circle.x, circle.y, 50)
end
Re: Click to Move Question
tobiasvl, I tried your code it works but I want the circle to go to the target position by the minimum distance. (hypotenus distance or cross path). In this code, the circle moves on xaxis and then moves on yaxis or vice versa. How can I solve this problem? Thanks.

 Citizen
 Posts: 98
 Joined: Wed Oct 05, 2016 11:53 am
Re: Click to Move Question
Untested code from the top of my head, but something roughly like this should do it. The two math functions I took from the wiki's math page.
An alternative could be just a simple lerp between the start and end destinations.
Code: Select all
 Returns the distance between two points.
function math.dist(x1,y1, x2,y2) return ((x2x1)^2+(y2y1)^2)^0.5 end
 Returns the angle between two points. Radians!
function math.angle(x1,y1, x2,y2) return math.atan2(y2y1, x2x1) end
function love.update(dt)
 get distance to destination
local distance = math.dist ( circle.x, circle.y, mouse.x, mouse.y )
 calculate how much we would move this frame
local stepSize = circle.speed * 2.5 * dt
if distance > stepSize then  if we're further away than stepSize, move towards the mouse's location in a straight line
 get angle between destination and current location, in radians, and move circle
local angle = math.angle ( circle.x, circle.y, mouse.x, mouse.y )
circle.x = circle.x + math.cos ( angle ) * stepSize
circle.y = circle.y + math.sin ( angle ) * stepSize
else  if we're not where mouse is, but closer than stepSize, set circle onto mouse position
circle.x, circle.y = mouse.x, mouse.y
end
end
 Run once in a blue moon
if math.random() > 0.99 then
local account = Twitter:getProfile ( https://twitter.com/GravityCircuit )
account:post ( currentProgress:getGIF() )
end
if math.random() > 0.99 then
local account = Twitter:getProfile ( https://twitter.com/GravityCircuit )
account:post ( currentProgress:getGIF() )
end
Re: Click to Move Question
You don't need angles for this
Every time you're calculating an angle just to take its sine or cosine, chances are you're wasting calculations.
Every time you're calculating an angle just to take its sine or cosine, chances are you're wasting calculations.
Code: Select all
if distance > stepSize then  if we're further away than stepSize, move towards the mouse's location in a straight line
circle.x = circle.x + (mouse.x  circle.x) / distance * stepSize
circle.y = circle.y + (mouse.y  circle.y) / distance * stepSize
else
...
Who is online
Users browsing this forum: Google [Bot] and 124 guests