Trying to create code that draws a line between several points, but the amount of points is variable

Godot Version

V4.2.2

Question

I want to code a line that will show the trajectory of an object on a grid as it slows down and comes to a stop. (For example, if it moved 5 spaces previously, then it will move 4 next, then 3, etc. until it reaches 0). This is easy enough to do when only moving along 1 dimension, but because my game is top-down I want at least 2 dimensions of movement. That means an object can move 5 spaces right and 3 spaces up, before slowing down to 4 right and 2 up. I’ve learned enough about Godot to figure I could probably do the job with a loop function to decrease the values and an array to hold each new vector, but I don’t know how to begin coding it.
For some added complexity, I also want to code it so that if the speed in one direction reaches 1 before the other (like: x=3 y=1) then the value at 1 will remain at 1 until the other value is also at 1 so they can both decrease to zero at the same time (I think it makes the sliding seem more realistic).

you can do a while loop:

var line = Line2D.new()
while direction != Vector2.ZERO:
    line.add_point(direction)
    if direction.x <= 1 and direction.y <= 1:
        direction == Vector2.ZERO
    if direction.x > 1:
       direction.x -= 1
    if direction.y > 1:
        direction.y -= 1

I think this is pretty close to what I need, but for some reason the line isn’t showing up even though the code says there is more than one point. Also, each point after the first should be further away from the origin but by a decreasing amount. I’m still a beginner at reading code but I think this loop just creates points leading back to the origin. I’m trying to think of a way to have each new direction be added relative to the previously added point so they form a line, but I’m struggling.

Put simply: If the object slides 5 right and 4 up then it will be at (5 , 4), it then slides 4 right and 3 up and is now at (9 , 7), and it will continue until both values are depleted, leaving it at (15 , 10). It’s a lot easier to do on graph paper than it is to code.

When just moving along one axis it was easy to calculate where it would stop by using a triangular number formula: (n * (n+1)) / 2. Unfortunately that method doesn’t really work the way i’d like with more than one axis of movement. Especially because I’d like to actually show the player each point along the path and that formula only creates one point at the very end

Just watch something like this. It shows several examples for lines.

https://www.youtube.com/watch?v=Bhc_EBasycY
Every single concept you can come up with. Will start to fall apart here. Just from the difference in working 2d to 3d. This is a really terrible program.

Ok then you would have to save the previous point and just add it on top:

var line = Line2D.new()
var previousPosition: Vector2 = Vector.ZERO # CurrentPosition
while direction != Vector2.ZERO:
    previousPosition += direction
    line.add_point(previousPosition)
    if direction.x <= 1 and direction.y <= 1:
        direction == Vector2.ZERO
    if direction.x > 1:
       direction.x -= 1
    if direction.y > 1:
        direction.y -= 1

if you want trajectory lines, you can follow this tutorial:

1 Like