Page 1 of 1
Point following
Posted: Sun Apr 15, 2012 9:44 am
by Delibrete
Hello,
I'm here to ask as to how one would go about making an object 'walk' to an x and y coordinate.
Say this object started at x = 300, y = 400 and you wanted that object to walk to x = 600, y = 800 at a speed of 16 pixels a second. How would you do this?
Re: Point following
Posted: Sun Apr 15, 2012 10:06 am
by timmeh42
Others can correct me if I'm wrong, but this would be what you want:
Code: Select all
function love.load()
object_x = 300
object_y = 400
point_x = 600
point_y = 800
object_speed = 16
end
function love.update(dt)
if ( (((point_x - object_x)^2 + (point_y - object_y)^2)^0.5) < (speed*dt) ) then
object_x = point_x
object_y = point_y
else
local angle = math.rad(math.atan2(point_x - object_x, point_y - object_y))
object_x = object_x + math.cos(angle) * (object_speed/dt)
object_y = object_y + math.tan(angle) * (object_speed/dt)
end
end
To break it down, we have:
Code: Select all
function love.load()
object_x = 300
object_y = 400
point_x = 600
point_y = 800
object_speed = 16
end
We assign the values you specified to variables.
Code: Select all
if ((((point_x - object_x)^2 + (point_y - object_y)^2)^0.5) < (object_speed*dt)) then
object_x = point_x
object_y = point_y
We check whether the object is closer than a single "step" to the destination point - this means that we can jump straight there without computing most of the trig functions. "(((x2-x1)^2+(y2-y1)^2)^0.5)" was taken from the wiki page for
Additional Math, and computes the distance to the point.
Code: Select all
local angle = math.rad(math.atan2(point_x - object_x, point_y - object_y))
Again taken from the Additional Math page, this computes the angle from the object to the point.
Code: Select all
object_x = object_x + math.cos(angle) * (object_speed/dt)
object_y = object_y + math.tan(angle) * (object_speed/dt)
Finally, the object is moved a single "step" towards the point. A step is equal to the distance it should travel in one step, ie, its speed divided by the Delta Time (
dt)
Be warned, I have not actually tried this, but theoretically it should work. I may have swapped some values around, which could make the object move away from the point instead. In that case, swap around the point_x/y and objectx/y values where the angle is computed.
Re: Point following
Posted: Sun Apr 15, 2012 10:17 am
by Delibrete
Yes that is what I wanted, thank you. But for some reason it's really slow.
Re: Point following
Posted: Sun Apr 15, 2012 10:24 am
by timmeh42
Ah, was still editing when you posted. May as well leave the explanation there in case others want it. I also had to fix some things where I forgot to use the variable names, so its for the best.
Can't think why it's slow, though - I don't have any experience in optimisation of code.
Re: Point following
Posted: Mon Apr 16, 2012 9:30 am
by Robin
timmeh42 wrote:Can't think why it's slow, though - I don't have any experience in optimisation of code.
The speed is 16 pixels per second. That means to travel the width of the default window (800px), it would take 50 seconds.
Re: Point following
Posted: Tue Apr 17, 2012 6:06 am
by bartbes
Actually, in irc we discussed the real problem,
timmeh42 wrote:
Code: Select all
object_x = object_x + math.cos(angle) * (object_speed/dt)
object_y = object_y + math.tan(angle) * (object_speed/dt)
That should be object_speed*dt, of course.
Re: Point following
Posted: Tue Apr 17, 2012 6:18 am
by trubblegum