SFML community forums

Help => Graphics => Topic started by: Paijan on July 09, 2015, 01:40:53 pm

Title: Comet tail
Post by: Paijan on July 09, 2015, 01:40:53 pm
I'm writing a game in which moving objects leave tails behind themselves, like comets. Objects can be moved either by player input or mathematical function (e.g. on sinusoid).

I couldn't find a good way to perform this so I tried the simplest one - creating CircleShape in regular intervals in objects' positions. And it worked, because of simplicity, but later on it became a performance bottleneck. It's snappy and the tails sometimes break -pics (http://i.imgur.com/FX02csX.png).

How to do it properly? Thor particles?
Title: Re: Comet tail
Post by: Jesper Juhl on July 09, 2015, 01:55:22 pm
If you want the tail to be contiguous and stay behind you could simply draw a line between each of the points denoting the previous positions.
If the tail is supposed to "fade out" then something like Thor's particles may be better suited.
Or you could use a shader to draw the tail I guess.
Title: Re: Comet tail
Post by: Paijan on July 09, 2015, 02:09:42 pm
Quote
If you want the tail to be contiguous and stay behind you could simply draw a line between each of the points denoting the previous positions.

You mean storing coordinates each frame and appending it to VertexArray?

EDIT: Well, it really works fine, only now I can't alter line thickness
Title: Re: Comet tail
Post by: kitteh-warrior on July 09, 2015, 05:12:56 pm
can't alter line thickness

This (https://github.com/SFML/SFML/wiki/Source:-Line-segment-with-thickness) shows how it is possible with a sf::VertexArray. If that doesn't work for you, the mathematical concepts behind it should help you.
Title: Re: Comet tail
Post by: Nexus on July 10, 2015, 09:58:30 am
This is a really typical use case for particles. The more flexibility you add to a handcrafted solution, the closer it will get to a freely configurable particle system ;)

In the Thor fireworks example, I've done exactly that -- tails for the firework explosions. You can have a look at it, even in case you don't end up using Thor.
Title: Re: Comet tail
Post by: Paijan on July 11, 2015, 03:33:02 pm
Thank you for your replies.

I decided using VertexArray and the tail works perfectly. I even tried fading it by iterating through all vertices and changing their alpha values calculated from quadratic function (to make it smooth). It looks nice (http://i.imgur.com/4yAfqyp.png).

Should I somehow dispose of faded vertices? Because they are faded, but they still exist in memory and are drawn each frame.
Title: Re: Comet tail
Post by: Nexus on July 11, 2015, 03:39:49 pm
Yes, definitely remove them. Maybe not every frame, because removing from the beginning of a vector is expensive, but once a second or so.