I want to show a trail of the last points that the object has been in, but I want that trail to always be a certain length, regardless of the object's speed. How can I do that?

At the moment, I have an array of 100 points. When it gets full, I overwrite the oldest one.

However, if the object is staying still (or moves slowly), the old points are overwritten and the trail becomes shorter. I want the the trail to always be 100 pixels total length.

  • \$\begingroup\$ It is very unclear what you're asking. \$\endgroup\$
    – Vaillancourt
    Apr 12, 2015 at 19:51
  • \$\begingroup\$ I assume you mean "trail", but it's not clear. Further, you'll need to tell us what's wrong with the way you're doing it now if we're to provide you with solutions to make it better. \$\endgroup\$
    – House
    Apr 12, 2015 at 20:35
  • \$\begingroup\$ I update the Question. \$\endgroup\$
    – ReymonARG
    Apr 12, 2015 at 22:37
  • \$\begingroup\$ Use a stack (FIFO). Push new (mouse?) locations onto the top if the stack is empty or if the new location is further away from the location on top of the stack by XX amount, or if YY seconds have passed. When adding a new point, if the stack contains 100 points, pop one from the bottom before adding the new one to the top. \$\endgroup\$
    – Jon
    Apr 13, 2015 at 5:00
  • \$\begingroup\$ I made a big "aggressive" edit. Hopefully it says what you mean now. I focused on the idea that the length of the trail should be the same. \$\endgroup\$
    – Anko
    Apr 13, 2015 at 13:13

1 Answer 1


There's a couple ways to do this. One way would be to store a dynamic number of previous positions instead of 100, and only throw old points away when the are beyond the length you want to show. A better way to do this though would probably be to store the last 100 evenly spaced positions where each position is totalDistance / 100 units apart. Basically, where you used to just put the player's position into the array, if the distance from the last point to the new point was 3/100 of the total distance you'd want to show, you'd calculate the 3 points at that fixed length interval from the old position to the new position and put them into your array, removing old points to make room if necessary. Hopefully that makes sense, let me know if not.

  • \$\begingroup\$ Good luck! For a fixed size array of points (if it's always 100 for instance), i'd strongly suggest a circular buffer if you aren't using one already. Super fast and efficient (: \$\endgroup\$
    – Alan Wolfe
    Apr 13, 2015 at 16:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .