Vertex Shader Particle Movement

I want to move my particles by a few given attributes. The particles should slow down to a stop as they reach the end of their lifetime

result = a_pos + a_direction * lifetime * mix(a_speed, a_speed_end, normalized_lifetime);
result.w = 1.0;
gl_Position = u_projection * u_model_view * result;

This doesn't work as I'd hoped it would because it doesn't consider previous movements, so when normalized_lifetime reaches 1, the particle is basically set to a_pos again since a_speed_end is 0. How do I go about this?

EDIT: More information:

a_pos = startposition

a_direction = normalized direction vector

lifetime = current lifetime of the particle

a_speed, a_speed_end = desired start/end speed

They should move on a straight line along the direction vector, no CPU updates

Answers


you want a equation like -a_deceleration*t^2+a_initial*t+a_pos

a bit of physics:

initial speed is a_speed a_speed_end is a_speed - a_deceleration*total_life so -a_deceleration = (a_speed_end-a_speed)/total_life

so your equation becomes

result = a_pos + a_direction * lifetime * a_speed 
               + a_direction * normalized_lifetime * normalized_lifetime * (a_speed_end-a_speed);

edit: looking at the formulas in wikipedia there is an easier one

result = a_pos + a_direction * lifetime * (a_speed + mix(a_speed, a_speed_end, normalized_lifetime))/2

Thanks for ratchet freak for pointing me to the right direction, helped a lot! The final solution was to calculate the acceleration first

a_acceleration = (endspeed - startspeed) / total_lifetime;

The final formula is then:

pos = a_pos + ((0.5 * a_acceleration * current_lifetime * current_lifetime) + (a_start_speed * current_lifetime)) * a_direction;
pos.w = 1.0;
gl_Position = u_projection * u_model_view * pos;

For example, with a startspeed of 5, endspeed of 0 and lifetime of 5 the particle will slowly decelerate and come to a total stop after 5 seconds.


Need Your Help

vector resizing - portable way to detect

c++ cross-platform vector

I have a vector that I am loading with a know amount of elements (N).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.