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?
Point following
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Point following
Others can correct me if I'm wrong, but this would be what you want:
To break it down, we have:
We assign the values you specified to variables.
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.
Again taken from the Additional Math page, this computes the angle from the object to the point.
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.
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
Code: Select all
function love.load()
object_x = 300
object_y = 400
point_x = 600
point_y = 800
object_speed = 16
end
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
Code: Select all
local angle = math.rad(math.atan2(point_x - object_x, point_y - object_y))
Code: Select all
object_x = object_x + math.cos(angle) * (object_speed/dt)
object_y = object_y + math.tan(angle) * (object_speed/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.
Re: Point following
Yes that is what I wanted, thank you. But for some reason it's really slow.
Re: Point following
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.
Can't think why it's slow, though - I don't have any experience in optimisation of code.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Point following
The speed is 16 pixels per second. That means to travel the width of the default window (800px), it would take 50 seconds.timmeh42 wrote:Can't think why it's slow, though - I don't have any experience in optimisation of code.
Help us help you: attach a .love.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Point following
Actually, in irc we discussed the real problem,
That should be object_speed*dt, of course.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)
- trubblegum
- Party member
- Posts: 192
- Joined: Wed Feb 22, 2012 10:40 pm
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 1 guest