Jump to content
  • Advertisement
Sign in to follow this  
CodeBoyCjy

how to sort the particles ??

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!