# how to sort the particles ??

This topic is 3746 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi : How to sort the particles according to their distance from the camera in one emitter? And how to sort them among emitters? Thanks for help.

##### Share on other sites
You don't need to sort the particles if you're doing additive blending.

If you do need to sort the particles, then you just... sort them. We'll need to know more information, such as what language you're using, what API, what container you have your particles in and so on in order to help.
Assuming C++ and a std::vector of structs, with one struct per particle, you can just use std::sort with a sort function that takes the distance between each particle and the camera (Or better, the squared distance so you avoid the sqrt() call).

##### Share on other sites
Quote:
 Original post by CodeBoyCjyHow to sort the particles according to their distance from the camera

Assuming the camera is at the origin of the coordinate system, you can just sort according to the squared distance from that origin (the squared distance is cheaper to compute than the real distance because you save the expensive square root operation).

Assuming C, it might look something like this:
#include <stdlib.h>struct particle{    float x, y, z;    float r, g, b;    /* other particle stuff */};int comparator(const void *a, const void *b){    const struct particle *p = a;    const struct particle *q = b;    const float pp = p->x*p->x + p->y*p->y + p->z*p->z;    const float qq = q->x*q->x + q->y*q->y + q->z*q->z;    union { float f; int i; } u;    u.f = pp - qq;    return u.i;}/* later in the program */struct particle particles[100];/* initialize particles */qsort(particles, 100, sizeof(struct particle), comparator);

Hm, maybe precalculating the squared distances is a good idea, so you don't have to do it more than once per particle:
#include <stdlib.h>struct particle{    float squared_distance;    float x, y, z;    float r, g, b;    /* other particle stuff */};int comparator(const void *a, const void *b){    const struct particle *p = a;    const struct particle *q = b;    union { float f; int i; } u;    u.f = p->squared_distance - q->squared_distance;    return u.i;}/* later in the program */#define NPARTICLES 100struct particle particles[NPARTICLES];/* initialize particles *//* calculate squared distances */const struct particle *q = particles + NPARTICLES;struct particle *p;for (p = particles; p != q; ++p)    p->squared_distance = p->x*p->x + p->y*p->y + p->z*p->z;qsort(particles, NPARTICLES, sizeof(struct particle), comparator);

##### Share on other sites
The problem of sorting things is really two problems: (a) comparing things, and (b) using comparison results to sort. (b) is already taken care of by the standard library; you just need to implement (a).

To sort according to distance from the camera, you must compare according to the distance from the camera. Thus, the function you write will look at two particles, and indicate which one is closer to the camera. This is simple: you calculate the distance to the camera for each one, and check which is less.

To sort all the particles, you just put them all in the same container, and don't care about what emitter they came from. In fact, it will probably never matter what emitter the particle came from, after it is emitted. For anything.

1. 1
Rutin
67
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633416
• Total Posts
3011780
• ### Who's Online (See full list)

There are no registered users currently online

×