Sign in to follow this  
CodeBoyCjy

how to sort the particles ??

Recommended Posts

Evil Steve    2017
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 this post


Link to post
Share on other sites
DevFred    840
Quote:
Original post by CodeBoyCjy
How 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 100
struct 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 this post


Link to post
Share on other sites
Zahlman    1682
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this