I currently have this code snippet running in my compute shader which clamps particles to a region, and bounces them back based on their velocity.
particles[i].position += particles[i].velocity * deltaTime;
if (particles[i].position.x > fieldSize) {
particles[i].position.x = fieldSize;
particles[i].velocity.x *= -1;
}
if (particles[i].position.x < -fieldSize) {
particles[i].position.x = -fieldSize;
particles[i].velocity.x *= -1;
}
if (particles[i].position.z > fieldSize) {
particles[i].position.z = fieldSize;
particles[i].velocity.z *= -1;
}
if (particles[i].position.z < -fieldSize) {
particles[i].position.z = -fieldSize;
particles[i].velocity.z *= -1;
}
I was wondering if there’s a better way to do this that’s more efficient, since running this on millions of particles tanks the FPS compared to what I had before, but it didn’t have the restitution effect:
particles[i].position.x = clamp(particles[i].position.x, -fieldSize, fieldSize);
particles[i].position.z = clamp(particles[i].position.z, -fieldSize, fieldSize);
Any help would be appreciated!