#Calculate movement on line

1 messages · Page 1 of 1 (latest)

night idol
#

Hello, I'm working on a way to "lead" my bullets, targeting an enemies, in my 2D tower defense game. To do this I need a way to calculate how far my enemies will move for "x" seconds. The enemies follow a line, built up by an array of 2d points. The enemies also move at a constant rate. Does anyone know how to do this, or have an idea? Additionally, I want to add that this calculation doesn't have to be too accurate, rather I would prefer having a fast way to do this.

rapid venture
#

You want to know the distance the bullets move in x time, is that right?

night idol
rapid venture
#

You know it's velocity, direction and starting position?

night idol
#

Yes, I know it's velocity and starting position, the problem i'm facing although is that objects direction changes, as it follows different points, all connecting to form a line.

rapid venture
#

So he is going in a bunch of lines, that is?

night idol
#

Yes the object follows a set line, built up by points for all corners.

rapid venture
#

Corners?

#

Can you draw it for me so I can understand it better?

#

Apologies for not understanding

night idol
#

Certainly

#

The black dots represents "pathPoints" all building up this line. What I want to know is if an object on this line moves for "x" seconds at "y" speed, where it would end up. This object would follow the path of the line, by moving towards a "pathPoints" and advancing to the next.

rapid venture
#

And the "x" seconds can represent a time the object is already beyond a pathPoint, correct?

night idol
#

Yes that is my problem, that I would have to think about that during these "x" seconds, the target could surpass a pathPoint and be given a new one, and therefor also a new direction.

rapid venture
#

Hmm, my approach would be to check in which section the object is currently in, add up the total times of all of the sections before, then add up the time it took it on its current section

night idol
#

Would that work though, as for giving me a point where it would end up?

rapid venture
#

You can calculate the time it takes by multiplying the length of each previous section by its velocity

#

So if my speed is 10

#

And I'm in between points 3 and 4, I would multiply the velocity by (length of 1-2 + length of 2-3)

night idol
#

Yes, but wouldn't this just give me a float value of how long the object has traveled along the path? I wish for a vector 2, point instead, so I would somehow have to get this time, and convert it to a point on the line.

rapid venture
#

Oh, that's right. Then what you could do is check how much time each section takes, and starting with the first section, find how many sections the object must have moved through, to find the current section. For example, x is 3s, 1st section takes 2.5 seconds, 2nd section takes 1s. You add the first section up, it's still less than x, so you know it's already past section 1. Now you add the second one, you get 3.5 seconds, which is more than x, so x is somewhere between point 2 and 3. Now, you know 2.5 < x < 3.5, and that x is 3. Now you can find the percentage the object has moved within the section, in this case 50%, and find the position accordingly.

night idol
rapid venture
#

You're welcome! Definitely let me know if it works

night idol
#

Will do

rapid venture
#

The last part is just a lerp function, I forgot to mention.

night idol
#

Right, lerping from the starting point to the the end point of that specific line-segment, for that amount of seconds, right?

rapid venture
#

That's right. In the example I gave, the 50% are actually 0.5 weight in the lerp function.

night idol