# How do I render an object's position trail of a constant length?

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.

• It is very unclear what you're asking. Commented Apr 12, 2015 at 19:51
• 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. Commented Apr 12, 2015 at 20:35
• I update the Question. Commented Apr 12, 2015 at 22:37
• 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.
– Jon
Commented Apr 13, 2015 at 5:00
• 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.
– Anko
Commented Apr 13, 2015 at 13:13

## 1 Answer

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.

• 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 (: Commented Apr 13, 2015 at 16:45