Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to draw a trail? sf::VertexArray?  (Read 4109 times)

0 Members and 2 Guests are viewing this topic.

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
How to draw a trail? sf::VertexArray?
« on: January 19, 2015, 04:11:24 pm »
Say I have an object and whenever it moves I want a continuous trail to appear behind it. The trail should never disappear but rather be persistent.

How would I draw such a trail? I thought about using sf::VertexArray and use lines and always append points or lines based on the distance moved...

Then I read the documentation and it says points and lines can only be 1 pixel thick, but I need the trail thickness to be bigger than 1 pixel (customizable).

What sf::PrimitiveType should I use, and is a vertex array even the right choice to solve this problem?

I also thought about using a vector and add rectangles but that doesn't seem as good, plus the trail generation should be smooth.

Edit:

sf::VertexArray vertices;
vertices.setPrimitiveType(sf::PrimitiveType::LinesStrip);
// dynamically append...
vertices.append(sf::Vertex(sf::Vector2f(10.0f, 10.0f)));
vertices.append(sf::Vertex(sf::Vector2f(20.0f, 20.0f)));
vertices.append(sf::Vertex(sf::Vector2f(30.0f, 20.0f)));
vertices.append(sf::Vertex(sf::Vector2f(40.0f, 30.0f)));
// ...
window->draw(vertices);

seems to be pretty good, except the line's thickness is only 1 pixel
« Last Edit: January 19, 2015, 04:22:31 pm by smguyk »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How to draw a trail? sf::VertexArray?
« Reply #1 on: January 19, 2015, 04:38:15 pm »
You could use a TrianglesStrip primitive and calculate the calculate the positions based on a line with a thickness.



The green points are where your object actually was. Then you add or subtract for example 2 pixels and that's your TrianglesStrip positions (red points) and since it's a TrianglesStrip the trail will be continues.
All you need to make sure, is that the points are added in the correct order. I've marked the two triangles with a slightly different color.
« Last Edit: January 19, 2015, 04:45:59 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: How to draw a trail? sf::VertexArray?
« Reply #2 on: January 19, 2015, 05:05:40 pm »
Thanks, that's exactly what I'm looking for.

I'll try to implement it now :)