Point following

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
Delibrete
Prole
Posts: 37
Joined: Mon Dec 05, 2011 11:51 am

Point following

Post 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?
User avatar
timmeh42
Citizen
Posts: 90
Joined: Wed Mar 07, 2012 7:32 pm
Location: Cape Town, South Africa

Re: Point following

Post 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.
Last edited by timmeh42 on Sun Apr 15, 2012 10:23 am, edited 1 time in total.
Delibrete
Prole
Posts: 37
Joined: Mon Dec 05, 2011 11:51 am

Re: Point following

Post by Delibrete »

Yes that is what I wanted, thank you. But for some reason it's really slow.
User avatar
timmeh42
Citizen
Posts: 90
Joined: Wed Mar 07, 2012 7:32 pm
Location: Cape Town, South Africa

Re: Point following

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Point following

Post 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.
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Point following

Post 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.
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Point following

Post by trubblegum »

viewtopic.php?f=4&t=8661 for more of the same.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 88 guests